예제 #1
0
    def compile(self, grammar):
        """
        Produce an object capable of parsing via this grammar.

        @param grammar: A string containing an OMeta grammar.
        """
        from pymeta.grammar import OMetaGrammar, NullOptimizer
        g = OMetaGrammar(dedent(grammar))
        tree = g.parseGrammar('TestGrammar', TreeBuilder)
        opt = NullOptimizer([tree])
        opt.builder = TreeBuilder("TestGrammar", opt)
        tree, err = opt.apply("grammar")
        grammarClass = moduleFromGrammar(tree, 'TestGrammar', OMetaBase, {})
        return HandyWrapper(grammarClass)
예제 #2
0
    def compile(self, grammar):
        """
        Produce an object capable of parsing via this grammar.

        @param grammar: A string containing an OMeta grammar.
        """
        from pymeta.grammar import OMetaGrammar, NullOptimizer
        g = OMetaGrammar(grammar)
        tree  = g.parseGrammar('TestGrammar', TreeBuilder)
        opt = NullOptimizer([tree])
        opt.builder = TreeBuilder("TestGrammar", opt)
        tree, err = opt.apply("grammar")
        grammarClass = moduleFromGrammar(tree, 'TestGrammar', OMetaBase, {})
        return HandyWrapper(grammarClass)
예제 #3
0
    def compile(self, grammar):
        """
        Produce an object capable of parsing via this grammar.

        @param grammar: A string containing an OMeta grammar.
        """
        from pymeta.grammar import OMetaGrammar, NullOptimizer
        g = OMetaGrammar(grammar)
        tree = g.parseGrammar('TestGrammar', TreeBuilder)
        opt = NullOptimizer([tree])
        opt.builder = AstBuilder("<grammar>", opt)
        methodDict = opt.apply("grammar")
        grammarClass = type("<grammar>", (OMetaBase,), methodDict)
        return HandyWrapper(grammarClass)
예제 #4
0
    def compile(self, grammar):
        """
        Produce an object capable of parsing via this grammar.

        @param grammar: A string containing an OMeta grammar.
        """
        from pymeta.grammar import OMetaGrammar, NullOptimizer
        g = OMetaGrammar(grammar)
        tree = g.parseGrammar('TestGrammar', TreeBuilder)
        opt = NullOptimizer([tree])
        opt.builder = AstBuilder("<grammar>", opt)
        methodDict = opt.apply("grammar")
        grammarClass = type("<grammar>", (OMetaBase, ), methodDict)
        return HandyWrapper(grammarClass)
예제 #5
0
operator = token("+") | token("-")
"""

Grammar = OMeta.makeGrammar(grammar, {})
parser = Grammar("5 + 7")
ast, err =  parser.apply("start")

print "AST: " + str(ast)

## parsing list with `ast` as input...

grammar_ast = """
start = ['+' number:a number:b] -> a + b
      | ['-' number:a number:b] -> a - b
number = :a
"""

ASTGrammar = OMetaGrammar(grammar_ast)
t = ASTGrammar.parseGrammar('SomeGrammarName', TreeBuilder)
ASTParser = moduleFromGrammar(t, 'SomeGrammarName', OMetaBase, {})
parser = ASTParser([ast])
res, err = parser.apply("start")
print "result: " + str(res)

### formating parse error
try:
    parser = Grammar("1 + a")
    ast, err =  parser.apply("start")
except Exception as err:
    print err.formatError(''.join(parser.input.data))