-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathptokens.l
200 lines (161 loc) · 5.06 KB
/
ptokens.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
// Python tokenizer
// Conforms to the Python 3.x language reference lexical structure:
// https://docs.python.org/3.6/reference/lexical_analysis.html
// Uses RE/flex matcher for lazy quants and for indent/dedent matching.
// inject code into the Lexer class for the scanner state and auxiliary methods
%class{
// emit token to Lexer's current stream out()
void emit(const char *token, const char *what = NULL)
{
out() << token << (what ? what : "") << std::endl;
}
// keep track of (, [, { nesting for implicit line joins
int join;
}
// import Python 3.x token definitions
%include "pdefs.l"
// implicit line joining is done with the JOINING start condition state
%x JOINING
// use full/fast option for speed and to eliminate tokenizer startup time
%option full
// set indent tabs=8
%option tabs=8
// dot matches \n
%option dotall
// warning: in unicode mode dot is a "catch all" and also matches invalid UTF-8
// so we use {any} to match anything that is valid Unicode/UTF-8, where {any}
// is defined in pdefs.l as all Unicode planes without the surrogate halves:
// {any} stands for \p{Unicode}
%%
^\f?\h* // eat nodent margin space
\r?\n |
#{any}*?\r?\n emit("NEWLINE");
^\f?\h+\i emit("INDENT");
^\f?\h*\j |
\j emit("DEDENT");
(?^\h+) // eat space and tabs
(?^^(\f?\h*(#{any}*?)?\r?\n)+)
// eat blank lines and comments, anchor ^ in (?^X)
(?^\\\r?\n\f?\h*) // explicit line joining by eating \ \n ...
[[({] // implicit line joining
join = 1;
matcher().push_stops(); // save the indent stops
start(JOINING);
emit("DELIMITER ", text());
<JOINING>{
\s+ // eat all white space
#{any}*?\r?\n // eat comments
[[({] ++join;
emit("DELIMITER ", text());
[])}] if (--join == 0)
{
matcher().pop_stops(); // restore the indent stops
start(INITIAL);
}
emit("DELIMITER ", text());
}
<*>{
{stringliteral} emit("STRING ", text());
{bytesliteral} emit("BYTES ", text());
{integer} emit("INTEGER ", text());
{floatnumber} emit("FLOAT ", text());
{imagnumber} emit("IMAG ", text());
False |
None |
True |
and |
as |
assert |
break |
class |
continue |
def |
del |
elif |
else |
except |
finally |
for |
from |
global |
if |
import |
in |
is |
lambda |
nonlocal |
not |
or |
pass |
raise |
return |
try |
while |
with |
yield emit("KEYWORD ", text());
{identifier} emit("IDENTIFIER ", text());
"+" |
"-" |
"*" |
"/" |
"%" |
"&" |
"|" |
"^" |
"~" |
"<" |
">" |
"**" |
"//" |
"<<" |
">>" |
"<=" |
">=" |
"==" |
"!=" emit("OPERATOR ", text());
"@" |
"->" |
"+=" |
"-=" |
"*=" |
"/=" |
"%=" |
"@=" |
"&=" |
"|=" |
"^=" |
">>=" |
"<<=" |
"**=" |
"//=" |
"=" |
"," |
":" |
"." |
";" emit("DELIMITER ", text());
. std::cerr << "Error: invalid input at line " << lineno() << " column " << columno() << std::endl;
return 0;
}
%%
int main(int argc, char **argv)
{
// in this example we'll use the Input class with a FILE or stdin to scan UTF-8/16/32 input
reflex::Input input;
if (argc > 1)
{
input = fopen(argv[1], "r");
if (input.file() == NULL)
{
perror("Cannot open file for reading");
exit(EXIT_FAILURE);
}
}
else
{
input = stdin;
}
Lexer(input).lex();
if (input.file() != stdin)
fclose(input.file());
return 0;
}