]> git.draconx.ca Git - cdecl99.git/blobdiff - src/scan.l
libcdecl: Factor out toplevel parser actions.
[cdecl99.git] / src / scan.l
index 1342e5fd3dbbac0f095686e1bebd5ec2dea59d96..7549e6f7baee99cafb40a46d72f2211f10d511ce 100644 (file)
@@ -19,6 +19,9 @@
 
 #include <config.h>
 #include "parse.h"
+
+#define YY_NO_INPUT 1
+#define YY_NO_UNPUT 1
 }
 
 %option nodefault noyywrap bison-locations reentrant never-interactive
 #define STRTOUMAX strtoul
 #endif
 
-#define dup_token() do { \
-       yylval->strval = malloc(yyleng+1); \
-       if (!yylval->strval) { \
-               cdecl__errmsg(CDECL__ENOMEM); \
-               return T_LEX_ERROR; \
-       } \
-       memcpy(yylval->strval, yytext, yyleng); \
-       yylval->strval[yyleng] = 0; \
-} while(0)
-
 static char *to_octal(char *dst, unsigned val)
 {
        unsigned i;
@@ -124,21 +117,27 @@ INTEGER 0x[[:xdigit:]]+|0[0-7]+|[[:digit:]]+
 
 %{
        char *c;
-
-       if (yyextra > 0) {
-               yyextra = -yyextra;
-               return T_ENGLISH;
-       }
 %}
 
-"..." return T_ELLIPSIS;
-";"   return T_SEMICOLON;
-"*"   return T_ASTERISK;
-"("   return T_LPAREN;
-")"   return T_RPAREN;
-"["   return T_LBRACKET;
-"]"   return T_RBRACKET;
-","   return T_COMMA;
+"..."|[][;*(),] {
+       unsigned char *match;
+       static const unsigned char tab[2][8] = {
+               "*[](),.;",
+               {
+                       PACK_TOKEN(T_ASTERISK),
+                       PACK_TOKEN(T_LBRACKET),
+                       PACK_TOKEN(T_RBRACKET),
+                       PACK_TOKEN(T_LPAREN),
+                       PACK_TOKEN(T_RPAREN),
+                       PACK_TOKEN(T_COMMA),
+                       PACK_TOKEN(T_ELLIPSIS),
+                       PACK_TOKEN(T_SEMICOLON)
+               }
+       };
+
+       match = memchr(&tab, yytext[0], sizeof tab[0]);
+       return UNPACK_TOKEN(match[sizeof tab[0]]);
+}
 
 {INTEGER} {
        char *end;
@@ -158,8 +157,11 @@ INTEGER 0x[[:xdigit:]]+|0[0-7]+|[[:digit:]]+
 }
 
 {IDENT} {
-       int ret = cdecl__to_keyword(yytext, yyleng, yyextra);
-       if (ret == T_IDENT) {
+       unsigned x = cdecl__to_keyword(yytext, yyleng, yyextra);
+       int tok;
+
+       yylval->spectype = UNPACK_SPEC(x & 0xff);
+       if ((tok = (x >> 8)) == PACK_TOKEN(T_IDENT)) {
                /*
                 * Our IDENT pattern includes hyphens so we can match
                 * "variable-length" as a keyword.  In all other cases a
@@ -176,9 +178,11 @@ INTEGER 0x[[:xdigit:]]+|0[0-7]+|[[:digit:]]+
 #else
                yyless(strcspn(yytext, "-"));
 #endif
-               dup_token();
+               if (!(yylval->item = cdecl__alloc_item(yyleng+1)))
+                       return T_LEX_ERROR; \
+               memcpy(yylval->item->s, yytext, yyleng+1);
        }
-       return ret;
+       return UNPACK_TOKEN(tok);
 }
 
 [[:space:]]+