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'],
url='http://code.facebook.com/thrift',
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']
)

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
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
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:
result+= " struct __isset {\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"
# bring it on home
@ -460,7 +460,7 @@ ${argsToStruct};
_iprot->readMessageEnd(_itrans);
${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")
if toCanonicalType(function.returnType()) != VOID_TYPE:
if not isVoidType(function.returnType()):
functionCallPrefix= "__result.success = "
functionCallSuffix = "\n __result.__isset.success = true;"
else:
@ -548,7 +548,7 @@ def toClientDeclaration(service, debugp=None):
def toClientFunctionDefinition(servicePrefix, function, debugp=None):
"""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())
@ -1044,7 +1044,7 @@ def toStructReaderDefinition(struct):
fieldSwitch=""
for field in fieldList:
if toCanonicalType(field.type) == VOID_TYPE:
if isVoidType(field.type):
continue;
fieldSwitch+= " case "+str(field.id)+": "
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)),
id=field.id,
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 ")
@ -1099,7 +1099,7 @@ def toResultStructWriterDefinition(struct):
type=toWireType(toCanonicalType(field.type)),
id=field.id,
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 ")

View File

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

View File

@ -50,8 +50,8 @@ class SymanticsError(Error):
return str(self.start)+": error: "+self.message
class ErrorException(Exception):
def __init__(self, errors=None):
Exception.__init__(self)
self.errors = errors
class Definition(object):
@ -82,14 +82,18 @@ class Identifier(Definition):
result+="="+str(self.id)
return result
def toCanonicalType(ttype):
if isinstance(ttype, TypedefType):
return toCanonicalType(ttype.definitionType)
else:
return ttype
def isVoidType(ttype):
"""Is canonnical type of the specified type void?"""
return toCanonicalType(ttype) == VOID_TYPE
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)
return isinstance(ttype, PrimitiveType) or isinstance(ttype, EnumType)
@ -227,14 +231,16 @@ class EnumType(Definition):
def assignId(enumDef, currentId, ids):
'Finds the next available id number for an enum definition'
id= currentId + 1
eid= currentId + 1
while id in ids:
id += 1
while eid in ids:
eid += 1
enumDef.id = id
enumDef.id = eid
ids[enumDef.id] = enumDef
return eid
# assign ids for all enum defs with unspecified ids
@ -302,16 +308,16 @@ def validateFieldList(fieldList):
def assignId(field, currentId, ids):
'Finds the next available id number for a field'
id = currentId - 1
fid = currentId - 1
while id in ids:
id-= 1
while fid in ids:
fid-= 1
field.id = id
field.id = fid
ids[field.id] = field
return id
return fid
# assign ids for all fields with unspecified ids
@ -340,7 +346,7 @@ class ExceptionType(StructType):
class Function(Definition):
def __init__(self, symbols, name, resultStruct, argsStruct, ):
def __init__(self, symbols, name, resultStruct, argsStruct):
Definition.__init__(self, symbols, name)
self.resultStruct = resultStruct
self.argsStruct = argsStruct
@ -373,7 +379,7 @@ class Service(Definition):
functionNames = {}
for function in self.functionList:
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)))
if len(errors):
@ -384,7 +390,7 @@ class Service(Definition):
class Program(object):
def __init__(self, symbols=None, name="",
def __init__(self, symbols=None, name="",
namespace="",
definitions=None,
serviceMap=None,
@ -470,16 +476,16 @@ class Program(object):
else:
raise ErrorException([SymanticsError(parent, "unknown symbol \""+str(symbol)+"\"")])
for map in (self.primitiveMap, self.collectionMap, self.typedefMap, self.enumMap, self.structMap):
if typeName in map:
return map[typeName]
for tmap in (self.primitiveMap, self.collectionMap, self.typedefMap, self.enumMap, self.structMap):
if typeName in tmap:
return tmap[typeName]
raise ErrorException([SymanticsError(parent, "\""+typeName+"\" is not defined.")])
def hasType(self, parent, 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):

File diff suppressed because it is too large Load Diff