wip II
[sdk] / compiler / libec / src / lexer.l
index 53497ff..62b047f 100644 (file)
@@ -9,6 +9,7 @@ IDENT    {L}({L}|{D})*
 %{
 import "ecdefs"
 #define YYLTYPE Location
+#define YY_NEVER_INTERACTIVE 1    // Avoid calling isatty on eC File object
 
 #include "grammar.h"
 
@@ -94,8 +95,10 @@ default:
 "return"             { return(RETURN); }
 "short"              { return(SHORT); }
 "signed"             { return(SIGNED); }
+"__signed"           { return(SIGNED); }
 "__signed__"         { return(SIGNED); }
 "sizeof"             { return(SIZEOF); }
+"__alignof__"        { return(ALIGNOF); }
 "static"             { return(STATIC); }
 "struct"             { return(STRUCT); }
 "switch"             { return(SWITCH); }
@@ -131,13 +134,31 @@ default:
 
  /* DID I MEAN? "__attribute__"" "*"(("" "*({D}|{L})*" "*("("({D}|{L})*(" "*","" "*({D}|{L})*)*" "*")")?" "*"))" { return(EXT_ATTRIB); } */
 
-"__attribute_deprecated__"(" "*)"(("(" "*)({D}|{L})*(" "*)("("({D}|{L}|\")*((" "*)","(" "*)({D}|{L}|\")*)*(" "*)")")?(" "*)(","(" "*)({D}|{L})*(" "*)("("({D}|{L}|\")*((" "*)","(" "*)({D}|{L}|\")*)*(" "*)")")?(" "*))*"))" { return(EXT_ATTRIB); }
-"__attribute__"(" "*)"(("(" "*)({D}|{L})*(" "*)("("({D}|{L}|\")*((" "*)","(" "*)({D}|{L}|\")*)*(" "*)")")?(" "*)(","(" "*)({D}|{L})*(" "*)("("({D}|{L}|\")*((" "*)","(" "*)({D}|{L}|\")*)*(" "*)")")?(" "*))*"))" { return(EXT_ATTRIB); }
-"__attribute"(" "*)"(("(" "*)({D}|{L})*(" "*)("("({D}|{L}|\")*((" "*)","(" "*)({D}|{L}|\")*)*(" "*)")")?(" "*)(","(" "*)({D}|{L})*(" "*)("("({D}|{L}|\")*((" "*)","(" "*)({D}|{L}|\")*)*(" "*)")")?(" "*))*"))" { return(EXT_ATTRIB); }
-
+ /*
+ "__attribute_deprecated__"(" "*)"(("(" "*)({D}|{L})*(" "*)("("({D}|{L}|\")*((" "*)","(" "*)({D}|{L}|\")*)*(" "*)")")?(" "*)(","(" "*)({D}|{L})*(" "*)("("({D}|{L}|\")*((" "*)","(" "*)({D}|{L}|\")*)*(" "*)")")?(" "*))*"))" { return(EXT_ATTRIB); }
+ "__attribute__"           (" "*)"(("(" "*)({D}|{L})*(" "*)("("({D}|{L}|\")*((" "*)","(" "*)({D}|{L}|\")*)*(" "*)")")?(" "*)(","(" "*)({D}|{L})*(" "*)("("({D}|{L}|\")*((" "*)","(" "*)({D}|{L}|\")*)*(" "*)")")?(" "*))*"))" { return(EXT_ATTRIB); }
+ "__attribute"             (" "*)"(("(" "*)({D}|{L})*(" "*)("("({D}|{L}|\")*((" "*)","(" "*)({D}|{L}|\")*)*(" "*)")")?(" "*)(","(" "*)({D}|{L})*(" "*)("("({D}|{L}|\")*((" "*)","(" "*)({D}|{L}|\")*)*(" "*)")")?(" "*))*"))" { return(EXT_ATTRIB); }
+ */
+ /*
+ [__attribute__] [spaces]
+   [((] [spaces] 
+      [digits | letters] [spaces]
+         ( [(]  [digits or letters or "]  ( [spaces] [,] [spaces] [digits or letters or "] )*  [spaces]  [)] )?
+      [spaces]
+      ( [,] [spaces]  [digits or letters]  [spaces]
+         ( [(]  [digits or letters or "]
+            ( [spaces] [,] [spaces] [digits or letters or "] )*  [spaces] [)]
+         )? [spaces] 
+      )*
+   [))]
+ */
  /* "__attribute__".?"((".?({D}|{L})*.?("("({D}|{L})*(.?",".?({D}|{L})*)*.?")")?.?"))" { return(EXT_ATTRIB); } */
  /* "__attribute".?"((".?({D}|{L})*.?("("({D}|{L})*(.?",".?({D}|{L})*)*.?")")?.?"))" { return(EXT_ATTRIB); }*/
 
