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;
141 else if(*iter=='e' || *iter=='E')
144 if(iter!=source_end && (*iter=='-' || *iter=='+'))
146 require_digit = true;
147 while(iter!=source_end)
152 require_digit = false;
157 if(!require_digit && iter!=source_end && *iter=='f')
160 else if(*iter=='u' || *iter=='f')
164 throw syntax_error(location, number, "Incomplete numeric literal");
165 else if(iter!=source_end && (isalnum(*iter) || *iter=='_'))
166 throw syntax_error(location, number, "Garbage at end of numeric literal");
171 string Tokenizer::parse_string()
173 string str(1, *iter++);
175 while(iter!=source_end)
181 else if(c=='"' && !escape)
190 string Tokenizer::parse_other()
195 string token(1, *iter++);
196 for(unsigned i=1; (i<3 && iter!=source_end); ++i)
198 bool matched = false;
199 for(const Operator *j=Operator::operators; (!matched && j->type); ++j)
201 matched = (j->token[i]==*iter);
202 for(unsigned k=0; (matched && k<i && j->token[k]); ++k)
203 matched = (j->token[k]==token[k]);
215 void Tokenizer::skip_comment_and_whitespace()
217 unsigned comment = 0;
218 while(iter!=source_end)
224 else if(!isspace(*iter))
245 else if(comment==3 && *iter=='*')
257 if(!suppress_line_advance)
259 allow_preprocess = (comment<3);
265 suppress_line_advance = false;