import sys from pyparsing import * ParserElement.enablePackrat() filename = sys.argv[1] code = file(filename).read() hlslFile = Forward() hlslStructDefinition = Forward() hlslExpression = Forward() hlslNoArithmeticExpr = Forward() hlslNoComparisonExpr = Forward() hlslStatement = Forward() hlslFunctionBody = Forward() hlslFunctionLine = Forward() hlslLine = hlslStatement + Suppress(';') hlslComment = cppStyleComment hlslIdentifier = Regex(r'[a-zA-Z_][a-zA-Z0-9_]*') hlslHexNumber = Regex(r'0x[0-9a-fA-F]+') hlslNumber = hlslHexNumber ^ Regex(r'-?[0-9]+(\.[0-9]*f?)?') hlslSubscript = Suppress('[') + hlslExpression + Suppress(']') hlslOperand = hlslIdentifier + ZeroOrMore((Suppress('.') + hlslIdentifier) ^ hlslSubscript) hlslVariableDecl = Group(hlslIdentifier + hlslIdentifier + Optional(hlslSubscript)) hlslAssignmentOp = Group(Optional(Regex(r'[\-+/*%\^&^]')) + Suppress('=')) hlslAssignment = (hlslVariableDecl ^ hlslOperand ^ hlslIdentifier) + hlslAssignmentOp + hlslExpression hlslStructMember = Group(hlslIdentifier + hlslIdentifier + Suppress(':') + hlslIdentifier + Suppress(';')) hlslStructDefinition << (Keyword('struct') + hlslIdentifier + Suppress('{') + ZeroOrMore(hlslStructMember) + Suppress('}') + Suppress(';')) hlslFunctionArgs = \ Group( OneOrMore(hlslIdentifier) + Optional(hlslSubscript) + Optional(Suppress('<') + hlslIdentifier + Suppress('>')) + Optional(hlslIdentifier) + Optional(Suppress(':') + hlslIdentifier) ) hlslMaxVertex = Group(Suppress('[') + Keyword('maxvertexcount') + Suppress('(') + hlslNumber + Suppress(')') + Suppress(']')) hlslFunctionDefinition = \ ( Optional(hlslMaxVertex) + OneOrMore(hlslIdentifier) + Suppress('(') + Optional(delimitedList(hlslFunctionArgs)) + Suppress(')') + Optional(Suppress(':') + hlslIdentifier) ) hlslStateDecl = \ Group( hlslIdentifier + Optional(hlslSubscript) + Suppress('=') + (hlslIdentifier ^ hlslNumber) + Suppress(';') ) hlslSamplerState = \ Group( Keyword('SamplerState') + hlslIdentifier + Suppress('{') + ZeroOrMore(hlslStateDecl) + Suppress('}') + Suppress(';') ) hlslBlendState = \ Group( Keyword('BlendState') + hlslIdentifier + Suppress('{') + ZeroOrMore(hlslStateDecl) + Suppress('}') + Suppress(';') ) hlslDepthStencilState = \ Group( Keyword('DepthStencilState') + hlslIdentifier + Suppress('{') + ZeroOrMore(hlslStateDecl) + Suppress('}') + Suppress(';') ) hlslCBuffer = \ Group( Keyword('cbuffer') + hlslIdentifier + Suppress('{') + ZeroOrMore((hlslAssignment ^ hlslVariableDecl) + Suppress(';')) + Suppress('}') + Suppress(';') ) hlslForLoop = \ Group( Keyword('for') + Suppress('(') + hlslExpression + Suppress(';') + hlslExpression + Suppress(';') + hlslExpression + Suppress(')') + Suppress('{') + hlslFunctionBody + Suppress('}') ) hlslIfStatement = \ Group( Optional(Keyword('branch') ^ Keyword('flatten')) + Keyword('if') + Suppress('(') + hlslExpression + Suppress(')') + ((Suppress('{') + hlslFunctionBody + Suppress('}')) ^ hlslFunctionLine) + Optional( Suppress('else') + ((Suppress('{') + hlslFunctionBody + Suppress('}')) ^ hlslFunctionLine) ) ) hlslPass = Keyword('pass') + hlslIdentifier + Suppress('{') + Group(ZeroOrMore(hlslLine)) + Suppress('}') hlslTechnique = Group(Keyword('technique10') + hlslIdentifier + Suppress('{') + Group(ZeroOrMore(hlslPass)) + Suppress('}')) hlslFunctionCall = hlslOperand + Suppress('(') + Group(Optional(delimitedList(hlslExpression))) + Suppress(')') hlslReturnStatement = Keyword('return') + hlslExpression hlslArithmetic = hlslNoArithmeticExpr + Regex(r'[\-+/*%\^&^]') + hlslExpression hlslIncDec = (hlslOperand ^ hlslIdentifier) + (Literal('--') ^ Literal('++')) hlslNegative = Literal('-') + hlslExpression hlslUnary = hlslIncDec ^ hlslNegative hlslComparison = hlslNoComparisonExpr + Regex(r'(<=^<^>=^>^==^!=)') + hlslExpression hlslCast = Suppress('(') + hlslIdentifier + Suppress(')') hlslGroup = Suppress('(') + hlslExpression + Suppress(')') hlslNoArithmeticExpr << \ Group( Optional(hlslCast) + ( hlslGroup ^ hlslAssignment ^ hlslFunctionCall ^ hlslOperand ^ hlslIdentifier ^ hlslNumber ^ hlslUnary ) ) hlslNoComparisonExpr << \ Group( Optional(hlslCast) + ( hlslGroup ^ hlslAssignment ^ hlslFunctionCall ^ hlslOperand ^ hlslIdentifier ^ hlslNumber ^ hlslArithmetic ^ hlslUnary ) ) hlslExpression << \ Group( Optional(hlslCast) + ( hlslGroup ^ hlslAssignment ^ hlslFunctionCall ^ hlslOperand ^ hlslIdentifier ^ hlslNumber ^ hlslArithmetic ^ hlslUnary ^ hlslComparison ) ) hlslStatement << Group(hlslReturnStatement | hlslVariableDecl | hlslExpression) hlslFunctionLine << hlslLine ^ hlslIfStatement ^ hlslForLoop hlslFunctionBody << Group(ZeroOrMore(hlslFunctionLine)) hlslFunction = Group(hlslFunctionDefinition + Suppress('{') + hlslFunctionBody + Suppress('}')) hlslAssignStmt = Group(hlslAssignment + Suppress(';')) hlslBlock = \ ( hlslFunction ^ hlslTechnique ^ hlslAssignStmt ^ hlslStructDefinition ^ hlslSamplerState ^ hlslDepthStencilState ^ hlslBlendState ^ hlslCBuffer ) hlslFile << (ZeroOrMore(hlslBlock) + StringEnd()) hlslFile.ignore(hlslComment) hlslFile.validate() try: print hlslFile.parseString(code) except ParseException, pe: print "Parsing failed" print pe.markInputline() print pe.msg