update the buffer pointer after possible relocation in realloc()
[fw/sdcc] / src / SDCC.lex
index babc6261bff4a5a90a429fcc2dfded9ec1e03a0f..b6ff35a6c0d77eed72505ce71b3d22ded639ece6 100644 (file)
@@ -34,6 +34,7 @@ IS       (u|U|l|L)*
 #include <string.h>
 #include <ctype.h>
 #include "common.h"
+#include "newalloc.h"
     
 char *stringLiteral();
 char *currFname;
@@ -53,7 +54,8 @@ int yywrap YY_PROTO((void))
 }
 #define TKEYWORD(token) return (isTargetKeyword(yytext) ? token :\
                                check_type(yytext))
-char asmbuff[MAX_INLINEASM]                    ;
+char *asmbuff=NULL;
+int asmbuffSize=0;
 char *asmp ;
 extern int check_type          ();
  extern int isTargetKeyword     ();
@@ -81,14 +83,41 @@ struct options  save_options  ;
 %}
 %x asm
 %%
-"_asm"        {  count(); asmp = asmbuff ;BEGIN(asm) ;}
-<asm>"_endasm" { count()               ; 
-                  *asmp = '\0'                         ; 
-                  strcpy(yylval.yyinline,asmbuff)              ; 
-                  BEGIN(INITIAL)       ;
-                  return (INLINEASM)                   ; }
-<asm>.         { *asmp++ = yytext[0]   ; }
-<asm>\n        { count(); *asmp++ = '\n' ;}
+"_asm"         {  
+  count(); 
+  asmp = asmbuff = Safe_realloc (asmbuff, INITIAL_INLINEASM);
+  asmbuffSize=INITIAL_INLINEASM;
+  BEGIN(asm) ;
+}
+<asm>"_endasm" { 
+  count();
+  *asmp = '\0';
+  yylval.yyinline = Safe_calloc (1, strlen(asmbuff)+1);
+  strcpy(yylval.yyinline,asmbuff);
+  BEGIN(INITIAL);
+  return (INLINEASM);
+}
+<asm>.         { 
+  if (asmp-asmbuff >= asmbuffSize-2) {
+    // increase the buffersize with 50%
+    int size=asmp-asmbuff;
+    asmbuffSize=asmbuffSize*3/2;
+    asmbuff = Safe_realloc (asmbuff, asmbuffSize); 
+    asmp=asmbuff+size;
+  }
+  *asmp++ = yytext[0];
+}
+<asm>\n        { 
+  count(); 
+  if (asmp-asmbuff >= asmbuffSize-3) {
+    // increase the buffersize with 50%
+    int size=asmp-asmbuff;
+    asmbuffSize=asmbuffSize*3/2;
+    asmbuff = Safe_realloc (asmbuff, asmbuffSize); 
+    asmp=asmbuff+size;
+  }
+  *asmp++ = '\n' ;
+}
 "/*"          { comment(); }
 "at"          { count(); TKEYWORD(AT)  ; }
 "auto"        { count(); return(AUTO); }
@@ -117,6 +146,7 @@ struct options  save_options  ;
 "if"           { count(); return(IF); }
 "int"          { count(); return(INT); }
 "interrupt"    { count(); return(INTERRUPT);}
+"nonbanked"    { count(); TKEYWORD(NONBANKED);}
 "banked"       { count(); TKEYWORD(BANKED);}
 "long"        { count(); return(LONG); }
 "near"        { count(); TKEYWORD(DATA);}
@@ -261,7 +291,7 @@ int checkCurrFile ( char *s)
        /* mark the end of the filename */
        while (*s != '"') s++;
        *s = '\0';
-       ALLOC_ATOMIC(currFname,strlen(sb)+1);
+       currFname = Safe_calloc(1,strlen(sb)+1);
        strcpy(currFname,sb);
        yylineno = lNum - 2;
     }
@@ -326,47 +356,67 @@ int check_type()
 
 char strLitBuff[2048]                  ;
 
+/*
+ * Change by JTV 2001-05-19 to not concantenate strings
+ * to support ANSI hex and octal escape sequences in string liteals 
+ */
+
 char *stringLiteral ()
+
 {
-       int ch;
-       char *str = strLitBuff                  ;
-       
-       *str++ = '\"'                   ;
-       /* put into the buffer till we hit the */
-       /* first \" */
-       while (1) {
-
-          ch = input()                 ;
-          if (!ch)         break       ; /* end of input */
-         /* if it is a \ then everything allowed */
-         if (ch == '\\') {
-            *str++ = ch     ; /* backslash in place */
-            *str++ = input()           ; /* following char in place */
-            continue                   ;      /* carry on */
-            }
-            
-        /* if new line we have a new line break */
-        if (ch == '\n') break          ;
-        
-        /* if this is a quote then we have work to do */
-        /* find the next non whitespace character     */
-        /* if that is a double quote then carry on    */
-        if (ch == '\"') {
-        
-            while ((ch = input()) && isspace(ch)) ;
-            if (!ch) break             ; 
-            if (ch != '\"') {
-                 unput(ch)                     ;
-                 break                 ;
-                 }
-                 
-                 continue              ;
-        }
-       *str++  = ch;     
-     }  
-     *str++ = '\"'                     ;
-     *str = '\0';
-     return strLitBuff                 ;
+int ch;
+char *str = strLitBuff                 ;
+
+*str++ = '\"'                  ;
+/* put into the buffer till we hit the */
+/* first \" */
+
+while (1) 
+  {
+  ch = input()                 ;
+
+  if (!ch)
+    break      ; /* end of input */
+
+  /* if it is a \ then everything allowed */
+
+  if (ch == '\\') 
+    {
+    *str++ = ch     ; /* backslash in place */
+    *str++ = input()           ; /* following char in place */
+    continue                   ;      /* carry on */
+    }
+
+  /* if new line we have a new line break */
+  if (ch == '\n') 
+    break              ;
+
+  /* if this is a quote then we have work to do */
+  /* find the next non whitespace character     */
+  /* if that is a double quote then carry on    */
+
+  if (ch == '\"') 
+    {
+    *str++  = ch ;     /* Pass end of this string or substring to evaluator */
+
+    while ((ch = input()) && isspace(ch)) ;
+
+    if (!ch) 
+      break ; 
+
+    if (ch != '\"') 
+      {
+      unput(ch) ;
+      break ;
+      }
+    }
+
+  *str++  = ch;              /* Put next substring introducer into output string */
+  }  
+
+*str = '\0';
+
+return strLitBuff                      ;
 }
 
 void doPragma (int op, char *cp)
@@ -520,3 +570,17 @@ int isTargetKeyword(char *s)
     
     return 0;
 }
+
+extern int fatalError;
+
+int yyerror(char *s)
+{
+   fflush(stdout);
+
+   if (yylineno && filename)
+       fprintf(stdout,"\n%s(%d) %s: token -> '%s' ; column %d\n",
+               filename,yylineno,
+               s,yytext,column);
+   fatalError++;
+   return 0;
+}