1 #include <msp/strings/utils.h>
2 #include "glsl_error.h"
3 #include "preprocessor.h"
12 Preprocessor::Preprocessor(Tokenizer &t):
16 void Preprocessor::preprocess()
18 string token = tokenizer.peek_token();
21 else if(token=="version")
23 else if(token=="line")
25 else if(token=="define" || token=="undef" || token=="if" || token=="ifdef" || token=="ifndef" || token=="else" ||
26 token=="elif" || token=="endif" || token=="error" || token=="extension")
27 throw invalid_shader_source(tokenizer.get_location(), "Unsupported preprocessor directive '%s'", token);
28 else if(!token.empty())
29 throw parse_error(tokenizer.get_location(), token, "a preprocessor directive");
32 void Preprocessor::preprocess_version()
34 tokenizer.expect("version");
35 string token = tokenizer.parse_token();
36 unsigned version = lexical_cast<unsigned>(token);
37 signal_version.emit(Version(version/100, version%100));
39 token = tokenizer.parse_token();
41 throw parse_error(tokenizer.get_location(), token, "end of line");
44 void Preprocessor::preprocess_line()
46 tokenizer.expect("line");
47 unsigned line_number = lexical_cast<unsigned>(tokenizer.parse_token());
49 int source_index = -1;
50 string token = tokenizer.parse_token();
52 source_index = lexical_cast<unsigned>(token);
54 token = tokenizer.parse_token();
56 throw parse_error(tokenizer.get_location(), token, "end of line");
58 signal_line.emit(source_index, line_number);
61 void Preprocessor::preprocess_pragma()
63 tokenizer.expect("pragma");
64 string token = tokenizer.parse_token();
66 preprocess_pragma_msp();
69 void Preprocessor::preprocess_pragma_msp()
71 string token = tokenizer.peek_token();
74 else if(token=="source")
77 throw invalid_shader_source(tokenizer.get_location(), "Unrecognized MSP pragma '%s'", token);
79 token = tokenizer.parse_token();
81 throw parse_error(tokenizer.get_location(), token, "end of line");
84 void Preprocessor::preprocess_stage()
86 tokenizer.expect("stage");
87 tokenizer.expect("(");
88 string token = tokenizer.parse_token();
89 Stage::Type stage = Stage::SHARED;
91 stage = Stage::VERTEX;
92 else if(token=="geometry")
93 stage = Stage::GEOMETRY;
94 else if(token=="fragment")
95 stage = Stage::FRAGMENT;
97 throw parse_error(tokenizer.get_location(), token, "stage identifier");
98 tokenizer.expect(")");
100 signal_stage_change.emit(stage);
103 void Preprocessor::preprocess_source()
105 tokenizer.expect("source");
106 tokenizer.expect("(");
107 unsigned source_index = lexical_cast<unsigned>(tokenizer.parse_token());
108 tokenizer.expect(",");
109 string filename = tokenizer.parse_token();
111 throw parse_error(tokenizer.get_location(), filename, "a string literal");
112 filename = c_unescape(filename.substr(1, filename.size()-2));
113 tokenizer.expect(")");
115 signal_source.emit(source_index, filename);