nsgenbind
Loading...
Searching...
No Matches
webidl-lexer.l
Go to the documentation of this file.
1%{
2
3/* This is a unicode tolerant lexer for web IDL
4 *
5 * This file is part of nsgenbind.
6 * Licensed under the MIT License,
7 * http://www.opensource.org/licenses/mit-license.php
8 * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
9 *
10 * Derived from:
11 *
12 * W3C WEB IDL - http://www.w3.org/TR/WebIDL/ (especially the grammar
13 * in apendix A)
14 *
15 * The ECMA script spec -
16 * http://ecma-international.org/ecma-262/5.1/#sec-7.2 (expecially
17 * section 7.2 for unicode value handling)
18 */
19
20#include <stdbool.h>
21#include <stdio.h>
22#include <string.h>
23
24#include "webidl-parser.h"
25
26#define YY_USER_ACTION yylloc->first_line = yylloc->last_line; \
27 yylloc->first_column = yylloc->last_column + 1; \
28 yylloc->last_column += yyleng;
29
30/* Ensure compatability with bison 2.6 and later */
31#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED && defined WEBIDL_STYPE_IS_DECLARED
32#define YYSTYPE WEBIDL_STYPE
33#endif
34
35#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED && defined WEBIDL_LTYPE_IS_DECLARED
36#define YYLTYPE WEBIDL_LTYPE
37#endif
38
39%}
40
41
42/* lexer options */
43%option never-interactive
44%option yylineno
45%option bison-bridge
46%option bison-locations
47%option warn
48%option prefix="webidl_"
49%option nounput
50%option noyywrap
51
52/* regular definitions */
53
54 /* ecmascript section 7.2 defines whitespace http://ecma-international.org/ecma-262/5.1/#sec-7.2
55 * Web IDL appendix A has the IDL grammar http://www.w3.org/TR/WebIDL/#idl-grammar
56 */
57
58 /* we do not define space, line feed, carriage return, tab, vertical
59 * tab or form feed here as they are the standard C escapes
60 */
61
62 /* other Unicode “space separator” */
63USP (\xe1\x9a\x80)|(\xe1\xa0\x8e)|(\xe2\x80[\x80-\x8a])|(\xe2\x80\xaf)|(\xe2\x81\x9f)|(\xe3\x80\x80)
64
65/* Line separator \u2028 */
66LS (\xe2\x80\xa8)
67
68/* paragraph separator \u2029 */
69PS (\xe2\x80\xa9)
70
71/* non breaking space \u00A0 */
72NBSP (\xc2\xa0)
73
74/* web idl grammar for whitespace matches single and multiline
75 * comments too. Here there are separate definitions for both single
76 * and multiline comments.
77 */
78whitespace ([ \t\v\f]|{NBSP}|{USP})
79multicomment \/\*(([^*])|(\*[^/]))*\*\/
80singlecomment \/\/
81lineend ([\n\r]|{LS}|{PS})
82
83/* integer numbers in hexidecimal, decimal and octal, slight extension
84 * to spec which only allows for decimal values
85 */
86hexdigit [0-9A-Fa-f]
87hexint 0(x|X){hexdigit}+
88
89decimalint 0|([\+\-]?[1-9][0-9]*)
90
91octalint (0[0-8]+)
92
93/* decimal floating point number */
94decimalexponent (e|E)[\+\-]?[0-9]+
95decimalfloat ({decimalint}\.[0-9]*{decimalexponent}?)|(\.[0-9]+{decimalexponent}?)|({decimalint}{decimalexponent}?)
96
97/* quoted string. spec simply has "[^"]*" but here escapes are allowed for */
98hexescseq x{hexdigit}{2}
99unicodeescseq u{hexdigit}{4}
100characterescseq ['\"\\bfnrtv]|[^'\"\\bfnrtv\n\r]
101escseq {characterescseq}|0|{hexescseq}|{unicodeescseq}
102quotedstring ([^\"\\\n\r]|\\{escseq})
103
104/* web idl identifier direct from spec */
105identifier [A-Z_a-z][0-9A-Z_a-z]*
106
107/* web idl other direct from spec */
108other [^\t\n\r 0-9A-Z_a-z]
109
110/* used for #include directive - not part of web idl spec */
111poundsign ^{whitespace}*#
112
113%x incl
115
116{whitespace} ++yylloc->last_column; /* skip whitespace */
YYLTYPE * yylloc
117
118{lineend} if (yytext[0] != '\r') {
119 /* update position counts */
120 ++yylloc->last_line;
121 yylloc->last_column = 0;
122 }
#define yytext
123
124 /* Simple text terminals */
125
126boolean return TOK_BOOLEAN;
@ TOK_BOOLEAN
127
128byte return TOK_BYTE;
@ TOK_BYTE
129
130octet return TOK_OCTET;
@ TOK_OCTET
131
132attribute return TOK_ATTRIBUTE;
@ TOK_ATTRIBUTE
133
134callback return TOK_CALLBACK;
@ TOK_CALLBACK
135
136const return TOK_CONST;
@ TOK_CONST
137
138creator return TOK_CREATOR;
@ TOK_CREATOR
139
140deleter return TOK_DELETER;
@ TOK_DELETER
141
142dictionary return TOK_DICTIONARY;
@ TOK_DICTIONARY
143
144enum return TOK_ENUM;
@ TOK_ENUM
145
146exception return TOK_EXCEPTION;
@ TOK_EXCEPTION
147
148getter return TOK_GETTER;
@ TOK_GETTER
149
150implements return TOK_IMPLEMENTS;
@ TOK_IMPLEMENTS
151
152inherit return TOK_INHERIT;
@ TOK_INHERIT
153
154interface return TOK_INTERFACE;
@ TOK_INTERFACE
155
156legacycaller return TOK_LEGACYCALLER;
@ TOK_LEGACYCALLER
157
158partial return TOK_PARTIAL;
@ TOK_PARTIAL
159
160setter return TOK_SETTER;
@ TOK_SETTER
161
162static return TOK_STATIC;
@ TOK_STATIC
163
164stringifier return TOK_STRINGIFIER;
@ TOK_STRINGIFIER
165
166typedef return TOK_TYPEDEF;
@ TOK_TYPEDEF
167
168unrestricted return TOK_UNRESTRICTED;
@ TOK_UNRESTRICTED
169
170"..." return TOK_ELLIPSIS;
@ TOK_ELLIPSIS
171
172Date return TOK_DATE;
@ TOK_DATE
173
174DOMString return TOK_STRING; /* dom strings are just strings */
@ TOK_STRING
175
176Infinity return TOK_INFINITY;
@ TOK_INFINITY
177
178NaN return TOK_NAN;
@ TOK_NAN
179
180any return TOK_ANY;
@ TOK_ANY
181
182double return TOK_DOUBLE;
@ TOK_DOUBLE
183
184false return TOK_FALSE;
@ TOK_FALSE
185
186float return TOK_FLOAT;
@ TOK_FLOAT
187
188long return TOK_LONG;
@ TOK_LONG
189
190null return TOK_NULL_LITERAL;
@ TOK_NULL_LITERAL
191
192object yylval->text = strdup(yytext); return TOK_IDENTIFIER;
YYSTYPE * yylval
@ TOK_IDENTIFIER
193
194or return TOK_OR;
@ TOK_OR
195
196optional return TOK_OPTIONAL;
@ TOK_OPTIONAL
197
198sequence return TOK_SEQUENCE;
@ TOK_SEQUENCE
199
200short return TOK_SHORT;
@ TOK_SHORT
201
202true return TOK_TRUE;
@ TOK_TRUE
203
204unsigned return TOK_UNSIGNED;
@ TOK_UNSIGNED
205
206void return TOK_VOID;
@ TOK_VOID
207
208readonly return TOK_READONLY;
@ TOK_READONLY
209
210Promise return TOK_PROMISE;
@ TOK_PROMISE
211
212iterable return TOK_ITERABLE;
@ TOK_ITERABLE
213
214legacyiterable return TOK_LEGACYITERABLE;
@ TOK_LEGACYITERABLE
215
216required return TOK_REQUIRED;
@ TOK_REQUIRED
217
218Constructor return TOK_CONSTRUCTOR;
@ TOK_CONSTRUCTOR
219
220NamedConstructor return TOK_NAMEDCONSTRUCTOR;
@ TOK_NAMEDCONSTRUCTOR
221
222{identifier} {
223 /* A leading "_" is used to escape an identifier from
224 * looking like a reserved word terminal. */
225 yylval->text = (yytext[0] == '_') ? strdup(yytext + 1) : strdup(yytext);
226 return TOK_IDENTIFIER;
227 }
228
229{decimalint} yylval->value = strtol(yytext, NULL, 10); return TOK_INT_LITERAL;
@ TOK_INT_LITERAL
230
231{octalint} yylval->value = strtol(yytext, NULL, 8); return TOK_INT_LITERAL;
232
233{hexint} yylval->value = strtol(yytext, NULL, 16); return TOK_INT_LITERAL;
234
235{decimalfloat} yylval->text = strdup(yytext); return TOK_FLOAT_LITERAL;
@ TOK_FLOAT_LITERAL
236
237\"{quotedstring}*\" yylval->text = strdup(yytext + 1); *(yylval->text + yyleng - 2) = 0; return TOK_STRING_LITERAL;
#define yyleng
@ TOK_STRING_LITERAL
238
239{multicomment} {
240 /* multicomment */
241 char* s = yytext;
242 for (; *s; ++s)
243 {
244 if (*s == '\n')
245 {
246 ++yylloc->last_line;
247 yylloc->last_column = 0;
248 }
249 else
250 {
252 }
253 }
254 if (strncmp(yytext, "/**", 3) == 0)
255 {
256 /* Javadoc style comment */
257 yylval->text = strdup(yytext);
258 return TOK_JAVADOC;
259 }
260 }
@ TOK_JAVADOC
261
262{singlecomment} {
263 /* singlecomment */
264 int c;
265
266 do {
267 c = input();
268 } while (c != '\n' && c != '\r' && c != EOF);
269 ++yylloc->last_line;
270 yylloc->last_column = 0;
271 }
272
273
274
275{poundsign}include BEGIN(incl);
#define BEGIN
#define incl
276
277{other} return (int) yytext[0];
278
279<incl>[ \t]*\" /* eat the whitespace and open quotes */
280
281<incl>[^\t\n\"]+ {
282 /* got the include file name */
283 yyin = fopen( yytext, "r" );
284
285 if ( ! yyin ) {
286 fprintf(stderr, "Unable to open include %s\n", yytext);
287 exit(3);
288 }
290
291 BEGIN(INITIAL);
292 }
#define yypush_buffer_state
#define yyin
#define INITIAL
#define yy_create_buffer
#define YY_BUF_SIZE
293
294<incl>\n BEGIN(INITIAL);
295
296<<EOF>> {
298
299 if ( !YY_CURRENT_BUFFER ) {
300 yyterminate();
301 } else {
302 BEGIN(incl);
303 }
304
305 }
#define YY_CURRENT_BUFFER
#define yypop_buffer_state
#define yyterminate()
306
307
308%%