MacroFlex.ll 16.2 KB
Newer Older
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
/*
 * Copyright (C) 2008 Dynare Team
 *
 * This file is part of Dynare.
 *
 * Dynare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dynare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 */

%{
using namespace std;

#include <fstream>

#include "MacroDriver.hh"
#include "MacroBison.hh"

// Announce to Flex the prototype we want for lexing function
#define YY_DECL                                              \
  Macro::parser::token_type                                  \
  MacroFlex::lex(Macro::parser::semantic_type *yylval,       \
                 Macro::parser::location_type *yylloc,       \
                 MacroDriver &driver)

// Shortcut to access tokens defined by Bison
typedef Macro::parser::token token;

/* By default yylex returns int, we use token_type.
   Unfortunately yyterminate by default returns 0, which is
   not of token_type.  */
#define yyterminate() return Macro::parser::token_type (0);
%}

%option c++

%option prefix="Macro"

%option case-insensitive noyywrap nounput batch debug never-interactive

50
51
%x STMT
%x EXPR
52
%x FOR_BODY
sebastien's avatar
sebastien committed
53
54
%x THEN_BODY
%x ELSE_BODY
55
56
57
58
59

%{
// Increments location counter for every token read
#define YY_USER_ACTION yylloc->columns(yyleng);
%}
sebastien's avatar
sebastien committed
60
61
62

SPC  [ \t]+
EOL  (\r)?\n
63
CONT \\\\
sebastien's avatar
sebastien committed
64

65
66
67
68
69
70
71
%%
 /* Code put at the beginning of yylex() */
%{
  // Reset location before reading token
  yylloc->step();
%}

72
<INITIAL>^{SPC}*@#{SPC}*include{SPC}+\"[^\"\r\n]*\"{SPC}*{EOL} {
sebastien's avatar
sebastien committed
73
74
75
76
77
78
79
80
81
82
83
84
85
                              yylloc->lines(1);
                              yylloc->step();

                              // Get filename
                              string *filename = new string(yytext);
                              int dblq_idx1 = filename->find('"');
                              int dblq_idx2 = filename->find('"', dblq_idx1 + 1);
                              filename->erase(dblq_idx2);
                              filename->erase(0, dblq_idx1 + 1);

                              create_include_context(filename, yylloc, driver);

                              BEGIN(INITIAL);
86
87
                            }

88
89
<INITIAL>^{SPC}*@#          { yylloc->step(); BEGIN(STMT); }
<INITIAL>@\{                { yylloc->step(); BEGIN(EXPR); }
90

91
<EXPR>\}                    { BEGIN(INITIAL); return token::EOL; }
92

93
94
<STMT>{CONT}{SPC}*{EOL}     { yylloc->lines(1); yylloc->step(); }
<STMT>{EOL}                 {
95
96
97
98
99
100
101
                              /* If parsing a @#for or an @#if, keep the location
                                 for reporting message in case of error */
                              if (reading_for_statement)
                                for_stmt_loc_tmp = *yylloc;
                              else if (reading_if_statement)
                                if_stmt_loc_tmp = *yylloc;

102
103
104
105
106
107
108
109
110
111
                              yylloc->lines(1);
                              yylloc->step();
                              if (reading_for_statement)
                                {
                                  reading_for_statement = false;
                                  for_body_tmp.erase();
                                  for_body_loc_tmp = *yylloc;
                                  nested_for_nb = 0;
                                  BEGIN(FOR_BODY);
                                }
sebastien's avatar
sebastien committed
112
113
114
115
116
117
118
119
                              else if (reading_if_statement)
                                {
                                  reading_if_statement = false;
                                  then_body_tmp.erase();
                                  then_body_loc_tmp = *yylloc;
                                  nested_if_nb = 0;
                                  BEGIN(THEN_BODY);
                                }
120
                              else
sebastien's avatar
sebastien committed
121
122
123
124
                                {
                                  *yyout << endl;
                                  BEGIN(INITIAL);
                                }
125
126
                              return token::EOL;
                            }
127

128
129
130
<STMT,EXPR>{SPC}+           { yylloc->step(); }

