Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Side by Side Diff: client/dom/scripts/idlparser.dart

Issue 8414016: Revert "PEG Parser: under review at http://codereview.chromium.org/8399029" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | client/dom/scripts/idlparser_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 // IDL grammar variants.
6 final int WEBIDL_SYNTAX = 0;
7 final int WEBKIT_SYNTAX = 1;
8 final int FREMONTCUT_SYNTAX = 2;
9
10 /**
11 * IDLFile is the top-level node in each IDL file. It may contain modules or
12 * interfaces.
13 */
14 class IDLFile extends IDLNode {
15
16 String filename;
17 List<IDLModule> modules;
18 List<IDLInterface> interfaces;
19
20 IDLFile(this.filename, this.modules, this.interfaces);
21 }
22
23 /**
24 * IDLModule has an id, and may contain interfaces, type defs andimplements
25 * statements.
26 */
27 class IDLModule extends IDLNode {
28 String id;
29 List interfaces;
30 List typedefs;
31 List implementsStatements;
32
33 IDLModule(String this.id, IDLExtAttrs extAttrs, IDLAnnotations annotations,
34 List<IDLNode> elements) {
35 setExtAttrs(extAttrs);
36 this.annotations = annotations;
37 this.interfaces = elements.filter((e) => e is IDLInterface);
38 this.typedefs = elements.filter((e) => e is IDLTypeDef);
39 this.implementsStatements =
40 elements.filter((e) => e is IDLImplementsStatement);
41 }
42
43 toString() => '<IDLModule $id $extAttrs $annotations>';
44 }
45
46 class IDLNode {
47 IDLExtAttrs extAttrs;
48 IDLAnnotations annotations;
49 IDLNode();
50
51 setExtAttrs(IDLExtAttrs ea) {
52 assert(ea != null);
53 this.extAttrs = ea != null ? ea : new IDLExtAttrs();
54 }
55 }
56
57 class IDLType extends IDLNode {
58 String id;
59 IDLType parameter;
60 bool nullable = false;
61 IDLType(String this.id, [IDLType this.parameter, bool this.nullable = false]);
62
63 // TODO: Figure out why this constructor was failing in mysterious ways.
64 // IDLType.nullable(IDLType base) {
65 // return new IDLType(base.id, base.parameter, true);
66 // }
67
68 //String toString() => '<IDLType $nullable $id $parameter>';
69 String toString() {
70 String nullableTag = nullable ? '?' : '';
71 return '<IDLType $id${parameter == null ? '' : ' $parameter'}$nullableTag>';
72 }
73 }
74
75 class IDLTypeDef extends IDLNode {
76 String id;
77 IDLType type;
78 IDLTypeDef(String this.id, IDLType this.type);
79
80 toString() => '<IDLTypeDef $id $type>';
81 }
82
83 class IDLImplementsStatement extends IDLNode {
84 }
85
86 class IDLInterface extends IDLNode {
87 String id;
88 List parents;
89 List operations;
90 List attributes;
91 List constants;
92 List snippets;
93
94 bool isSupplemental;
95 bool isNoInterfaceObject;
96 bool isFcSuppressed;
97
98 IDLInterface(String this.id, IDLExtAttrs ea, IDLAnnotations ann,
99 List this.parents, List members) {
100 setExtAttrs(ea);
101 this.annotations = ann;
102 if (this.parents == null) this.parents = [];
103
104 operations = members.filter((e) => e is IDLOperation);
105 attributes = members.filter((e) => e is IDLAttribute);
106 constants = members.filter((e) => e is IDLConstant);
107 snippets = members.filter((e) => e is IDLSnippet);
108
109 isSupplemental = extAttrs.has('Supplemental');
110 isNoInterfaceObject = extAttrs.has('NoInterfaceObject');
111 isFcSuppressed = extAttrs.has('Suppressed');
112 }
113
114 toString() => '<IDLInterface $id $extAttrs $annotations>';
115 }
116
117 class IDLMember extends IDLNode {
118 String id;
119 IDLType type;
120 bool isFcSuppressed;
121
122 IDLMember(String this.id, IDLType this.type, IDLExtAttrs ea, IDLAnnotations an n) {
123 setExtAttrs(ea);
124 this.annotations = ann;
125
126 isFcSuppressed = extAttrs.has('Suppressed');
127 }
128 }
129
130 class IDLOperation extends IDLMember {
131 List arguments;
132
133 // Ignore all forms of raises for now.
134 List specials;
135 bool isStringifier;
136
137 IDLOperation(String id, IDLType type, IDLExtAttrs ea, IDLAnnotations ann,
138 List this.arguments, List this.specials, bool this.isStringifier)
139 : super(id, type, ea, ann) {
140 }
141
142 toString() => '<IDLOperation $type $id ${printList(arguments)}>';
143 }
144
145 class IDLAttribute extends IDLMember {
146 }
147
148 class IDLConstant extends IDLMember {
149 var value;
150 IDLConstant(String id, IDLType type, IDLExtAttrs ea, IDLAnnotations ann,
151 var this.value)
152 : super(id, type, ea, ann);
153 }
154
155 class IDLSnippet extends IDLMember {
156 String text;
157 IDLSnippet(IDLAnnotations ann, String this.text)
158 : super(null, null, new IDLExtAttrs(), ann);
159 }
160
161 /** Maps string to something. */
162 class IDLDictNode {
163 Map<String, Object> map;
164 IDLDictNode() {
165 map = new Map<String, Object>();
166 }
167
168 setMap(List associationList) {
169 if (associationList == null) return;
170 for (var element in associationList) {
171 var name = element[0];
172 var value = element[1];
173 map[name] = value;
174 }
175 }
176
177 bool has(String key) => map.containsKey(key);
178
179 formatMap() {
180 if (map.isEmpty())
181 return '';
182 StringBuffer sb = new StringBuffer();
183 map.forEach((k, v) {
184 sb.add(' $k');
185 if (v != null) {
186 sb.add('=$v');
187 }
188 });
189 return sb.toString();
190 }
191
192 }
193
194 class IDLExtAttrs extends IDLDictNode {
195 IDLExtAttrs([List attrs = const []]) {
196 setMap(attrs);
197 }
198
199 toString() => '<IDLExtAttrs${formatMap()}>';
200 }
201
202 class IDLArgument extends IDLNode {
203 String id;
204 IDLType type;
205 bool isOptional;
206 bool isIn;
207 bool hasElipsis;
208 IDLArgument(String this.id, IDLType this.type, IDLExtAttrs extAttrs,
209 bool this.isOptional, bool this.isIn, bool this.hasElipsis) {
210 setExtAttrs(extAttrs);
211 }
212
213 toString() => '<IDLArgument $id>';
214 }
215
216 class IDLAnnotations extends IDLDictNode {
217 IDLAnnotations(List annotations) {
218 for (var annotation in annotations) {
219 map[annotation.id] = annotation;
220 }
221 }
222
223 toString() => '<IDLAnnotations${formatMap()}>';
224 }
225
226 class IDLAnnotation extends IDLDictNode {
227 String id;
228 IDLAnnotation(String this.id, List args) {
229 setMap(args);
230 }
231
232 toString() => '<IDLAnnotation $id${formatMap()}>';
233 }
234
235 class IDLExtAttrFunctionValue extends IDLNode {
236 String name;
237 List arguments;
238 IDLExtAttrFunctionValue(String this.name, this.arguments);
239
240 toString() => '<IDLExtAttrFunctionValue $name(${arguments.length})>';
241 }
242
243 class IDLParentInterface extends IDLNode {}
244
245 ////////////////////////////////////////////////////////////////////////////////
246
247 class IDLParser {
248 final int syntax;
249 Grammar grammar;
250 var axiom;
251
252 IDLParser([syntax=WEBIDL_SYNTAX]) : syntax = syntax {
253 grammar = new Grammar();
254 axiom = _makeParser();
255 }
256
257 _makeParser() {
258 Grammar g = grammar;
259
260 syntax_switch([WebIDL, WebKit, FremontCut]) {
261 assert(WebIDL != null && WebKit != null); // Not options, just want names .
262 if (syntax == WEBIDL_SYNTAX)
263 return WebIDL;
264 if (syntax == WEBKIT_SYNTAX)
265 return WebKit;
266 if (syntax == FREMONTCUT_SYNTAX)
267 return FremontCut == null ? WebIDL : FremontCut;
268 throw new Exception('unsupported IDL syntax $syntax');
269 }
270
271 var idStartCharSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_' ;
272 var idNextCharSet = idStartCharSet + '0123456789';
273 var hexCharSet = '0123456789ABCDEFabcdef';
274
275 var idStartChar = CHAR(idStartCharSet);
276 var idNextChar = CHAR(idNextCharSet);
277
278 var digit = CHAR('0123456789');
279
280 var Id = TEXT(LEX('an identifier',[idStartChar, MANY(idNextChar, min:0)]));
281
282 var IN = SKIP(LEX("'in'", ['in',NOT(idNextChar)]));
283
284 var BooleanLiteral = OR(['true', 'false']);
285 var IntegerLiteral = TEXT(LEX('hex-literal', OR([['0x', MANY(CHAR(hexCharSet ))],
286 [MANY(digit)]])));
287 var FloatLiteral = TEXT(LEX('float-literal', [MANY(digit), '.', MANY(digit, min:0)]));
288
289
290 var Argument = g['Argument'];
291 var Module = g['Module'];
292 var Member = g['Member'];
293 var Interface = g['Interface'];
294 var ExceptionDef = g['ExceptionDef'];
295 var Type = g['Type'];
296 var TypeDef = g['TypeDef'];
297 var ImplStmt = g['ImplStmt'];
298 var ValueTypeDef = g['ValueTypeDef'];
299 var Const = g['Const'];
300 var Attribute = g['Attribute'];
301 var Operation = g['Operation'];
302 var Snippet = g['Snippet'];
303 var ExtAttrs = g['ExtAttrs'];
304 var MaybeExtAttrs = g['MaybeExtAttrs'];
305 var MaybeAnnotations = g['MaybeAnnotations'];
306 var ParentInterfaces = g['ParentInterfaces'];
307
308
309 final ScopedName = TEXT(LEX('scoped-name', MANY(CHAR(idStartCharSet + '_:.<> '))));
310
311 final ScopedNames = MANY(ScopedName, separator:',');
312
313 // Types
314
315 final IntegerTypeName = OR([
316 ['byte', () => 'byte'],
317 ['int', () => 'int'],
318 ['long', 'long', () => 'long long'],
319 ['long', () => 'long'],
320 ['octet', () => 'octet'],
321 ['short', () => 'short']]);
322
323 final IntegerType = OR([
324 ['unsigned', IntegerTypeName, (name) => new IDLType('unsigned $name')],
325 [IntegerTypeName, (name) => new IDLType(name)]]);
326
327 final BooleanType = ['boolean', () => new IDLType('boolean')];
328 final OctetType = ['octet', () => new IDLType('octet')];
329 final FloatType = ['float', () => new IDLType('float')];
330 final DoubleType = ['double', () => new IDLType('double')];
331
332 final SequenceType = ['sequence', '<', Type, '>',
333 (type) => new IDLType('sequence', type)];
334
335 final ScopedNameType = [ScopedName, (name) => new IDLType(name)];
336
337 final NullableType =
338 [OR([IntegerType, BooleanType, OctetType, FloatType,
339 DoubleType, SequenceType, ScopedNameType]),
340 MAYBE('?'),
341 (type, nullable) =>
342 nullable ? new IDLType(type.id, type.parameter, true) : type];
343
344 final VoidType = ['void', () => new IDLType('void')];
345 final AnyType = ['any', () => new IDLType('any')];
346 final ObjectType = ['object', () => new IDLType('object')];
347
348 Type.def = OR([AnyType, ObjectType, NullableType]);
349
350 final ReturnType = OR([VoidType, Type]);
351
352 var Definition = syntax_switch(
353 WebIDL: OR([Module, Interface, ExceptionDef, TypeDef, ImplStmt,
354 ValueTypeDef, Const]),
355 WebKit: OR([Module, Interface]));
356
357 var Definitions = MANY(Definition, min:0);
358
359 Module.def = syntax_switch(
360 WebIDL: [MaybeExtAttrs, 'module', Id, '{', Definitions, '}',
361 SKIP(MAYBE(';')),
362 (ea, id, defs) => new IDLModule(id, ea, null, defs)],
363 WebKit: ['module', MaybeExtAttrs, Id, '{', Definitions, '}',
364 SKIP(MAYBE(';')),
365 (ea, id, defs) => new IDLModule(id, ea, null, defs)],
366 FremontCut: [MaybeAnnotations, MaybeExtAttrs, 'module', Id,
367 '{', Definitions, '}', SKIP(MAYBE(';')),
368 (ann, ea, id, defs) => new IDLModule(id, ea, ann, defs)]);
369
370 Interface.def = syntax_switch(
371 WebIDL: [MaybeExtAttrs, 'interface', Id, MAYBE(ParentInterfaces),
372 MAYBE(['{', MANY0(Member), '}']), ';',
373 (ea, id, p, ms) => new IDLInterface(id, ea, null, p, ms)],
374 WebKit: ['interface', MaybeExtAttrs, Id, MAYBE(ParentInterfaces),
375 MAYBE(['{', MANY0(Member), '}']), ';',
376 (ea, id, p, ms) => new IDLInterface(id, ea, null, p, ms)],
377 FremontCut: [MaybeAnnotations, MaybeExtAttrs, 'interface',
378 Id, MAYBE(ParentInterfaces),
379 MAYBE(['{', MANY0(Member), '}']), ';',
380 (ann, ea, id, p, ms) => new IDLInterface(id, ea, ann, p, ms )]);
381
382 Member.def = syntax_switch(
383 WebIDL: OR([Const, Attribute, Operation, ExtAttrs]),
384 WebKit: OR([Const, Attribute, Operation]),
385 FremontCut: OR([Const, Attribute, Operation, Snippet]));
386
387 var InterfaceType = ScopedName;
388
389 var ParentInterface = syntax_switch(
390 WebIDL: [InterfaceType],
391 WebKit: [InterfaceType],
392 FremontCut: [MaybeAnnotations, InterfaceType]);
393
394 ParentInterfaces.def = [':', MANY(ParentInterface, ',')];
395
396 // TypeDef (Web IDL):
397 TypeDef.def = ['typedef', Type, Id, ';', (type, id) => new IDLTypeDef(id, ty pe)];
398
399 // TypeDef (Old-school W3C IDLs)
400 ValueTypeDef.def = ['valuetype', Id, Type, ';'];
401
402 // Implements Statement (Web IDL):
403 var ImplStmtImplementor = ScopedName;
404 var ImplStmtImplemented = ScopedName;
405
406 ImplStmt.def = [ImplStmtImplementor, 'implements', ImplStmtImplemented, ';'] ;
407
408 var ConstExpr = OR([BooleanLiteral, IntegerLiteral, FloatLiteral]);
409
410 Const.def = syntax_switch(
411 WebIDL: [MaybeExtAttrs, 'const', Type, Id, '=', ConstExpr, ';',
412 (ea, type, id, v) => new IDLConstant(id, type, ea, null, v)],
413 WebKit: ['const', MaybeExtAttrs, Type, Id, '=', ConstExpr, ';',
414 (ea, type, id, v) => new IDLConstant(id, type, ea, null, v)],
415 FremontCut: [MaybeAnnotations, MaybeExtAttrs,
416 'const', Type, Id, '=', ConstExpr, ';',
417 (ann, ea, type, id, v) =>
418 new IDLConstant(id, type, ea, ann, v)]);
419
420 // Attributes
421
422 var Stringifier = 'stringifier';
423 var AttrGetter = 'getter';
424 var AttrSetter = 'setter';
425 var ReadOnly = 'readonly';
426 var AttrGetterSetter = OR([AttrGetter, AttrSetter]);
427
428 var GetRaises = syntax_switch(
429 WebIDL: ['getraises', '(', ScopedNames, ')'],
430 WebKit: ['getter', 'raises', '(', ScopedNames, ')']);
431
432 var SetRaises = syntax_switch(
433 WebIDL: ['setraises', '(', ScopedNames, ')'],
434 WebKit: ['setter', 'raises', '(', ScopedNames, ')']);
435
436 var Raises = ['raises', '(', ScopedNames, ')'];
437
438 var AttrRaises = syntax_switch(
439 WebIDL: MANY(OR([GetRaises, SetRaises])),
440 WebKit: MANY(OR([GetRaises, SetRaises, Raises]), separator:','));
441
442 Attribute.def = syntax_switch(
443 WebIDL: [MaybeExtAttrs, MAYBE(Stringifier), MAYBE(ReadOnly),
444 'attribute', Type, Id, MAYBE(AttrRaises), ';'],
445 WebKit: [MAYBE(Stringifier), MAYBE(ReadOnly), 'attribute',
446 MaybeExtAttrs, Type, Id, MAYBE(AttrRaises), ';'],
447 FremontCut: [MaybeAnnotations, MaybeExtAttrs,
448 MAYBE(AttrGetterSetter), MAYBE(Stringifier), MAYBE(ReadOnly ),
449 'attribute', Type, Id, MAYBE(AttrRaises), ';'
450 ]);
451
452 // Operations
453
454 final Special = TEXT(OR(['getter', 'setter', 'creator', 'deleter', 'caller'] ));
455 final Specials = MANY(Special);
456
457 final Optional = 'optional';
458 final AnEllipsis = '...';
459
460 Argument.def = syntax_switch(
461 WebIDL: SEQ(MaybeExtAttrs, MAYBE(Optional), MAYBE(IN),
462 MAYBE(Optional), Type, MAYBE(AnEllipsis), Id,
463 (e, opt1, isin, opt2, type, el, id) =>
464 new IDLArgument(id, type, e, opt1 || opt2, isin, el)),
465
466 WebKit: SEQ(MAYBE(Optional), MAYBE('in'), MAYBE(Optional),
467 MaybeExtAttrs, Type, Id
468 (opt1, isin, opt2, e, type, id) =>
469 new IDLArgument(id, type, e, opt1 || opt2, isin, false)) );
470
471 final Arguments = MANY0(Argument, ',');
472
473 Operation.def = syntax_switch(
474 WebIDL: [MaybeExtAttrs, MAYBE(Stringifier), MAYBE(Specials),
475 ReturnType, MAYBE(Id), '(', Arguments, ')', MAYBE(Raises), ';',
476 (ea, isStringifier, specials, type, id, args, raises) =>
477 new IDLOperation(id, type, ea, null, args, specials, isStri ngifier)
478 ],
479 WebKit: [MaybeExtAttrs, ReturnType, MAYBE(Id), '(', Arguments, ')',
480 MAYBE(Raises), ';',
481 (ea, type, id, args, raises) =>
482 new IDLOperation(id, type, ea, null, args, [], false)
483 ],
484 FremontCut: [MaybeAnnotations, MaybeExtAttrs, MAYBE(Stringifier),
485 MAYBE(Specials), ReturnType, MAYBE(Id), '(', Arguments, ')' ,
486 MAYBE(Raises), ';',
487 (ann, ea, isStringifier, specials, type, id, args, raises) =>
488 new IDLOperation(id, type, ea, ann, args, specials, isStr ingifier)
489 ]);
490
491 // Exceptions
492
493 final ExceptionField = [Type, Id, ';'];
494 final ExceptionMember = OR([Const, ExceptionField, ExtAttrs]);
495 ExceptionDef.def = ['exception', Id, '{', MANY0(ExceptionMember), '}', ';'];
496
497 // ExtendedAttributes
498
499 var ExtAttrArgList = ['(', MANY0(Argument, ','), ')'];
500
501 var ExtAttrFunctionValue =
502 [Id, '(', MANY0(Argument, ','), ')',
503 (name, args) => new IDLExtAttrFunctionValue(name, args)
504 ];
505
506 var ExtAttrValue = OR([ExtAttrFunctionValue,
507 TEXT(LEX('value', MANY(CHAR(idNextCharSet + '&:-|'))) )]);
508
509 var ExtAttr = [Id, MAYBE(OR([['=', ExtAttrValue], ExtAttrArgList]))];
510
511 ExtAttrs.def = ['[', MANY(ExtAttr, ','), ']',
512 (list) => new IDLExtAttrs(list)];;
513
514 MaybeExtAttrs.def = OR(ExtAttrs,
515 [ () => new IDLExtAttrs() ] );
516
517 // Annotations - used in the FremontCut IDL grammar.
518
519 var AnnotationArgValue = TEXT(LEX('xx', MANY(CHAR(idNextCharSet + '&:-|')))) ;
520
521 var AnnotationArg = [Id, MAYBE(['=', AnnotationArgValue])];
522
523 var AnnotationBody = ['(', MANY0(AnnotationArg, ','), ')'];
524
525 var Annotation = ['@', Id, MAYBE(AnnotationBody),
526 (id, body) => new IDLAnnotation(id, body)];
527
528 MaybeAnnotations.def = [MANY0(Annotation), (list) => new IDLAnnotations(list )];
529
530 // Snippets - used in the FremontCut IDL grammar.
531
532 final SnippetText = TEXT(LEX('snippet body', MANY0([NOT('}'), CHAR()])));
533 Snippet.def = [MaybeAnnotations, 'snippet', '{', SnippetText, '}', ';',
534 (ann, text) => new IDLSnippet(ann, text)];
535
536
537 grammar.whitespace =
538 OR([MANY(CHAR(' \t\r\n')),
539 ['//', MANY0([NOT(CHAR('\r\n')), CHAR()])],
540 ['#', MANY0([NOT(CHAR('\r\n')), CHAR()])],
541 ['/*', MANY0([NOT('*/'), CHAR()]), '*/']]);
542
543 // Top level - at least one definition.
544 return MANY(Definition);
545
546 }
547 }
OLDNEW
« no previous file with comments | « no previous file | client/dom/scripts/idlparser_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698