Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2017 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | |
| 18 | # A parser for enum types defined in HIDL. |
| 19 | # This script can parse HIDL files and generate a parse tree. |
| 20 | # To use, import and call parse("path/to/file.hal") |
Enrico Granata | 1ddfdef | 2017-05-31 11:50:08 -0700 | [diff] [blame] | 21 | # It will return a Python dictionary with three keys: |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 22 | # - header: an instance of Header |
| 23 | # - enums: a dictionary of EnumDecl objects by name |
Enrico Granata | 1ddfdef | 2017-05-31 11:50:08 -0700 | [diff] [blame] | 24 | # - structs: a dictionary of StructDecl objects by name |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 25 | |
| 26 | # It requires 'ply' (Python Lex/Yacc). |
| 27 | |
Enrico Granata | 3e8eeed | 2017-05-05 15:48:01 -0700 | [diff] [blame] | 28 | from __future__ import print_function |
| 29 | |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 30 | import ply |
| 31 | |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 32 | tokens = ('package', 'import', 'enum', 'struct', 'typedef', |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 33 | 'COLON', 'IDENTIFIER', 'COMMENT', 'NUMBER', 'HEX', 'OR', 'EQUALS', |
| 34 | 'LPAREN', 'RPAREN', 'LBRACE', 'RBRACE', 'DOT', 'SEMICOLON', 'VERSION', |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 35 | 'COMMA', 'SHIFT', 'LESSTHAN', 'GREATERTHAN') |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 36 | |
| 37 | t_COLON = r':' |
| 38 | t_NUMBER = r'[0-9]+' |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 39 | t_HEX = r'0[x|X][0-9A-Fa-f]+' |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 40 | t_OR = r'\|' |
| 41 | t_EQUALS = r'=' |
| 42 | t_LPAREN = r'\(' |
| 43 | t_RPAREN = r'\)' |
| 44 | t_SHIFT = r'<<' |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 45 | t_LESSTHAN = r'<' |
| 46 | t_GREATERTHAN = r'>' |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 47 | |
| 48 | def t_COMMENT(t): |
| 49 | r'(/\*(.|\n)*?\*/)|(//.*)' |
| 50 | pass |
| 51 | |
| 52 | t_LBRACE = r'{' |
| 53 | t_RBRACE = r'}' |
| 54 | t_DOT = r'\.' |
| 55 | t_SEMICOLON = r';' |
| 56 | t_VERSION = r'@[0-9].[0-9]' |
| 57 | t_COMMA = r',' |
| 58 | t_ignore = ' \n\t' |
| 59 | |
| 60 | def t_IDENTIFIER(t): |
| 61 | r'[a-zA-Z_][a-zA-Z_0-9]*' |
| 62 | if t.value == 'package': |
| 63 | t.type = 'package' |
| 64 | elif t.value == 'import': |
| 65 | t.type = 'import' |
| 66 | elif t.value == 'enum': |
| 67 | t.type = 'enum' |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 68 | elif t.value == 'struct': |
| 69 | t.type = 'struct' |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 70 | elif t.value == 'typedef': |
| 71 | t.type = 'typedef' |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 72 | return t |
| 73 | |
| 74 | def t_error(t): |
| 75 | t.type = t.value[0] |
| 76 | t.value = t.value[0] |
| 77 | t.lexer.skip(1) |
| 78 | return t |
| 79 | |
| 80 | import ply.lex as lex |
| 81 | lexer = lex.lex() |
| 82 | |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 83 | class Typename(object): |
| 84 | pass |
| 85 | |
| 86 | class SimpleTypename(Typename): |
| 87 | def __init__(self, name): |
| 88 | self.name = name |
| 89 | |
| 90 | def __str__(self): |
| 91 | return self.name |
| 92 | |
| 93 | class GenericTypename(Typename): |
| 94 | def __init__(self, name, arg): |
| 95 | self.name = name |
| 96 | self.arg = arg |
| 97 | |
| 98 | def __str__(self): |
| 99 | return '%s<%s>' % (self.name, self.arg) |
| 100 | |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 101 | class EnumHeader(object): |
| 102 | def __init__(self, name, base): |
| 103 | self.name = name |
| 104 | self.base = base |
| 105 | |
| 106 | def __str__(self): |
| 107 | return '%s%s' % (self.name, ' %s' % self.base if self.base else '') |
| 108 | |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 109 | class StructHeader(object): |
| 110 | def __init__(self, name): |
| 111 | self.name = name |
| 112 | |
| 113 | def __str__(self): |
| 114 | return 'struct %s' % self.name |
| 115 | |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 116 | class TypedefHeader(object): |
| 117 | def __init__(self, name): |
| 118 | self.name = name |
| 119 | |
| 120 | def __str__(self): |
| 121 | return 'typedef %s' % self.name |
| 122 | |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 123 | class EnumDecl(object): |
| 124 | def __init__(self, header, cases): |
| 125 | self.header = header |
| 126 | self.cases = cases |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 127 | self.fillInValues() |
| 128 | |
| 129 | def fillInValues(self): |
| 130 | # if no cases, we're done |
| 131 | if len(self.cases) < 1: return |
| 132 | # then, if case 0 has no value, set it to 0 |
| 133 | if self.cases[0].value is None: |
| 134 | self.cases[0].value = EnumValueConstant("0") |
| 135 | # then for all other cases... |
| 136 | for i in range(1,len(self.cases)): |
| 137 | # ...if there's no value |
| 138 | if self.cases[i].value is None: |
| 139 | # set to previous case + 1 |
| 140 | self.cases[i].value = EnumValueSuccessor( |
| 141 | EnumValueLocalRef(self.cases[i-1].name)) |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 142 | |
| 143 | def __str__(self): |
| 144 | return '%s {\n%s\n}' % (self.header, |
| 145 | '\n'.join(str(x) for x in self.cases)) |
| 146 | |
| 147 | def __repr__(self): |
| 148 | return self.__str__() |
| 149 | |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 150 | class StructDecl(object): |
| 151 | def __init__(self, header, items): |
| 152 | self.header = header |
| 153 | self.items = items |
| 154 | |
| 155 | def __str__(self): |
| 156 | return '%s {\n%s\n}' % (self.header, |
| 157 | '\n'.join(str(x) for x in self.items)) |
| 158 | |
| 159 | def __repr__(self): |
| 160 | return self.__str__() |
| 161 | |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 162 | class TypedefDecl(object): |
| 163 | def __init__(self, header, name): |
| 164 | self.header = header |
| 165 | self.name = name |
| 166 | |
| 167 | def __str__(self): |
| 168 | return '%s {\n%s\n}' % (self.header, |
| 169 | self.name) |
| 170 | |
| 171 | def __repr__(self): |
| 172 | return self.__str__() |
| 173 | |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 174 | class StructElement(object): |
| 175 | pass |
| 176 | |
| 177 | class StructElementIVar(StructElement): |
| 178 | def __init__(self, typename, name): |
| 179 | self.typename = typename |
| 180 | self.name = name |
| 181 | |
| 182 | def __str__(self): |
| 183 | return '%s %s' % (self.typename, self.name) |
| 184 | |
| 185 | class StructElementStruct(StructElement): |
| 186 | def __init__(self, struct): |
| 187 | self.name = struct.header.name |
| 188 | self.struct = struct |
| 189 | |
| 190 | def __str__(self): |
| 191 | return self.struct.__str__() |
| 192 | |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 193 | class EnumCase(object): |
| 194 | def __init__(self, name, value): |
| 195 | self.name = name |
| 196 | self.value = value |
| 197 | |
| 198 | def __str__(self): |
| 199 | return '%s = %s' % (self.name, self.value) |
| 200 | |
| 201 | class PackageID(object): |
| 202 | def __init__(self, name, version): |
| 203 | self.name = name |
| 204 | self.version = version |
| 205 | |
| 206 | def __str__(self): |
| 207 | return '%s%s' % (self.name, self.version) |
| 208 | |
| 209 | class Package(object): |
| 210 | def __init__(self, package): |
| 211 | self.package = package |
| 212 | |
| 213 | def __str__(self): |
| 214 | return 'package %s' % self.package |
| 215 | |
| 216 | class Import(object): |
| 217 | def __init__(self, package): |
| 218 | self.package = package |
| 219 | |
| 220 | def __str__(self): |
| 221 | return 'import %s' % self.package |
| 222 | |
| 223 | class Header(object): |
| 224 | def __init__(self, package, imports): |
| 225 | self.package = package |
| 226 | self.imports = imports |
| 227 | |
| 228 | def __str__(self): |
| 229 | return str(self.package) + "\n" + \ |
| 230 | '\n'.join(str(x) for x in self.imports) |
| 231 | |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 232 | class EnumValue(object): |
| 233 | def resolve(self, enum, document): |
| 234 | pass |
| 235 | |
| 236 | class EnumValueConstant(EnumValue): |
| 237 | def __init__(self, value): |
| 238 | self.value = value |
| 239 | |
| 240 | def __str__(self): |
| 241 | return self.value |
| 242 | |
| 243 | def resolve(self, enum, document): |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 244 | if self.value.lower().startswith("0x"): |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 245 | return int(self.value, 16) |
| 246 | else: |
| 247 | return int(self.value, 10) |
| 248 | |
| 249 | class EnumValueSuccessor(EnumValue): |
| 250 | def __init__(self, value): |
| 251 | self.value = value |
| 252 | |
| 253 | def __str__(self): |
| 254 | return '%s + 1' % self.value |
| 255 | |
| 256 | def resolve(self, enum, document): |
| 257 | return self.value.resolve(enum, document) + 1 |
| 258 | |
| 259 | class EnumValueLocalRef(EnumValue): |
| 260 | def __init__(self, ref): |
| 261 | self.ref = ref |
| 262 | |
| 263 | def __str__(self): |
| 264 | return self.ref |
| 265 | |
| 266 | def resolve(self, enum, document): |
| 267 | for case in enum.cases: |
| 268 | if case.name == self.ref: return case.value.resolve(enum, document) |
| 269 | |
Scott Randolph | 4ce965a | 2017-05-08 15:52:34 -0700 | [diff] [blame] | 270 | class EnumValueLShift(EnumValue): |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 271 | def __init__(self, base, offset): |
| 272 | self.base = base |
| 273 | self.offset = offset |
| 274 | |
| 275 | def __str__(self): |
| 276 | return '%s << %s' % (self.base, self.offset) |
| 277 | |
| 278 | def resolve(self, enum, document): |
| 279 | base = self.base.resolve(enum, document) |
| 280 | offset = self.offset.resolve(enum, document) |
| 281 | return base << offset |
| 282 | |
| 283 | class EnumValueOr(EnumValue): |
| 284 | def __init__(self, param1, param2): |
| 285 | self.param1 = param1 |
| 286 | self.param2 = param2 |
| 287 | |
| 288 | def __str__(self): |
| 289 | return '%s | %s' % (self.param1, self.param2) |
| 290 | |
| 291 | def resolve(self, enum, document): |
| 292 | param1 = self.param1.resolve(enum, document) |
| 293 | param2 = self.param2.resolve(enum, document) |
| 294 | return param1 | param2 |
| 295 | |
| 296 | class EnumValueExternRef(EnumValue): |
| 297 | def __init__(self, where, ref): |
| 298 | self.where = where |
| 299 | self.ref = ref |
| 300 | |
| 301 | def __str__(self): |
| 302 | return '%s:%s' % (self.where, self.ref) |
| 303 | |
| 304 | def resolve(self, enum, document): |
| 305 | enum = document['enums'][self.where] |
| 306 | return EnumValueLocalRef(self.ref).resolve(enum, document) |
| 307 | |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 308 | class Typedef(object): |
| 309 | def __init__(self, header, name): |
| 310 | self.header = header |
| 311 | self.name = name |
| 312 | |
| 313 | def __str__(self): |
| 314 | return 'typedef %s %s' % (self.typename, self.name) |
| 315 | |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 316 | # Error rule for syntax errors |
| 317 | def p_error(p): |
| 318 | print("Syntax error in input: %s" % p) |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 319 | try: |
| 320 | while True: |
| 321 | print(p.lexer.next().value, end=' ') |
| 322 | except: |
| 323 | pass |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 324 | |
| 325 | def p_document(t): |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 326 | 'document : header type_decls' |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 327 | enums = {} |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 328 | structs = {} |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 329 | typedefs = {} |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 330 | for enum in t[2]: |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 331 | if not isinstance(enum, EnumDecl): continue |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 332 | enums[enum.header.name] = enum |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 333 | for struct in t[2]: |
| 334 | if not isinstance(struct, StructDecl): continue |
| 335 | structs[struct.header.name] = struct |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 336 | for typedef in t[2]: |
| 337 | if not isinstance(typedef, TypedefDecl): continue |
| 338 | typedefs[typedef.header.name] = typedef |
| 339 | |
| 340 | t[0] = {'header' : t[1], 'enums' : enums, 'structs' : structs, 'typedefs' : typedef} |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 341 | |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 342 | def p_type_decls_1(t): |
| 343 | 'type_decls : type_decl' |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 344 | t[0] = [t[1]] |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 345 | def p_type_decls_2(t): |
| 346 | 'type_decls : type_decls type_decl' |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 347 | t[0] = t[1] + [t[2]] |
| 348 | |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 349 | def p_type_decl_e(t): |
| 350 | 'type_decl : enum_decl' |
| 351 | t[0] = t[1] |
| 352 | def p_type_decl_s(t): |
| 353 | 'type_decl : struct_decl' |
| 354 | t[0] = t[1] |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 355 | def p_type_decl_t(t): |
| 356 | 'type_decl : typedef_decl' |
| 357 | t[0] = t[1] |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 358 | |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 359 | def p_enum_cases_1(t): |
| 360 | 'enum_cases : enum_case' |
| 361 | t[0] = [t[1]] |
| 362 | def p_enum_cases_2(t): |
| 363 | 'enum_cases : enum_cases COMMA enum_case' |
| 364 | t[0] = t[1] + [t[3]] |
| 365 | |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 366 | def p_struct_elements_1(t): |
| 367 | 'struct_elements : struct_element' |
| 368 | t[0] = [t[1]] |
| 369 | def p_struct_elements_2(t): |
| 370 | 'struct_elements : struct_elements struct_element' |
| 371 | t[0] = t[1] + [t[2]] |
| 372 | |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 373 | def p_enum_base_1(t): |
| 374 | 'enum_base : VERSION COLON COLON IDENTIFIER' |
| 375 | t[0] = '%s::%s' % (t[1], t[4]) |
| 376 | def p_enum_base_2(t): |
| 377 | 'enum_base : IDENTIFIER' |
| 378 | t[0] = t[1] |
| 379 | |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 380 | def p_typedef_name(t): |
| 381 | 'typedef_name : IDENTIFIER' |
| 382 | t[0] = t[1] |
| 383 | |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 384 | def p_struct_header(t): |
| 385 | 'struct_header : struct IDENTIFIER' |
| 386 | t[0] = StructHeader(t[2]) |
| 387 | |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 388 | def p_enum_header_1(t): |
| 389 | 'enum_header : enum IDENTIFIER' |
| 390 | t[0] = EnumHeader(t[2], None) |
| 391 | def p_enum_header_2(t): |
| 392 | 'enum_header : enum IDENTIFIER COLON enum_base' |
| 393 | t[0] = EnumHeader(t[2], t[4]) |
| 394 | |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 395 | def p_typedef_header(t): |
| 396 | 'typedef_header : typedef IDENTIFIER' |
| 397 | t[0] = TypedefHeader(t[2]) |
| 398 | |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 399 | def p_struct_decl(t): |
| 400 | 'struct_decl : struct_header LBRACE struct_elements RBRACE SEMICOLON' |
| 401 | t[0] = StructDecl(t[1], t[3]) |
| 402 | |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 403 | def p_enum_decl_1(t): |
| 404 | 'enum_decl : enum_header LBRACE enum_cases RBRACE SEMICOLON' |
| 405 | t[0] = EnumDecl(t[1], t[3]) |
| 406 | def p_enum_decl_2(t): |
| 407 | 'enum_decl : enum_header LBRACE enum_cases COMMA RBRACE SEMICOLON' |
| 408 | t[0] = EnumDecl(t[1], t[3]) |
| 409 | |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 410 | def p_typedef_decl(t): |
| 411 | 'typedef_decl : typedef_header typedef_name SEMICOLON' |
| 412 | t[0] = TypedefDecl(t[1], t[2]) |
| 413 | |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 414 | def p_enum_value_1(t): |
| 415 | '''enum_value : NUMBER |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 416 | | HEX''' |
| 417 | t[0] = EnumValueConstant(t[1]) |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 418 | def p_enum_value_2(t): |
| 419 | 'enum_value : enum_value SHIFT NUMBER' |
Scott Randolph | 4ce965a | 2017-05-08 15:52:34 -0700 | [diff] [blame] | 420 | t[0] = EnumValueLShift(t[1], EnumValueConstant(t[3])) |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 421 | def p_enum_value_3(t): |
| 422 | 'enum_value : enum_value OR enum_value' |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 423 | t[0] = EnumValueOr(t[1], t[3]) |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 424 | def p_enum_value_4(t): |
| 425 | 'enum_value : LPAREN enum_value RPAREN' |
| 426 | t[0] = t[2] |
| 427 | def p_enum_value_5(t): |
| 428 | 'enum_value : IDENTIFIER COLON IDENTIFIER' |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 429 | t[0] = EnumValueExternRef(t[1],t[3]) |
| 430 | def p_enum_value_6(t): |
| 431 | 'enum_value : IDENTIFIER' |
| 432 | t[0] = EnumValueLocalRef(t[1]) |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 433 | |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 434 | def p_typename_v(t): |
| 435 | 'typename : IDENTIFIER' |
| 436 | t[0] = SimpleTypename(t[1]) |
| 437 | def p_typename_g(t): |
| 438 | 'typename : IDENTIFIER LESSTHAN IDENTIFIER GREATERTHAN' |
| 439 | t[0] = GenericTypename(t[1], t[3]) |
| 440 | |
| 441 | def p_struct_element_ivar(t): |
| 442 | 'struct_element : typename IDENTIFIER SEMICOLON' |
| 443 | t[0] = StructElementIVar(t[1], t[2]) |
| 444 | |
| 445 | def p_struct_element_struct(t): |
| 446 | 'struct_element : struct_decl' |
| 447 | t[0] = StructElementStruct(t[1]) |
| 448 | |
| 449 | def p_enum_case_v(t): |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 450 | 'enum_case : IDENTIFIER EQUALS enum_value' |
| 451 | t[0] = EnumCase(t[1], t[3]) |
Enrico Granata | a109ed6 | 2017-03-28 17:01:32 -0700 | [diff] [blame] | 452 | def p_enum_case_b(t): |
| 453 | 'enum_case : IDENTIFIER' |
| 454 | t[0] = EnumCase(t[1], None) |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 455 | |
| 456 | def p_header_1(t): |
| 457 | 'header : package_decl' |
| 458 | t[0] = Header(t[1], []) |
| 459 | |
| 460 | def p_header_2(t): |
| 461 | 'header : package_decl import_decls' |
| 462 | t[0] = Header(t[1], t[2]) |
| 463 | |
| 464 | def p_import_decls_1(t): |
| 465 | 'import_decls : import_decl' |
| 466 | t[0] = [t[1]] |
| 467 | |
| 468 | def p_import_decls_2(t): |
| 469 | 'import_decls : import_decls import_decl' |
| 470 | t[0] = t[1] + [t[2]] |
| 471 | |
| 472 | def p_package_decl(t): |
| 473 | 'package_decl : package package_ID SEMICOLON' |
| 474 | t[0] = Package(t[2]) |
| 475 | |
| 476 | def p_import_decl(t): |
| 477 | 'import_decl : import package_ID SEMICOLON' |
| 478 | t[0] = Import(t[2]) |
| 479 | |
| 480 | def p_package_ID(t): |
| 481 | 'package_ID : dotted_identifier VERSION' |
| 482 | t[0] = PackageID(t[1], t[2]) |
| 483 | |
| 484 | def p_dotted_identifier_1(t): |
| 485 | 'dotted_identifier : IDENTIFIER' |
| 486 | t[0] = t[1] |
| 487 | def p_dotted_identifier_2(t): |
| 488 | 'dotted_identifier : dotted_identifier DOT IDENTIFIER' |
| 489 | t[0] = t[1] + '.' + t[3] |
| 490 | |
| 491 | class SilentLogger(object): |
| 492 | def warning(*args): |
| 493 | pass |
Sam Lin | 3ca52fa | 2020-09-26 15:04:46 -0700 | [diff] [blame^] | 494 | def error(*args): |
| 495 | print(args) |
Enrico Granata | 4c43add | 2017-03-15 18:01:34 -0700 | [diff] [blame] | 496 | |
| 497 | import ply.yacc as yacc |
| 498 | parser = yacc.yacc(debug=False, write_tables=False, errorlog=SilentLogger()) |
| 499 | import sys |
| 500 | |
| 501 | def parse(filename): |
| 502 | return parser.parse(open(filename, 'r').read()) |