-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.l
213 lines (191 loc) · 6.37 KB
/
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
%{
#include <cerrno>
#include <climits>
#include <cstdlib>
#include <cstring> // strerror
#include <string>
#include <iostream>
#include "driver.hh"
#include "parser.hh"
%}
%option noyywrap nounput noinput batch debug
%option c++
%option yyclass="Scanner"
%{
// Code definitions at the end of scanner.cpp
// A number symbol corresponding to the value in S.
yy::parser::symbol_type make_NUMBER(
const std::string &s,
const yy::parser::location_type& loc
);
yy::parser::symbol_type make_REAL(
const std::string &s,
const yy::parser::location_type& loc
);
yy::parser::symbol_type make_CHARLITERAL(
const std::string &s,
const yy::parser::location_type& loc
);
yy::parser::symbol_type make_SIZE_T(
const std::string &s,
const yy::parser::location_type& loc
);
void Scanner::UpdateLocation() {
if (driver.location_debug) {
std::cerr << "Action called " << driver.location << std::endl;
}
driver.location.columns(yyleng);
}
%}
id [a-zA-Z][a-zA-Z_0-9]*
int [0-9]+
float [0-9]+\.[0-9]+
size_t [0-9]+usize
blank [ \t\r]
string_literal \"([^\\\"]|\\.)*\"
char_literal '[^'\\\n]'
comment_line "//".*"\n"
%{
// Code run each time a pattern is matched.
#define YY_USER_ACTION UpdateLocation();
%}
%%
%{
// A handy shortcut to the location held by the driver.
yy::location& loc = driver.location;
if (driver.location_debug) {
// Code run each time yylex is called.
std::cerr << "BEFORE " << loc << std::endl;
}
// loc.step();
if (driver.location_debug) {
std::cerr << "AFTER " << loc << std::endl;
}
%}
{blank}+ {
if (driver.location_debug) {
std::cerr << "Blank matched" << std::endl;
}
// loc.step();
}
\n+ {
if (driver.location_debug) {
std::cerr << "EOL called" << std::endl;
}
loc.lines(yyleng);
loc.step();
}
"let" return yy::parser::make_LET(loc);
"mut" return yy::parser::make_MUT(loc);
"return" return yy::parser::make_RETURN(loc);
"break" return yy::parser::make_BREAK(loc);
"continue" return yy::parser::make_CONTINUE(loc);
"push" return yy::parser::make_PUSH(loc);
"pop" return yy::parser::make_POP(loc);
"len" return yy::parser::make_LEN(loc);
"as" return yy::parser::make_AS(loc);
"fn" return yy::parser::make_FN(loc);
"->" return yy::parser::make_ARROW(loc);
":" return yy::parser::make_COLON(loc);
"i32" return yy::parser::make_INT32(loc);
"usize" return yy::parser::make_USIZE(loc);
"f64" return yy::parser::make_FLOAT64(loc);
"String" return yy::parser::make_STRING(loc);
"bool" return yy::parser::make_BOOL(loc);
"char" return yy::parser::make_CHAR(loc);
"Vec" return yy::parser::make_VEC(loc);
"vec!" return yy::parser::make_VECMACROS(loc);
"true" return yy::parser::make_TRUE(loc);
"false" return yy::parser::make_FALSE(loc);
"-" {
return yy::parser::make_MINUS (loc);
}
"+" return yy::parser::make_PLUS (loc);
"+=" return yy::parser::make_PLUSASSIGN(loc);
"-=" return yy::parser::make_MINUSASSIGN(loc);
"*=" return yy::parser::make_STARASSIGN(loc);
"%=" return yy::parser::make_PERCENTASSIGN(loc);
"/=" return yy::parser::make_SLASHASSIGN(loc);
"*" return yy::parser::make_STAR (loc);
"/" return yy::parser::make_SLASH (loc);
"%" return yy::parser::make_PERCENT (loc);
"{" return yy::parser::make_LBRACE (loc);
"}" return yy::parser::make_RBRACE (loc);
"(" return yy::parser::make_LPAREN (loc);
")" return yy::parser::make_RPAREN (loc);
"[" return yy::parser::make_LSUBSCR (loc);
"]" return yy::parser::make_RSUBSCR (loc);
"!" return yy::parser::make_NOT (loc);
"=" return yy::parser::make_ASSIGN (loc);
";" return yy::parser::make_SEMICOLON(loc);
"," return yy::parser::make_COMMA(loc);
"if" return yy::parser::make_IF(loc);
"else" return yy::parser::make_ELSE(loc);
"while" return yy::parser::make_WHILE(loc);
"for" return yy::parser::make_FOR(loc);
"in" return yy::parser::make_IN(loc);
"." return yy::parser::make_DOT(loc);
".." return yy::parser::make_RANGE(loc);
"..=" return yy::parser::make_RANGEEQ(loc);
">" return yy::parser::make_GREATER(loc);
">=" return yy::parser::make_GREQ(loc);
"==" return yy::parser::make_EQUAL(loc);
"!=" return yy::parser::make_NEQUAL(loc);
"<=" return yy::parser::make_LEEQ(loc);
"<" return yy::parser::make_LESS(loc);
"&&" return yy::parser::make_AND(loc);
"||" return yy::parser::make_OR(loc);
"print!" return yy::parser::make_PRINT(loc);
"println!" return yy::parser::make_PRINTLN(loc);
{comment_line} return yy::parser::make_COMMENTLINE(yytext, loc);
{string_literal} return yy::parser::make_STRLITERAL(yytext, loc);
{char_literal} return make_CHARLITERAL(yytext, loc);
{float} return make_REAL(yytext, loc);
{size_t} return make_SIZE_T(yytext, loc);
{int} return make_NUMBER(yytext, loc);
{id} {
if (driver.location_debug) {
std::cerr << "ID found " << yytext << std::endl;
}
return yy::parser::make_IDENTIFIER(yytext, loc);
}
. {
throw yy::parser::syntax_error(loc, "invalid character: " + std::string(yytext));
}
<<EOF>> return yy::parser::make_END (loc);
%%
yy::parser::symbol_type make_NUMBER(
const std::string &s,
const yy::parser::location_type& loc
) {
errno = 0;
long n = strtol(s.c_str(), NULL, 10);
if (! (INT_MIN <= n && n <= INT_MAX && errno != ERANGE))
throw yy::parser::syntax_error(loc, "integer is out of range: " + s);
return yy::parser::make_NUMBER((int) n, loc);
}
yy::parser::symbol_type make_REAL(
const std::string &s,
const yy::parser::location_type& loc
) {
errno = 0;
double n = strtod(s.c_str(), NULL);
return yy::parser::make_REAL(n, loc);
}
yy::parser::symbol_type make_CHARLITERAL(
const std::string &s,
const yy::parser::location_type& loc
) {
errno = 0;
return yy::parser::make_CHARLITERAL(s[1], loc);
}
yy::parser::symbol_type make_SIZE_T(
const std::string &s,
const yy::parser::location_type& loc
) {
errno = 0;
size_t n;
std::string stripped = s.substr(0, s.find("usize"));
sscanf(stripped.c_str(), "%zu", &n);
return yy::parser::make_SIZE_T(n, loc);
}