-
Notifications
You must be signed in to change notification settings - Fork 2
/
scanner.l
176 lines (149 loc) · 6.84 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
/*
Copyright (C) 2013 Lucas Beyer (http://lucasb.eyer.be)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
%{
#include <stack>
#include <iostream>
/* globals to track current indentation */
int g_current_line_indent = 0; /* indentation of the current line */
std::stack<size_t> g_indent_levels;
int g_is_fake_outdent_symbol = 0;
/* TODO: error-out on tabs/spaces mix? */
static const unsigned int TAB_WIDTH = 2;
extern const char* g_current_filename;
/* Don't mangle yylex please! */
#define YY_DECL extern "C" int yylex()
#define YYSTYPE int
#include "parser.hh"
#define YY_USER_INIT { \
g_indent_levels.push(0); \
BEGIN(initial); \
}
int yycolumn = 1;
void set_yycolumn(int val) {
yycolumn = val;
yylloc.first_column = yycolumn;
yylloc.last_column = yycolumn + yyleng - 1;
}
#define YY_USER_ACTION { \
yylloc.first_line = yylloc.last_line = yylineno; \
set_yycolumn(yycolumn); \
yycolumn += yyleng; \
}
%}
/* This is a sub-parser (state) for indentation-sensitive scoping */
%x initial
%x indent
%s normal
/* %option 8bit reentrant bison-bridge */
%option warn
%option nodefault
%option yylineno
%option noyywrap
/*%option debug*/
%%
int indent_caller = normal;
/* This helps to keep track of the column number.
* Note that it won't work if you have a rule which includes a newline and is
* longer than one character because in that case that rule will be favored
* and this one here won't get called.
* TL;DR: If you have a rule which includes \n and more, you need to reset
* yycolumn inside that rule!
*/
<*>\n { set_yycolumn(0); yylineno--; REJECT; }
/* Everything runs in the <normal> mode and enters the <indent> mode
when a newline symbol is encountered.
There is no newline symbol before the first line, so we need to go
into the <indent> mode by hand there.
*/
<initial>. { set_yycolumn(yycolumn-1); indent_caller = normal; yyless(0); BEGIN(indent); }
<initial>\n { indent_caller = normal; yyless(0); BEGIN(indent); }
/* The following are the rules that keep track of indentation. */
<indent>" " { g_current_line_indent++; }
<indent>\t { g_current_line_indent = (g_current_line_indent + TAB_WIDTH) & ~(TAB_WIDTH-1); }
<indent>\n { g_current_line_indent = 0; /* ignoring blank line */ }
<indent><<EOF>> {
// When encountering the end of file, we want to emit an
// outdent for all indents currently left.
if(g_indent_levels.top() != 0) {
g_indent_levels.pop();
// See the same code below (<indent>.) for a rationale.
if(g_current_line_indent != g_indent_levels.top()) {
unput('\n');
for(size_t i = 0 ; i < g_indent_levels.top() ; ++i) {
unput(' ');
}
} else {
BEGIN(indent_caller);
}
return TOK_OUTDENT;
} else {
yyterminate();
}
}
<indent>. {
if(!g_is_fake_outdent_symbol) {
unput(*yytext);
}
set_yycolumn(yycolumn-1);
g_is_fake_outdent_symbol = 0;
// Indentation level has increased. It can only ever
// increase by one level at a time. Remember how many
// spaces this level has and emit an indentation token.
if(g_current_line_indent > g_indent_levels.top()) {
g_indent_levels.push(g_current_line_indent);
BEGIN(indent_caller);
return TOK_INDENT;
} else if(g_current_line_indent < g_indent_levels.top()) {
// Outdenting is the most difficult, as we might need to
// outdent multiple times at once, but flex doesn't allow
// emitting multiple tokens at once! So we fake this by
// 'unput'ting fake lines which will give us the next
// outdent.
g_indent_levels.pop();
if(g_current_line_indent != g_indent_levels.top()) {
// Unput the rest of the current line, including the newline.
// We want to keep it untouched.
for(size_t i = 0 ; i < g_current_line_indent ; ++i) {
unput(' ');
}
unput('\n');
// Now, insert a fake character indented just so
// that we get a correct outdent the next time.
unput('.');
// Though we need to remember that it's a fake one
// so we can ignore the symbol.
g_is_fake_outdent_symbol = 1;
for(size_t i = 0 ; i < g_indent_levels.top() ; ++i) {
unput(' ');
}
unput('\n');
} else {
BEGIN(indent_caller);
}
return TOK_OUTDENT;
} else {
// No change in indentation, not much to do here...
BEGIN(indent_caller);
}
}
<normal>\n { g_current_line_indent = 0; indent_caller = YY_START; BEGIN(indent); }
<normal>stuff { return TOK_STUFF; }
<normal>" ".+ { return TOK_REST; }
(.|\n) { std::cerr << g_current_filename << ":" << yylineno << ":" << yycolumn << ": Unexpected character: " << yytext << std::endl; exit(1); }
%%