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