]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/sourcemap.cpp
e74f1b67408367ab2ff2a3a28c6381b6aa54656e
[libs/gl.git] / source / glsl / sourcemap.cpp
1 #include <msp/strings/format.h>
2 #include <msp/strings/regex.h>
3 #include <msp/strings/utils.h>
4 #include "sourcemap.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10 namespace SL {
11
12 SourceMap::SourceMap():
13         base_index(0)
14 { }
15
16 void SourceMap::set_name(unsigned i, const std::string &n)
17 {
18         if(source_names.empty())
19                 base_index = i;
20         else if(i<base_index)
21         {
22                 source_names.insert(source_names.begin(), base_index-i, string());
23                 base_index = i;
24         }
25         i -= base_index;
26
27         if(source_names.size()<=i)
28                 source_names.resize(i+1);
29         source_names[i] = n;
30 }
31
32 void SourceMap::merge_from(const SourceMap &other)
33 {
34         if(other.base_index<base_index)
35         {
36                 source_names.insert(source_names.begin(), base_index-other.base_index, string());
37                 base_index = other.base_index;
38         }
39         if(get_count()<other.get_count())
40                 source_names.resize(other.get_count()-base_index);
41
42         for(unsigned i=0; i<other.source_names.size(); ++i)
43                 if(!other.source_names[i].empty())
44                         source_names[i+other.base_index-base_index] = other.source_names[i];
45 }
46
47 string SourceMap::translate_errors(const string &errors) const
48 {
49         static const Regex r_message("^(([0-9]+)\\(([0-9]+)\\) :|ERROR: ([0-9]+):([0-9]+):) (.*)$");
50         vector<string> lines = split(errors, '\n');
51         string translated;
52         for(vector<string>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
53         {
54                 RegMatch m = r_message.match(*i);
55                 if(m)
56                 {
57                         unsigned index = 0;
58                         unsigned line = 0;
59                         if(m[2])
60                         {
61                                 index = lexical_cast<unsigned>(m[2].str);
62                                 line = lexical_cast<unsigned>(m[3].str);
63                         }
64                         else if(m[4])
65                         {
66                                 index = lexical_cast<unsigned>(m[4].str);
67                                 line = lexical_cast<unsigned>(m[5].str);
68                         }
69                         const char *src = "<unknown>";
70                         if(index<source_names.size() && !source_names[index].empty())
71                                 src = source_names[index].c_str();
72                         translated += format("%s:%d: %s", src, line, m[6].str);
73                 }
74                 else
75                         translated += *i;
76                 translated += '\n';
77         }
78
79         return translated;
80 }
81
82 } // namespace SL
83 } // namespace GL
84 } // namespace Msp