Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 1 | #!python |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 2 | """ Thrift IDL parser/compiler |
| 3 | |
| 4 | This parser uses the Python PLY LALR parser generator to build a parser for the Thrift IDL grammar. |
| 5 | |
| 6 | If a compiles \"thyc\" file exists for a given source \"thrift\" file it computes a hash of the file and determines |
| 7 | if if it is the source of the \"thyc\" file. If so, it simply returns the parse tree previously computed, otherwise it |
| 8 | parses the source and generates a new \"thyc\" file (assuming of course the source file contains no errors.) |
| 9 | |
| 10 | When the parser encounters import statements it searches for corresponding \"thrift\" or \"thyc\" files in paths corresponding to |
| 11 | the specified namespace. |
| 12 | |
| 13 | Author(s): Mark Slee(mclee@facebook.com), Marc Kwiatkowski (marc@facebook.com) |
| 14 | |
| 15 | $Id: |
| 16 | """ |
| 17 | |
| 18 | import lex |
| 19 | import os |
| 20 | import pickle |
| 21 | import string |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 22 | import yacc |
| 23 | |
| 24 | class Error(object): |
| 25 | |
| 26 | def __init__(self, start=0, end=0, message=""): |
| 27 | if len(message) == 0: |
| 28 | raise Exception, "NO MESSAGE" |
| 29 | self.message = message |
| 30 | self.start = start |
| 31 | self.end = end |
| 32 | |
| 33 | def __str__(self): |
| 34 | return str(self.start)+": error: "+self.message |
| 35 | |
| 36 | class SyntaxError(Error): |
Marc Slemko | 27340eb | 2006-08-10 20:45:55 +0000 | [diff] [blame] | 37 | def __init__(self, yaccSymbol): |
| 38 | if isinstance(yaccSymbol, yacc.YaccSymbol): |
| 39 | Error.__init__(self, yaccSymbol.lineno, yaccSymbol.lineno, "syntax error "+str(yaccSymbol.value)) |
| 40 | else: |
| 41 | Error.__init__(self, 1, 1, "syntax error "+str(yaccSymbol)) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 42 | |
| 43 | class SymanticsError(Error): |
| 44 | |
| 45 | def __init__(self, definition, message): |
| 46 | Error.__init__(self, definition.start, definition.end, message) |
| 47 | self.definition = definition |
| 48 | |
| 49 | def __str__(self): |
| 50 | return str(self.start)+": error: "+self.message |
| 51 | |
| 52 | class ErrorException(Exception): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 53 | def __init__(self, errors=None): |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 54 | Exception.__init__(self) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 55 | self.errors = errors |
| 56 | |
| 57 | class Definition(object): |
| 58 | """ Abstract thrift IDL definition unit """ |
| 59 | |
| 60 | def __init__(self, symbols=None, name="", id=None): |
| 61 | if symbols: |
| 62 | self.lines(symbols) |
| 63 | self.name = name |
| 64 | self.id = id |
| 65 | |
| 66 | def validate(self): |
| 67 | pass |
| 68 | |
| 69 | def lines(self, symbols): |
| 70 | self.start = symbols.lineno(1) |
| 71 | self.end = symbols.lineno(len(symbols) - 1) |
| 72 | |
| 73 | class Identifier(Definition): |
| 74 | """ An Identifier - name and optional integer id """ |
| 75 | |
| 76 | def __init__(self, symbols, name, id=None): |
| 77 | Definition.__init__(self, symbols, name, id) |
| 78 | |
| 79 | def __str__(self): |
| 80 | result = self.name |
| 81 | if self.id != 0: |
| 82 | result+="="+str(self.id) |
| 83 | return result |
| 84 | |
Marc Slemko | 27340eb | 2006-08-10 20:45:55 +0000 | [diff] [blame] | 85 | def toCanonicalType(ttype): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 86 | if isinstance(ttype, TypedefType): |
Marc Slemko | 27340eb | 2006-08-10 20:45:55 +0000 | [diff] [blame] | 87 | return toCanonicalType(ttype.definitionType) |
| 88 | else: |
| 89 | return ttype |
| 90 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 91 | def isVoidType(ttype): |
| 92 | """Is canonnical type of the specified type void?""" |
| 93 | return toCanonicalType(ttype) == VOID_TYPE |
| 94 | |
Marc Slemko | 27340eb | 2006-08-10 20:45:55 +0000 | [diff] [blame] | 95 | def isComparableType(ttype): |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 96 | """Is the type one that is implicitly comparable and thus is suitable for use in C++ maps and sets and java Sets and Maps?""" |
Marc Slemko | 27340eb | 2006-08-10 20:45:55 +0000 | [diff] [blame] | 97 | ttype = toCanonicalType(ttype) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 98 | return isinstance(ttype, PrimitiveType) or isinstance(ttype, EnumType) |
Marc Slemko | 27340eb | 2006-08-10 20:45:55 +0000 | [diff] [blame] | 99 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 100 | class Type(Definition): |
| 101 | """ Abstract Type definition """ |
| 102 | |
| 103 | def __init__(self, symbols, name): |
| 104 | Definition.__init__(self, symbols, name) |
| 105 | self.name = name |
| 106 | |
| 107 | def __str__(self): |
| 108 | return self.name |
| 109 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 110 | class TypedefType(Type): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 111 | |
| 112 | def __init__(self, symbols, name, definitionType): |
| 113 | Type.__init__(self, symbols, name) |
| 114 | self.definitionType = definitionType |
| 115 | |
| 116 | def __str__(self): |
| 117 | return self.name+"<"+str(self.name)+", "+str(self.definitionType)+">" |
| 118 | |
| 119 | """ Primitive Types """ |
| 120 | |
| 121 | class PrimitiveType(Type): |
| 122 | |
| 123 | def __init__(self, name): |
| 124 | Type.__init__(self, None, name) |
| 125 | |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 126 | STOP_TYPE = PrimitiveType("stop") |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 127 | VOID_TYPE = PrimitiveType("void") |
| 128 | BOOL_TYPE = PrimitiveType("bool") |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 129 | STRING_TYPE = PrimitiveType("utf7") |
| 130 | UTF7_TYPE = STRING_TYPE |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 131 | UTF8_TYPE = PrimitiveType("utf8") |
| 132 | UTF16_TYPE = PrimitiveType("utf16") |
| 133 | BYTE_TYPE = PrimitiveType("u08") |
| 134 | I08_TYPE = PrimitiveType("i08") |
| 135 | I16_TYPE = PrimitiveType("i16") |
| 136 | I32_TYPE = PrimitiveType("i32") |
| 137 | I64_TYPE = PrimitiveType("i64") |
| 138 | U08_TYPE = PrimitiveType("u08") |
| 139 | U16_TYPE = PrimitiveType("u16") |
| 140 | U32_TYPE = PrimitiveType("u32") |
| 141 | U64_TYPE = PrimitiveType("u64") |
| 142 | FLOAT_TYPE = PrimitiveType("float") |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 143 | DOUBLE_TYPE = PrimitiveType("double") |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 144 | |
| 145 | PRIMITIVE_MAP = { |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 146 | "stop" : STOP_TYPE, |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 147 | "void" : VOID_TYPE, |
| 148 | "bool" : BOOL_TYPE, |
| 149 | "string": UTF7_TYPE, |
| 150 | "utf7": UTF7_TYPE, |
| 151 | "utf8": UTF8_TYPE, |
| 152 | "utf16": UTF16_TYPE, |
| 153 | "byte" : U08_TYPE, |
| 154 | "i08": I08_TYPE, |
| 155 | "i16": I16_TYPE, |
| 156 | "i32": I32_TYPE, |
| 157 | "i64": I64_TYPE, |
| 158 | "u08": U08_TYPE, |
| 159 | "u16": U16_TYPE, |
| 160 | "u32": U32_TYPE, |
| 161 | "u64": U64_TYPE, |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 162 | "float": FLOAT_TYPE, |
| 163 | "double": DOUBLE_TYPE |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | """ Collection Types """ |
| 167 | |
| 168 | class CollectionType(Type): |
| 169 | |
| 170 | def __init__(self, symbols, name): |
| 171 | Type.__init__(self, symbols, name) |
| 172 | |
Marc Slemko | 27340eb | 2006-08-10 20:45:55 +0000 | [diff] [blame] | 173 | def validate(self): |
| 174 | return True |
| 175 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 176 | class MapType(CollectionType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 177 | |
| 178 | def __init__(self, symbols, keyType, valueType): |
| 179 | CollectionType.__init__(self, symbols, "map<"+keyType.name+","+valueType.name +">") |
| 180 | self.keyType = keyType |
| 181 | self.valueType = valueType |
| 182 | |
Marc Slemko | 27340eb | 2006-08-10 20:45:55 +0000 | [diff] [blame] | 183 | def validate(self): |
| 184 | if not isComparableType(self.keyType): |
| 185 | raise ErrorException([SymanticsError(self, "key type \""+str(self.keyType)+"\" is not a comparable type.")]) |
| 186 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 187 | class SetType(CollectionType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 188 | |
| 189 | def __init__(self, symbols, valueType): |
| 190 | CollectionType.__init__(self, symbols, "set<"+valueType.name+">") |
| 191 | self.valueType = valueType |
| 192 | |
Marc Slemko | 27340eb | 2006-08-10 20:45:55 +0000 | [diff] [blame] | 193 | def validate(self): |
| 194 | if not isComparableType(self.valueType): |
| 195 | raise ErrorException([SymanticsError(self, "value type \""+str(self.valueType)+"\" is not a comparable type.")]) |
| 196 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 197 | class ListType(CollectionType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 198 | |
| 199 | def __init__(self, symbols, valueType): |
| 200 | CollectionType.__init__(self, symbols, "list<"+valueType.name+">") |
| 201 | self.valueType = valueType |
| 202 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 203 | class EnumType(Definition): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 204 | |
| 205 | def __init__(self, symbols, name, enumDefs): |
| 206 | Definition.__init__(self, symbols, name) |
| 207 | self.enumDefs = enumDefs |
| 208 | |
| 209 | def validate(self): |
| 210 | ids = {} |
| 211 | names = {} |
| 212 | errors = [] |
| 213 | |
| 214 | for enumDef in self.enumDefs: |
| 215 | |
| 216 | if enumDef.name in names: |
| 217 | errors.append(SymanticsError(enumDef, self.name+"."+str(enumDef.name)+" already defined at line "+str(names[enumDef.name].start))) |
| 218 | else: |
| 219 | names[enumDef.name] = enumDef |
| 220 | |
| 221 | if enumDef.id != None: |
| 222 | oldEnumDef = ids.get(enumDef.id) |
| 223 | if oldEnumDef: |
| 224 | errors.append(SymanticsError(enumDef, "enum "+self.name+" \""+str(enumDef.name)+"\" uses constant already assigned to \""+oldEnumDef.name+"\"")) |
| 225 | else: |
| 226 | ids[enumDef.id] = enumDef |
| 227 | |
| 228 | if len(errors): |
| 229 | raise ErrorException(errors) |
| 230 | |
| 231 | def assignId(enumDef, currentId, ids): |
| 232 | 'Finds the next available id number for an enum definition' |
| 233 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 234 | eid= currentId + 1 |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 235 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 236 | while eid in ids: |
| 237 | eid += 1 |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 238 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 239 | enumDef.id = eid |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 240 | |
| 241 | ids[enumDef.id] = enumDef |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 242 | |
| 243 | return eid |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 244 | |
| 245 | # assign ids for all enum defs with unspecified ids |
| 246 | |
| 247 | currentId = 0 |
| 248 | |
| 249 | for enumDef in self.enumDefs: |
| 250 | if not enumDef.id: |
| 251 | assignId(enumDef, currentId, ids) |
| 252 | currentId = enumDef.id |
| 253 | |
| 254 | def __repr__(self): |
| 255 | return str(self) |
| 256 | |
| 257 | def __str__(self): |
| 258 | return self.name+"<"+string.join(map(lambda enumDef: str(enumDef), self.enumDefs), ", ") |
| 259 | |
| 260 | class EnumDef(Definition): |
| 261 | |
| 262 | def __init__(self, symbols, name, id=None): |
| 263 | Definition.__init__(self, symbols, name, id) |
| 264 | |
| 265 | def __repr__(self): |
| 266 | return str(self) |
| 267 | |
| 268 | def __str__(self): |
| 269 | result = self.name |
| 270 | if self.id: |
| 271 | result+= ":"+str(self.id) |
| 272 | return result |
| 273 | |
| 274 | |
| 275 | class Field(Definition): |
| 276 | |
| 277 | def __init__(self, symbols, type, identifier): |
| 278 | Definition.__init__(self, symbols, identifier.name, identifier.id) |
| 279 | self.type = type |
| 280 | self.identifier = identifier |
| 281 | |
| 282 | def __str__(self): |
| 283 | return "<"+str(self.type)+", "+str(self.identifier)+">" |
| 284 | |
| 285 | def validateFieldList(fieldList): |
| 286 | |
| 287 | errors = [] |
| 288 | names = {} |
| 289 | ids = {} |
| 290 | |
| 291 | for field in fieldList: |
| 292 | |
| 293 | if field.name in names: |
| 294 | oldField = names[field.name] |
| 295 | errors.append(SymanticsError(field, "field \""+field.name+"\" already defined at "+str(oldField.start))) |
| 296 | else: |
| 297 | names[field.name] = field |
| 298 | |
| 299 | if field.id != None: |
| 300 | oldField = ids.get(field.id) |
| 301 | if oldField: |
| 302 | errors.append(SymanticsError(field, "field \""+field.name+"\" uses constant already assigned to \""+oldField.name+"\"")) |
| 303 | else: |
| 304 | ids[field.id] = field |
| 305 | |
| 306 | if len(errors): |
| 307 | raise ErrorException(errors) |
| 308 | |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 309 | def assignId(field, currentId, ids): |
| 310 | 'Finds the next available id number for a field' |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 311 | fid = currentId - 1 |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 312 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 313 | while fid in ids: |
| 314 | fid-= 1 |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 315 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 316 | field.id = fid |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 317 | |
| 318 | ids[field.id] = field |
| 319 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 320 | return fid |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 321 | |
| 322 | # assign ids for all fields with unspecified ids |
| 323 | |
| 324 | currentId = 0 |
| 325 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 326 | for field in fieldList: |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 327 | if not field.id: |
| 328 | currentId = assignId(field, currentId, ids) |
| 329 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 330 | class StructType(Type): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 331 | |
| 332 | def __init__(self, symbols, name, fieldList): |
| 333 | Type.__init__(self, symbols, name) |
| 334 | self.fieldList = fieldList |
| 335 | |
| 336 | def validate(self): |
| 337 | validateFieldList(self.fieldList) |
| 338 | |
| 339 | def __str__(self): |
| 340 | return self.name+"<"+string.join(map(lambda a: str(a), self.fieldList), ", ")+">" |
| 341 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 342 | class ExceptionType(StructType): |
| 343 | |
| 344 | def __init__(self, symbols, name, fieldList): |
| 345 | StructType.__init__(self, symbols, name, fieldList) |
| 346 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 347 | class Function(Definition): |
| 348 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 349 | def __init__(self, symbols, name, resultStruct, argsStruct): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 350 | Definition.__init__(self, symbols, name) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 351 | self.resultStruct = resultStruct |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 352 | self.argsStruct = argsStruct |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 353 | |
| 354 | def validate(self): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 355 | validateFieldList(self.argsStruct.fieldList) |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 356 | validateFieldList(self.resultStruct.fieldList) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 357 | |
| 358 | def args(self): |
| 359 | return self.argsStruct.fieldList |
| 360 | |
| 361 | def returnType(self): |
| 362 | return self.resultStruct.fieldList[0].type |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 363 | |
| 364 | def exceptions(self): |
| 365 | return self.resultStruct.fieldList[1:] |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 366 | |
| 367 | def __str__(self): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 368 | return self.name+"("+str(self.argsStruct)+") => "+str(self.resultStruct) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 369 | |
| 370 | class Service(Definition): |
| 371 | |
| 372 | def __init__(self, symbols, name, functionList): |
| 373 | Definition.__init__(self, symbols, name) |
| 374 | self.functionList = functionList |
| 375 | |
| 376 | def validate(self): |
| 377 | |
| 378 | errors = [] |
| 379 | functionNames = {} |
| 380 | for function in self.functionList: |
| 381 | if function.name in functionNames: |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 382 | oldFunction = functionNames[function.name] |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 383 | errors.append(SymanticsError(function, "function "+function.name+" already defined at "+str(oldFunction.start))) |
| 384 | |
| 385 | if len(errors): |
| 386 | raise ErrorException(errors) |
| 387 | |
| 388 | def __str__(self): |
| 389 | return self.name+"("+string.join(map(lambda a: str(a), self.functionList), ", ")+")" |
| 390 | |
| 391 | class Program(object): |
| 392 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 393 | def __init__(self, symbols=None, name="", |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 394 | namespace="", |
| 395 | definitions=None, |
| 396 | serviceMap=None, |
| 397 | typedefMap=None, |
| 398 | enumMap=None, structMap=None, |
| 399 | collectionMap=None, |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 400 | primitiveMap=None): |
| 401 | |
| 402 | self.name = name |
| 403 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 404 | self.namespace = namespace |
| 405 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 406 | if not definitions: |
| 407 | definitions = [] |
| 408 | self.definitions = definitions |
| 409 | |
| 410 | if not serviceMap: |
| 411 | serviceMap = {} |
| 412 | self.serviceMap = serviceMap |
| 413 | |
| 414 | if not typedefMap: |
| 415 | typedefMap = {} |
| 416 | self.typedefMap = typedefMap |
| 417 | |
| 418 | if not enumMap: |
| 419 | enumMap = {} |
| 420 | self.enumMap = enumMap |
| 421 | |
| 422 | if not structMap: |
| 423 | structMap = {} |
| 424 | self.structMap = structMap |
| 425 | |
| 426 | if not collectionMap: |
| 427 | collectionMap = {} |
| 428 | self.collectionMap = collectionMap |
| 429 | |
| 430 | if not primitiveMap: |
| 431 | primitiveMap = PRIMITIVE_MAP |
| 432 | self.primitiveMap = primitiveMap |
| 433 | |
| 434 | def addDefinition(self, definition, definitionMap, definitionTypeName): |
| 435 | |
| 436 | oldDefinition = definitionMap.get(definition.name) |
| 437 | if oldDefinition: |
| 438 | raise ErrorException([SymanticsError(definition, definitionTypeName+" "+definition.name+" is already defined at "+str(oldDefinition.start))]) |
| 439 | else: |
| 440 | definitionMap[definition.name] = definition |
| 441 | |
| 442 | # keep an ordered list of definitions so that stub/skel generators can determine the original order |
| 443 | |
| 444 | self.definitions.append(definition) |
| 445 | |
| 446 | def addStruct(self, struct): |
| 447 | self.addDefinition(struct, self.structMap, "struct") |
| 448 | |
| 449 | def addTypedef(self, typedef): |
| 450 | self.addDefinition(typedef, self.typedefMap, "typedef") |
| 451 | |
| 452 | def addEnum(self, enum): |
| 453 | self.addDefinition(enum, self.enumMap, "enum") |
| 454 | |
| 455 | def addService(self, service): |
| 456 | self.addDefinition(service, self.serviceMap, "service") |
| 457 | |
| 458 | def addCollection(self, collection): |
| 459 | if collection.name in self.collectionMap: |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 460 | return self.collectionMap[collection.name] |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 461 | else: |
| 462 | self.collectionMap[collection.name] = collection |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 463 | return collection |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 464 | |
| 465 | def getType(self, parent, symbol): |
| 466 | """ Get the type definition for a symbol""" |
| 467 | |
| 468 | typeName = None |
| 469 | |
| 470 | if isinstance(symbol, Type): |
| 471 | return symbol |
| 472 | elif isinstance(symbol, Field): |
| 473 | typeName = symbol.type.name |
| 474 | elif isinstance(symbol, Identifier): |
| 475 | typeName = symbol.name |
| 476 | else: |
| 477 | raise ErrorException([SymanticsError(parent, "unknown symbol \""+str(symbol)+"\"")]) |
| 478 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 479 | for tmap in (self.primitiveMap, self.collectionMap, self.typedefMap, self.enumMap, self.structMap): |
| 480 | if typeName in tmap: |
| 481 | return tmap[typeName] |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 482 | |
| 483 | raise ErrorException([SymanticsError(parent, "\""+typeName+"\" is not defined.")]) |
| 484 | |
| 485 | def hasType(self, parent, symbol): |
| 486 | """ Determine if a type definition exists for the symbol""" |
| 487 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 488 | return self.getType(parent, symbol) != None |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 489 | |
| 490 | def validate(self): |
| 491 | |
| 492 | errors = [] |
| 493 | |
| 494 | # Verify that struct fields types, collection key and element types, and typedef defined types exists and replaces |
| 495 | # type names with references to the type objects |
| 496 | |
| 497 | for struct in self.structMap.values(): |
| 498 | for field in struct.fieldList: |
| 499 | try: |
| 500 | field.type = self.getType(struct, field) |
| 501 | except ErrorException, e: |
| 502 | errors+= e.errors |
| 503 | |
| 504 | for collection in self.collectionMap.values(): |
| 505 | try: |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 506 | if isinstance(collection, MapType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 507 | collection.keyType = self.getType(collection, collection.keyType) |
| 508 | |
| 509 | collection.valueType = self.getType(collection, collection.valueType) |
Marc Slemko | 27340eb | 2006-08-10 20:45:55 +0000 | [diff] [blame] | 510 | |
| 511 | collection.validate() |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 512 | |
| 513 | except ErrorException, e: |
| 514 | errors+= e.errors |
| 515 | |
| 516 | for typedef in self.typedefMap.values(): |
| 517 | try: |
| 518 | typedef.definitionType = self.getType(self, typedef.definitionType) |
| 519 | |
| 520 | except ErrorException, e: |
| 521 | errors+= e.errors |
| 522 | |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 523 | # Verify that service function result and arg list types exist and replace type name with reference to definition |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 524 | |
| 525 | for service in self.serviceMap.values(): |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 526 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 527 | for function in service.functionList: |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 528 | |
| 529 | for field in function.resultStruct.fieldList: |
| 530 | try: |
| 531 | field.type = self.getType(function, field) |
| 532 | except ErrorException, e: |
| 533 | errors+= e.errors |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 534 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 535 | for field in function.argsStruct.fieldList: |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 536 | try: |
| 537 | field.type = self.getType(function, field) |
| 538 | except ErrorException, e: |
| 539 | errors+= e.errors |
| 540 | |
| 541 | if len(errors): |
| 542 | raise ErrorException(errors) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 543 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 544 | def validateNamespace(self, namespace): |
| 545 | |
| 546 | if self.namespace != "": |
| 547 | raise ErrorException([SymanticsError, self, "namespace already defined as \""+self.namespace+"\""]) |
| 548 | self.namespace = namespace |
| 549 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 550 | class Parser(object): |
| 551 | |
| 552 | reserved = ("BYTE", |
| 553 | # "CONST", |
| 554 | "DOUBLE", |
| 555 | "ENUM", |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 556 | "EXCEPTION", |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 557 | # "EXTENDS", |
| 558 | "I08", |
| 559 | "I16", |
| 560 | "I32", |
| 561 | "I64", |
| 562 | "LIST", |
| 563 | "MAP", |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 564 | "NAMESPACE", |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 565 | "SERVICE", |
| 566 | "SET", |
| 567 | # "STATIC", |
| 568 | "STRING", |
| 569 | "STRUCT", |
| 570 | # "SYNCHRONIZED", |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 571 | "THROWS", |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 572 | "TYPEDEF", |
| 573 | "U08", |
| 574 | "U16", |
| 575 | "U32", |
| 576 | "U64", |
| 577 | "UTF16", |
| 578 | "UTF8", |
| 579 | "VOID" |
| 580 | ) |
| 581 | |
| 582 | tokens = reserved + ( |
| 583 | # Literals (identifier, integer constant, float constant, string constant, char const) |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 584 | 'ID', 'ICONST', # 'SCONST', 'FCONST', |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 585 | # Operators default=, optional*, variable... |
| 586 | 'ASSIGN', #'OPTIONAL', 'ELLIPSIS', |
| 587 | # Delimeters ( ) { } < > , . ; : |
| 588 | 'LPAREN', 'RPAREN', |
| 589 | 'LBRACE', 'RBRACE', |
| 590 | 'LANGLE', 'RANGLE', |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 591 | 'COMMA', 'PERIOD' #, 'SEMI' , 'COLON' |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 592 | ) |
| 593 | |
| 594 | precendence = () |
| 595 | |
| 596 | reserved_map = {} |
| 597 | |
| 598 | for r in reserved: |
| 599 | reserved_map[r.lower()] = r |
| 600 | |
| 601 | def t_ID(self, t): |
| 602 | r'[A-Za-z_][\w_]*' |
| 603 | t.type = self.reserved_map.get(t.value,"ID") |
| 604 | return t |
| 605 | |
| 606 | # Completely ignored characters |
| 607 | t_ignore = ' \t\x0c' |
| 608 | |
| 609 | # t_OPTIONAL = r'\*' |
| 610 | t_ASSIGN = r'=' |
| 611 | |
| 612 | # Delimeters |
| 613 | t_LPAREN = r'\(' |
| 614 | t_RPAREN = r'\)' |
| 615 | t_LANGLE = r'\<' |
| 616 | t_RANGLE = r'\>' |
| 617 | t_LBRACE = r'\{' |
| 618 | t_RBRACE = r'\}' |
| 619 | t_COMMA = r',' |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 620 | t_PERIOD = r'\.' |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 621 | # t_SEMI = r';' |
| 622 | # t_COLON = r':' |
| 623 | # t_ELLIPSIS = r'\.\.\.' |
| 624 | |
| 625 | # Integer literal |
| 626 | t_ICONST = r'\d+([uU]|[lL]|[uU][lL]|[lL][uU])?' |
| 627 | |
| 628 | # Floating literal |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 629 | # t_FCONST = r'((\d+)(\.\d+)(e(\+|-)?(\d+))? | (\d+)e(\+|-)?(\d+))([lL]|[fF])?' |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 630 | |
| 631 | # String literal |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 632 | # t_SCONST = r'\"([^\\\n]|(\\.))*?\"' |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 633 | |
| 634 | # Comments |
| 635 | def t_comment(self, t): |
| 636 | r'(?:/\*(.|\n)*?\*/)|(?://[^\n]*\n)' |
| 637 | t.lineno += t.value.count('\n') |
| 638 | |
| 639 | def t_error(self, t): |
| 640 | print "Illegal character %s" % repr(t.value[0]) |
| 641 | t.skip(1) |
| 642 | |
| 643 | # Newlines |
| 644 | def t_newline(self, t): |
| 645 | r'\n+' |
| 646 | t.lineno += t.value.count("\n") |
| 647 | |
| 648 | def p_program(self, p): |
| 649 | 'program : definitionlist' |
| 650 | pass |
| 651 | |
| 652 | def p_definitionlist_1(self, p): |
| 653 | 'definitionlist : definitionlist definition' |
| 654 | pass |
| 655 | |
| 656 | def p_definitionlist_2(self, p): |
| 657 | 'definitionlist :' |
| 658 | pass |
| 659 | |
| 660 | def p_definition_1(self, p): |
| 661 | 'definition : typedef' |
| 662 | self.pdebug("p_definition_1", p) |
| 663 | p[0] = p[1] |
| 664 | try: |
| 665 | self.program.addTypedef(p[0]) |
| 666 | except ErrorException, e: |
| 667 | self.errors+= e.errors |
| 668 | |
| 669 | def p_definition_2(self, p): |
| 670 | 'definition : enum' |
| 671 | self.pdebug("p_definition_2", p) |
| 672 | p[0] = p[1] |
| 673 | try: |
| 674 | self.program.addEnum(p[0]) |
| 675 | except ErrorException, e: |
| 676 | self.errors+= e.errors |
| 677 | |
| 678 | def p_definition_3(self, p): |
| 679 | 'definition : struct' |
| 680 | self.pdebug("p_definition_3", p) |
| 681 | p[0] = p[1] |
| 682 | try: |
| 683 | self.program.addStruct(p[0]) |
| 684 | except ErrorException, e: |
| 685 | self.errors+= e.errors |
| 686 | |
| 687 | def p_definition_4(self, p): |
| 688 | 'definition : service' |
| 689 | self.pdebug("p_definition_4", p) |
| 690 | p[0] = p[1] |
| 691 | try: |
| 692 | self.program.addService(p[0]) |
| 693 | except ErrorException, e: |
| 694 | self.errors+= e.errors |
| 695 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 696 | def p_definition_5(self, p): |
| 697 | 'definition : exception' |
| 698 | self.pdebug("p_definition_5", p) |
| 699 | p[0] = p[1] |
| 700 | try: |
| 701 | self.program.addStruct(p[0]) |
| 702 | except ErrorException, e: |
| 703 | self.errors+= e.errors |
| 704 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 705 | def p_definition_6(self, p): |
| 706 | 'definition : namespace' |
| 707 | self.pdebug("p_definition_6", p) |
| 708 | p[0] = p[1] |
| 709 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 710 | def p_typedef(self, p): |
| 711 | 'typedef : TYPEDEF definitiontype ID' |
| 712 | self.pdebug("p_typedef", p) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 713 | p[0] = TypedefType(p, p[3], p[2]) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 714 | try: |
| 715 | p[0].validate() |
| 716 | |
| 717 | except ErrorException, e: |
| 718 | self.errors+= e.errors |
| 719 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 720 | def p_enum(self, p): |
| 721 | 'enum : ENUM ID LBRACE enumdeflist RBRACE' |
| 722 | self.pdebug("p_enum", p) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 723 | p[0] = EnumType(p, p[2], p[4]) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 724 | |
| 725 | try: |
| 726 | p[0].validate() |
| 727 | except ErrorException, e: |
| 728 | self.errors+= e.errors |
| 729 | |
| 730 | def p_enumdeflist_1(self, p): |
| 731 | 'enumdeflist : enumdeflist COMMA enumdef' |
| 732 | self.pdebug("p_enumdeflist_1", p) |
| 733 | p[0] = p[1] + (p[3],) |
| 734 | |
| 735 | def p_enumdeflist_2(self, p): |
| 736 | 'enumdeflist : enumdef' |
| 737 | self.pdebug("p_enumdeflist_2", p) |
| 738 | p[0] = (p[1],) |
| 739 | |
| 740 | def p_enumdef_0(self, p): |
| 741 | 'enumdef : ID ASSIGN ICONST' |
| 742 | self.pdebug("p_enumdef_0", p) |
| 743 | p[0] = EnumDef(p, p[1], int(p[3])) |
| 744 | |
| 745 | def p_enumdef_1(self, p): |
| 746 | 'enumdef : ID' |
| 747 | self.pdebug("p_enumdef_1", p) |
| 748 | p[0] = EnumDef(p, p[1]) |
| 749 | |
| 750 | def p_struct(self, p): |
| 751 | 'struct : STRUCT ID LBRACE fieldlist RBRACE' |
| 752 | self.pdebug("p_struct", p) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 753 | p[0] = StructType(p, p[2], p[4]) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 754 | |
| 755 | try: |
| 756 | p[0].validate() |
| 757 | except ErrorException, e: |
| 758 | self.errors+= e.errors |
| 759 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 760 | def p_exception(self, p): |
| 761 | 'exception : EXCEPTION ID LBRACE fieldlist RBRACE' |
| 762 | self.pdebug("p_struct", p) |
| 763 | p[0] = ExceptionType(p, p[2], p[4]) |
| 764 | |
| 765 | try: |
| 766 | p[0].validate() |
| 767 | except ErrorException, e: |
| 768 | self.errors+= e.errors |
| 769 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 770 | def p_namespace(self, p): |
| 771 | 'namespace : NAMESPACE namespacespecifier' |
| 772 | self.pdebug("p_struct", p) |
| 773 | p[0] = p[2] |
| 774 | |
| 775 | try: |
| 776 | self.program.validateNamespace(p[0]) |
| 777 | except ErrorException, e: |
| 778 | self.errors+= e.errors |
| 779 | |
| 780 | def p_namespacespecifier_1(self, p): |
| 781 | 'namespacespecifier : ID' |
| 782 | self.pdebug("p_namespacespecifier", p) |
| 783 | p[0] = p[1] |
| 784 | |
| 785 | def p_namespacespecifier_2(self, p): |
| 786 | 'namespacespecifier : ID PERIOD namespacespecifier' |
| 787 | self.pdebug("p_namespacespecifier", p) |
| 788 | p[0] = p[1]+"."+p[3] |
| 789 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 790 | def p_service(self, p): |
| 791 | 'service : SERVICE ID LBRACE functionlist RBRACE' |
| 792 | self.pdebug("p_service", p) |
| 793 | p[0] = Service(p, p[2], p[4]) |
| 794 | try: |
| 795 | p[0].validate() |
| 796 | except ErrorException, e: |
| 797 | self.errors+= e.errors |
| 798 | |
| 799 | def p_functionlist_1(self, p): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 800 | 'functionlist : function COMMA functionlist' |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 801 | self.pdebug("p_functionlist_1", p) |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 802 | p[0] = (p[1],) + p[3] |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 803 | |
| 804 | def p_functionlist_2(self, p): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 805 | 'functionlist : function' |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 806 | self.pdebug("p_functionlist_2", p) |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 807 | p[0] = (p[1],) |
| 808 | |
| 809 | def p_functionlist_3(self, p): |
| 810 | 'functionlist : ' |
| 811 | self.pdebug("p_functionlist_3", p) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 812 | p[0] = () |
| 813 | |
| 814 | def p_function(self, p): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 815 | 'function : functiontype functionmodifiers ID LPAREN fieldlist RPAREN exceptionspecifier' |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 816 | self.pdebug("p_function", p) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 817 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 818 | resultStruct = StructType(p, p[3]+"_result", (Field(p, p[1], Identifier(None, "success", 0)),)+p[7]) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 819 | |
| 820 | p[0] = Function(p, p[3], resultStruct, StructType(p, p[3]+"_args", p[5])) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 821 | try: |
| 822 | p[0].validate() |
| 823 | except ErrorException, e: |
| 824 | self.errors+= e.errors |
| 825 | |
| 826 | def p_functionmodifiers(self, p): |
| 827 | 'functionmodifiers :' |
| 828 | self.pdebug("p_functionmodifiers", p) |
| 829 | p[0] = () |
| 830 | |
| 831 | def p_fieldlist_1(self, p): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 832 | 'fieldlist : field COMMA fieldlist' |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 833 | self.pdebug("p_fieldlist_1", p) |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 834 | p[0] = (p[1],) + p[3] |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 835 | |
| 836 | def p_fieldlist_2(self, p): |
| 837 | 'fieldlist : field' |
| 838 | self.pdebug("p_fieldlist_2", p) |
| 839 | p[0] = (p[1],) |
| 840 | |
| 841 | def p_fieldlist_3(self, p): |
| 842 | 'fieldlist :' |
| 843 | self.pdebug("p_fieldlist_3", p) |
| 844 | p[0] = () |
| 845 | |
| 846 | def p_field_1(self, p): |
| 847 | 'field : fieldtype ID ASSIGN ICONST' |
| 848 | self.pdebug("p_field_1", p) |
| 849 | p[0] = Field(p, p[1], Identifier(None, p[2], int(p[4]))) |
| 850 | |
| 851 | def p_field_2(self, p): |
| 852 | 'field : fieldtype ID' |
| 853 | self.pdebug("p_field_2", p) |
| 854 | p[0] = Field(p, p[1], Identifier(None, p[2])) |
| 855 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 856 | def p_exceptionSpecifier_1(self, p): |
| 857 | 'exceptionspecifier : THROWS LPAREN fieldlist RPAREN' |
| 858 | self.pdebug("p_exceptionspecifier", p) |
| 859 | p[0] = p[3] |
| 860 | |
| 861 | def p_exceptionSpecifier_2(self, p): |
| 862 | 'exceptionspecifier : empty' |
| 863 | self.pdebug("p_exceptionspecifier", p) |
| 864 | p[0] = () |
| 865 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 866 | def p_definitiontype_1(self, p): |
| 867 | 'definitiontype : basetype' |
| 868 | self.pdebug("p_definitiontype_1", p) |
| 869 | p[0] = p[1] |
| 870 | |
| 871 | def p_definitiontype_2(self, p): |
| 872 | 'definitiontype : collectiontype' |
| 873 | self.pdebug("p_definitiontype_2", p) |
| 874 | p[0] = p[1] |
| 875 | |
| 876 | def p_functiontype_1(self, p): |
| 877 | 'functiontype : fieldtype' |
| 878 | self.pdebug("p_functiontype_1", p) |
| 879 | p[0] = p[1] |
| 880 | |
| 881 | def p_functiontype_2(self, p): |
| 882 | 'functiontype : VOID' |
| 883 | self.pdebug("p_functiontype_2", p) |
| 884 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 885 | |
| 886 | def p_fieldtype_1(self, p): |
| 887 | 'fieldtype : ID' |
| 888 | self.pdebug("p_fieldtype_1", p) |
| 889 | p[0] = Identifier(p, p[1]) |
| 890 | |
| 891 | def p_fieldtype_2(self, p): |
| 892 | 'fieldtype : basetype' |
| 893 | self.pdebug("p_fieldtype_2", p) |
| 894 | p[0] = p[1] |
| 895 | |
| 896 | def p_fieldtype_3(self, p): |
| 897 | 'fieldtype : collectiontype' |
| 898 | self.pdebug("p_fieldtype_3", p) |
| 899 | p[0] = p[1] |
| 900 | |
| 901 | def p_basetype_1(self, p): |
| 902 | 'basetype : STRING' |
| 903 | self.pdebug("p_basetype_1", p) |
| 904 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 905 | |
| 906 | def p_basetype_2(self, p): |
| 907 | 'basetype : BYTE' |
| 908 | self.pdebug("p_basetype_2", p) |
| 909 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 910 | |
| 911 | def p_basetype_3(self, p): |
| 912 | 'basetype : I08' |
| 913 | self.pdebug("p_basetype_3", p) |
| 914 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 915 | |
| 916 | def p_basetype_4(self, p): |
| 917 | 'basetype : U08' |
| 918 | self.pdebug("p_basetype_4", p) |
| 919 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 920 | |
| 921 | def p_basetype_5(self, p): |
| 922 | 'basetype : I16' |
| 923 | self.pdebug("p_basetype_5", p) |
| 924 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 925 | |
| 926 | def p_basetype_6(self, p): |
| 927 | 'basetype : U16' |
| 928 | self.pdebug("p_basetype_6", p) |
| 929 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 930 | |
| 931 | def p_basetype_7(self, p): |
| 932 | 'basetype : I32' |
| 933 | self.pdebug("p_basetype_7", p) |
| 934 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 935 | |
| 936 | def p_basetype_8(self, p): |
| 937 | 'basetype : U32' |
| 938 | self.pdebug("p_basetype_8", p) |
| 939 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 940 | |
| 941 | def p_basetype_9(self, p): |
| 942 | 'basetype : I64' |
| 943 | self.pdebug("p_basetype_9", p) |
| 944 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 945 | |
| 946 | def p_basetype_10(self, p): |
| 947 | 'basetype : U64' |
| 948 | self.pdebug("p_basetype_10", p) |
| 949 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 950 | |
| 951 | def p_basetype_11(self, p): |
| 952 | 'basetype : UTF8' |
| 953 | self.pdebug("p_basetype_11", p) |
| 954 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 955 | |
| 956 | def p_basetype_12(self, p): |
| 957 | 'basetype : UTF16' |
| 958 | self.pdebug("p_basetype_12", p) |
| 959 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 960 | |
| 961 | def p_basetype_13(self, p): |
| 962 | 'basetype : DOUBLE' |
| 963 | self.pdebug("p_basetype_13", p) |
| 964 | p[0] = self.program.primitiveMap[p[1].lower()] |
| 965 | |
| 966 | def p_collectiontype_1(self, p): |
| 967 | 'collectiontype : maptype' |
| 968 | self.pdebug("p_collectiontype_1", p) |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 969 | p[0] = self.program.addCollection(p[1]) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 970 | |
| 971 | def p_collectiontype_2(self, p): |
| 972 | 'collectiontype : settype' |
| 973 | self.pdebug("p_collectiontype_2", p) |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 974 | p[0] = self.program.addCollection(p[1]) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 975 | |
| 976 | def p_collectiontype_3(self, p): |
| 977 | 'collectiontype : listtype' |
| 978 | self.pdebug("p_collectiontype_3", p) |
Marc Slemko | c0e07a2 | 2006-08-09 23:34:57 +0000 | [diff] [blame] | 979 | p[0] = self.program.addCollection(p[1]) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 980 | |
| 981 | def p_maptype(self, p): |
| 982 | 'maptype : MAP LANGLE fieldtype COMMA fieldtype RANGLE' |
| 983 | self.pdebug("p_maptype", p) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 984 | p[0] = MapType(p, p[3], p[5]) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 985 | |
| 986 | def p_settype(self, p): |
| 987 | 'settype : SET LANGLE fieldtype RANGLE' |
| 988 | self.pdebug("p_settype", p) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 989 | p[0] = SetType(p, p[3]) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 990 | |
| 991 | def p_listtype(self, p): |
| 992 | 'listtype : LIST LANGLE fieldtype RANGLE' |
| 993 | self.pdebug("p_listtype", p) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 994 | p[0] = ListType(p, p[3]) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 995 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 996 | def p_empty(self, p): |
| 997 | "empty : " |
| 998 | pass |
| 999 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1000 | def p_error(self, p): |
Marc Slemko | 27340eb | 2006-08-10 20:45:55 +0000 | [diff] [blame] | 1001 | # p_error is called with an empty token if eof was encountered unexpectedly. |
| 1002 | if not p: |
| 1003 | self.errors.append(SyntaxError("Unexpected end of file")) |
| 1004 | else: |
| 1005 | self.errors.append(SyntaxError(p)) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1006 | |
| 1007 | def pdebug(self, name, p): |
| 1008 | if self.debug: |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1009 | print(name+"("+string.join(["P["+str(ix)+"]<<"+str(p[ix])+">>" for ix in range(len(p))], ", ")+")") |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1010 | |
| 1011 | def __init__(self, **kw): |
| 1012 | self.debug = kw.get('debug', 0) |
| 1013 | self.names = { } |
| 1014 | self.program = Program() |
| 1015 | self.errors = [] |
| 1016 | |
| 1017 | try: |
| 1018 | modname = os.path.split(os.path.splitext(__file__)[0])[1] + "_" + self.__class__.__name__ |
| 1019 | except: |
| 1020 | modname = "parser"+"_"+self.__class__.__name__ |
| 1021 | self.debugfile = modname + ".dbg" |
| 1022 | self.tabmodule = modname + "_" + "parsetab" |
| 1023 | #print self.debugfile, self.tabmodule |
| 1024 | |
| 1025 | # Build the lexer and parser |
| 1026 | lex.lex(module=self, debug=self.debug) |
| 1027 | yacc.yacc(module=self, |
| 1028 | debug=self.debug, |
| 1029 | debugfile=self.debugfile, |
| 1030 | tabmodule=self.tabmodule) |
| 1031 | |
| 1032 | def parsestring(self, s, filename=""): |
| 1033 | yacc.parse(s) |
| 1034 | |
| 1035 | if len(self.errors) == 0: |
| 1036 | try: |
| 1037 | self.program.validate() |
| 1038 | except ErrorException, e: |
| 1039 | self.errors+= e.errors |
| 1040 | |
| 1041 | if len(self.errors): |
| 1042 | for error in self.errors: |
| 1043 | print(filename+":"+str(error)) |
| 1044 | |
| 1045 | def parse(self, filename, doPickle=True): |
| 1046 | |
| 1047 | f = file(filename, "r") |
| 1048 | |
| 1049 | self.parsestring(f.read(), filename) |
| 1050 | |
| 1051 | if len(self.errors) == 0 and doPickle: |
| 1052 | |
| 1053 | outf = file(os.path.splitext(filename)[0]+".thyc", "w") |
| 1054 | |
| 1055 | pickle.dump(self.program, outf) |