Thrift php generator in python.

Cleaned up parser and cpp generator
    


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664765 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Marc Slemko 2006-08-23 02:15:31 +00:00
parent 056f9ba9a0
commit c6936407d1
6 changed files with 1051 additions and 30 deletions

View File

@ -7,7 +7,7 @@ setup(name='Thrift',
author_email= ['mcslee@facebook.com', 'marc@facebook.com'], author_email= ['mcslee@facebook.com', 'marc@facebook.com'],
url='http://code.facebook.com/thrift', url='http://code.facebook.com/thrift',
package_dir={'thrift' : 'src'}, package_dir={'thrift' : 'src'},
py_modules = ['thrift.parser', 'thrift.cpp_generator', 'thrift.generator'], py_modules = ['thrift.parser', 'thrift.cpp_generator', 'thrift.generator', 'thrift.php_generator'],
scripts = ['src/thrift'] scripts = ['src/thrift']
) )

View File

@ -1 +1 @@
__all__ = ['parser', 'generator', 'cpp_generator'] __all__ = ['parser', 'generator', 'cpp_generator', 'php_generator']

View File

@ -226,16 +226,16 @@ def toStructDefinition(struct):
# Field declarations # Field declarations
result+= string.join([" "+toCTypeDeclaration(field)+";\n" for field in struct.fieldList if toCanonicalType(field.type) != VOID_TYPE], "") result+= string.join([" "+toCTypeDeclaration(field)+";\n" for field in struct.fieldList if not isVoidType(field.type)], "")
# is-field-set struct and ctor # is-field-set struct and ctor
ctorValues = string.join([field.name+"(false)" for field in struct.fieldList if toCanonicalType(field.type) != VOID_TYPE], ", ") ctorValues = string.join([field.name+"(false)" for field in struct.fieldList if not isVoidType(field.type)], ", ")
if len(ctorValues) > 0: if len(ctorValues) > 0:
result+= " struct __isset {\n" result+= " struct __isset {\n"
result+= " __isset() : "+ctorValues+" {}\n" result+= " __isset() : "+ctorValues+" {}\n"
result+= string.join([" bool "+field.name+";\n" for field in struct.fieldList if toCanonicalType(field.type) != VOID_TYPE], "") result+= string.join([" bool "+field.name+";\n" for field in struct.fieldList if not isVoidType(field.type)], "")
result+= " } __isset;\n" result+= " } __isset;\n"
# bring it on home # bring it on home
@ -460,7 +460,7 @@ ${argsToStruct};
_iprot->readMessageEnd(_itrans); _iprot->readMessageEnd(_itrans);
${returnResult} ${returnResult}
throw """+CPP_EXCEPTION+"""(\"${function} failed: unknown result"); throw """+CPP_EXCEPTION+"""(\"${function} failed: unknown result\");
} }
""") """)
@ -493,7 +493,7 @@ def toServerFunctionDefinition(servicePrefix, function, debugp=None):
resultStructWriter = toWriterCall("__result", function.resultStruct, "_oprot") resultStructWriter = toWriterCall("__result", function.resultStruct, "_oprot")
if toCanonicalType(function.returnType()) != VOID_TYPE: if not isVoidType(function.returnType()):
functionCallPrefix= "__result.success = " functionCallPrefix= "__result.success = "
functionCallSuffix = "\n __result.__isset.success = true;" functionCallSuffix = "\n __result.__isset.success = true;"
else: else:
@ -548,7 +548,7 @@ def toClientDeclaration(service, debugp=None):
def toClientFunctionDefinition(servicePrefix, function, debugp=None): def toClientFunctionDefinition(servicePrefix, function, debugp=None):
"""Converts a thrift service method declaration to a client stub implementation""" """Converts a thrift service method declaration to a client stub implementation"""
isVoid = toCanonicalType(function.returnType()) == VOID_TYPE isVoid = isVoidType(function.returnType())
returnDeclaration = toCTypeDeclaration(function.returnType()) returnDeclaration = toCTypeDeclaration(function.returnType())
@ -1044,7 +1044,7 @@ def toStructReaderDefinition(struct):
fieldSwitch="" fieldSwitch=""
for field in fieldList: for field in fieldList:
if toCanonicalType(field.type) == VOID_TYPE: if isVoidType(field.type):
continue; continue;
fieldSwitch+= " case "+str(field.id)+": " fieldSwitch+= " case "+str(field.id)+": "
fieldSwitch+= toReaderCall("value."+field.name, field.type)+"; value.__isset."+field.name+" = true; break;\n" fieldSwitch+= toReaderCall("value."+field.name, field.type)+"; value.__isset."+field.name+" = true; break;\n"
@ -1063,7 +1063,7 @@ def toStructWriterDefinition(struct):
type=toWireType(toCanonicalType(field.type)), type=toWireType(toCanonicalType(field.type)),
id=field.id, id=field.id,
fieldWriterCall=toWriterCall("value."+field.name, field.type))+";" fieldWriterCall=toWriterCall("value."+field.name, field.type))+";"
for field in struct.fieldList if toCanonicalType(field.type) != VOID_TYPE for field in struct.fieldList if not isVoidType(field.type)
] ]
fieldWriterCalls = " "+string.join(fieldWriterCalls, "\n ") fieldWriterCalls = " "+string.join(fieldWriterCalls, "\n ")
@ -1099,7 +1099,7 @@ def toResultStructWriterDefinition(struct):
type=toWireType(toCanonicalType(field.type)), type=toWireType(toCanonicalType(field.type)),
id=field.id, id=field.id,
fieldWriterCall=toWriterCall("value."+field.name, field.type))+";}" fieldWriterCall=toWriterCall("value."+field.name, field.type))+";}"
for field in struct.fieldList if toCanonicalType(field.type) != VOID_TYPE] for field in struct.fieldList if not isVoidType(field.type)]
fieldWriterCalls = " "+string.join(fieldWriterCalls, "\n else ") fieldWriterCalls = " "+string.join(fieldWriterCalls, "\n else ")

