1 |
/* |
/* |
2 |
* Copyright (c) 2014 Christian Schoenebeck and Andreas Persson |
* Copyright (c) 2014-2017 Christian Schoenebeck and Andreas Persson |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
7 |
* See README file for details. |
* See README file for details. |
8 |
*/ |
*/ |
9 |
|
|
10 |
/* Token scanner for instrument script language. */ |
/* Token scanner for NKSP real-time instrument script language. */ |
|
|
|
|
/* FIXME: line numbers (i.e. on error/warning messages) are incorrect, because |
|
|
the current grammar does not process new line characters. \n is currently |
|
|
filtered out in this lexer. Due to this, the parser will sometimes read |
|
|
several lines before it matches the next complete grammar rule, causing the |
|
|
incorrect lines in the error/warning messages. */ |
|
11 |
|
|
12 |
%{ |
%{ |
13 |
|
|
20 |
{ \ |
{ \ |
21 |
yylloc->first_line = yylineno; \ |
yylloc->first_line = yylineno; \ |
22 |
yylloc->last_line = yylineno; \ |
yylloc->last_line = yylineno; \ |
23 |
/* first_column = TODO */ \ |
yylloc->first_column = yycolumn; \ |
24 |
/* last_column = TODO */ \ |
yycolumn += yyleng; \ |
25 |
|
yylloc->last_column = yycolumn - 1; \ |
26 |
/*printf("lex: line '%s'\n", yytext);*/ \ |
/*printf("lex: line '%s'\n", yytext);*/ \ |
27 |
} |
} |
28 |
// custom (f)lex input for reading from std::istream object |
// custom (f)lex input for reading from std::istream object |
38 |
} |
} |
39 |
|
|
40 |
static void scanner_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err) { |
static void scanner_error(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* err) { |
41 |
context->addErr(locp->first_line, err); |
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) { |
static void scanner_warning(YYLTYPE* locp, LinuxSampler::ParserContext* context, const char* txt) { |
45 |
context->addWrn(locp->first_line, txt); |
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) |
#define SCANNER_ERR(txt) scanner_error(yylloc, yyextra, txt) |
49 |
#define SCANNER_WRN(txt) scanner_warning(yylloc, yyextra, txt) |
#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; |
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 |
%} |
%} |
80 |
|
|
81 |
/* use Flex's built-in support for line numbers */ |
/* use Flex's built-in support for line numbers |
82 |
%option yylineno |
(disabled, because it seems to be unreliable, so we are using our own |
83 |
|
tracking code in the respective scanner rules below) */ |
84 |
|
/*%option yylineno*/ |
85 |
/* generate a reentrant safe scanner */ |
/* generate a reentrant safe scanner */ |
86 |
%option reentrant |
%option reentrant |
87 |
/* avoid symbol collision with other (i.e. future) scanner symbols */ |
/* avoid symbol collision with other (i.e. future) scanner symbols */ |
101 |
%x PREPROC_SET_COND PREPROC_RESET_COND PREPROC_IF PREPROC_IF_NOT PREPROC_BODY_EAT PREPROC_PRE_BODY_USE PREPROC_PRE_BODY_EAT |
%x PREPROC_SET_COND PREPROC_RESET_COND PREPROC_IF PREPROC_IF_NOT PREPROC_BODY_EAT PREPROC_PRE_BODY_USE PREPROC_PRE_BODY_EAT |
102 |
|
|
103 |
DIGIT [0-9] |
DIGIT [0-9] |
104 |
ID [a-zA-Z0-9_]* |
ID [a-zA-Z0-9_]+ |
105 |
|
|
106 |
%% |
%% |
107 |
|
|
116 |
return INTEGER; |
return INTEGER; |
117 |
} |
} |
118 |
|
|
119 |
{DIGIT}+"."{DIGIT}* { |
/* there is currently no support for floating point numbers in NKSP yet */ |
120 |
|
/*{DIGIT}+"."{DIGIT}* { |
121 |
printf("A float: %s (%g)\n", yytext, atof(yytext)); |
printf("A float: %s (%g)\n", yytext, atof(yytext)); |
122 |
} |
}*/ |
123 |
|
|
124 |
|
|
125 |
/* Preprocessor statement: SET_CONDITION(name) */ |
/* Preprocessor statement: SET_CONDITION(name) */ |
212 |
//printf("-->END_USE_CODE\n"); |
//printf("-->END_USE_CODE\n"); |
213 |
yy_pop_state(yyscanner); |
yy_pop_state(yyscanner); |
214 |
} |
} |
215 |
<PREPROC_BODY_EAT>[ \t\r\n]* /* eat up code block filtered out by preprocessor */ |
<PREPROC_BODY_EAT>[ \t\r\n]* { /* eat up code block filtered out by preprocessor */ |
216 |
<PREPROC_BODY_EAT>.* /* eat up code block filtered out by preprocessor */ |
processLocation(); |
217 |
|
} |
218 |
|
<PREPROC_BODY_EAT>.* { /* eat up code block filtered out by preprocessor */ |
219 |
|
yyextra->addPreprocessorComment(yylloc->first_line, yylloc->last_line, |
220 |
|
yylloc->first_column+1, yylloc->last_column+1); |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
/* Language keywords */ |
/* Language keywords */ |
230 |
"declare" return DECLARE; |
"declare" return DECLARE; |
231 |
"while" return WHILE; |
"while" return WHILE; |
232 |
"if" return IF; |
"if" return IF; |
233 |
|
".or." return BITWISE_OR; |
234 |
"or" return OR; |
"or" return OR; |
235 |
"release" return RELEASE; |
"release" return RELEASE; |
236 |
|
".and." return BITWISE_AND; |
237 |
"and" return AND; |
"and" return AND; |
238 |
|
".not." return BITWISE_NOT; |
239 |
"not" return NOT; |
"not" return NOT; |
240 |
"else" return ELSE; |
"else" return ELSE; |
241 |
"controller" return CONTROLLER; |
"controller" return CONTROLLER; |
247 |
"const" return CONST_; // note: "CONST" is already defined for C/C++ compilers on Windows by default |
"const" return CONST_; // note: "CONST" is already defined for C/C++ compilers on Windows by default |
248 |
"polyphonic" return POLYPHONIC; |
"polyphonic" return POLYPHONIC; |
249 |
"mod" return MOD; |
"mod" return MOD; |
250 |
|
"function" return FUNCTION; |
251 |
|
"call" return CALL; |
252 |
|
"synchronized" return SYNCHRONIZED; |
253 |
|
|
254 |
on|end|note|init|declare|if|then|begin|end|procedure|function { |
[&,()[\]<>=*+#/-] { |
255 |
printf("A keyword: %s\n", yytext); |
return *yytext; |
256 |
} |
} |
257 |
|
|
258 |
[&,()[\]<>=*+#/-] { return *yytext; } |
("$"|"@"|"%"){ID} { |
|
|
|
|
("$"|"@"){ID} { |
|
|
yylval->sValue = strdup(yytext); |
|
|
return VARIABLE; |
|
|
} |
|
|
|
|
|
"%"{ID} { |
|
259 |
yylval->sValue = strdup(yytext); |
yylval->sValue = strdup(yytext); |
260 |
return VARIABLE; |
return VARIABLE; |
261 |
} |
} |
268 |
":=" return ASSIGNMENT; |
":=" return ASSIGNMENT; |
269 |
|
|
270 |
\n+ { |
\n+ { |
271 |
|
yylineno += countNewLineChars(yytext); |
272 |
|
yycolumn = 0; |
273 |
//printf("lex: new line %d\n", yylineno, yytext); |
//printf("lex: new line %d\n", yylineno, yytext); |
274 |
//return LF; |
//return LF; |
275 |
} |
} |
276 |
|
|
277 |
"{"[^}]*"}" /* eat up comments */ |
"{"[^}]*"}" { /* eat up comments */ |
278 |
|
processLocation(); |
279 |
|
} |
280 |
|
|
281 |
[ \t\r]+ /* eat up whitespace */ |
[ \t\r]+ /* eat up whitespace */ |
282 |
|
|
283 |
"..." /* eat up */ |
. { |
284 |
|
printf( "Unrecognized character: '%s' (line %d, column %d)\n", yytext, yylineno, yycolumn); |
285 |
. printf( "Unrecognized character: %s\n", yytext ); |
return UNKNOWN_CHAR; |
286 |
|
} |
287 |
|
|
288 |
%% |
%% |
289 |
|
|