-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvolverData.l
246 lines (203 loc) · 5.74 KB
/
EvolverData.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* @file EvolverData.l
* @author Dan R. Lipsa
* @brief Lexical analyser for parsing a .dmp file produced by Surface Evolver.
* @ingroup parser
*/
/*
* WARNING. If a character not described in this file is matched,
* it is simply printed to stdout. So, an important way to check
* for parsing errors, is to make sure you have no extra
* characters to stdout, even if there are no parsing errors.
*/
/** @cond */
%{
#include "Debug.h"
#include "ParsingData.h"
#include "EvolverData_yacc.h"
using EvolverData::parser;
#define YY_EXTRA_TYPE ParsingData*
%}
%option reentrant noyywrap nounput yylineno header-file="EvolverData_lex.h"
%option bison-bridge bison-locations
%option debug
ID [a-zA-Z][a-zA-Z0-9_]*
D [0-9]
H [0-9A-Fa-f]
E [Ee][+-]?{D}+
NEWLINE ("\n"|"\r\n")
SPACENONEWLINE ([[:space:]]{-}[{NEWLINE}])
%%
"/*area" {
yylval->m_id = yyextra->CreateIdentifier (yytext + 2);
yylloc->begin.line = yylineno;
return parser::token::AREA;
}
"/*actual" {
yylval->m_id = yyextra->CreateIdentifier (yytext + 2);
yylloc->begin.line = yylineno;
return parser::token::ACTUAL;
}
"*/" {
yylval->m_id = yyextra->CreateIdentifier (yytext);
yylloc->begin.line = yylineno;
return parser::token::END_COMMENT;
}
"/*" { /* eat comment */
register int c;
while (1)
{ while ( (c = yyinput(yyscanner)) != '*' && c != 0 )
; /* eat up text of comment */
if ( c == '*' )
{ while ( (c = yyinput(yyscanner)) == '*' ) ;
if ( c == '/' ) break; /* found the end */
}
if ( c == 0 && yywrap(yyscanner) )
ThrowException ("Scanner: end-of-file in comment\n");
}
}
"//".* /* eat comments */
"\\"{NEWLINE} /* splice a line terminated in \ to the next line */
[+-]?{D}+ { /* decimal integers */
yylval->m_int = yyextra->ReadInteger (yytext, 10);
yylloc->begin.line = yylineno;
return parser::token::INTEGER_VALUE;
}
[+-]?0x{H}+ { /* hexadecimal integers */
yylval->m_int = yyextra->ReadInteger (yytext, 16);
yylloc->begin.line = yylineno;
return parser::token::INTEGER_VALUE;
}
[+-]?[01]+[Bb] { /* binary integers */
yylval->m_int = yyextra->ReadInteger (yytext, 2);
yylloc->begin.line = yylineno;
return parser::token::INTEGER_VALUE;
}
[+-]?{D}+"."{D}*({E})? |
[+-]?{D}*"."{D}+({E})? |
[+-]?{D}+{E} { /* reals */
char *tail = yytext;
errno = 0;
yylval->m_real = strtod (yytext, &tail);
yylloc->begin.line = yylineno;
// report error only for overflow, not for underflow
if (errno && (yylval->m_real == HUGE_VAL || yylval->m_real == -HUGE_VAL))
{
ThrowException (string("Scanner: overflow ") + yytext);
errno = 0;
}
return parser::token::REAL_VALUE;
}
{ID} { /*identifiers*/
int id = ParsingDriver::GetKeywordId (yytext);
yylloc->begin.line = yylineno;
if (! yyextra->KeywordsIgnored () && id)
{
if (id == parser::token::READ)
// ignore READ and everything after it.
yyterminate();
else
{
yylval->m_id = yyextra->CreateIdentifier (yytext);
return id;
}
}
else
{
const char* id = yyextra->CreateIdentifier (yytext);
yylval->m_id = id;
if (yyextra->IsAttribute (id))
return parser::token::ATTRIBUTE_ID;
else if (yyextra->IsMethodOrQuantity (id))
return parser::token::METHOD_OR_QUANTITY_ID;
else
return parser::token::IDENTIFIER;
}
}
">="|"<=" {
yylval->m_id = yyextra->CreateIdentifier (yytext);
yylloc->begin.line = yylineno;
return *yytext == '>' ? parser::token::GE : parser::token::LE;
}
"&&" {
yylval->m_id = yyextra->CreateIdentifier (yytext);
yylloc->begin.line = yylineno;
return parser::token::AND;
}
"||" {
yylval->m_id = yyextra->CreateIdentifier (yytext);
yylloc->begin.line = yylineno;
return parser::token::OR;
}
"^"|"**" {/*exponentiation*/
yylval->m_id = yyextra->CreateIdentifier ("^");
yylloc->begin.line = yylineno;
return *yytext;
}
"+"|"-"|"*"|"/"|">"|"<"|"!"|"." {/*operators*/
yylval->m_id = yyextra->CreateIdentifier (yytext);
yylloc->begin.line = yylineno;
return *yytext;
}
":"|"=" {/*assignments*/
yylval->m_id = yyextra->CreateIdentifier (yytext);
yylloc->begin.line = yylineno;
return *yytext;
}
"?" {/*conditional*/
yylloc->begin.line = yylineno;
return *yytext;
}
"(" {/*open paranthesis: expressions*/
yyextra->OpenParenthesis ();
yylloc->begin.line = yylineno;
return *yytext;
}
")" {/*close paranthesis: expressions*/
yyextra->CloseParenthesis ();
yylloc->begin.line = yylineno;
return *yytext;
}
"["|"]" {/*brakets: size of arrays*/
yylloc->begin.line = yylineno;
return *yytext;
}
"{"|"}" {/*curly brakets: elements of arrays*/
yylloc->begin.line = yylineno;
return *yytext;
}
","|"\""|";" {/*element separator for arrays, quote, semicolon*/
yylloc->begin.line = yylineno;
return *yytext;
}
{SPACENONEWLINE}*{NEWLINE}{SPACENONEWLINE}* {
yylloc->begin.line = yylineno;
return '\n';
}
{SPACENONEWLINE}+ {
/* ignore white spaces, for some reason this eats
* the end of the line too so that is why we need
* the rule above */
if (yyextra->IsSpaceSignificant ())
return parser::token::SPACE;
}
%%
void ParsingDriver::ScanBegin ()
{
EvolverDatalex_init (&m_scanner);
EvolverDataset_extra (static_cast<ParsingData*>(this), m_scanner);
EvolverDataset_debug(m_debugScanning, m_scanner);
FILE* dataFile = fopen (m_file.c_str (), "r");
if (! dataFile)
ThrowException (string () + "Scanner: cannot open " + m_file);
EvolverDataset_in (dataFile, m_scanner);
}
void ParsingDriver::ScanEnd ()
{
fclose ( EvolverDataget_in(m_scanner));
EvolverDatalex_destroy(m_scanner);
}
/** @endcond */
// Local Variables:
// mode: c++
// End: