import sys from pyparsing import * filename = sys.argv[1] code = file(filename).read() hlslComment = cppStyleComment hlslIdentifier = Regex(r'[a-zA-Z_][a-zA-Z0-9_]*') hlslNumber = Regex(r'[0-9]+(\.[0-9]*f)?') hlslFile = Forward() hlslFunctionArgs = Group(hlslIdentifier + hlslIdentifier + Suppress(':') + hlslIdentifier) hlslFunctionDefinition = (hlslIdentifier + hlslIdentifier + Suppress('(') + delimitedList(hlslFunctionArgs) + Suppress(')') + Suppress(':') + hlslIdentifier) hlslStatement = Forward() hlslAssignment = hlslIdentifier + Keyword('=') + hlslStatement hlslFunctionCall = hlslIdentifier + Suppress('(') + delimitedList(hlslStatement) + Suppress(')') hlslReturnStatement = Keyword("return") + hlslStatement hlslStatement << (hlslReturnStatement | hlslAssignment | hlslFunctionCall | hlslIdentifier | hlslNumber) hlslLine = hlslStatement + Suppress(';') hlslFunctionBody = ZeroOrMore(hlslLine) hlslFunction = (hlslFunctionDefinition + Suppress('{') + hlslFunctionBody + Suppress('}')) hlslFile << (ZeroOrMore(hlslFunction) + StringEnd()) hlslFile.ignore(hlslComment) try: print hlslFile.parseString(code) except ParseException, pe: print "Parsing failed" print pe