View File

@ -1,3 +1,3 @@
class Generator(object): class Generator(object):
def __call__(self, program, filename, gendir): def __call__(self, program, filename, gendir, debugp=None):
raise Exception, "Not implemented" raise Exception, "Not implemented"

View File

@ -50,8 +50,8 @@ class SymanticsError(Error):
return str(self.start)+": error: "+self.message return str(self.start)+": error: "+self.message
class ErrorException(Exception): class ErrorException(Exception):
def __init__(self, errors=None): def __init__(self, errors=None):
Exception.__init__(self)
self.errors = errors self.errors = errors
class Definition(object): class Definition(object):
@ -82,14 +82,18 @@ class Identifier(Definition):
result+="="+str(self.id) result+="="+str(self.id)
return result return result
def toCanonicalType(ttype): def toCanonicalType(ttype):
if isinstance(ttype, TypedefType): if isinstance(ttype, TypedefType):
return toCanonicalType(ttype.definitionType) return toCanonicalType(ttype.definitionType)
else: else:
return ttype return ttype
def isVoidType(ttype):
"""Is canonnical type of the specified type void?"""
return toCanonicalType(ttype) == VOID_TYPE
def isComparableType(ttype): def isComparableType(ttype):
"""Is the type one that is implicitly comparable and thus is suitable for use in C++ maps and sets and java Sets and Maps?"""
ttype = toCanonicalType(ttype) ttype = toCanonicalType(ttype)
return isinstance(ttype, PrimitiveType) or isinstance(ttype, EnumType) return isinstance(ttype, PrimitiveType) or isinstance(ttype, EnumType)
@ -227,15 +231,17 @@ class EnumType(Definition):
def assignId(enumDef, currentId, ids): def assignId(enumDef, currentId, ids):
'Finds the next available id number for an enum definition' 'Finds the next available id number for an enum definition'
id= currentId + 1 eid= currentId + 1
while id in ids: while eid in ids:
id += 1 eid += 1
enumDef.id = id enumDef.id = eid
ids[enumDef.id] = enumDef ids[enumDef.id] = enumDef
return eid
# assign ids for all enum defs with unspecified ids # assign ids for all enum defs with unspecified ids
currentId = 0 currentId = 0
@ -302,16 +308,16 @@ def validateFieldList(fieldList):
def assignId(field, currentId, ids): def assignId(field, currentId, ids):
'Finds the next available id number for a field' 'Finds the next available id number for a field'
id = currentId - 1 fid = currentId - 1
while id in ids: while fid in ids:
id-= 1 fid-= 1
field.id = id field.id = fid
ids[field.id] = field ids[field.id] = field
return id return fid
# assign ids for all fields with unspecified ids # assign ids for all fields with unspecified ids
@ -340,7 +346,7 @@ class ExceptionType(StructType):
class Function(Definition): class Function(Definition):
def __init__(self, symbols, name, resultStruct, argsStruct, ): def __init__(self, symbols, name, resultStruct, argsStruct):
Definition.__init__(self, symbols, name) Definition.__init__(self, symbols, name)
self.resultStruct = resultStruct self.resultStruct = resultStruct
self.argsStruct = argsStruct self.argsStruct = argsStruct
@ -373,7 +379,7 @@ class Service(Definition):
functionNames = {} functionNames = {}
for function in self.functionList: for function in self.functionList:
if function.name in functionNames: if function.name in functionNames:
oldFunction = functionName[function.name] oldFunction = functionNames[function.name]
errors.append(SymanticsError(function, "function "+function.name+" already defined at "+str(oldFunction.start))) errors.append(SymanticsError(function, "function "+function.name+" already defined at "+str(oldFunction.start)))
if len(errors): if len(errors):
@ -470,16 +476,16 @@ class Program(object):
else: else:
raise ErrorException([SymanticsError(parent, "unknown symbol \""+str(symbol)+"\"")]) raise ErrorException([SymanticsError(parent, "unknown symbol \""+str(symbol)+"\"")])
for map in (self.primitiveMap, self.collectionMap, self.typedefMap, self.enumMap, self.structMap): for tmap in (self.primitiveMap, self.collectionMap, self.typedefMap, self.enumMap, self.structMap):
if typeName in map: if typeName in tmap:
return map[typeName] return tmap[typeName]
raise ErrorException([SymanticsError(parent, "\""+typeName+"\" is not defined.")]) raise ErrorException([SymanticsError(parent, "\""+typeName+"\" is not defined.")])
def hasType(self, parent, symbol): def hasType(self, parent, symbol):
""" Determine if a type definition exists for the symbol""" """ Determine if a type definition exists for the symbol"""
return self.getType(parent, symbol) == True return self.getType(parent, symbol) != None
def validate(self): def validate(self):

File diff suppressed because it is too large Load Diff