Commit 8b7201ac authored by sebastien's avatar sebastien
Browse files

v4 preprocessor/macro: changed escape character one more time.

New syntax:
* directives: @#include "filename.mod"
* expressions to be substituted: @{expr}


git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1788 ac1d8469-bf42-47a9-8791-bf33cf982152
parent cefbbd3a
......@@ -71,7 +71,7 @@ int sigma_e = 0;
%}
/* Rules for matching $line directives */
<*>^\$line\ \" { line_caller = YYSTATE; BEGIN(LINE1); }
<*>^@#line\ \" { line_caller = YYSTATE; BEGIN(LINE1); }
<LINE1>[^\"]* {
if (yylloc->begin.filename)
delete yylloc->begin.filename;
......
......@@ -40,8 +40,8 @@ class MacroDriver;
{
// Initialize the location filenames
@$.begin.filename = @$.end.filename = &driver.file;
// Output first $line statement
out << "$line \"" << driver.file << "\" 1" << endl;
// Output first @#line statement
out << "@#line \"" << driver.file << "\" 1" << endl;
};
%debug
......@@ -118,7 +118,7 @@ statement : expr
| ERROR expr
{ TYPERR_CATCH(driver.error(@$, $2), @$); }
| LINE STRING INTEGER
/* Ignore $line declarations */
/* Ignore @#line declarations */
;
expr : INTEGER
......
......@@ -83,7 +83,7 @@ MacroDriver::init_loop(const string &name, const MacroValue *value) throw (Macro
const ArrayMV<int> *mv1 = dynamic_cast<const ArrayMV<int> *>(value);
const ArrayMV<string> *mv2 = dynamic_cast<const ArrayMV<string> *>(value);
if (!mv1 && !mv2)
throw MacroValue::TypeError("Argument of $for loop must be an array expression");
throw MacroValue::TypeError("Argument of @#for loop must be an array expression");
loop_stack.push(make_pair(name, make_pair(value, 0)));
}
......@@ -132,7 +132,7 @@ MacroDriver::begin_if(const MacroValue *value) throw (MacroValue::TypeError)
{
const IntMV *ival = dynamic_cast<const IntMV *>(value);
if (!ival)
throw MacroValue::TypeError("Argument of $if must be an integer");
throw MacroValue::TypeError("Argument of @#if must be an integer");
last_if = (bool) ival->value;
}
......@@ -141,7 +141,7 @@ MacroDriver::echo(const Macro::parser::location_type &l, const MacroValue *value
{
const StringMV *sval = dynamic_cast<const StringMV *>(value);
if (!sval)
throw MacroValue::TypeError("Argument of $echo must be a string");
throw MacroValue::TypeError("Argument of @#echo must be a string");
cerr << "ECHO in macro-processor: " << l << ": " << sval->value << endl;
}
......@@ -151,7 +151,7 @@ MacroDriver::error(const Macro::parser::location_type &l, const MacroValue *valu
{
const StringMV *sval = dynamic_cast<const StringMV *>(value);
if (!sval)
throw MacroValue::TypeError("Argument of $error must be a string");
throw MacroValue::TypeError("Argument of @#error must be a string");
error(l, sval->value);
}
......@@ -81,12 +81,12 @@ private:
string for_body_tmp;
//! Temporary variable used in FOR_BODY mode
Macro::parser::location_type for_body_loc_tmp;
//! Temporary variable used in FOR_BODY mode. Keeps track of number of nested $for/$endfor
//! Temporary variable used in FOR_BODY mode. Keeps track of number of nested @#for/@#endfor
int nested_for_nb;
//! Set to true while parsing a FOR statement (only the statement, not the loop body)
bool reading_for_statement;
//! Temporary variable used in THEN_BODY and ELSE_BODY modes. Keeps track of number of nested $if
//! Temporary variable used in THEN_BODY and ELSE_BODY modes. Keeps track of number of nested @#if
int nested_if_nb;
//! Temporary variable used in THEN_BODY mode
string then_body_tmp;
......@@ -99,7 +99,7 @@ private:
//! Set to true while parsing an IF statement (only the statement, not the body)
bool reading_if_statement;
//! Output the $line declaration
//! Output the @#line declaration
void output_line(Macro::parser::location_type *yylloc) const;
//! Save current scanning context
......@@ -170,7 +170,7 @@ public:
//! Reference to the lexer
class MacroFlex *lexer;
//! Used to store the value of the last $if condition
//! Used to store the value of the last @#if condition
bool last_if;
//! Error handler
......@@ -191,13 +191,13 @@ public:
/*! Returns false if iteration is no more possible (end of loop); in that case it destroys the pointer given to init_loop() */
bool iter_loop();
//! Begins an $if statement
//! Begins an @#if statement
void begin_if(const MacroValue *value) throw (MacroValue::TypeError);
//! Executes $echo directive
//! Executes @#echo directive
void echo(const Macro::parser::location_type &l, const MacroValue *value) const throw (MacroValue::TypeError);
//! Executes $error directive
//! Executes @#error directive
void error(const Macro::parser::location_type &l, const MacroValue *value) const throw (MacroValue::TypeError);
};
......
......@@ -69,7 +69,7 @@ CONT \\\\
yylloc->step();
%}
<INITIAL>^{SPC}*\${SPC}*include{SPC}+\"[^\"\r\n]*\"{SPC}*{EOL} {
<INITIAL>^{SPC}*@#{SPC}*include{SPC}+\"[^\"\r\n]*\"{SPC}*{EOL} {
yylloc->lines(1);
yylloc->step();
......@@ -85,8 +85,8 @@ CONT \\\\
BEGIN(INITIAL);
}
<INITIAL>^{SPC}*\$ { yylloc->step(); BEGIN(STMT); }
<INITIAL>\$\{ { yylloc->step(); BEGIN(EXPR); }
<INITIAL>^{SPC}*@# { yylloc->step(); BEGIN(STMT); }
<INITIAL>@\{ { yylloc->step(); BEGIN(EXPR); }
<EXPR>\} { BEGIN(INITIAL); return token::EOL; }
......@@ -156,11 +156,11 @@ CONT \\\\
<STMT>for { reading_for_statement = true; return token::FOR; }
<STMT>in { return token::IN; }
<STMT>endfor { driver.error(*yylloc, "$endfor is not matched by a $for statement"); }
<STMT>endfor { driver.error(*yylloc, "@#endfor is not matched by a @#for statement"); }
<STMT>if { reading_if_statement = true; return token::IF; }
<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"); }
<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"); }
<STMT>echo { return token::ECHO_DIR; }
<STMT>error { return token::ERROR; }
......@@ -174,22 +174,20 @@ CONT \\\\
<STMT><<EOF>> { driver.error(*yylloc, "Unexpected end of file while parsing a macro statement"); }
<FOR_BODY>{EOL} { yylloc->lines(1); yylloc->step(); for_body_tmp.append(yytext); }
<FOR_BODY>^{SPC}*\${SPC}*for({SPC}|{CONT}) {
/* In order to catch nested $for, it is necessary to start from the beginning of
the line (otherwise we could catch something like "${var} for" */
<FOR_BODY>^{SPC}*@#{SPC}*for({SPC}|{CONT}) {
nested_for_nb++;
for_body_tmp.append(yytext);
yylloc->step();
}
<FOR_BODY>. { for_body_tmp.append(yytext); yylloc->step(); }
<FOR_BODY><<EOF>> { driver.error(*yylloc, "Unexpected end of file: $for loop not matched by an $endfor"); }
<FOR_BODY>^{SPC}*\${SPC}*endfor{SPC}*{EOL} {
<FOR_BODY><<EOF>> { driver.error(*yylloc, "Unexpected end of file: @#for loop not matched by an @#endfor"); }
<FOR_BODY>^{SPC}*@#{SPC}*endfor{SPC}*{EOL} {
yylloc->lines(1);
yylloc->step();
if (nested_for_nb)
{
/* This $endfor is not the end of the loop body,
but only that of a nested $for loop */
/* This @#endfor is not the end of the loop body,
but only that of a nested @#for loop */
nested_for_nb--;
for_body_tmp.append(yytext);
}
......@@ -208,16 +206,14 @@ CONT \\\\
}
<THEN_BODY>{EOL} { yylloc->lines(1); yylloc->step(); then_body_tmp.append(yytext); }
<THEN_BODY>^{SPC}*\${SPC}*if({SPC}|{CONT}) {
/* In order to catch nested $if, it is necessary to start from the beginning of
the line (otherwise we could catch something like "${var} if" */
<THEN_BODY>^{SPC}*@#{SPC}*if({SPC}|{CONT}) {
nested_if_nb++;
then_body_tmp.append(yytext);
yylloc->step();
}
<THEN_BODY>. { then_body_tmp.append(yytext); yylloc->step(); }
<THEN_BODY><<EOF>> { driver.error(*yylloc, "Unexpected end of file: $if not matched by an $endif"); }
<THEN_BODY>^{SPC}*\${SPC}*else{SPC}*{EOL} {
<THEN_BODY><<EOF>> { driver.error(*yylloc, "Unexpected end of file: @#if not matched by an @#endif"); }
<THEN_BODY>^{SPC}*@#{SPC}*else{SPC}*{EOL} {
yylloc->lines(1);
yylloc->step();
if (nested_if_nb)
......@@ -230,13 +226,13 @@ CONT \\\\
}
}
<THEN_BODY>^{SPC}*\${SPC}*endif{SPC}*{EOL} {
<THEN_BODY>^{SPC}*@#{SPC}*endif{SPC}*{EOL} {
yylloc->lines(1);
yylloc->step();
if (nested_if_nb)
{
/* This $endif is not the end of the $if we're parsing,
but only that of a nested $if */
/* This @#endif is not the end of the @#if we're parsing,
but only that of a nested @#if */
nested_if_nb--;
then_body_tmp.append(yytext);
}
......@@ -252,23 +248,21 @@ CONT \\\\
}
<ELSE_BODY>{EOL} { yylloc->lines(1); yylloc->step(); else_body_tmp.append(yytext); }
<ELSE_BODY>^{SPC}*\${SPC}*if({SPC}|{CONT}) {
/* In order to catch nested $if, it is necessary to start from the beginning of
the line (otherwise we could catch something like "${var} if" */
<ELSE_BODY>^{SPC}*@#{SPC}*if({SPC}|{CONT}) {
nested_if_nb++;
else_body_tmp.append(yytext);
yylloc->step();
}
<ELSE_BODY>. { else_body_tmp.append(yytext); yylloc->step(); }
<ELSE_BODY><<EOF>> { driver.error(*yylloc, "Unexpected end of file: $if not matched by an $endif"); }
<ELSE_BODY><<EOF>> { driver.error(*yylloc, "Unexpected end of file: @#if not matched by an @#endif"); }
<ELSE_BODY>^{SPC}*\${SPC}*endif{SPC}*{EOL} {
<ELSE_BODY>^{SPC}*@#{SPC}*endif{SPC}*{EOL} {
yylloc->lines(1);
yylloc->step();
if (nested_if_nb)
{
/* This $endif is not the end of the $if we're parsing,
but only that of a nested $if */
/* This @#endif is not the end of the @#if we're parsing,
but only that of a nested @#if */
nested_if_nb--;
else_body_tmp.append(yytext);
}
......@@ -318,7 +312,7 @@ MacroFlex::MacroFlex(istream* in, ostream* out)
void
MacroFlex::output_line(Macro::parser::location_type *yylloc) const
{
*yyout << endl << "$line \"" << *yylloc->begin.filename << "\" "
*yyout << endl << "@#line \"" << *yylloc->begin.filename << "\" "
<< yylloc->begin.line << endl;
}
......@@ -338,7 +332,7 @@ MacroFlex::restore_context(Macro::parser::location_type *yylloc)
for_body_loc = context_stack.top().for_body_loc;
// Remove top of stack
context_stack.pop();
// Dump $line instruction
// Dump @#line instruction
output_line(yylloc);
}
......@@ -357,7 +351,7 @@ MacroFlex::create_include_context(string *filename, Macro::parser::location_type
yylloc->begin.column = yylloc->end.column = 0;
// We are not in a loop body
for_body.clear();
// Output $line information
// Output @#line information
output_line(yylloc);
// Switch to new buffer
yy_switch_to_buffer(yy_create_buffer(input, YY_BUF_SIZE));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment