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
time-shift
sebastien 2008-04-10 08:16:54 +00:00
parent 2d4403b2ae
commit 39f4ef9d90
8 changed files with 43 additions and 49 deletions

View File

@ -71,7 +71,7 @@ int sigma_e = 0;
%} %}
/* Rules for matching $line directives */ /* Rules for matching $line directives */
<*>^\$line\ \" { line_caller = YYSTATE; BEGIN(LINE1); } <*>^@#line\ \" { line_caller = YYSTATE; BEGIN(LINE1); }
<LINE1>[^\"]* { <LINE1>[^\"]* {
if (yylloc->begin.filename) if (yylloc->begin.filename)
delete yylloc->begin.filename; delete yylloc->begin.filename;

View File

@ -40,8 +40,8 @@ class MacroDriver;
{ {
// Initialize the location filenames // Initialize the location filenames
@$.begin.filename = @$.end.filename = &driver.file; @$.begin.filename = @$.end.filename = &driver.file;
// Output first $line statement // Output first @#line statement
out << "$line \"" << driver.file << "\" 1" << endl; out << "@#line \"" << driver.file << "\" 1" << endl;
}; };
%debug %debug
@ -118,7 +118,7 @@ statement : expr
| ERROR expr | ERROR expr
{ TYPERR_CATCH(driver.error(@$, $2), @$); } { TYPERR_CATCH(driver.error(@$, $2), @$); }
| LINE STRING INTEGER | LINE STRING INTEGER
/* Ignore $line declarations */ /* Ignore @#line declarations */
; ;
expr : INTEGER expr : INTEGER

View File

@ -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<int> *mv1 = dynamic_cast<const ArrayMV<int> *>(value);
const ArrayMV<string> *mv2 = dynamic_cast<const ArrayMV<string> *>(value); const ArrayMV<string> *mv2 = dynamic_cast<const ArrayMV<string> *>(value);
if (!mv1 && !mv2) 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))); 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); const IntMV *ival = dynamic_cast<const IntMV *>(value);
if (!ival) 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; 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); const StringMV *sval = dynamic_cast<const StringMV *>(value);
if (!sval) 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; 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); const StringMV *sval = dynamic_cast<const StringMV *>(value);
if (!sval) 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); error(l, sval->value);
} }

View File

@ -81,12 +81,12 @@ private:
string for_body_tmp; string for_body_tmp;
//! Temporary variable used in FOR_BODY mode //! Temporary variable used in FOR_BODY mode
Macro::parser::location_type for_body_loc_tmp; 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; int nested_for_nb;
//! Set to true while parsing a FOR statement (only the statement, not the loop body) //! Set to true while parsing a FOR statement (only the statement, not the loop body)
bool reading_for_statement; 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; int nested_if_nb;
//! Temporary variable used in THEN_BODY mode //! Temporary variable used in THEN_BODY mode
string then_body_tmp; string then_body_tmp;
@ -99,7 +99,7 @@ private:
//! Set to true while parsing an IF statement (only the statement, not the body) //! Set to true while parsing an IF statement (only the statement, not the body)
bool reading_if_statement; bool reading_if_statement;
//! Output the $line declaration //! Output the @#line declaration
void output_line(Macro::parser::location_type *yylloc) const; void output_line(Macro::parser::location_type *yylloc) const;
//! Save current scanning context //! Save current scanning context
@ -170,7 +170,7 @@ public:
//! Reference to the lexer //! Reference to the lexer
class MacroFlex *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; bool last_if;
//! Error handler //! 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() */ /*! 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(); bool iter_loop();
//! Begins an $if statement //! Begins an @#if statement
void begin_if(const MacroValue *value) throw (MacroValue::TypeError); 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); 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); void error(const Macro::parser::location_type &l, const MacroValue *value) const throw (MacroValue::TypeError);
}; };

View File

