All pastes #382113 Raw Edit

Untitled

public text v1 · immutable
·published 2007-03-05 09:33 UTC
rendered paste body
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('{') + Group(ZeroOrMore(hlslLine)) + Suppress('}')

hlslTechnique = Group(Keyword('technique10') + hlslIdentifier + Suppress('{') + Group(ZeroOrMore(hlslPass)) + Suppress('}'))

hlslAssignment = hlslIdentifier + Keyword('=') + hlslExpression
hlslFunctionCall = hlslIdentifier + Suppress('(') + Group(Optional(delimitedList(hlslExpression))) + Suppress(')')
hlslReturnStatement = Keyword("return") + hlslExpression

hlslExpression << (hlslAssignment | hlslFunctionCall | hlslIdentifier | hlslNumber)
hlslStatement << Group(hlslReturnStatement | hlslExpression)

hlslFunctionBody = Group(ZeroOrMore(hlslLine))

hlslFunction = Group(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.markInputline()
    print pe.msg