All pastes #391387 Raw Edit

Cody Brocious

public python v1 · immutable
·published 2007-03-12 02:57 UTC
rendered paste body
import sysfrom 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 + hlslExpressionhlslStructMember = 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') + hlslExpressionhlslArithmetic = hlslNoArithmeticExpr + Regex(r'[\-+/*%\^&^]') + hlslExpressionhlslIncDec = (hlslOperand ^ hlslIdentifier) + (Literal('--') ^ Literal('++'))hlslNegative = Literal('-') + hlslExpressionhlslUnary = hlslIncDec ^ hlslNegativehlslComparison = hlslNoComparisonExpr + Regex(r'(<=^<^>=^>^==^!=)') + hlslExpressionhlslCast = 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 ^ hlslForLoophlslFunctionBody << 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