<STMT,EXPR>[0-9]+           {
131
132
133
                              yylval->int_val = atoi(yytext);
                              return token::INTEGER;
                            }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<STMT,EXPR>\(               { return token::LPAREN; }
<STMT,EXPR>\)               { return token::RPAREN; }
<STMT,EXPR>\[               { return token::LBRACKET; }
<STMT,EXPR>\]               { return token::RBRACKET; }
<STMT,EXPR>:                { return token::COLON; }
<STMT,EXPR>,                { return token::COMMA; }
<STMT,EXPR>=                { return token::EQUAL; }
<STMT,EXPR>[!]              { return token::EXCLAMATION; }
<STMT,EXPR>"||"             { return token::LOGICAL_OR; }
<STMT,EXPR>&&               { return token::LOGICAL_AND; }
<STMT,EXPR>"<="             { return token::LESS_EQUAL; }
<STMT,EXPR>">="             { return token::GREATER_EQUAL; }
<STMT,EXPR>"<"              { return token::LESS; }
<STMT,EXPR>">"              { return token::GREATER; }
<STMT,EXPR>"=="             { return token::EQUAL_EQUAL; }
<STMT,EXPR>"!="             { return token::EXCLAMATION_EQUAL; }
<STMT,EXPR>[+]              { return token::PLUS; }
<STMT,EXPR>[-]              { return token::MINUS; }
<STMT,EXPR>[*]              { return token::TIMES; }
<STMT,EXPR>[/]              { return token::DIVIDE; }
154
<STMT,EXPR>in               { return token::IN; }
155
156

<STMT,EXPR>\"[^\"]*\"       {
157
158
159
160
161
                              yylval->string_val = new string(yytext + 1);
                              yylval->string_val->resize(yylval->string_val->length() - 1);
                              return token::STRING;
                            }

162
163
<STMT>line                  { return token::LINE; }
<STMT>define                { return token::DEFINE; }
sebastien's avatar
sebastien committed
164

165
<STMT>for                   { reading_for_statement = true; return token::FOR; }
166
<STMT>endfor                { driver.error(*yylloc, "@#endfor is not matched by a @#for statement"); }
167

168
<STMT>if                    { reading_if_statement = true; return token::IF; }
169
170
<STMT>else                  { driver.error(*yylloc, "@#else is not matched by an @#if statement"); }
<STMT>endif                 { driver.error(*yylloc, "@#endif is not matched by an @#if statement"); }
sebastien's avatar
sebastien committed
171

172
173
<STMT>echo                  { return token::ECHO_DIR; }
<STMT>error                 { return token::ERROR; }
sebastien's avatar
sebastien committed
174

175
<STMT,EXPR>[A-Za-z_][A-Za-z0-9_]* {
sebastien's avatar
sebastien committed
176
177
178
                              yylval->string_val = new string(yytext);
                              return token::NAME;
                            }
179

180
181
<EXPR><<EOF>>               { driver.error(*yylloc, "Unexpected end of file while parsing a macro expression"); }
<STMT><<EOF>>               { driver.error(*yylloc, "Unexpected end of file while parsing a macro statement"); }
182

sebastien's avatar
sebastien committed
183
<FOR_BODY>{EOL}             { yylloc->lines(1); yylloc->step(); for_body_tmp.append(yytext); }
184
<FOR_BODY>^{SPC}*@#{SPC}*for({SPC}|{CONT}) {
185
186
                              nested_for_nb++;
                              for_body_tmp.append(yytext);
187
                              yylloc->step();
188
                            }
189
<FOR_BODY>.                 { for_body_tmp.append(yytext); yylloc->step(); }
190
<FOR_BODY><<EOF>>           { driver.error(for_stmt_loc_tmp, "@#for loop not matched by an @#endfor (unexpected end of file)"); }
191
<FOR_BODY>^{SPC}*@#{SPC}*endfor{SPC}*{EOL} {
sebastien's avatar
sebastien committed
192
193
194
195
                              yylloc->lines(1);
                              yylloc->step();
                              if (nested_for_nb)
                                {
196
197
                                  /* This @#endfor is not the end of the loop body,
                                     but only that of a nested @#for loop */
sebastien's avatar
sebastien committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
                                  nested_for_nb--;
                                  for_body_tmp.append(yytext);
                                }
                              else
                                {
                                  // Save old buffer state and location
                                  save_context(yylloc);

                                  for_body = for_body_tmp;
                                  for_body_loc = for_body_loc_tmp;

                                  iter_loop(driver, yylloc);

                                  BEGIN(INITIAL);
                                }
                            }

