-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlex.l
136 lines (113 loc) · 3.68 KB
/
lex.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
%{
#define YY_NO_UNISTD_H
#include "Any.h"
#include "AST.h"
struct TerminalSymbol {
string lexem;
int line;
TerminalSymbol(): line(0){}
TerminalSymbol(const string& _lexem, int _line): lexem(_lexem), line(_line){}
};
#define YYSTYPE Any
static void onTokenFound(bool isAttr);
int isatty(int );
int getTokenLine();
#include "yacc.h"
%}
Number [0-9]+
Number16 0x[0-9a-fA-F]+
Float {Number}?\.{Number}
NumberOrFloat ({Number}|{Float})
EFloat {NumberOrFloat}[eE]\-?{NumberOrFloat}
%%
"--"[^\n]*\n {onTokenFound(false);}
[ \t]+ {onTokenFound(false);}
"+" {onTokenFound(false); return OP_ADD; }
"-" {onTokenFound(false); return OP_SUB; }
"*" {onTokenFound(false); return OP_MUL; }
"/" {onTokenFound(false); return OP_DIV; }
"%" {onTokenFound(false); return OP_MOD; }
"^" {onTokenFound(false); return OP_POWER; }
"#" {onTokenFound(false); return OP_LEN; }
"==" {onTokenFound(false); return OP_EQUAL; }
"~=" {onTokenFound(false); return OP_NEQUAL; }
"<=" {onTokenFound(false); return OP_LEQUAL; }
">=" {onTokenFound(false); return OP_GEQUAL; }
"<" {onTokenFound(false); return OP_LESS; }
">" {onTokenFound(false); return OP_GREATER; }
"=" {onTokenFound(false); return OP_ASSIGN; }
".." {onTokenFound(false); return OP_CONCAT; }
"..." {onTokenFound(false); return OP_ARGS; }
"and" {onTokenFound(false); return AND;}
"break" {onTokenFound(false); return BREAK;}
"continue" {onTokenFound(false); return CONTINUE;}
"do" {onTokenFound(false); return DO;}
"else" {onTokenFound(false); return ELSE;}
"elseif" {onTokenFound(false); return ELSEIF;}
"end" {onTokenFound(false); return END;}
"false" {onTokenFound(false); return FALSE;}
"for" {onTokenFound(false); return FOR;}
"function" {onTokenFound(false); return FUNCTION;}
"if" {onTokenFound(false); return IF;}
"in" {onTokenFound(false); return IN;}
"local" {onTokenFound(false); return LOCAL;}
"nil" {onTokenFound(false); return NIL;}
"not" {onTokenFound(false); return NOT;}
"or" {onTokenFound(false); return OR;}
"repeat" {onTokenFound(false); return REPEAT;}
"return" {onTokenFound(false); return RETURN;}
"then" {onTokenFound(false); return THEN;}
"true" {onTokenFound(false); return TRUE;}
"until" {onTokenFound(false); return UNTIL;}
"while" {onTokenFound(false); return WHILE;}
\"([^\\\"]|\\.)*\" { onTokenFound(true);return LITERAL1;}
'([^\\']|\\.)*' { onTokenFound(true);return LITERAL2;}
"[["([^\]]|\][^\]])*"]]" { onTokenFound(true);return LITERAL3;}
{Number16} { onTokenFound(true); return NUMBER1; }
{NumberOrFloat} { onTokenFound(true); return NUMBER2; }
{EFloat} { onTokenFound(true); return NUMBER3; }
[_a-zA-Z][_a-zA-Z0-9]* { onTokenFound(true); return ID; }
\n+ {
onTokenFound(false);
}
. { onTokenFound(false); return yytext[0]; }
%%
static int g_line = 1;
static int g_column = 1;
static int g_parenthesisLevel = 0;
static int g_bracketLevel = 0;
static int g_braceLevel = 0;
static void onTokenFound(bool isAttr) {
if (isAttr) yylval = TerminalSymbol(string(yytext, yytext + yyleng), g_line);
g_column += yyleng;
for (int i = 0; i < (int)yyleng; ++i) {
switch (yytext[i]) {
case '\n':
++g_line;
g_column = 1;
break;
case '[': ++g_bracketLevel; break;
case ']': --g_bracketLevel; break;
case '(': ++g_parenthesisLevel; break;
case ')': --g_parenthesisLevel; break;
case '{': ++g_braceLevel; break;
case '}': --g_braceLevel; break;
default: break;
}
}
}
int getTokenLine() {
return g_line;
}
void yyerror(const char *msg) {
printf("yyerror [%d-%d]: %s\n", g_line, g_column, msg);
}
int yywrap() {
g_line = 1;
g_column = 1;
(void)yyunput;
return 1;
}
int isatty(int ) {
return 0;
}