All pastes #382568 Raw Edit

Cody Brocious

public python v1 · immutable
·published 2007-03-05 19:14 UTC
rendered paste body
import sysfrom 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)?')hlslOperand = hlslIdentifier + Optional((Suppress('.') + hlslIdentifier) | (Suppress('[') + hlslNumber + Suppress(']')))hlslFile = Forward()hlslStructDefinition = Forward()hlslStructMember = Group(hlslIdentifier + hlslIdentifier + Suppress(':') + hlslIdentifier + Suppress(';'))hlslStructDefinition << (Suppress('struct') + hlslIdentifier + Suppress('{') + ZeroOrMore(hlslStructMember) + Suppress('};'))hlslFunctionArgs = Group(hlslIdentifier + hlslIdentifier + Optional(Suppress(':') +        hlslIdentifier))hlslFunctionDefinition = (hlslIdentifier + hlslIdentifier + Suppress('(') +        Optional(delimitedList(hlslFunctionArgs)) + Suppress(')') + Optional(Suppress(':') +        hlslIdentifier))hlslExpression = Forward()hlslStatement = Forward()hlslLine = hlslStatement + Suppress(';')hlslPass = Keyword('pass') + hlslIdentifier + Suppress('{') + Group(ZeroOrMore(hlslLine)) + Suppress('}')hlslTechnique = Group(Keyword('technique10') + hlslIdentifier + Suppress('{') + Group(ZeroOrMore(hlslPass)) + Suppress('}'))hlslVariableDecl = hlslIdentifier + hlslIdentifier + Optional(Suppress('[') + hlslNumber + Suppress(']'))hlslAssignmentOp = Optional(Regex(r'[\-+/*%\^&|]')) + Suppress('=')hlslAssignment = (hlslOperand | hlslVariableDecl | hlslIdentifier) + hlslAssignmentOp + hlslExpressionhlslFunctionCall = hlslIdentifier + Suppress('(') + Group(Optional(delimitedList(hlslExpression))) + Suppress(')')hlslReturnStatement = Keyword("return") + hlslExpressionhlslCast = Suppress('(') + hlslIdentifier + Suppress(')')hlslExpression << Optional(hlslCast) + (hlslAssignment | hlslFunctionCall | hlslIdentifier | hlslNumber | hlslOperand)hlslStatement << Group(hlslReturnStatement | hlslExpression)hlslDeclStmt = (hlslVariableDecl | hlslAssignment) + Suppress(';')hlslFunctionBody = Group(ZeroOrMore(hlslLine))hlslFunction = Group(hlslFunctionDefinition + Suppress('{') + hlslFunctionBody +        Suppress('}'))hlslBlock = hlslFunction | hlslTechnique | hlslDeclStmt | hlslStructDefinition;hlslFile << (ZeroOrMore(hlslBlock) + StringEnd())hlslFile.ignore(hlslComment)try:    print hlslFile.parseString(code)except ParseException, pe:    print "Parsing failed"    print pe.markInputline()    print pe.msg