From 6582341799d35e80c59f6f3559dbd18e70443747 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtanb@gmail.com>
Date: Fri, 18 Jun 2010 12:39:29 +0200
Subject: [PATCH] enable multi-line native matlab statements
---
preprocessor/DynareFlex.ll | 56 +++++++++++++++++++++--------------
preprocessor/ParsingDriver.cc | 12 ++++++--
preprocessor/ParsingDriver.hh | 4 ++-
3 files changed, 46 insertions(+), 26 deletions(-)
diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll
index 83d7671205..d4e896238f 100644
--- a/preprocessor/DynareFlex.ll
+++ b/preprocessor/DynareFlex.ll
@@ -44,6 +44,7 @@ int comment_caller, line_caller;
this flag is set to 1, when command finished it is set to 0
*/
int sigma_e = 0;
+string eofbuff;
%}
%option c++
@@ -52,11 +53,12 @@ int sigma_e = 0;
%option case-insensitive noyywrap nounput batch debug never-interactive
- /* NB: if new start conditions are defined, add them in the line for [\n]+ */
+ /* NB: if new start conditions are defined, add them in the line for [\n]+ and <<EOF>>*/
%x COMMENT
%x DYNARE_STATEMENT
%x DYNARE_BLOCK
%x NATIVE
+%x NATIVE_COMMENT
%x LINE1
%x LINE2
%x LINE3
@@ -569,29 +571,39 @@ int sigma_e = 0;
/* Add the native statement */
<NATIVE>{
- [^/%*\n]* |
- "*" |
- "/" { yymore(); }
- \n {
- driver.add_native_remove_charset(yytext, "\n");
- BEGIN INITIAL;
- }
- "%".* {
- driver.add_native_remove_charset(yytext, "%");
- BEGIN INITIAL;
- }
- "//".* {
- driver.add_native_remove_charset(yytext, "//");
- BEGIN INITIAL;
- }
- "/*" {
- driver.add_native_remove_charset(yytext, "/*");
- comment_caller = INITIAL;
- BEGIN COMMENT;
- }
+ [^/%*\n\.]* |
+ \.{1,2} |
+ "*" |
+ "/" { yymore(); eofbuff = string(yytext); }
+ \.{3,}[[:space:]]*\n { driver.add_native_remove_charset(yytext, "\n"); }
+ \n {
+ if (strlen(yytext) > 1)
+ driver.add_native_remove_charset(yytext, "\n");
+ BEGIN INITIAL;
+ }
+ <<EOF>> {
+ driver.add_native(eofbuff);
+ yyterminate();
+ }
+ \.{3,}[[:space:]]*"%".*\n |
+ "%"[^\n]* { driver.add_native_remove_charset(yytext, "%"); }
+ \.{3,}[[:space:]]*"//".*\n |
+ "//"[^\n]* { driver.add_native_remove_charset(yytext, "//"); }
+ \.{3,}[[:space:]]*"/*" {
+ driver.add_native_remove_charset(yytext, "/*");
+ BEGIN NATIVE_COMMENT;
+ }
+ "/*" {
+ driver.add_native_remove_charset(yytext, "/*");
+ comment_caller = NATIVE;
+ BEGIN COMMENT;
+ }
}
-<*><<EOF>> { yyterminate(); }
+<NATIVE_COMMENT>"*/"[[:space:]]*\n { BEGIN NATIVE; }
+<NATIVE_COMMENT>.
+
+<INITIAL,DYNARE_STATEMENT,DYNARE_BLOCK,COMMENT,LINE1,LINE2,LINE3,NATIVE_COMMENT><<EOF>> { yyterminate(); }
<*>. { driver.error(*yylloc, "character unrecognized by lexer"); }
%%
diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc
index 846a0989f8..13fdc33184 100644
--- a/preprocessor/ParsingDriver.cc
+++ b/preprocessor/ParsingDriver.cc
@@ -1840,14 +1840,20 @@ ParsingDriver::add_model_var_or_external_function(string *function_name, bool in
}
void
-ParsingDriver::add_native_remove_charset(const char *s, const char *token)
+ParsingDriver::add_native(const string &s)
+{
+ mod_file->addStatement(new NativeStatement(s));
+}
+
+void
+ParsingDriver::add_native_remove_charset(const char *s, const string &token)
{
string str = string(s);
- string tok = string(token);
size_t found = str.find(token);
assert(found != string::npos);
- mod_file->addStatement(new NativeStatement(str.substr(0, found)));
+ str.resize(found);
+ add_native(str);
}
void
diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh
index 987feb1f87..3acb8460c7 100644
--- a/preprocessor/ParsingDriver.hh
+++ b/preprocessor/ParsingDriver.hh
@@ -489,8 +489,10 @@ public:
void add_external_function_arg(NodeID arg);
//! Adds an external function call node
NodeID add_model_var_or_external_function(string *function_name, bool in_model_block);
+ //! Adds a native statement
+ void add_native(const string &s);
//! Adds a native statement, first removing the set of characters passed in token (and everything after)
- void add_native_remove_charset(const char *s, const char *token);
+ void add_native_remove_charset(const char *s, const string &token);
//! Resets data_tree and model_tree pointers to default (i.e. mod_file->expressions_tree)
void reset_data_tree();
//! Begin a steady_state_model block
--
GitLab