1 #include <msp/core/raii.h>
2 #include "glsl_error.h"
3 #include "preprocessor.h"
13 Tokenizer::Tokenizer():
14 allow_preprocess(true),
15 suppress_line_advance(false)
19 source_end = empty.end();
22 void Tokenizer::begin(const string &name, const string &src)
25 source_end = src.end();
28 allow_preprocess = true;
33 const string &Tokenizer::peek_token(unsigned index)
35 while(next_tokens.size()<=index)
36 next_tokens.push_back(parse_token_());
37 return (last_token = next_tokens[index]);
40 const string &Tokenizer::parse_token()
42 if(!next_tokens.empty())
44 last_token = next_tokens.front();
45 next_tokens.pop_front();
49 return (last_token = parse_token_());
52 void Tokenizer::expect(const string &token)
54 string parsed = parse_token();
56 throw parse_error(location, parsed, format("'%s'", token));
59 void Tokenizer::set_location(const Location &loc)
64 string Tokenizer::parse_token_()
68 skip_comment_and_whitespace();
69 bool allow_preproc = allow_preprocess;
70 allow_preprocess = false;
73 else if(allow_preproc && *iter=='#')
78 else if(isalpha(*iter) || *iter=='_')
79 return parse_identifier();
80 else if(isdigit(*iter))
81 return parse_number();
83 return parse_string();
84 else if(*iter=='#' || *iter=='$' || *iter=='\'' || *iter=='@' || *iter=='\\' || *iter=='`')
85 throw syntax_error(location, string(1, *iter), "Invalid character in source");
91 void Tokenizer::preprocess()
93 SetForScope<deque<string> > clear_tokens(next_tokens, deque<string>());
95 string::const_iterator line_end = iter;
96 for(; (line_end!=source_end && *line_end!='\n'); ++line_end) ;
97 SetForScope<string::const_iterator> stop_at_line_end(source_end, line_end);
99 signal_preprocess.emit();
104 string Tokenizer::parse_identifier()
107 while(iter!=source_end)
109 if(isalnum(*iter) || *iter=='_')
118 string Tokenizer::parse_number()
120 bool got_fract = false;
122 while(iter!=source_end)
126 else if(!got_fract && *iter=='.')
135 bool require_digit = false;
136 if(iter!=source_end && (*iter=='e' || *iter=='E'))
139 if(iter!=source_end && (*iter=='-' || *iter=='+'))
141 require_digit = true;
142 while(iter!=source_end)
147 require_digit = false;
155 throw syntax_error(location, number, "Incomplete numeric literal");
156 else if(isalnum(*iter) || *iter=='_')
157 throw syntax_error(location, number, "Garbage at end of numeric literal");
162 string Tokenizer::parse_string()
164 string str(1, *iter++);
166 while(iter!=source_end)
172 else if(c=='"' && !escape)
181 string Tokenizer::parse_other()
186 string token(1, *iter++);
187 for(unsigned i=1; (i<3 && iter!=source_end); ++i)
189 bool matched = false;
190 for(const Operator *j=Operator::operators; (!matched && j->type); ++j)
192 matched = (j->token[i]==*iter);
193 for(unsigned k=0; (matched && k<i && j->token[k]); ++k)
194 matched = (j->token[k]==token[k]);
206 void Tokenizer::skip_comment_and_whitespace()
208 unsigned comment = 0;
209 while(iter!=source_end)
215 else if(!isspace(*iter))
236 else if(comment==3 && *iter=='*')
248 if(!suppress_line_advance)
250 allow_preprocess = (comment<3);
256 suppress_line_advance = false;