+"__attribute_deprecated__" { return ATTRIB_DEP; }
+"__attribute__" { return ATTRIB; }
+"__attribute" { return __ATTRIB; }
+
 "__inline__"               { return(EXT_STORAGE); }
 "_inline"                  { return(EXT_STORAGE); }
 "__inline"                 { return(EXT_STORAGE); }
@@ -245,8 +266,15 @@ L?\"(\\.|[^\\"])*\"     { return(STRING_LITERAL); }
 "$"         { return('$'); }
 
 <<EOF>> {
+      while(include_stack_ptr && !fileStack[include_stack_ptr-1])
+      {
+         --include_stack_ptr;
+         defaultDeclMode = declMode = declModeStack[include_stack_ptr];
+      }
+
       if ( --include_stack_ptr < 0 )
       {
+         include_stack_ptr = 0;
          yyterminate();
       }
       else
@@ -279,6 +307,8 @@ L?\"(\\.|[^\\"])*\"     { return(STRING_LITERAL); }
 
 %%
 
+private:
+
 yywrap()
 {
    return(1);
@@ -380,6 +410,8 @@ int preprocessor()
                char fileName[MAX_LOCATION];
                
                int inOut;
+
+               fileName[0] = 0;
                GetString(&pointer, fileName, MAX_LOCATION);
                inOut = GetValue(&pointer);
                               
@@ -393,6 +425,7 @@ int preprocessor()
                   if(!strcmp(extension, "c") || !strcmp(extension, "h"))
                      declMode = defaultDeclMode = defaultAccess;
 
+                  fileStack[include_stack_ptr] = null;
                   include_stack_ptr++;
                }
                else if(inOut == 2)
@@ -404,7 +437,11 @@ int preprocessor()
                yylloc.end.charPos = 1;
                yylloc.end.line = lineNumber;
                //yylloc.end.pos -= count;