<THEN_BODY>{EOL}            { yylloc->lines(1); yylloc->step(); then_body_tmp.append(yytext); }
216
<THEN_BODY>^{SPC}*@#{SPC}*if({SPC}|{CONT}) {
217
218
                              nested_if_nb++;
                              then_body_tmp.append(yytext);
219
                              yylloc->step();
220
                            }
221
<THEN_BODY>.                { then_body_tmp.append(yytext); yylloc->step(); }
222
<THEN_BODY><<EOF>>          { driver.error(if_stmt_loc_tmp, "@#if not matched by an @#endif (unexpected end of file)"); }
223
<THEN_BODY>^{SPC}*@#{SPC}*else{SPC}*{EOL} {
sebastien's avatar
sebastien committed
224
225
226
227
228
229
230
231
232
233
234
235
                              yylloc->lines(1);
                              yylloc->step();
                              if (nested_if_nb)
                                then_body_tmp.append(yytext);
                              else
                                {
                                  else_body_tmp.erase();
                                  else_body_loc_tmp = *yylloc;
                                  BEGIN(ELSE_BODY);
                                }
                             }

236
<THEN_BODY>^{SPC}*@#{SPC}*endif{SPC}*{EOL} {
sebastien's avatar
sebastien committed
237
238
239
240
                              yylloc->lines(1);
                              yylloc->step();
                              if (nested_if_nb)
                                {
241
242
                                  /* This @#endif is not the end of the @#if we're parsing,
                                     but only that of a nested @#if */
sebastien's avatar
sebastien committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
                                  nested_if_nb--;
                                  then_body_tmp.append(yytext);
                                }
                              else
                                {
                                  if (driver.last_if)
                                    create_then_context(yylloc);
                                  else
                                    output_line(yylloc);

                                  BEGIN(INITIAL);
                                }
                            }

<ELSE_BODY>{EOL}            { yylloc->lines(1); yylloc->step(); else_body_tmp.append(yytext); }
258
<ELSE_BODY>^{SPC}*@#{SPC}*if({SPC}|{CONT}) {
259
260
                              nested_if_nb++;
                              else_body_tmp.append(yytext);
261
                              yylloc->step();
262
                            }
263
<ELSE_BODY>.                { else_body_tmp.append(yytext); yylloc->step(); }
264
<ELSE_BODY><<EOF>>          { driver.error(if_stmt_loc_tmp, "@#if not matched by an @#endif (unexpected end of file)"); }
sebastien's avatar
sebastien committed
265

266
<ELSE_BODY>^{SPC}*@#{SPC}*endif{SPC}*{EOL} {
sebastien's avatar
sebastien committed
267
268
269
270
                              yylloc->lines(1);
                              yylloc->step();
                              if (nested_if_nb)
                                {
271
272
                                  /* This @#endif is not the end of the @#if we're parsing,
                                     but only that of a nested @#if */
sebastien's avatar
sebastien committed
273
274
275
276
277
278
279
280
281
282
283
284
285
                                  nested_if_nb--;
                                  else_body_tmp.append(yytext);
                                }
                              else
                                {
                                  if (driver.last_if)
                                    create_then_context(yylloc);
                                  else
                                    create_else_context(yylloc);

                                  BEGIN(INITIAL);
                                }
                            }
286

287
<INITIAL><<EOF>>            {
288
                              // Quit lexer if end of main file
289
                              if (context_stack.empty())
290
291
292
                                {
                                  yyterminate();
                                }
sebastien's avatar
sebastien committed
293

294
                              // Else clean current scanning context
295
                              yy_delete_buffer(YY_CURRENT_BUFFER);
296
                              delete input;
297
                              delete yylloc->begin.filename;
298

sebastien's avatar
sebastien committed
299
300
                              /* If we are not in a loop body, or if the loop has terminated,
                                 pop a context */
301
                              if (for_body.empty() || !iter_loop(driver, yylloc))
sebastien's avatar
sebastien committed
302
                                restore_context(yylloc);
303
304
                            }

305
306
 /* We don't use echo, because under Cygwin it will add an extra \r */
<INITIAL>{EOL}              { yylloc->lines(1); yylloc->step(); *yyout << endl; }
307

308
 /* Copy everything else to output */
309
310
311
312
313
314
<INITIAL>.                  { yylloc->step(); ECHO; }

