v4 preprocessor/macro: cosmetic changes in the stack used for nested includes

git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1720 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
sebastien 2008-02-22 14:03:46 +00:00
parent 2af63803e8
commit c43a8d6bdf
2 changed files with 20 additions and 22 deletions

View File

@ -48,10 +48,12 @@ using namespace std;
class MacroFlex : public MacroFlexLexer class MacroFlex : public MacroFlexLexer
{ {
private: private:
//! The stack of scanner states //! The stack used to handle (possibly nested) includes
/*! We could have used instead yypush_buffer_state() and yypop_buffer_state(), /*! Keeps track of buffer state and associated location, as they were just before switching to
but those functions do not exist in Flex 2.5.4 */ included file.
stack<struct yy_buffer_state *> state_stack; Note that we could have used yypush_buffer_state() and yypop_buffer_state()
instead of a stack for buffer states, but those functions do not exist in Flex 2.5.4 */
stack<pair<struct yy_buffer_state *, Macro::parser::location_type> > include_stack;
public: public:
MacroFlex(istream* in = 0, ostream* out = 0); MacroFlex(istream* in = 0, ostream* out = 0);
@ -89,9 +91,6 @@ public:
//! Pointer to keep track of the input file stream currently scanned //! Pointer to keep track of the input file stream currently scanned
ifstream *ifs; ifstream *ifs;
//! Stack of locations used for (possibly nested) includes
stack<Macro::parser::location_type> loc_stack;
//! Name of main file being parsed //! Name of main file being parsed
string file; string file;

View File

@ -68,20 +68,19 @@ typedef Macro::parser::token token;
driver.ifs = new ifstream(yytext, ios::binary); driver.ifs = new ifstream(yytext, ios::binary);
if (driver.ifs->fail()) if (driver.ifs->fail())
driver.error(*yylloc, "Could not open " + string(yytext)); driver.error(*yylloc, "Could not open " + string(yytext));
// Save old location // Save old buffer state and location
/* We don't use yypush_buffer_state(), since it doesn't exist in
Flex 2.5.4 (see Flex 2.5.33 info file - section 11 - for code
example with yypush_buffer_state()) */
yylloc->step(); yylloc->step();
driver.loc_stack.push(*yylloc); include_stack.push(make_pair(YY_CURRENT_BUFFER, *yylloc));
// Reset location // Reset location
yylloc->begin.filename = yylloc->end.filename = new string(yytext); yylloc->begin.filename = yylloc->end.filename = new string(yytext);
yylloc->begin.line = yylloc->end.line = 1; yylloc->begin.line = yylloc->end.line = 1;
yylloc->begin.column = yylloc->end.column = 0; yylloc->begin.column = yylloc->end.column = 0;
// Display @line // Output @line information
*yyout << "@line \"" << *yylloc->begin.filename << "\" 1" << endl; *yyout << "@line \"" << *yylloc->begin.filename << "\" 1" << endl;
// Switch to new buffer // Switch to new buffer
/* We don't use yypush_buffer_state(), since it doesn't exist in
Flex 2.5.4 (see Flex 2.5.33 info file - section 11 - for code
example with yypush_buffer_state()) */
state_stack.push(YY_CURRENT_BUFFER);
yy_switch_to_buffer(yy_create_buffer(driver.ifs, YY_BUF_SIZE)); yy_switch_to_buffer(yy_create_buffer(driver.ifs, YY_BUF_SIZE));
BEGIN(INITIAL); BEGIN(INITIAL);
} }
@ -142,22 +141,22 @@ typedef Macro::parser::token token;
<<EOF>> { <<EOF>> {
/* We don't use yypop_buffer_state(), since it doesn't exist in
Flex 2.5.4 (see Flex 2.5.33 info file - section 11 - for code
example with yypop_buffer_state()) */
// Quit lexer if end of main file // Quit lexer if end of main file
if (state_stack.empty()) if (include_stack.empty())
{ {
yyterminate(); yyterminate();
} }
// Else restore old flex buffer // Else restore old flex buffer
/* We don't use yypop_buffer_state(), since it doesn't exist in
Flex 2.5.4 (see Flex 2.5.33 info file - section 11 - for code
example with yypop_buffer_state()) */
yy_delete_buffer(YY_CURRENT_BUFFER); yy_delete_buffer(YY_CURRENT_BUFFER);
yy_switch_to_buffer(state_stack.top()); yy_switch_to_buffer(include_stack.top().first);
state_stack.pop();
// And restore old location // And restore old location
delete yylloc->begin.filename; delete yylloc->begin.filename;
*yylloc = driver.loc_stack.top(); *yylloc = include_stack.top().second;
driver.loc_stack.pop(); // Remove top of stack
include_stack.pop();
BEGIN(END_INCLUDE); BEGIN(END_INCLUDE);
} }