import sys from pyparsing import * ParserElement.enablePackrat() def DelimitedList(expr, delim=',', combine=False): return delimitedList(expr, delim=delim, combine=combine) + Optional(Suppress(delim)) filename = sys.argv[1] code = file(filename).read() hlslFile = Forward() hlslStructDefinition = Forward() hlslExpression = Forward() hlslNoArithmeticExpr = Forward() hlslNoComparisonExpr = Forward() hlslNoLogicalExpr = Forward() hlslNoTernaryExpr = Forward() hlslStatement = Forward() hlslFunctionBody = Forward() hlslFunctionLine = Forward() hlslLine = Forward() 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?)?') hlslString = QuotedString(quoteChar='"', escChar='\\', multiline=True) hlslSubscript = Suppress('[') + hlslExpression + Suppress(']') hlslObjectReference = Suppress('.') + hlslExpression hlslOperand = Group(hlslIdentifier + ZeroOrMore(hlslObjectReference ^ hlslSubscript)) hlslArray = Group(Suppress('{') + DelimitedList(hlslExpression) + Suppress('}')) hlslGroup = Suppress('(') + hlslExpression + Suppress(')') hlslVariableDecl = \ Group( OneOrMore(hlslIdentifier) + Optional(Suppress('<') + hlslIdentifier + Suppress('>')) + hlslIdentifier + Optional(hlslSubscript) + Optional(Group(Suppress(':') + hlslIdentifier)) ) hlslAssignmentOp = Group(Optional(Regex(r'[\-+/*%\^&^]')) + Suppress('=')) hlslAssignment = (hlslVariableDecl ^ hlslOperand ^ hlslIdentifier) + hlslAssignmentOp + hlslExpression hlslStructMember = \ Group( OneOrMore(hlslIdentifier) + hlslIdentifier + ZeroOrMore(hlslSubscript) + Optional(Suppress(':') + hlslIdentifier) + Optional(Suppress('=') + hlslExpression) + 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(';') ) hlslRasterizerState = \ Group( Keyword('RasterizerState') + hlslIdentifier + Suppress('{') + ZeroOrMore(hlslStateDecl) + Suppress('}') + Suppress(';') ) hlslCBuffer = \ Group( Keyword('cbuffer') + hlslIdentifier + Suppress('{') + ZeroOrMore((hlslAssignment ^ hlslVariableDecl) + Suppress(';')) + Suppress('}') + Suppress(';') ) hlslForLoop = \ Group( Optional(Suppress('[') + Keyword('unroll') + Suppress(']')) + Keyword('for') + Suppress('(') + hlslExpression + Suppress(';') + hlslExpression + Suppress(';') + hlslExpression + Suppress(')') + Optional( ( Suppress('{') + hlslFunctionBody + Suppress('}') ) | hlslFunctionLine ) ) hlslIfStatement = \ Group( Optional(Keyword('branch') ^ Keyword('flatten')) + Keyword('if') + Suppress('(') + hlslExpression + Suppress(')') + (hlslFunctionLine | (Suppress('{') + hlslFunctionBody + Suppress('}'))) + 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('}') + Optional(Suppress(';'))) hlslFunctionCall = hlslOperand + Suppress('(') + Group(Optional(DelimitedList(hlslExpression))) + Suppress(')') hlslReturnStatement = Keyword('return') + Optional(hlslExpression) hlslArithmeticOp = Regex(r'[\-+/*%\^&|]') hlslArithmetic = hlslNoArithmeticExpr + hlslArithmeticOp + hlslExpression hlslPreIncDec = (Literal('--') ^ Literal('++')) + (hlslOperand ^ hlslIdentifier) hlslPostIncDec = (hlslOperand ^ hlslIdentifier) + (Literal('--') ^ Literal('++')) hlslNegative = Literal('-') + hlslExpression hlslNot = Literal('!') + hlslExpression hlslUnary = hlslPreIncDec ^ hlslPostIncDec ^ hlslNegative ^ hlslNot hlslComparison = hlslNoComparisonExpr + Regex(r'(<=|<|>=|>|==|!=)') + hlslExpression hlslLogicalAndOr = hlslNoLogicalExpr + Regex(r'(\|\||&&)') + hlslExpression hlslTernary = hlslNoTernaryExpr + Suppress('?') + hlslExpression + ':' + hlslExpression hlslCast = Suppress('(') + hlslIdentifier + Suppress(')') + FollowedBy(hlslExpression) hlslNoArithmeticExpr << \ Group( Optional(hlslCast) + ( hlslAssignment ^ hlslFunctionCall ^ hlslOperand ^ hlslIdentifier ^ hlslNumber ^ hlslUnary ^ hlslArray ^ hlslGroup ^ hlslString ) ) hlslNoComparisonExpr << \ Group( Optional(hlslCast) + ( hlslAssignment ^ hlslFunctionCall ^ hlslOperand ^ hlslIdentifier ^ hlslNumber ^ hlslArithmetic ^ hlslUnary ^ hlslArray ^ hlslGroup ^ hlslString ) ) hlslNoLogicalExpr << \ Group( Optional(hlslCast) + ( hlslAssignment ^ hlslFunctionCall ^ hlslOperand ^ hlslIdentifier ^ hlslNumber ^ hlslArithmetic ^ hlslUnary ^ hlslComparison ^ hlslArray ^ hlslGroup ^ hlslString ) ) hlslNoTernaryExpr << \ Group( Optional(hlslCast) + ( hlslAssignment ^ hlslFunctionCall ^ hlslOperand ^ hlslIdentifier ^ hlslNumber ^ hlslArithmetic ^ hlslUnary ^ hlslComparison ^ hlslArray ^ hlslGroup ^ hlslLogicalAndOr ^ hlslString ) ) hlslExpression << \ Group( Optional(hlslCast) + ( hlslAssignment ^ hlslFunctionCall ^ hlslOperand ^ hlslIdentifier ^ hlslNumber ^ hlslArithmetic ^ hlslUnary ^ hlslComparison ^ hlslArray ^ hlslGroup ^ hlslLogicalAndOr ^ hlslTernary ^ hlslString ) ) hlslStatement << Group(hlslReturnStatement ^ hlslVariableDecl ^ hlslExpression) hlslLine << (hlslStatement + Suppress(';')) hlslBlock = Group(Suppress('{') + hlslFunctionBody + Suppress('}')) hlslFunctionLine << (hlslLine | hlslIfStatement | hlslForLoop | hlslBlock) hlslFunctionBody << Group(ZeroOrMore(hlslFunctionLine)) hlslFunction = Group(hlslFunctionDefinition + Suppress('{') + hlslFunctionBody + Suppress('}')) hlslAssignStmt = Group((hlslAssignment | hlslVariableDecl) + Suppress(';')) hlslBlock = \ ( hlslFunction ^ hlslTechnique ^ hlslAssignStmt ^ hlslStructDefinition ^ hlslSamplerState ^ hlslBlendState ^ hlslDepthStencilState ^ hlslRasterizerState ^ 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.lineno, pe.column, pe.msg