@ -69,7 +69,7 @@ CONT \\\\
yylloc->step(); yylloc->step();
%} %}
<INITIAL>^{SPC}*\${SPC}*include{SPC}+\"[^\"\r\n]*\"{SPC}*{EOL} { <INITIAL>^{SPC}*@#{SPC}*include{SPC}+\"[^\"\r\n]*\"{SPC}*{EOL} {
yylloc->lines(1); yylloc->lines(1);
yylloc->step(); yylloc->step();
@ -85,8 +85,8 @@ CONT \\\\
BEGIN(INITIAL); BEGIN(INITIAL);
} }
<INITIAL>^{SPC}*\$ { yylloc->step(); BEGIN(STMT); } <INITIAL>^{SPC}*@# { yylloc->step(); BEGIN(STMT); }
<INITIAL>\$\{ { yylloc->step(); BEGIN(EXPR); } <INITIAL>@\{ { yylloc->step(); BEGIN(EXPR); }
<EXPR>\} { BEGIN(INITIAL); return token::EOL; } <EXPR>\} { BEGIN(INITIAL); return token::EOL; }
@ -156,11 +156,11 @@ CONT \\\\
<STMT>for { reading_for_statement = true; return token::FOR; } <STMT>for { reading_for_statement = true; return token::FOR; }
<STMT>in { return token::IN; } <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>if { reading_if_statement = true; return token::IF; }
<STMT>else { driver.error(*yylloc, "$else 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>endif { driver.error(*yylloc, "@#endif is not matched by an @#if statement"); }
<STMT>echo { return token::ECHO_DIR; } <STMT>echo { return token::ECHO_DIR; }
<STMT>error { return token::ERROR; } <STMT>error { return token::ERROR; }
@ -174,22 +174,20 @@ CONT \\\\
<STMT><<EOF>> { driver.error(*yylloc, "Unexpected end of file while parsing a macro statement"); } <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>{EOL} { yylloc->lines(1); yylloc->step(); for_body_tmp.append(yytext); }
<FOR_BODY>^{SPC}*\${SPC}*for({SPC}|{CONT}) { <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" */
nested_for_nb++; nested_for_nb++;
for_body_tmp.append(yytext); for_body_tmp.append(yytext);
yylloc->step(); yylloc->step();
} }
<FOR_BODY>. { 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><<EOF>> { driver.error(*yylloc, "Unexpected end of file: @#for loop not matched by an @#endfor"); }
<FOR_BODY>^{SPC}*\${SPC}*endfor{SPC}*{EOL} { <FOR_BODY>^{SPC}*@#{SPC}*endfor{SPC}*{EOL} {
yylloc->lines(1); yylloc->lines(1);
yylloc->step(); yylloc->step();
if (nested_for_nb) if (nested_for_nb)
{ {
/* This $endfor is not the end of the loop body, /* This @#endfor is not the end of the loop body,
but only that of a nested $for loop */ but only that of a nested @#for loop */
nested_for_nb--; nested_for_nb--;
for_body_tmp.append(yytext); 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>{EOL} { yylloc->lines(1); yylloc->step(); then_body_tmp.append(yytext); }
<THEN_BODY>^{SPC}*\${SPC}*if({SPC}|{CONT}) { <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" */
nested_if_nb++; nested_if_nb++;
then_body_tmp.append(yytext); then_body_tmp.append(yytext);
yylloc->step(); yylloc->step();
} }
<THEN_BODY>. { 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><<EOF>> { driver.error(*yylloc, "Unexpected end of file: @#if not matched by an @#endif"); }
<THEN_BODY>^{SPC}*\${SPC}*else{SPC}*{EOL} { <THEN_BODY>^{SPC}*@#{SPC}*else{SPC}*{EOL} {
yylloc->lines(1); yylloc->lines(1);
yylloc->step(); yylloc->step();
if (nested_if_nb) 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->lines(1);
yylloc->step(); yylloc->step();
if (nested_if_nb) if (nested_if_nb)
{ {
/* This $endif is not the end of the $if we're parsing, /* This @#endif is not the end of the @#if we're parsing,
but only that of a nested $if */ but only that of a nested @#if */
nested_if_nb--; nested_if_nb--;
then_body_tmp.append(yytext); 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>{EOL} { yylloc->lines(1); yylloc->step(); else_body_tmp.append(yytext); }
<ELSE_BODY>^{SPC}*\${SPC}*if({SPC}|{CONT}) { <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" */
nested_if_nb++; nested_if_nb++;
else_body_tmp.append(yytext); else_body_tmp.append(yytext);
yylloc->step(); yylloc->step();
} }
<ELSE_BODY>. { 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->lines(1);
yylloc->step(); yylloc->step();
if (nested_if_nb) if (nested_if_nb)
{ {
/* This $endif is not the end of the $if we're parsing, /* This @#endif is not the end of the @#if we're parsing,
but only that of a nested $if */ but only that of a nested @#if */
nested_if_nb--; nested_if_nb--;
else_body_tmp.append(yytext); else_body_tmp.append(yytext);
} }
@ -318,7 +312,7 @@ MacroFlex::MacroFlex(istream* in, ostream* out)
void void
MacroFlex::output_line(Macro::parser::location_type *yylloc) const 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; << 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; for_body_loc = context_stack.top().for_body_loc;
// Remove top of stack // Remove top of stack
context_stack.pop(); context_stack.pop();
// Dump $line instruction // Dump @#line instruction
output_line(yylloc); output_line(yylloc);
} }
@ -357,7 +351,7 @@ MacroFlex::create_include_context(string *filename, Macro::parser::location_type
yylloc->begin.column = yylloc->end.column = 0; yylloc->begin.column = yylloc->end.column = 0;
// We are not in a loop body // We are not in a loop body
for_body.clear(); for_body.clear();
// Output $line information // Output @#line information
output_line(yylloc); output_line(yylloc);
// Switch to new buffer // Switch to new buffer
yy_switch_to_buffer(yy_create_buffer(input, YY_BUF_SIZE)); yy_switch_to_buffer(yy_create_buffer(input, YY_BUF_SIZE));

View File

@ -1,6 +1,6 @@
// Computes the steady state which should be arrived at in ramst_homotopy.mod // Computes the steady state which should be arrived at in ramst_homotopy.mod
@include "common.mod" @#include "common.mod"
bet = 0.1; bet = 0.1;

View File

@ -2,7 +2,7 @@
// The values in initval block are obtained from ramst_initial.mod // The values in initval block are obtained from ramst_initial.mod
// Result of the computation should be the same than in ramst_final.mod // Result of the computation should be the same than in ramst_final.mod
@include "common.mod" @#include "common.mod"
initval; initval;
k = 12.75; k = 12.75;

View File

@ -1,6 +1,6 @@
// Computes the steady state used in initval block of ramst_homotopy.mod // Computes the steady state used in initval block of ramst_homotopy.mod
@include "common.mod" @#include "common.mod"
bet=0.05; bet=0.05;