-               yylloc.end.included = (include_stack_ptr > 0) ? GetIncludeFileID(fileName) : 0;
+
+               if(include_stack_ptr > 0 || (lineNumber && fileName[0]))
+                  yylloc.end.included = GetIncludeFileID(fileName);
+               else
+                  yylloc.end.included = 0;
             }
             /*
             int lineNumber = strtol(line+1, &endPtr, 0);
@@ -448,7 +485,7 @@ int preprocessor()
    {
       char includeFile[MAX_LOCATION] = "";
 
-      strcpy(line, line+c);
+      memmove(line, line+c, strlen(line+c)+1);
       TrimLSpaces(line, line);
       if(line[0] == '\"')
       {
@@ -475,7 +512,7 @@ int preprocessor()
 
       if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
       {
-         fprintf( stderr, "Includes nested too deeply" );
+         fprintf( stderr, $"Includes nested too deeply" );
          exit( 1 );
       }
 
@@ -517,17 +554,17 @@ public void resetScanner()
    yylloc.start.charPos = yylloc.end.charPos = 1;
    yylloc.start.line = yylloc.end.line = 1;
    yylloc.start.pos = yylloc.end.pos = 0;
-   yylloc.start.included = yylloc.end.included = false;
+   yylloc.start.included = yylloc.end.included = 0;
 
    expression_yylloc.start.charPos = expression_yylloc.end.charPos = 1;
    expression_yylloc.start.line = expression_yylloc.end.line = 1;
    expression_yylloc.start.pos = expression_yylloc.end.pos = 0;
-   expression_yylloc.start.included = expression_yylloc.end.included = false;
+   expression_yylloc.start.included = expression_yylloc.end.included = 0;
 
    type_yylloc.start.charPos = type_yylloc.end.charPos = 1;
    type_yylloc.start.line = type_yylloc.end.line = 1;
    type_yylloc.start.pos = type_yylloc.end.pos = 0;
-   type_yylloc.start.included = type_yylloc.end.included = false;
+   type_yylloc.start.included = type_yylloc.end.included = 0;
 
    include_stack_ptr = 0;
 }
@@ -539,3 +576,112 @@ void resetScannerPos(CodePosition pos)
    type_yylloc.start = type_yylloc.end = pos;
    expression_yylloc.start = expression_yylloc.end = pos;
 }
+
+class LexerBackup
+{
+   YYLTYPE yylloc;
+   YYLTYPE type_yylloc;
+   YYLTYPE expression_yylloc;
+
+   AccessMode declMode;
+   AccessMode defaultDeclMode;
+
+   File fileInput;
+   YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
+   File fileStack[MAX_INCLUDE_DEPTH];
+   char sourceFileStack[MAX_INCLUDE_DEPTH][MAX_LOCATION];
+   YYLTYPE locStack[MAX_INCLUDE_DEPTH];
+   AccessMode declModeStack[MAX_INCLUDE_DEPTH];
+   int include_stack_ptr;
+   YY_BUFFER_STATE buffer;
+
+   int yy_n_chars;
+   char * yytext_ptr;
+   char * yy_c_buf_p;
+   FILE * yyin;
+   char yy_hold_char;
+   int yychar;
+   int yy_init;
+   int yy_start;
+
+};
+
+LexerBackup pushLexer()
+{
+   LexerBackup backup { };
+
+   backup.yylloc = yylloc;
+   backup.type_yylloc = type_yylloc;
+   backup.expression_yylloc = expression_yylloc;
+   backup.fileInput = fileInput;
+   memcpy(backup.include_stack, include_stack, sizeof(include_stack));
+   memcpy(backup.fileStack, fileStack, sizeof(fileStack));
+   memcpy(backup.sourceFileStack, sourceFileStack, sizeof(sourceFileStack));
+   memcpy(backup.locStack, locStack, sizeof(locStack));
+   memcpy(backup.declModeStack, declModeStack, sizeof(declModeStack));
+   backup.include_stack_ptr = include_stack_ptr;
+   backup.defaultDeclMode = defaultDeclMode;
+   backup.declMode = declMode;
+   backup.buffer = yy_current_buffer;
+
+   backup.yy_n_chars = yy_n_chars;
+   backup.yytext_ptr = yytext_ptr;
+   backup.yy_c_buf_p = yy_c_buf_p;
+   backup.yyin = yyin;
+   backup.yy_hold_char = yy_hold_char;
+   backup.yychar = yychar;
+   backup.yy_init = yy_init;
+   backup.yy_start = yy_start;
+
+   yy_init = 1;
+
+   yy_current_buffer = 0;
+
+   yylloc.start.charPos = yylloc.end.charPos = 1;
+   yylloc.start.line = yylloc.end.line = 1;
+   yylloc.start.pos = yylloc.end.pos = 0;
+   yylloc.start.included = yylloc.end.included = 0;
+
+   expression_yylloc.start.charPos = expression_yylloc.end.charPos = 1;
+   expression_yylloc.start.line = expression_yylloc.end.line = 1;
+   expression_yylloc.start.pos = expression_yylloc.end.pos = 0;
+   expression_yylloc.start.included = expression_yylloc.end.included = 0;
+
+   type_yylloc.start.charPos = type_yylloc.end.charPos = 1;
+   type_yylloc.start.line = type_yylloc.end.line = 1;
+   type_yylloc.start.pos = type_yylloc.end.pos = 0;
+   type_yylloc.start.included = type_yylloc.end.included = 0;
+
+   include_stack_ptr = 0;
+
+   return backup;
+}
+
+void popLexer(LexerBackup backup)
+{
+   yylloc = backup.yylloc;
+   type_yylloc = backup.type_yylloc;
+   expression_yylloc = backup.expression_yylloc;
+   fileInput = backup.fileInput;
+   memcpy(include_stack, backup.include_stack, sizeof(include_stack));
+   memcpy(fileStack, backup.fileStack, sizeof(fileStack));
+   memcpy(sourceFileStack, backup.sourceFileStack, sizeof(sourceFileStack));
+   memcpy(locStack, backup.locStack, sizeof(locStack));
+   memcpy(declModeStack, backup.declModeStack, sizeof(declModeStack));
+   include_stack_ptr = backup.include_stack_ptr;
+   defaultDeclMode = backup.defaultDeclMode;
+   declMode = backup.declMode;
+
+   // yy_switch_to_buffer(backup.buffer);
+   yy_current_buffer = backup.buffer;
+   yy_n_chars = backup.yy_n_chars;
+   yytext_ptr = backup.yytext_ptr;
+   yy_c_buf_p = backup.yy_c_buf_p;
+   yyin = backup.yyin;
+   yy_hold_char = backup.yy_hold_char;
+   yychar = backup.yychar;
+   yy_init = backup.yy_init;
+   yy_start = backup.yy_start;
+
+   delete backup;
+}