| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 3 # for details. All rights reserved. Use of this source code is governed by a | |
| 4 # BSD-style license that can be found in the LICENSE file. | |
| 5 | |
| 6 import re | |
| 7 import subprocess | |
| 8 import tempfile | |
| 9 | |
| 10 from pegparser import * | |
| 11 | |
| 12 # IDL grammar variants. | |
| 13 WEBIDL_SYNTAX = 0 | |
| 14 WEBKIT_SYNTAX = 1 | |
| 15 FREMONTCUT_SYNTAX = 2 | |
| 16 | |
| 17 | |
| 18 class IDLParser(object): | |
| 19 """IDLParser is a PEG based IDL files parser.""" | |
| 20 | |
| 21 def __init__(self, syntax=WEBIDL_SYNTAX): | |
| 22 """Constructor. | |
| 23 | |
| 24 Initializes the IDLParser by defining the grammar and initializing | |
| 25 a PEGParserinstance. | |
| 26 | |
| 27 Args: | |
| 28 syntax -- supports either WEBIDL_SYNTAX (0) or WEBKIT_SYNTAX (1) | |
| 29 """ | |
| 30 self._syntax = syntax | |
| 31 self._pegparser = PegParser(self._idl_grammar(), | |
| 32 self._whitespace_grammar(), | |
| 33 strings_are_tokens=True) | |
| 34 | |
| 35 def _idl_grammar(self): | |
| 36 """Returns the PEG grammar for IDL parsing.""" | |
| 37 | |
| 38 # utilities: | |
| 39 def syntax_switch(w3c_syntax, webkit_syntax, fremontcut_syntax=None): | |
| 40 """Returns w3c_syntax or web_syntax, depending on the current | |
| 41 configuration. | |
| 42 """ | |
| 43 if self._syntax == WEBIDL_SYNTAX: | |
| 44 return w3c_syntax | |
| 45 elif self._syntax == WEBKIT_SYNTAX: | |
| 46 return webkit_syntax | |
| 47 elif self._syntax == FREMONTCUT_SYNTAX: | |
| 48 if fremontcut_syntax is not None: | |
| 49 return fremontcut_syntax | |
| 50 return w3c_syntax | |
| 51 else: | |
| 52 raise RuntimeError('unsupported IDL syntax %s' % syntax) | |
| 53 | |
| 54 # The following grammar is based on the Web IDL's LL(1) grammar | |
| 55 # (specified in: http://dev.w3.org/2006/webapi/WebIDL/#idl-grammar). | |
| 56 # It is adjusted to PEG grammar, as well as to also support | |
| 57 # WebKit IDL and FremontCut grammar. | |
| 58 | |
| 59 ###################### BEGIN GRAMMAR ##################### | |
| 60 | |
| 61 def Id(): | |
| 62 return re.compile(r'[\w\_]+') | |
| 63 | |
| 64 def _Definitions(): | |
| 65 return MAYBE(MANY(_Definition)) | |
| 66 | |
| 67 def _Definition(): | |
| 68 return syntax_switch( | |
| 69 # Web IDL: | |
| 70 OR(Module, Interface, ExceptionDef, TypeDef, ImplStmt, | |
| 71 ValueTypeDef, Const, Enum), | |
| 72 # WebKit: | |
| 73 OR(Module, Interface, Enum, TypeDef, ImplStmt, CallbackDeclaration)) | |
| 74 | |
| 75 def Enum(): | |
| 76 def StringLiteral(): | |
| 77 return re.compile(r'"\w*"') | |
| 78 | |
| 79 return ['enum', Id, '{', MAYBE(MANY(StringLiteral, ',')), '}', ';'] | |
| 80 | |
| 81 def Module(): | |
| 82 return syntax_switch( | |
| 83 # Web IDL: | |
| 84 [MAYBE(ExtAttrs), 'module', Id, '{', _Definitions, '}', | |
| 85 MAYBE(';')], | |
| 86 # WebKit: | |
| 87 ['module', MAYBE(ExtAttrs), Id, '{', _Definitions, '}', | |
| 88 MAYBE(';')], | |
| 89 # FremontCut: | |
| 90 [MAYBE(_Annotations), MAYBE(ExtAttrs), 'module', Id, | |
| 91 '{', _Definitions, '}', MAYBE(';')]) | |
| 92 | |
| 93 def CallbackDeclaration(): | |
| 94 return [Callback, Type, '=', Type,'(', _Arguments, ')', ';'] | |
| 95 | |
| 96 def Callback(): | |
| 97 return ['callback'] | |
| 98 | |
| 99 def Partial(): | |
| 100 return ['partial'] | |
| 101 | |
| 102 def Interface(): | |
| 103 return syntax_switch( | |
| 104 # Web IDL: | |
| 105 [MAYBE(ExtAttrs), MAYBE(Partial), MAYBE(Callback), 'interface', Id, MAYB
E(_ParentInterfaces), | |
| 106 MAYBE(['{', MAYBE(MANY(_Member)), '}']), ';'], | |
| 107 # WebKit: | |
| 108 [MAYBE(ExtAttrs), MAYBE(Partial), MAYBE(Callback), OR('interface', 'exce
ption'), MAYBE(ExtAttrs), Id, MAYBE(_ParentInterfaces), | |
| 109 MAYBE(['{', MAYBE(MANY(_Member)), '}']), MAYBE(';')], | |
| 110 # FremontCut: | |
| 111 [MAYBE(_Annotations), MAYBE(ExtAttrs), 'interface', | |
| 112 Id, MAYBE(_ParentInterfaces), MAYBE(['{', MAYBE(MANY(_Member)), | |
| 113 '}']), ';']) | |
| 114 | |
| 115 def _Member(): | |
| 116 return syntax_switch( | |
| 117 # Web IDL: | |
| 118 OR(Const, Attribute, Operation, ExtAttrs), | |
| 119 # WebKit: | |
| 120 OR(Const, Attribute, Operation), | |
| 121 # FremontCut: | |
| 122 OR(Const, Attribute, Operation)) | |
| 123 | |
| 124 # Interface inheritance: | |
| 125 def _ParentInterfaces(): | |
| 126 return [':', MANY(ParentInterface, separator=',')] | |
| 127 | |
| 128 def ParentInterface(): | |
| 129 return syntax_switch( | |
| 130 # Web IDL: | |
| 131 [InterfaceType], | |
| 132 # WebKit: | |
| 133 [InterfaceType], | |
| 134 # FremontCut: | |
| 135 [MAYBE(_Annotations), InterfaceType]) | |
| 136 | |
| 137 # TypeDef (Web IDL): | |
| 138 def TypeDef(): | |
| 139 return ['typedef', Type, Id, ';'] | |
| 140 | |
| 141 # TypeDef (Old-school W3C IDLs) | |
| 142 def ValueTypeDef(): | |
| 143 return ['valuetype', Id, Type, ';'] | |
| 144 | |
| 145 # Implements Statement (Web IDL): | |
| 146 def ImplStmt(): | |
| 147 return [ImplStmtImplementor, 'implements', ImplStmtImplemented, | |
| 148 ';'] | |
| 149 | |
| 150 def ImplStmtImplementor(): | |
| 151 return ScopedName | |
| 152 | |
| 153 def ImplStmtImplemented(): | |
| 154 return ScopedName | |
| 155 | |
| 156 # Constants: | |
| 157 def Const(): | |
| 158 return syntax_switch( | |
| 159 # Web IDL: | |
| 160 [MAYBE(ExtAttrs), 'const', Type, Id, '=', ConstExpr, ';'], | |
| 161 # WebKit: | |
| 162 [MAYBE(ExtAttrs), 'const', Type, Id, '=', ConstExpr, ';'], | |
| 163 # FremontCut: | |
| 164 [MAYBE(_Annotations), MAYBE(ExtAttrs), 'const', Type, Id, '=', | |
| 165 ConstExpr, ';']) | |
| 166 | |
| 167 def ConstExpr(): | |
| 168 return OR(_BooleanLiteral, | |
| 169 _IntegerLiteral, | |
| 170 _FloatLiteral) | |
| 171 | |
| 172 def _BooleanLiteral(): | |
| 173 return re.compile(r'true|false') | |
| 174 | |
| 175 def _IntegerLiteral(): | |
| 176 return OR(re.compile(r'(0x)?[0-9ABCDEF]+'), | |
| 177 re.compile(r'[0-9]+')) | |
| 178 | |
| 179 def _FloatLiteral(): | |
| 180 return re.compile(r'[0-9]+\.[0-9]*') | |
| 181 | |
| 182 # Attributes: | |
| 183 def Attribute(): | |
| 184 return syntax_switch( | |
| 185 # Web IDL: | |
| 186 [MAYBE(ExtAttrs), MAYBE(Stringifier), MAYBE(ReadOnly), | |
| 187 'attribute', Type, Id, ';'], | |
| 188 # WebKit: | |
| 189 [ | |
| 190 MAYBE(Stringifier), | |
| 191 MAYBE(ExtAttrs), MAYBE(Static), MAYBE(ReadOnly), 'attribute', MAYBE(Ex
tAttrs), | |
| 192 Type, Id, ';'], | |
| 193 # FremontCut: | |
| 194 [MAYBE(_Annotations), MAYBE(ExtAttrs), | |
| 195 MAYBE(_AttrGetterSetter), MAYBE(Stringifier), MAYBE(ReadOnly), | |
| 196 'attribute', Type, Id, ';']) | |
| 197 | |
| 198 # Special fremontcut feature: | |
| 199 def _AttrGetterSetter(): | |
| 200 return OR(AttrGetter, AttrSetter) | |
| 201 | |
| 202 def AttrGetter(): | |
| 203 return 'getter' | |
| 204 | |
| 205 def AttrSetter(): | |
| 206 return 'setter' | |
| 207 | |
| 208 def ReadOnly(): | |
| 209 return 'readonly' | |
| 210 | |
| 211 # Operation: | |
| 212 def Operation(): | |
| 213 return syntax_switch( | |
| 214 # Web IDL: | |
| 215 [MAYBE(ExtAttrs), MAYBE(Static), MAYBE(Stringifier), MAYBE(_Specials), | |
| 216 ReturnType, MAYBE(Id), '(', _Arguments, ')', ';'], | |
| 217 # WebKit: | |
| 218 [MAYBE(ExtAttrs), MAYBE(Static), MAYBE(_Specials), | |
| 219 ReturnType, MAYBE(Id), '(', _Arguments, ')', ';'], | |
| 220 # FremontCut: | |
| 221 [MAYBE(_Annotations), MAYBE(ExtAttrs), MAYBE(Static), MAYBE(Stringifier)
, | |
| 222 MAYBE(_Specials), ReturnType, MAYBE(Id), '(', _Arguments, ')', ';']) | |
| 223 | |
| 224 def Static(): | |
| 225 return 'static' | |
| 226 | |
| 227 def _Specials(): | |
| 228 return MANY(Special) | |
| 229 | |
| 230 def Special(): | |
| 231 return re.compile(r'getter|setter|creator|deleter|caller') | |
| 232 | |
| 233 def Stringifier(): | |
| 234 return 'stringifier' | |
| 235 | |
| 236 # Operation arguments: | |
| 237 def _Arguments(): | |
| 238 return MAYBE(MANY(Argument, ',')) | |
| 239 | |
| 240 def Argument(): | |
| 241 return syntax_switch( | |
| 242 # Web IDL: | |
| 243 [MAYBE(ExtAttrs), MAYBE(Optional), MAYBE('in'), | |
| 244 MAYBE(Optional), Type, MAYBE(AnEllipsis), Id], | |
| 245 # WebKit: | |
| 246 [MAYBE(ExtAttrs), MAYBE(Optional), Type, MAYBE(AnEllipsis), Id]) | |
| 247 | |
| 248 def Optional(): | |
| 249 return 'optional' | |
| 250 | |
| 251 def AnEllipsis(): | |
| 252 return '...' | |
| 253 | |
| 254 # Exceptions (Web IDL). | |
| 255 def ExceptionDef(): | |
| 256 return ['exception', Id, '{', MAYBE(MANY(_ExceptionMember)), '}', | |
| 257 ';'] | |
| 258 | |
| 259 def _ExceptionMember(): | |
| 260 return OR(Const, ExceptionField, ExtAttrs) | |
| 261 | |
| 262 def ExceptionField(): | |
| 263 return [Type, Id, ';'] | |
| 264 | |
| 265 # Types: | |
| 266 def Type(): | |
| 267 return _Type | |
| 268 | |
| 269 def UnionType(): | |
| 270 return ['(', Type, 'or', Type, ')'] | |
| 271 | |
| 272 def ReturnType(): | |
| 273 return OR(VoidType, _Type, UnionType) | |
| 274 | |
| 275 def InterfaceType(): | |
| 276 return ScopedName | |
| 277 | |
| 278 def ArrayModifiers(): | |
| 279 return re.compile(r'(\[\])+') | |
| 280 | |
| 281 def _Type(): | |
| 282 return OR( | |
| 283 [OR(AnyType, ObjectType), MAYBE([ArrayModifiers, MAYBE(Nullable)])], | |
| 284 [_NullableNonArrayType(), MAYBE(ArrayModifiers), MAYBE(Nullable)]) | |
| 285 | |
| 286 def _NullableNonArrayType(): | |
| 287 return [OR(_IntegerType, BooleanType, OctetType, FloatType, | |
| 288 DoubleType, SequenceType, ScopedName)] | |
| 289 | |
| 290 def Nullable(): | |
| 291 return '?' | |
| 292 | |
| 293 def SequenceType(): | |
| 294 return ['sequence', '<', Type, '>'] | |
| 295 | |
| 296 def AnyType(): | |
| 297 return 'any' | |
| 298 | |
| 299 def ObjectType(): | |
| 300 return re.compile(r'(object|Object)\b') # both spellings. | |
| 301 | |
| 302 def VoidType(): | |
| 303 return 'void' | |
| 304 | |
| 305 def _IntegerType(): | |
| 306 return [MAYBE(Unsigned), OR(ByteType, IntType, LongLongType, | |
| 307 LongType, OctetType, ShortType)] | |
| 308 | |
| 309 def Unsigned(): | |
| 310 return 'unsigned' | |
| 311 | |
| 312 def ShortType(): | |
| 313 return 'short' | |
| 314 | |
| 315 def LongLongType(): | |
| 316 return ['long', 'long'] | |
| 317 | |
| 318 def LongType(): | |
| 319 return 'long' | |
| 320 | |
| 321 def IntType(): | |
| 322 return 'int' | |
| 323 | |
| 324 def ByteType(): | |
| 325 return 'byte' | |
| 326 | |
| 327 def OctetType(): | |
| 328 return 'octet' | |
| 329 | |
| 330 def BooleanType(): | |
| 331 return 'boolean' | |
| 332 | |
| 333 def FloatType(): | |
| 334 return 'float' | |
| 335 | |
| 336 def DoubleType(): | |
| 337 return 'double' | |
| 338 | |
| 339 def _ScopedNames(): | |
| 340 return MANY(ScopedName, separator=',') | |
| 341 | |
| 342 def ScopedName(): | |
| 343 return re.compile(r'[\w\_\:\.\<\>]+') | |
| 344 | |
| 345 # Extended Attributes: | |
| 346 def ExtAttrs(): | |
| 347 return ['[', MAYBE(MANY(ExtAttr, ',')), ']'] | |
| 348 | |
| 349 def ExtAttr(): | |
| 350 return [Id, MAYBE(OR(['=', ExtAttrValue], ExtAttrArgList))] | |
| 351 | |
| 352 def ExtAttrValue(): | |
| 353 return OR(ExtAttrFunctionValue, re.compile(r'[\w&0-9:\-\| ]+'), re.compile
(r'"[^"]+"\|"[^"]+"'), re.compile(r'"[^"]+"')) | |
| 354 | |
| 355 def ExtAttrFunctionValue(): | |
| 356 return [Id, ExtAttrArgList] | |
| 357 | |
| 358 def ExtAttrArgList(): | |
| 359 return ['(', MAYBE(MANY(Argument, ',')), ')'] | |
| 360 | |
| 361 # Annotations - used in the FremontCut IDL grammar: | |
| 362 def _Annotations(): | |
| 363 return MANY(Annotation) | |
| 364 | |
| 365 def Annotation(): | |
| 366 return ['@', Id, MAYBE(_AnnotationBody)] | |
| 367 | |
| 368 def _AnnotationBody(): | |
| 369 return ['(', MAYBE(MANY(AnnotationArg, ',')), ')'] | |
| 370 | |
| 371 def AnnotationArg(): | |
| 372 return [Id, MAYBE(['=', AnnotationArgValue])] | |
| 373 | |
| 374 def AnnotationArgValue(): | |
| 375 return re.compile(r'[\w&0-9:/\-\.]+') | |
| 376 | |
| 377 ###################### END GRAMMAR ##################### | |
| 378 | |
| 379 # Return the grammar's root rule: | |
| 380 return MANY(_Definition) | |
| 381 | |
| 382 def _whitespace_grammar(self): | |
| 383 return OR(re.compile(r'\s+'), | |
| 384 re.compile(r'//.*'), | |
| 385 re.compile(r'#.*'), | |
| 386 re.compile(r'/\*.*?\*/', re.S)) | |
| 387 | |
| 388 def parse(self, content): | |
| 389 """Parse the give content string. | |
| 390 | |
| 391 Returns: | |
| 392 An abstract syntax tree (AST). | |
| 393 | |
| 394 Args: | |
| 395 content -- text to parse. | |
| 396 """ | |
| 397 | |
| 398 return self._pegparser.parse(content) | |
| OLD | NEW |