4 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors or contributors be held liable for any damages
8 // arising from the use of this software.
10 // Permission is hereby granted to everyone, free of charge, to use, copy,
11 // modify, prepare derivative works of, publish, distribute, perform,
12 // sublicense, and/or sell copies of the Software, provided that the above
13 // copyright notice and disclaimer of warranty be included in all copies or
14 // substantial portions of this software.
26 int curline, comment_level=0;
34 interface return TOK_IFACE;
35 const return TOK_CONST;
39 ushort return TOK_USHORT;
41 ulong return TOK_ULONG;
42 short return TOK_SHORT;
45 fshort return TOK_FSHORT;
46 flong return TOK_FLONG;
47 struct return TOK_STRUCT;
49 octet return TOK_OCTET;
60 \.\.\. return TOK_3DOT;
65 bitfield return TOK_BITFIELD;
67 namespace return TOK_NAMESPACE;
68 using return TOK_USING;
69 async return TOK_ASYNC;
71 inout return TOK_INOUT;
72 shared return TOK_SHARED;
74 typedef return TOK_TYPEDEF;
75 alias return TOK_ALIAS;
76 upcast return TOK_INVALID;
77 downcast return TOK_INVALID;
78 ifaceref return TOK_INVALID;
79 virtual return TOK_VIRTUAL;
81 inline return TOK_INLINE;
82 static return TOK_STATIC;
83 immutable return TOK_IMMUTABLE;
85 false return TOK_FALSE;
87 name return TOK_NAME; // CDL tokens
88 method return TOK_METHOD;
89 class return TOK_CLASS;
93 <*>\r // Put up with DOS files
95 [[:alpha:]][[:alnum:]_]* {
96 // Leading underscores and digits are prohibited by the regex.
97 if (yytext[yyleng - 1] == '_') {
98 yyerrorf("Identifier \"%s\" has a trailing underscore.",
100 } else if (strstr(yytext, "__")) {
101 yyerrorf("Identifier \"%s\" has contiguous underscores.",
103 } else if (!strncmp(yytext, "IDL_", 4)) {
104 yyerrorf("Identifier \"%s\" begins with the reserved "
105 "\"IDL_\" prefix.", yytext);
106 } else if (strstr(yytext, "IDLNS")) {
107 yyerrorf("Identifier \"%s\" contains the reserved sequence "
108 "\"IDLNS\".", yytext);
109 } else if (!strcmp(yytext + yyleng - 3, "_ns")) {
110 yyerrorf("Identifier \"%s\" ends with the reserved suffix \"_ns\".", yytext);
113 *yylval_string = new String(yytext, cur_input_file, curline, TOK_IDENT);
121 if (strchr(yytext, '8') || strchr(yytext, '9')) {
122 yyerrorf("Invalid digit in octal constant %s.", yytext);
123 yylval_con->type = TOK_INVALID;
124 yylval_con->con.ucon = 0;
128 yylval_con->con.ucon = strtoull(yytext, NULL, 8);
130 if (errno == ERANGE) {
131 yyerrorf("Constant %s is out of range.", yytext);
132 yylval_con->type = TOK_INVALID;
133 yylval_con->con.ucon = 0;
134 } else if (yylval_con->con.ucon > LLONG_MAX) {
135 yylval_con->type = TOK_UCON;
137 yylval_con->type = TOK_ICON;
140 return yylval_con->type;
145 yylval_con->con.ucon = strtoull(yytext, NULL, 16);
147 if (errno == ERANGE) {
148 yyerrorf("Constant %s is out of range.", yytext);
149 yylval_con->type = TOK_INVALID;
150 yylval_con->con.ucon = 0;
151 } else if (yylval_con->con.ucon > LLONG_MAX) {
152 yylval_con->type = TOK_UCON;
154 yylval_con->type = TOK_ICON;
157 return yylval_con->type;
162 yylval_con->con.ucon = strtoull(yytext, NULL, 10);
164 if (errno == ERANGE) {
165 yyerrorf("Constant %s is out of range.", yytext);
166 yylval_con->type = TOK_INVALID;
167 yylval_con->con.ucon = 0;
168 } else if (yylval_con->con.ucon > LLONG_MAX) {
169 yylval_con->type = TOK_UCON;
171 yylval_con->type = TOK_ICON;
174 return yylval_con->type;
177 [0-9]*\.[0-9]+([eE][+-][0-9]+)? {
179 yylval_con->con.fcon = strtod(yytext, NULL);
181 if (errno == ERANGE) {
182 yyerrorf("Constant %s is out of range.", yytext);
183 yylval_con->con.fcon = 0;
184 yylval_con->type = TOK_INVALID;
186 yylval_con->type = TOK_FCON;
189 return yylval_con->type;
196 *yylval_string = new String("", cur_input_file, curline, TOK_STR);
205 \\\" **yylval_string += '"';
206 \\\n **yylval_string += '\n';
207 \\\\ **yylval_string += '\\';
209 yyerrorf("Unknown escape sequence '\\%c'.", yytext[1]);
210 **yylval_string += yytext[1];
213 yyerrorf("Unterminated string literal at line %d.", curline);
217 [^\\\"\n]+ **yylval_string += yytext;
226 "/*" comment_level++;
227 "*/" if (!(--comment_level)) BEGIN(INITIAL);
234 "//" BEGIN(LINECOMMENT);
241 [[:blank:]]+ /* Whitespace */
248 yyerrorf("End of file within comment.");
250 // Keep message from occurring twice if parse is OK
258 if (isprint(yytext[0]))
259 yyerrorf("Unexpected character '%c'.", yytext[0]);
261 yyerrorf("Unexpected character #%d.", yytext[0]);
269 yyerrorf("End of file within comment.");