]> git.draconx.ca Git - cdecl99.git/blob - src/scan.l
Add nodefault to flex options.
[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 nodefault noyywrap bison-locations reentrant never-interactive
24 %option extra-type="_Bool"
25 %option prefix="cdecl__yy"
26
27 %{
28 #define lex_error(msg) do { \
29         cdecl__yyerror(yylloc, NULL, NULL, (msg)); \
30         return T_LEX_ERROR; \
31 } while(0)
32
33 #define dup_token() do { \
34         yylval->strval = malloc(yyleng+1); \
35         if (!yylval->strval) \
36                 lex_error("failed to allocate memory"); \
37         strcpy(yylval->strval, yytext); \
38 } while(0)
39 %}
40
41 %s ENGLISH
42
43 IDENT [_[:alpha:]][_[:alnum:]]*
44 INTEGER 0x[[:xdigit:]]+|0[0-7]+|[[:digit:]]+
45
46 %%
47
48 %{
49         if (yyextra) {
50                 yyextra = 0;
51                 BEGIN(ENGLISH);
52                 return T_ENGLISH;
53         }
54 %}
55
56 "..." return T_ELLIPSIS;
57 ";"   return T_SEMICOLON;
58 "*"   return T_ASTERISK;
59 "("   return T_LPAREN;
60 ")"   return T_RPAREN;
61 "["   return T_LBRACKET;
62 "]"   return T_RBRACKET;
63 ","   return T_COMMA;
64
65 "typedef"    return T_TYPEDEF;
66 "extern"     return T_EXTERN;
67 "static"     return T_STATIC;
68 "auto"       return T_AUTO;
69 "register"   return T_REGISTER;
70
71 "restrict"   return T_RESTRICT;
72 "volatile"   return T_VOLATILE;
73 "const"      return T_CONST;
74
75 "inline"     return T_INLINE;
76
77 "void"       return T_VOID;
78 "char"       return T_CHAR;
79 "short"      return T_SHORT;
80 "int"        return T_INT;
81 "long"       return T_LONG;
82 "float"      return T_FLOAT;
83 "double"     return T_DOUBLE;
84 "signed"     return T_SIGNED;
85 "unsigned"   return T_UNSIGNED;
86 "_Bool"      return T_BOOL;
87 "_Complex"   return T_COMPLEX;
88 "_Imaginary" return T_IMAGINARY;
89
90 "struct"     return T_STRUCT;
91 "union"      return T_UNION;
92 "enum"       return T_ENUM;
93
94 {INTEGER} {
95         char *end;
96
97         errno = 0;
98         yylval->uintval = strtoumax(yytext, &end, 0);
99         if (errno == ERANGE)
100                 lex_error("integer constant out of range");
101         if (*end)
102                 lex_error("invalid integer constant");
103
104         return T_UINT;
105 }
106
107 <ENGLISH>{
108         "variable-length" return T_VLA;
109         "type"            return T_TYPE;
110         "declare"         return T_DECLARE;
111         "pointer"         return T_POINTER;
112         "function"        return T_FUNCTION;
113         "returning"       return T_RETURNING;
114         "array"           return T_ARRAY;
115         "to"              return T_TO;
116         "of"              return T_OF;
117         "as"              return T_AS;
118 }
119
120 {IDENT} { dup_token(); return T_IDENT; }
121
122 [[:space:]]+
123 . {
124         char buf[] = "syntax error, unexpected #";
125         *strchr(buf, '#') = *yytext;
126         lex_error(buf);
127 }