-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzu_scanner.l
99 lines (75 loc) · 3.08 KB
/
zu_scanner.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
%option c++ yyclass="zu_scanner" outfile="zu_scanner.cpp"
%option stack noyywrap yylineno 8bit debug
%{
/* $Id: zu_scanner.l,v 1.2 2016/03/16 22:16:34 ist169481 Exp $ */
// make relevant includes before including the parser's tab file
#include <string>
#include <cdk/ast/sequence_node.h>
#include <cdk/ast/expression_node.h>
#include "zu_scanner.h"
#include "zu_parser.tab.h"
// don't change this
#define yyerror LexerError
%}
INTEGER 0|([1-9][0-9]*)
HEX_INT 0x[0-9a-fA-F]+
EXPONENT [eE][+\-]?[0-9]+
DEC \.[0-9]+
DOUBLE [0-9]+(({DEC}?{EXPONENT})|{DEC})
%x X_STRING X_IGNORE_STRING X_COMMENT X_STRING_SPEC
%%
{ set_debug(1); }
"//".* ; /* ignore comments */
\/\* yy_push_state(X_COMMENT);
<X_COMMENT>\*\/ yy_pop_state();
<X_COMMENT>\/\* yy_push_state(X_COMMENT);
<X_COMMENT>.|\n ; /* ignore */
">=" return tGE;
"<=" return tLE;
"==" return tEQ;
"!=" return tNE;
"><" return tBREAK;
"<>" return tCONTINUE;
"!!!" return tRETURN;
"!!" return tPRINTLN;
[#%$] return *yytext; /* zu_types */
/**
* IDENTIFICADORES
*/
[_A-Za-z][A-Za-z0-9_]* yylval.s = new std::string(yytext); return tIDENTIFIER;
/**
* STRINGS
*/
\" { yy_push_state(X_STRING); yylval.s = new std::string(""); }
<X_STRING>\" { yy_pop_state(); return tSTRING; }
<X_STRING>\\ yy_push_state(X_STRING_SPEC);
<X_STRING>\\\" *yylval.s += yytext + 1;
<X_STRING>. *yylval.s += yytext;
<X_STRING>\n yyerror("new line in string");
<X_STRING_SPEC>n { yy_pop_state(); *yylval.s += std::string(1,10); }
<X_STRING_SPEC>r { yy_pop_state(); *yylval.s += std::string(1,13); }
<X_STRING_SPEC>t { yy_pop_state(); *yylval.s += std::string(1,9); }
<X_STRING_SPEC>[\"\\] { yy_pop_state(); *yylval.s += yytext; }
<X_STRING_SPEC>0[0-9a-fA-F]+ { yy_pop_state(); *yylval.s += std::string(1,strtol(yytext, nullptr, 16)); }
<X_STRING_SPEC>[1-9a-fA-F][0-9a-fA-F]* { yy_pop_state(); *yylval.s += std::string(1,strtol(yytext, nullptr, 16)); }
<X_STRING_SPEC>0 { yy_pop_state(); yy_pop_state(); yy_push_state(X_IGNORE_STRING); }
<X_STRING_SPEC>.|\n yyerror("Invalid special character");
<X_IGNORE_STRING>\" { yy_pop_state(); return tSTRING; }
<X_IGNORE_STRING>. ; /* ignore everything else */
<X_IGNORE_STRING>\n yyerror("newline in string");
/**
* REAIS / INTEIROS
*/
{DOUBLE} { yylval.d = strtol(yytext, nullptr, 10); if(errno == ERANGE) yyerror("Invalid number range, overflow occurred"); return tDOUBLE; }
{INTEGER} { yylval.i = strtol(yytext, nullptr, 10); if(errno == ERANGE) yyerror("Invalid number range, overflow occurred"); return tINTEGER; }
{HEX_INT} { yylval.i = strtol(yytext, nullptr, 16); if(errno == ERANGE) yyerror("Invalid number range, overflow occurred"); return tINTEGER; } /* TODO should i ofset to remove the 0x ? */
/**
* SYMBOLS
*/
[,;:()\[\]{}] return *yytext;
[-<>=+*/%&|!?] return *yytext;
[ \t\n]+ ; /* ignore whitespace */
. yyerror("Unknown character");
%%
// Very, very dirty hack: flex is a mess generating C++ scanners.
int zu_scanner::yywrap() { return 1; }