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 &src, const string &name)
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 next_tokens[index];
40 const string &Tokenizer::parse_token()
44 if(!next_tokens.empty())
46 last_token = next_tokens.front();
47 next_tokens.pop_front();
51 return (last_token = parse_token_());
54 void Tokenizer::expect(const string &token)
56 string parsed = parse_token();
58 throw parse_error(location, parsed, format("'%s'", token));
61 void Tokenizer::set_location(const Location &loc)
64 suppress_line_advance = true;
67 string Tokenizer::parse_token_()
71 skip_comment_and_whitespace();
72 bool allow_preproc = allow_preprocess;
73 allow_preprocess = false;
76 else if(allow_preproc && *iter=='#')
81 else if(isalpha(*iter) || *iter=='_')
82 return parse_identifier();
83 else if(isdigit(*iter))
84 return parse_number();
86 return parse_string();
87 else if(*iter=='#' || *iter=='$' || *iter=='\'' || *iter=='@' || *iter=='\\' || *iter=='`')
88 throw syntax_error(location, string(1, *iter), "Invalid character in source");
94 void Tokenizer::preprocess()
96 SetForScope<deque<string> > clear_tokens(next_tokens, deque<string>());
98 string::const_iterator line_end = iter;
99 for(; (line_end!=source_end && *line_end!='\n'); ++line_end) ;
100 SetForScope<string::const_iterator> stop_at_line_end(source_end, line_end);
102 signal_preprocess.emit();
107 string Tokenizer::parse_identifier()
110 while(iter!=source_end)
112 if(isalnum(*iter) || *iter=='_')
121 string Tokenizer::parse_number()
123 bool got_fract = false;
125 while(iter!=source_end)
129 else if(!got_fract && *iter=='.')
138 bool require_digit = false;
139 if(iter!=source_end && (*iter=='e' || *iter=='E'))
142 if(iter!=source_end && (*iter=='-' || *iter=='+'))
144 require_digit = true;
145 while(iter!=source_end)
150 require_digit = false;
158 throw syntax_error(location, number, "Incomplete numeric literal");
159 else if(isalnum(*iter) || *iter=='_')
160 throw syntax_error(location, number, "Garbage at end of numeric literal");
165 string Tokenizer::parse_string()
167 string str(1, *iter++);
169 while(iter!=source_end)
175 else if(c=='"' && !escape)
184 string Tokenizer::parse_other()
189 string token(1, *iter++);
190 for(unsigned i=1; (i<3 && iter!=source_end); ++i)
192 bool matched = false;
193 for(const Operator *j=Operator::operators; (!matched && j->type); ++j)
195 matched = (j->token[i]==*iter);
196 for(unsigned k=0; (matched && k<i && j->token[k]); ++k)
197 matched = (j->token[k]==token[k]);
209 void Tokenizer::skip_comment_and_whitespace()
211 unsigned comment = 0;
212 while(iter!=source_end)
218 else if(!isspace(*iter))
239 else if(comment==3 && *iter=='*')
251 if(!suppress_line_advance)
253 allow_preprocess = (comment<3);
259 suppress_line_advance = false;