From ba27ff51f2122032edbe234966928ba91f7faf37 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 18 Nov 2024 12:12:16 +0100
Subject: [PATCH] Flex scanner: give a name to some regular expressions

---
 src/DynareFlex.ll | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/DynareFlex.ll b/src/DynareFlex.ll
index 7e72922c..3a526150 100644
--- a/src/DynareFlex.ll
+++ b/src/DynareFlex.ll
@@ -69,6 +69,8 @@ string eofbuff;
 #define YY_USER_ACTION location_increment(yylloc, yytext);
 %}
 
+NAME [a-z_][a-z0-9_]*
+FLOAT_NUMBER ((([0-9]*\.[0-9]+)|([0-9]+\.))([ed][-+]?[0-9]+)?)|([0-9]+[ed][-+]?[0-9]+)
 DATE -?[0-9]+([ya]|m([1-9]|1[0-2])|q[1-4])
 
 %%
@@ -1048,12 +1050,12 @@ DATE -?[0-9]+([ya]|m([1-9]|1[0-2])|q[1-4])
 <DYNARE_STATEMENT>use_shock_groups {return token::USE_SHOCK_GROUPS;}
 <DYNARE_STATEMENT>colormap {return token::COLORMAP;}
 
-<DYNARE_STATEMENT,DYNARE_BLOCK>[a-z_][a-z0-9_]* {
+<DYNARE_STATEMENT,DYNARE_BLOCK>{NAME} {
   yylval->emplace<string>(yytext);
   return token::NAME;
 }
 
-<DYNARE_STATEMENT,DYNARE_BLOCK>((([0-9]*\.[0-9]+)|([0-9]+\.))([ed][-+]?[0-9]+)?)|([0-9]+[ed][-+]?[0-9]+) {
+<DYNARE_STATEMENT,DYNARE_BLOCK>{FLOAT_NUMBER} {
   yylval->emplace<string>(yytext);
   return token::FLOAT_NUMBER;
 }
@@ -1100,7 +1102,7 @@ DATE -?[0-9]+([ya]|m([1-9]|1[0-2])|q[1-4])
     element in initval (in which case Dynare recognizes the matrix name as an external
     function symbol), and may want to modify the matrix later with Matlab statements.
  */
-<INITIAL>[a-z_][a-z0-9_]* {
+<INITIAL>{NAME} {
   if (driver.symbol_exists_and_is_not_modfile_local_or_external_function(yytext))
     {
       BEGIN DYNARE_STATEMENT;
@@ -1125,7 +1127,7 @@ DATE -?[0-9]+([ya]|m([1-9]|1[0-2])|q[1-4])
     be able to back out of the statement if we realize it's a native statement
     and move to the NATIVE context
  */
-<INITIAL>\[([[:space:]]*[a-z_][a-z0-9_]*[[:space:]]*,{1}[[:space:]]*)*([[:space:]]*[a-z_][a-z0-9_]*[[:space:]]*){1}\] {
+<INITIAL>\[([[:space:]]*{NAME}[[:space:]]*,{1}[[:space:]]*)*([[:space:]]*{NAME}[[:space:]]*){1}\] {
   string yytextcpy{yytext};
   yytextcpy.erase(remove(yytextcpy.begin(), yytextcpy.end(), '['), yytextcpy.end());
   yytextcpy.erase(remove(yytextcpy.begin(), yytextcpy.end(), ']'), yytextcpy.end());
-- 
GitLab