<*>.                        { driver.error(*yylloc, "Macro lexer error: '" + string(yytext) + "'"); }
%%

MacroFlex::MacroFlex(istream* in, ostream* out)
sebastien's avatar
sebastien committed
315
  : MacroFlexLexer(in, out), input(in), reading_for_statement(false), reading_if_statement(false)
316
317
318
319
{
}

void
sebastien's avatar
sebastien committed
320
MacroFlex::output_line(Macro::parser::location_type *yylloc) const
321
{
322
  *yyout << endl << "@#line \"" << *yylloc->begin.filename << "\" "
323
324
325
         << yylloc->begin.line << endl;
}

sebastien's avatar
sebastien committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
void
MacroFlex::save_context(Macro::parser::location_type *yylloc)
{
  context_stack.push(ScanContext(input, YY_CURRENT_BUFFER, *yylloc, for_body, for_body_loc));
}

void
MacroFlex::restore_context(Macro::parser::location_type *yylloc)
{
  input = context_stack.top().input;
  yy_switch_to_buffer(context_stack.top().buffer);
  *yylloc = context_stack.top().yylloc;
  for_body = context_stack.top().for_body;
  for_body_loc = context_stack.top().for_body_loc;
  // Remove top of stack
  context_stack.pop();
342
  // Dump @#line instruction
sebastien's avatar
sebastien committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
  output_line(yylloc);
}

void
MacroFlex::create_include_context(string *filename, Macro::parser::location_type *yylloc,
                                  MacroDriver &driver)
{
  save_context(yylloc);
  // Open new file
  input = new ifstream(filename->c_str(), ios::binary);
  if (input->fail())
    driver.error(*yylloc, "Could not open " + *filename);
  // Reset location
  yylloc->begin.filename = yylloc->end.filename = filename;
  yylloc->begin.line = yylloc->end.line = 1;
  yylloc->begin.column = yylloc->end.column = 0;
  // We are not in a loop body
  for_body.clear();
361
  // Output @#line information
sebastien's avatar
sebastien committed
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
  output_line(yylloc);
  // Switch to new buffer
  yy_switch_to_buffer(yy_create_buffer(input, YY_BUF_SIZE));
}

void
MacroFlex::create_then_context(Macro::parser::location_type *yylloc)
{
  save_context(yylloc);
  input = new stringstream(then_body_tmp);
  *yylloc = then_body_loc_tmp;
  yylloc->begin.filename = yylloc->end.filename = new string(*then_body_loc_tmp.begin.filename);
  for_body.clear();
  output_line(yylloc);
  yy_switch_to_buffer(yy_create_buffer(input, YY_BUF_SIZE));
}

void
MacroFlex::create_else_context(Macro::parser::location_type *yylloc)
{
  save_context(yylloc);
  input = new stringstream(else_body_tmp);
  *yylloc = else_body_loc_tmp;
  yylloc->begin.filename = yylloc->end.filename = new string(*else_body_loc_tmp.begin.filename);
  for_body.clear();
  output_line(yylloc);
  yy_switch_to_buffer(yy_create_buffer(input, YY_BUF_SIZE));
}

391
392
393
394
395
396
397
398
399
400
401
402
403
bool
MacroFlex::iter_loop(MacroDriver &driver, Macro::parser::location_type *yylloc)
{
  if (!driver.iter_loop())
    return false;

  input = new stringstream(for_body);
  *yylloc = for_body_loc;
  yylloc->begin.filename = yylloc->end.filename = new string(*for_body_loc.begin.filename);
  output_line(yylloc);
  yy_switch_to_buffer(yy_create_buffer(input, YY_BUF_SIZE));

  return true;
404
405
406
407
408
409
410
411
412
413
414
415
416
417
}

/* This implementation of MacroFlexLexer::yylex() is required to fill the
 * vtable of the class MacroFlexLexer. We define the scanner's main yylex
 * function via YY_DECL to reside in the MacroFlex class instead. */

#ifdef yylex
# undef yylex
#endif

int
MacroFlexLexer::yylex()
{
  cerr << "MacroFlexLexer::yylex() has been called, that should never happen!" << endl;
418
  exit(EXIT_FAILURE);
419
420
421
422
423
424
425
}

/*
  Local variables:
  mode: C++
  End:
*/