1 |
/* |
2 |
* Copyright (c) 2014-2017 Christian Schoenebeck and Andreas Persson |
3 |
* |
4 |
* http://www.linuxsampler.org |
5 |
* |
6 |
* This file is part of LinuxSampler and released under the same terms. |
7 |
* See README file for details. |
8 |
*/ |
9 |
|
10 |
/* Token scanner for NKSP real-time instrument script language. */ |
11 |
|
12 |
%{ |
13 |
|
14 |
#include "parser_shared.h" |
15 |
#include <math.h> |
16 |
// reentrant scanner data context |
17 |
#define YY_EXTRA_TYPE ParserContext* |
18 |
// set line number each time a token is recognized |
19 |
#define YY_USER_ACTION \ |
20 |
{ \ |
21 |
yylloc->first_line = yylineno; \ |
22 |
yylloc->last_line = yylineno; \ |
23 |
yylloc->first_column = yycolumn; \ |
24 |
yycolumn += yyleng; \ |
25 |
yylloc->last_column = yycolumn - 1; \ |
26 |
/*printf("lex: line '%s'\n", yytext);*/ \ |
27 |
} |
28 |
// custom (f)lex input for reading from std::istream object |
29 |
#define YY_INPUT(buf,result,max_size) \ |
30 |
{ \ |
31 |
char c = yyextra->is->get(); \ |
32 |
if (yyextra->is->eof()) \ |
33 |
result = YY_NULL; \ |
34 |
else { \ |
35 |
buf[0] = c; \ |
36 |
result = 1; \ |
37 |
} \ |
38 |
} |
39 |
|
40 |
static void scanner_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err) { |
41 |
context->addErr(locp->first_line, locp->last_line, locp->first_column, locp->last_column, err); |
42 |
} |
43 |
|
44 |
static void scanner_warning(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* txt) { |
45 |
context->addWrn(locp->first_line, locp->last_line, locp->first_column, locp->last_column, txt); |
46 |
} |
47 |
|
48 |
#define SCANNER_ERR(txt) scanner_error(yylloc, yyextra, txt) |
49 |
#define SCANNER_WRN(txt) scanner_warning(yylloc, yyextra, txt) |
50 |
|
51 |
// shut up warning that 'register' keyword is deprecated as of C++11 |
52 |
#if defined(__cplusplus) && __cplusplus >= 201103L |
53 |
# define register |
54 |
#endif |
55 |
|
56 |
using namespace LinuxSampler; |
57 |
|
58 |
static int countNewLineChars(const char* txt) { |
59 |
int n = 0; |
60 |
for (int i = 0; txt[i]; ++i) |
61 |
if (txt[i] == '\n') ++n; |
62 |
return n; |
63 |
} |
64 |
|
65 |
static int countCharsPastLastNewLine(const char* txt) { |
66 |
const int n = (int)strlen(txt); |
67 |
for (int i = n - 1; i >= 0; --i) |
68 |
if (txt[i] == '\n') |
69 |
return n - i - 1; |
70 |
return n; |
71 |
} |
72 |
|
73 |
#define processLocation() { \ |
74 |
const int nl = countNewLineChars(yytext); \ |
75 |
yylineno += nl; \ |
76 |
if (nl) yycolumn = countCharsPastLastNewLine(yytext); \ |
77 |
} |
78 |
|
79 |
// if compiled for debugging, throw an exception instead of exiting on fatal |
80 |
// lexer errors (so the debugger may pause with the appropriate back trace) |
81 |
#if DEBUG |
82 |
# include <stdexcept> |
83 |
# define YY_FATAL_ERROR(msg) throw std::runtime_error(msg) |
84 |
#endif |
85 |
|
86 |
%} |
87 |
|
88 |
/* use Flex's built-in support for line numbers |
89 |
(disabled, because it seems to be unreliable, so we are using our own |
90 |
tracking code in the respective scanner rules below) */ |
91 |
/*%option yylineno*/ |
92 |
/* generate a reentrant safe scanner */ |
93 |
%option reentrant |
94 |
/* avoid symbol collision with other (i.e. future) scanner symbols */ |
95 |
%option prefix="InstrScript_" |
96 |
/* bison-bridge adds an argument yylval to yylex, and bison-locations adds an |
97 |
argument code yylloc for location tracking. */ |
98 |
%option bison-bridge |
99 |
%option bison-locations |
100 |
/* yywrap() would be called at EOF, we don't need it */ |
101 |
%option noyywrap |
102 |
/* enable functions yy_push_state(), yy_pop_state(), yy_top_state() */ |
103 |
%option stack |
104 |
|
105 |
/* inclusive scanner conditions */ |
106 |
%s PREPROC_BODY_USE |
107 |
/* exclusive scanner conditions */ |
108 |
%x PREPROC_SET_COND PREPROC_RESET_COND PREPROC_IF PREPROC_IF_NOT PREPROC_BODY_EAT PREPROC_PRE_BODY_USE PREPROC_PRE_BODY_EAT |
109 |
|
110 |
DIGIT [0-9] |
111 |
ID [a-zA-Z0-9_]+ |
112 |
|
113 |
%% |
114 |
|
115 |
\"[^"]*\" { |
116 |
yylval->sValue = strdup(yytext + 1); |
117 |
yylval->sValue[strlen(yylval->sValue) - 1] = '\0'; |
118 |
return STRING; |
119 |
} |
120 |
|
121 |
{DIGIT}+ { |
122 |
if (sizeof(vmint) < 8) |
123 |
yylval->iValue = atoi(yytext); |
124 |
else |
125 |
yylval->iValue = atoll(yytext); |
126 |
return INTEGER; |
127 |
} |
128 |
|
129 |
/* there is currently no support for floating point numbers in NKSP yet */ |
130 |
/*{DIGIT}+"."{DIGIT}* { |
131 |
printf("A float: %s (%g)\n", yytext, atof(yytext)); |
132 |
}*/ |
133 |
|
134 |
|
135 |
/* Preprocessor statement: SET_CONDITION(name) */ |
136 |
|
137 |
<*>"SET_CONDITION"[ \t]*"(" { |
138 |
//printf("SET_CONDITION\n"); |
139 |
yy_push_state(PREPROC_SET_COND, yyscanner); |
140 |
} |
141 |
<PREPROC_SET_COND>{ID} { |
142 |
//printf("preproc set condition '%s'\n", yytext); |
143 |
bool success = yyextra->setPreprocessorCondition(yytext); |
144 |
if (!success) { |
145 |
SCANNER_WRN((String("Preprocessor: Condition '") + |
146 |
yytext + "' is already set.").c_str()); |
147 |
} |
148 |
} |
149 |
<PREPROC_SET_COND>[ \t]*")" { |
150 |
//printf("End of PREPROC_SET_COND\n"); |
151 |
yy_pop_state(yyscanner); |
152 |
} |
153 |
|
154 |
|
155 |
/* Preprocessor statement: RESET_CONDITION(name) */ |
156 |
|
157 |
<*>"RESET_CONDITION"[ \t]*"(" { |
158 |
//printf("RESET_CONDITION\n"); |
159 |
yy_push_state(PREPROC_RESET_COND, yyscanner); |
160 |
} |
161 |
<PREPROC_RESET_COND>{ID} { |
162 |
//printf("preproc reset condition '%s'\n", yytext); |
163 |
bool success = yyextra->resetPreprocessorCondition(yytext); |
164 |
if (!success) { |
165 |
SCANNER_ERR((String("Preprocessor: could not reset condition '") + |
166 |
yytext + "' (either not set or a built-in condition).").c_str()); |
167 |
} |
168 |
} |
169 |
<PREPROC_RESET_COND>[ \t]*")" { |
170 |
//printf("End of RESET_CONDITION\n"); |
171 |
yy_pop_state(yyscanner); |
172 |
} |
173 |
|
174 |
|
175 |
/* Preprocessor conditional statements: |
176 |
|
177 |
USE_CODE_IF(name) |
178 |
... |
179 |
END_USE_CODE |
180 |
|
181 |
and: |
182 |
|
183 |
USE_CODE_IF_NOT(name) |
184 |
... |
185 |
END_USE_CODE |
186 |
*/ |
187 |
|
188 |
<*>"USE_CODE_IF"[ \t]*"(" { |
189 |
//printf("{%s}\n", yytext); |
190 |
yy_push_state(PREPROC_IF, yyscanner); |
191 |
} |
192 |
<PREPROC_BODY_EAT>"USE_CODE_IF"[ \t]*"("{ID}")" { |
193 |
//printf("[EAT{%s}\n", yytext); |
194 |
yy_push_state(PREPROC_BODY_EAT, yyscanner); |
195 |
} |
196 |
<*>"USE_CODE_IF_NOT"[ \t]*"(" { |
197 |
//printf("USE_CODE_IF_NOT\n"); |
198 |
yy_push_state(PREPROC_IF_NOT, yyscanner); |
199 |
} |
200 |
<PREPROC_BODY_EAT>"USE_CODE_IF_NOT"[ \t]*"("{ID}")" { |
201 |
//printf("[EAT{%s}\n", yytext); |
202 |
yy_push_state(PREPROC_BODY_EAT, yyscanner); |
203 |
} |
204 |
<PREPROC_IF>{ID} { |
205 |
//printf("preproc use code if '%s'\n", yytext); |
206 |
yy_pop_state(yyscanner); |
207 |
if (yyextra->isPreprocessorConditionSet(yytext)) |
208 |
yy_push_state(PREPROC_PRE_BODY_USE, yyscanner); |
209 |
else |
210 |
yy_push_state(PREPROC_PRE_BODY_EAT, yyscanner); |
211 |
} |
212 |
<PREPROC_IF_NOT>{ID} { |
213 |
//printf("preproc use code if not '%s'\n", yytext); |
214 |
yy_pop_state(yyscanner); |
215 |
if (!yyextra->isPreprocessorConditionSet(yytext)) |
216 |
yy_push_state(PREPROC_PRE_BODY_USE, yyscanner); |
217 |
else |
218 |
yy_push_state(PREPROC_PRE_BODY_EAT, yyscanner); |
219 |
} |
220 |
<PREPROC_PRE_BODY_USE>[ \t]*")" { |
221 |
yy_pop_state(yyscanner); |
222 |
yy_push_state(PREPROC_BODY_USE, yyscanner); |
223 |
} |
224 |
<PREPROC_PRE_BODY_EAT>[ \t]*")" { |
225 |
//printf("PREPROCESSOR EAT : {%s}\n", yytext); |
226 |
yy_pop_state(yyscanner); |
227 |
yy_push_state(PREPROC_BODY_EAT, yyscanner); |
228 |
} |
229 |
<PREPROC_BODY_EAT,PREPROC_BODY_USE>"END_USE_CODE" { |
230 |
//printf("-->END_USE_CODE\n"); |
231 |
yy_pop_state(yyscanner); |
232 |
} |
233 |
<PREPROC_BODY_EAT>[ \t\r\n]* { /* eat up code block filtered out by preprocessor */ |
234 |
//printf("PREPROCESSOR EAT2 : {%s}\n", yytext); |
235 |
processLocation(); |
236 |
} |
237 |
<PREPROC_BODY_EAT>.* { /* eat up code block filtered out by preprocessor */ |
238 |
//printf("PREPROCESSOR EAT3 : {%s}\n", yytext); |
239 |
yyextra->addPreprocessorComment(yylloc->first_line, yylloc->last_line, |
240 |
yylloc->first_column+1, yylloc->last_column+1); |
241 |
} |
242 |
|
243 |
|
244 |
/* Language keywords */ |
245 |
|
246 |
"on" return ON; |
247 |
"end" return END; |
248 |
"note" return NOTE; |
249 |
"init" return INIT; |
250 |
"declare" return DECLARE; |
251 |
"while" return WHILE; |
252 |
"if" return IF; |
253 |
".or." return BITWISE_OR; |
254 |
"or" return OR; |
255 |
"release" return RELEASE; |
256 |
".and." return BITWISE_AND; |
257 |
"and" return AND; |
258 |
".not." return BITWISE_NOT; |
259 |
"not" return NOT; |
260 |
"else" return ELSE; |
261 |
"controller" return CONTROLLER; |
262 |
"case" return CASE; |
263 |
"select" return SELECT; |
264 |
"to" return TO; |
265 |
"<=" return LE; |
266 |
">=" return GE; |
267 |
"const" return CONST_; // note: "CONST" is already defined for C/C++ compilers on Windows by default |
268 |
"polyphonic" return POLYPHONIC; |
269 |
"mod" return MOD; |
270 |
"function" return FUNCTION; |
271 |
"call" return CALL; |
272 |
"synchronized" return SYNCHRONIZED; |
273 |
|
274 |
[&,()[\]<>=*+#/-] { |
275 |
return *yytext; |
276 |
} |
277 |
|
278 |
("$"|"@"|"%"){ID} { |
279 |
yylval->sValue = strdup(yytext); |
280 |
return VARIABLE; |
281 |
} |
282 |
|
283 |
{ID} { |
284 |
yylval->sValue = strdup(yytext); |
285 |
return IDENTIFIER; |
286 |
} |
287 |
|
288 |
":=" return ASSIGNMENT; |
289 |
|
290 |
\n+ { |
291 |
yylineno += countNewLineChars(yytext); |
292 |
yycolumn = 0; |
293 |
//printf("lex: new line %d\n", yylineno, yytext); |
294 |
//return LF; |
295 |
} |
296 |
|
297 |
"{"[^}]*"}" { /* eat up comments */ |
298 |
processLocation(); |
299 |
} |
300 |
|
301 |
[ \t\r]+ /* eat up whitespace */ |
302 |
|
303 |
. { |
304 |
printf( "Unrecognized character: '%s' (line %d, column %d)\n", yytext, yylineno, yycolumn); |
305 |
return UNKNOWN_CHAR; |
306 |
} |
307 |
|
308 |
%% |
309 |
|
310 |
namespace LinuxSampler { |
311 |
|
312 |
void ParserContext::createScanner(std::istream* is) { |
313 |
if (scanner) destroyScanner(); |
314 |
this->is = is; |
315 |
yylex_init(&scanner); |
316 |
yyset_extra(this, scanner); |
317 |
} |
318 |
|
319 |
void ParserContext::destroyScanner() { |
320 |
if (!scanner) return; |
321 |
yylex_destroy(scanner); |
322 |
scanner = NULL; |
323 |
} |
324 |
|
325 |
} // namespace LinuxSampler |