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) hlslExpression = Forward() hlslStatement = Forward() hlslLine = hlslStatement + Suppress(';') hlslPass = Keyword('pass') + hlslIdentifier + Suppress('{') + ZeroOrMore(hlslLine) + Suppress('}') hlslTechnique = Keyword('technique10') + hlslIdentifier + Suppress('{') + ZeroOrMore(hlslPass) + Suppress('}') hlslAssignment = hlslIdentifier + Keyword('=') + hlslExpression hlslFunctionCall = hlslIdentifier + Suppress('(') + delimitedList(hlslExpression) + Suppress(')') hlslReturnStatement = Keyword("return") + hlslExpression hlslExpression << (hlslAssignment | hlslFunctionCall | hlslIdentifier | hlslNumber) hlslStatement << (hlslReturnStatement | hlslAssignment | hlslFunctionCall) hlslFunctionBody = ZeroOrMore(hlslLine) hlslFunction = (hlslFunctionDefinition + Suppress('{') + hlslFunctionBody + Suppress('}')) hlslBlock = hlslFunction | hlslTechnique hlslFile << (ZeroOrMore(hlslBlock) + StringEnd()) hlslFile.ignore(hlslComment) try: print hlslFile.parseString(code) except ParseException, pe: print "Parsing failed" print pe