4 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
6 // Permission is hereby granted, free of charge, to any person obtaining a copy of
7 // this software and associated documentation files (the "Software"), to deal with
8 // the Software without restriction, including without limitation the rights to
9 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 // of the Software, and to permit persons to whom the Software is furnished to do
11 // so, subject to the following condition:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
34 int curline, comment_level=0;
42 interface return TOK_IFACE;
43 const return TOK_CONST;
47 ushort return TOK_USHORT;
49 ulong return TOK_ULONG;
50 short return TOK_SHORT;
53 fshort return TOK_FSHORT;
54 flong return TOK_FLONG;
55 struct return TOK_STRUCT;
57 octet return TOK_OCTET;
68 \.\.\. return TOK_3DOT;
73 bitfield return TOK_BITFIELD;
75 namespace return TOK_NAMESPACE;
76 using return TOK_USING;
77 async return TOK_ASYNC;
79 inout return TOK_INOUT;
80 shared return TOK_SHARED;
82 typedef return TOK_TYPEDEF;
83 alias return TOK_ALIAS;
84 upcast return TOK_INVALID;
85 downcast return TOK_INVALID;
86 ifaceref return TOK_INVALID;
87 virtual return TOK_VIRTUAL;
89 inline return TOK_INLINE;
90 static return TOK_STATIC;
91 immutable return TOK_IMMUTABLE;
93 false return TOK_FALSE;
95 name return TOK_NAME; // CDL tokens
96 method return TOK_METHOD;
97 class return TOK_CLASS;
101 <*>\r // Put up with DOS files
103 [[:alpha:]][[:alnum:]_]* {
104 // Leading underscores and digits are prohibited by the regex.
105 if (yytext[yyleng - 1] == '_') {
106 yyerrorf("Identifier \"%s\" has a trailing underscore.",
108 } else if (strstr(yytext, "__")) {
109 yyerrorf("Identifier \"%s\" has contiguous underscores.",
111 } else if (!strncmp(yytext, "IDL_", 4)) {
112 yyerrorf("Identifier \"%s\" begins with the reserved "
113 "\"IDL_\" prefix.", yytext);
114 } else if (strstr(yytext, "IDLNS")) {
115 yyerrorf("Identifier \"%s\" contains the reserved sequence "
116 "\"IDLNS\".", yytext);
117 } else if (!strcmp(yytext + yyleng - 3, "_ns")) {
118 yyerrorf("Identifier \"%s\" ends with the reserved suffix \"_ns\".", yytext);
121 *yylval_string = new String(yytext, cur_input_file, curline, TOK_IDENT);
129 if (strchr(yytext, '8') || strchr(yytext, '9')) {
130 yyerrorf("Invalid digit in octal constant %s.", yytext);
131 yylval_con->type = TOK_INVALID;
132 yylval_con->con.ucon = 0;
136 yylval_con->con.ucon = strtoull(yytext, NULL, 8);
138 if (errno == ERANGE) {
139 yyerrorf("Constant %s is out of range.", yytext);
140 yylval_con->type = TOK_INVALID;
141 yylval_con->con.ucon = 0;
142 } else if (yylval_con->con.ucon > LLONG_MAX) {
143 yylval_con->type = TOK_UCON;
145 yylval_con->type = TOK_ICON;
148 return yylval_con->type;
153 yylval_con->con.ucon = strtoull(yytext, NULL, 16);
155 if (errno == ERANGE) {
156 yyerrorf("Constant %s is out of range.", yytext);
157 yylval_con->type = TOK_INVALID;
158 yylval_con->con.ucon = 0;
159 } else if (yylval_con->con.ucon > LLONG_MAX) {
160 yylval_con->type = TOK_UCON;
162 yylval_con->type = TOK_ICON;
165 return yylval_con->type;
170 yylval_con->con.ucon = strtoull(yytext, NULL, 10);
172 if (errno == ERANGE) {
173 yyerrorf("Constant %s is out of range.", yytext);
174 yylval_con->type = TOK_INVALID;
175 yylval_con->con.ucon = 0;
176 } else if (yylval_con->con.ucon > LLONG_MAX) {
177 yylval_con->type = TOK_UCON;
179 yylval_con->type = TOK_ICON;
182 return yylval_con->type;
185 [0-9]*\.[0-9]+([eE][+-][0-9]+)? {
187 yylval_con->con.fcon = strtod(yytext, NULL);
189 if (errno == ERANGE) {
190 yyerrorf("Constant %s is out of range.", yytext);
191 yylval_con->con.fcon = 0;
192 yylval_con->type = TOK_INVALID;
194 yylval_con->type = TOK_FCON;
197 return yylval_con->type;
204 *yylval_string = new String("", cur_input_file, curline, TOK_STR);
213 \\\" **yylval_string += '"';
214 \\\n **yylval_string += '\n';
215 \\\\ **yylval_string += '\\';
217 yyerrorf("Unknown escape sequence '\\%c'.", yytext[1]);
218 **yylval_string += yytext[1];
221 yyerrorf("Unterminated string literal at line %d.", curline);
225 [^\\\"\n]+ **yylval_string += yytext;
234 "/*" comment_level++;
235 "*/" if (!(--comment_level)) BEGIN(INITIAL);
242 "//" BEGIN(LINECOMMENT);
249 [[:blank:]]+ /* Whitespace */
256 yyerrorf("End of file within comment.");
258 // Keep message from occurring twice if parse is OK
266 if (isprint(yytext[0]))
267 yyerrorf("Unexpected character '%c'.", yytext[0]);
269 yyerrorf("Unexpected character #%d.", yytext[0]);
277 yyerrorf("End of file within comment.");