]> git.draconx.ca Git - cdecl99.git/blob - src/scan.l
Fix error name of ellipsis token.
[cdecl99.git] / src / scan.l
1 %top{
2 /*
3  *  Scanner for C declarations.
4  *  Copyright © 2011 Nick Bowler
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20  #include "parse.h"
21 }
22
23 %option noyywrap bison-locations reentrant
24
25 %{
26 #define lex_error(msg) do { \
27         yyerror(yylloc, NULL, NULL, (msg)); \
28         return T_LEX_ERROR; \
29 } while(0)
30 %}
31
32 IDENT [_[:alpha:]][_[:alnum:]]*
33 INTEGER 0x[[:xdigit:]]+|0[0-7]+|[[:digit:]]+
34
35 %%
36
37 "..." return T_ELLIPSIS;
38 ";"   return T_SEMICOLON;
39 "*"   return T_ASTERISK;
40 "("   return T_LPAREN;
41 ")"   return T_RPAREN;
42 "["   return T_LBRACKET;
43 "]"   return T_RBRACKET;
44 ","   return T_COMMA;
45
46 "typedef"    return T_TYPEDEF;
47 "extern"     return T_EXTERN;
48 "static"     return T_STATIC;
49 "auto"       return T_AUTO;
50 "register"   return T_REGISTER;
51
52 "restrict"   return T_RESTRICT;
53 "volatile"   return T_VOLATILE;
54 "const"      return T_CONST;
55
56 "inline"     return T_INLINE;
57
58 "void"       return T_VOID;
59 "char"       return T_CHAR;
60 "short"      return T_SHORT;
61 "int"        return T_INT;
62 "long"       return T_LONG;
63 "float"      return T_FLOAT;
64 "double"     return T_DOUBLE;
65 "signed"     return T_SIGNED;
66 "unsigned"   return T_UNSIGNED;
67 "_Bool"      return T_BOOL;
68 "_Complex"   return T_COMPLEX;
69 "_Imaginary" return T_IMAGINARY;
70
71 "struct"     return T_STRUCT;
72 "union"      return T_UNION;
73 "enum"       return T_ENUM;
74
75 {INTEGER} {
76         char *end;
77
78         errno = 0;
79         yylval->uintval = strtoumax(yytext, &end, 0);
80         if (errno == ERANGE)
81                 lex_error("integer constant out of range");
82         if (*end)
83                 lex_error("invalid integer constant");
84
85         return T_UINT;
86 }
87
88 {IDENT} {
89         yylval->strval = malloc(yyleng+1);
90         if (!yylval->strval)
91                 lex_error("failed to allocate memory");
92
93         strcpy(yylval->strval, yytext);
94         return T_IDENT;
95 }
96
97 [[:space:]]+
98 . {
99         char buf[] = "syntax error, unexpected #";
100         *strchr(buf, '#') = *yytext;
101         lex_error(buf);
102 }