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

        @param grammar: A string containing an OMeta grammar.
        """
        g = self.classTested(dedent(grammar))
        tree = g.parseGrammar('TestGrammar', TreeBuilder)
        result = moduleFromGrammar(tree, 'TestGrammar', OMetaBase, {})
        return HandyWrapper(result)
Beispiel #2
0
    def compile(self, grammar):
        """
        Produce an object capable of parsing via this grammar.

        @param grammar: A string containing an OMeta grammar.
        """
        g = self.classTested(dedent(grammar))
        tree = g.parseGrammar("TestGrammar", TreeBuilder)
        result = moduleFromGrammar(tree, "TestGrammar", OMetaBase, {})
        return HandyWrapper(result)
Beispiel #3
0
    def compile(self, grammar):
        """
        Produce an object capable of parsing via this grammar.

        @param grammar: A string containing an OMeta grammar.
        """
        g = BootOMetaGrammar(grammar)
        tree = g.parseGrammar('TestGrammar', TreeBuilder)
        result = moduleFromGrammar(tree, 'TestGrammar', OMetaBase, {})
        return HandyWrapper(result)
Beispiel #4
0
    def _get_compiler(response):
        # self._compiler = OMeta.makeGrammar(compile_grammar, {'builder':self._builder})
        _builder = CodeBuilder(response)

        name = "Grammar"
        globals = {'builder':_builder}
        # g = OMeta.metagrammarClass(compile_grammar)
        # tree = g.parseGrammar(name, TreeBuilder)
        tree = ['Grammar', 'Grammar', [['Rule', 'compile', ['And', [['And', []], ['Or', [['And', [['Apply', 'prolog', 'compile', ()], ['Many', ['Apply', 'rule', 'compile', ()]], ['Python', 'builder.finish()']]]]]]]], ['Rule', 'prolog', ['And', [['And', []], ['Or', [['And', [['Exactly', 'template'], ['Python', 'builder.start()']]]]]]]], ['Rule', 'rule', ['And', [['And', []], ['Or', [['And', [['Apply', 'literal', 'rule', ()]]], ['And', [['Apply', 'expand', 'rule', ()]]], ['And', [['Apply', 'escapedexpand', 'rule', ()]]], ['And', [['Apply', 'comment', 'rule', ()]]], ['And', [['Apply', 'block', 'rule', ()]]], ['And', [['Apply', 'invertedblock', 'rule', ()]]], ['And', [['Apply', 'partial', 'rule', ()]]]]]]]], ['Rule', 'block', ['And', [['And', []], ['Or', [['And', [['List', ['Or', [['And', [['Exactly', 'block'], ['Bind', 'symbol', ['Apply', 'anything', 'block', ()]], ['List', ['Or', [['And', [['Bind', 'arguments', ['Many', ['Apply', 'arg', 'block', ()]]]]]]]], ['List', ['Or', [['And', [['Bind', 't', ['Apply', 'compile', 'block', ()]]]]]]], ['List', ['Or', [['And', [['Bind', 'alt_t', ['Optional', ['Apply', 'compile', 'block', ()]]]]]]]]]]]]], ['Python', 'builder.add_block(symbol, arguments, t, alt_t)']]]]]]]], ['Rule', 'comment', ['And', [['And', []], ['Or', [['And', [['List', ['Or', [['And', [['Exactly', 'comment']]]]]]]]]]]]], ['Rule', 'literal', ['And', [['And', []], ['Or', [['And', [['List', ['Or', [['And', [['Exactly', 'literal'], ['Bind', 'value', ['Apply', 'anything', 'literal', ()]]]]]]], ['Python', 'builder.add_literal(value)']]]]]]]], ['Rule', 'expand', ['And', [['And', []], ['Or', [['And', [['List', ['Or', [['And', [['Exactly', 'expand'], ['Bind', 'value', ['Apply', 'path', 'expand', ()]], ['List', ['Or', [['And', [['Bind', 'arguments', ['Many', ['Apply', 'arg', 'expand', ()]]]]]]]]]]]]], ['Python', 'builder.add_expand(value, arguments)']]]]]]]], ['Rule', 'escapedexpand', ['And', [['And', []], ['Or', [['And', [['List', ['Or', [['And', [['Exactly', 'escapedexpand'], ['Bind', 'value', ['Apply', 'path', 'escapedexpand', ()]], ['List', ['Or', [['And', [['Bind', 'arguments', ['Many', ['Apply', 'arg', 'escapedexpand', ()]]]]]]]]]]]]], ['Python', 'builder.add_escaped_expand(value, arguments)']]]]]]]], ['Rule', 'invertedblock', ['And', [['And', []], ['Or', [['And', [['List', ['Or', [['And', [['Exactly', 'invertedblock'], ['Bind', 'symbol', ['Apply', 'anything', 'invertedblock', ()]], ['List', ['Or', [['And', [['Bind', 'arguments', ['Many', ['Apply', 'arg', 'invertedblock', ()]]]]]]]], ['List', ['Or', [['And', [['Bind', 't', ['Apply', 'compile', 'invertedblock', ()]]]]]]]]]]]], ['Python', 'builder.add_invertedblock(symbol, arguments, t)']]]]]]]], ['Rule', 'partial', ['And', [['And', []], ['Or', [['And', [['List', ['Or', [['And', [['Exactly', 'partial'], ['Bind', 'symbol', ['Apply', 'anything', 'partial', ()]], ['List', ['Or', [['And', [['Bind', 'arguments', ['Many', ['Apply', 'arg', 'partial', ()]]]]]]]]]]]]], ['Python', 'builder.add_partial(symbol, arguments)']]]]]]]], ['Rule', 'path', ['And', [['And', []], ['Or', [['And', [['List', ['Or', [['And', [['Exactly', 'path'], ['List', ['Or', [['And', [['Bind', 'segment', ['Apply', 'pathseg', 'path', ()]]]]]]]]]]]], ['Python', '("simple", segment)']]], ['And', [['List', ['Or', [['And', [['Exactly', 'path'], ['List', ['Or', [['And', [['Bind', 'segments', ['Many1', ['Apply', 'pathseg', 'path', ()]]]]]]]]]]]]], ['Python', '("complex", u\'resolve(context, "\'  + u\'","\'.join(segments) + u\'")\' )']]]]]]]], ['Rule', 'simplearg', ['And', [['And', []], ['Or', [['And', [['List', ['Or', [['And', [['Exactly', 'path'], ['List', ['Or', [['And', [['Bind', 'segments', ['Many1', ['Apply', 'pathseg', 'simplearg', ()]]]]]]]]]]]]], ['Python', 'u\'resolve(context, "\'  + u\'","\'.join(segments) + u\'")\'']]], ['And', [['List', ['Or', [['And', [['Exactly', 'literalparam'], ['Bind', 'value', ['Apply', 'anything', 'simplearg', ()]]]]]]], ['Python', 'unicode(value)']]]]]]]], ['Rule', 'arg', ['And', [['And', []], ['Or', [['And', [['List', ['Or', [['And', [['Exactly', 'kwparam'], ['Bind', 'symbol', ['Apply', 'anything', 'arg', ()]], ['Bind', 'a', ['Apply', 'simplearg', 'arg', ()]]]]]]], ['Python', "unicode(symbol) + '=' + a"]]], ['And', [['Apply', 'simplearg', 'arg', ()]]]]]]]], ['Rule', 'pathseg', ['Or', [['And', [['And', []], ['Or', [['And', [['Exactly', '/'], ['Python', "''"]]], ['And', [['Exactly', '.'], ['Python', "''"]]], ['And', [['Exactly', ''], ['Python', "''"]]], ['And', [['Exactly', 'this'], ['Python', "''"]]]]]]], ['And', [['And', []], ['Or', [['And', [['Bind', 'symbol', ['Apply', 'anything', 'pathseg', ()]], ['Python', "u''.join(symbol)"]]]]]]]]]]]]
        return moduleFromGrammar(tree, name, OMeta, globals)
Beispiel #5
0
    def _get_handlebars_template():
        # self._handlebars = OMeta.makeGrammar(handlebars_grammar, {}, 'handlebars')

        name = "handlebars"
        globals = {}
        # g = OMeta.metagrammarClass(handlebars_grammar)
        # tree = g.parseGrammar(name, TreeBuilder)
        tree = ['Grammar', 'handlebars', [['Rule', 'template', ['And', [['And', []], ['Or', [['And', [['Bind', 'body', ['Many', ['Or', [['And', [['Apply', 'text', 'template', ()]]], ['And', [['Apply', 'templatecommand', 'template', ()]]]]]]], ['Python', "['template'] + body"]]]]]]]], ['Rule', 'text', ['And', [['And', []], ['Or', [['And', [['Bind', 'text', ['Many1', ['Or', [['And', [['Not', ['Or', [['And', [['Apply', 'start', 'text', ()]]]]]], ['Apply', 'anything', 'text', ()]]]]]]], ['Python', "('literal', u''.join(text))"]]]]]]]], ['Rule', 'other', ['And', [['And', []], ['Or', [['And', [['Bind', 'char', ['Apply', 'anything', 'other', ()]], ['Python', "('literal', char)"]]]]]]]], ['Rule', 'templatecommand', ['And', [['And', []], ['Or', [['And', [['Apply', 'blockrule', 'templatecommand', ()]]], ['And', [['Apply', 'comment', 'templatecommand', ()]]], ['And', [['Apply', 'escapedexpression', 'templatecommand', ()]]], ['And', [['Apply', 'expression', 'templatecommand', ()]]], ['And', [['Apply', 'partial', 'templatecommand', ()]]]]]]]], ['Rule', 'start', ['And', [['And', []], ['Or', [['And', [['Exactly', '{'], ['Exactly', '{']]]]]]]], ['Rule', 'finish', ['And', [['And', []], ['Or', [['And', [['Exactly', '}'], ['Exactly', '}']]]]]]]], ['Rule', 'comment', ['And', [['And', []], ['Or', [['And', [['Apply', 'start', 'comment', ()], ['Exactly', '!'], ['Many', ['Or', [['And', [['Not', ['Or', [['And', [['Apply', 'finish', 'comment', ()]]]]]], ['Apply', 'anything', 'comment', ()]]]]]], ['Apply', 'finish', 'comment', ()], ['Python', "('comment', )"]]]]]]]], ['Rule', 'space', ['And', [['And', []], ['Or', [['And', [['Exactly', ' ']]], ['And', [['Exactly', '\t']]], ['And', [['Exactly', '\r']]], ['And', [['Exactly', '\n']]]]]]]], ['Rule', 'arguments', ['And', [['And', []], ['Or', [['And', [['Bind', 'arguments', ['Many', ['Or', [['And', [['Many1', ['Apply', 'space', 'arguments', ()]], ['Or', [['And', [['Apply', 'kwliteral', 'arguments', ()]]], ['And', [['Apply', 'literal', 'arguments', ()]]], ['And', [['Apply', 'path', 'arguments', ()]]]]]]]]]]], ['Python', 'arguments']]]]]]]], ['Rule', 'expression_inner', ['And', [['And', []], ['Or', [['And', [['Apply', 'spaces', 'expression_inner', ()], ['Bind', 'p', ['Apply', 'path', 'expression_inner', ()]], ['Bind', 'arguments', ['Apply', 'arguments', 'expression_inner', ()]], ['Apply', 'spaces', 'expression_inner', ()], ['Apply', 'finish', 'expression_inner', ()], ['Python', '(p, arguments)']]]]]]]], ['Rule', 'expression', ['And', [['And', []], ['Or', [['And', [['Apply', 'start', 'expression', ()], ['Exactly', '{'], ['Bind', 'e', ['Apply', 'expression_inner', 'expression', ()]], ['Exactly', '}'], ['Python', "('expand', ) + e"]]], ['And', [['Apply', 'start', 'expression', ()], ['Exactly', '&'], ['Bind', 'e', ['Apply', 'expression_inner', 'expression', ()]], ['Python', "('expand', ) + e"]]]]]]]], ['Rule', 'escapedexpression', ['And', [['And', []], ['Or', [['And', [['Apply', 'start', 'escapedexpression', ()], ['Bind', 'e', ['Apply', 'expression_inner', 'escapedexpression', ()]], ['Python', "('escapedexpand', ) + e"]]]]]]]], ['Rule', 'block_inner', ['And', [['And', []], ['Or', [['And', [['Apply', 'spaces', 'block_inner', ()], ['Bind', 's', ['Apply', 'symbol', 'block_inner', ()]], ['Bind', 'args', ['Apply', 'arguments', 'block_inner', ()]], ['Apply', 'spaces', 'block_inner', ()], ['Apply', 'finish', 'block_inner', ()], ['Python', "(u''.join(s), args)"]]]]]]]], ['Rule', 'partial', ['And', [['And', []], ['Or', [['And', [['Apply', 'start', 'partial', ()], ['Exactly', '>'], ['Bind', 'i', ['Apply', 'block_inner', 'partial', ()]], ['Python', "('partial',) + i"]]]]]]]], ['Rule', 'path', ['And', [['And', []], ['Or', [['And', [['Not', ['Or', [['And', [['Exactly', '/']]]]]], ['Bind', 'segments', ['Many1', ['Apply', 'pathseg', 'path', ()]]], ['Python', "('path', segments)"]]]]]]]], ['Rule', 'kwliteral', ['And', [['And', []], ['Or', [['And', [['Bind', 's', ['Apply', 'symbol', 'kwliteral', ()]], ['Exactly', '='], ['Bind', 'v', ['Or', [['And', [['Apply', 'literal', 'kwliteral', ()]]], ['And', [['Apply', 'path', 'kwliteral', ()]]]]]], ['Python', "('kwparam', s, v)"]]]]]]]], ['Rule', 'literal', ['And', [['And', []], ['Or', [['And', [['Bind', 'thing', ['Or', [['And', [['Apply', 'string', 'literal', ()]]], ['And', [['Apply', 'integer', 'literal', ()]]], ['And', [['Apply', 'boolean', 'literal', ()]]]]]], ['Python', "('literalparam', thing)"]]]]]]]], ['Rule', 'string', ['And', [['And', []], ['Or', [['And', [['Exactly', '"'], ['Bind', 'ls', ['Many', ['Apply', 'notquote', 'string', ()]]], ['Exactly', '"'], ['Python', 'u\'"\' + u\'\'.join(ls) + u\'"\'']]]]]]]], ['Rule', 'integer', ['And', [['And', []], ['Or', [['And', [['Bind', 'ds', ['Many1', ['Apply', 'digit', 'integer', ()]]], ['Python', "int(''.join(ds))"]]]]]]]], ['Rule', 'boolean', ['And', [['And', []], ['Or', [['And', [['Apply', 'false', 'boolean', ()]]], ['And', [['Apply', 'true', 'boolean', ()]]]]]]]], ['Rule', 'false', ['And', [['And', []], ['Or', [['And', [['Exactly', 'f'], ['Exactly', 'a'], ['Exactly', 'l'], ['Exactly', 's'], ['Exactly', 'e'], ['Python', 'False']]]]]]]], ['Rule', 'true', ['And', [['And', []], ['Or', [['And', [['Exactly', 't'], ['Exactly', 'r'], ['Exactly', 'u'], ['Exactly', 'e'], ['Python', 'True']]]]]]]], ['Rule', 'notquote', ['And', [['And', []], ['Or', [['And', [['Apply', 'escapedquote', 'notquote', ()]]], ['And', [['Or', [['And', [['Not', ['Or', [['And', [['Exactly', '"']]]]]], ['Apply', 'anything', 'notquote', ()]]]]]]]]]]]], ['Rule', 'escapedquote', ['And', [['And', []], ['Or', [['And', [['Exactly', '\\'], ['Exactly', '"'], ['Python', '\'\\\\"\'']]]]]]]], ['Rule', 'symbol', ['And', [['And', []], ['Or', [['And', [['Optional', ['Exactly', '[']], ['Bind', 'symbol', ['Many1', ['Or', [['And', [['Apply', 'letterOrDigit', 'symbol', ()]]], ['And', [['Exactly', '-']]], ['And', [['Exactly', '@']]]]]]], ['Optional', ['Exactly', ']']], ['Python', "u''.join(symbol)"]]]]]]]], ['Rule', 'pathseg', ['And', [['And', []], ['Or', [['And', [['Apply', 'symbol', 'pathseg', ()]]], ['And', [['Exactly', '/'], ['Python', "u''"]]], ['And', [['Or', [['And', [['Exactly', '.'], ['Exactly', '.'], ['Exactly', '/']]]]], ['Python', "u'__parent'"]]], ['And', [['Exactly', '.'], ['Python', "u''"]]]]]]]], ['Rule', 'pathfinish', ['And', [['And', [['Bind', 'expected', ['Apply', 'anything', 'pathfinish', ()]]]], ['Or', [['And', [['Apply', 'start', 'pathfinish', ()], ['Exactly', '/'], ['Bind', 'found', ['Apply', 'path', 'pathfinish', ()]], ['Predicate', ['Python', 'found == expected']], ['Apply', 'finish', 'pathfinish', ()]]]]]]]], ['Rule', 'symbolfinish', ['And', [['And', [['Bind', 'expected', ['Apply', 'anything', 'symbolfinish', ()]]]], ['Or', [['And', [['Apply', 'start', 'symbolfinish', ()], ['Exactly', '/'], ['Bind', 'found', ['Apply', 'symbol', 'symbolfinish', ()]], ['Predicate', ['Python', 'found == expected']], ['Apply', 'finish', 'symbolfinish', ()]]]]]]]], ['Rule', 'blockrule', ['And', [['And', []], ['Or', [['And', [['Apply', 'start', 'blockrule', ()], ['Exactly', '#'], ['Bind', 'i', ['Apply', 'block_inner', 'blockrule', ()]], ['Bind', 't', ['Apply', 'template', 'blockrule', ()]], ['Bind', 'alt_t', ['Apply', 'alttemplate', 'blockrule', ()]], ['Apply', 'symbolfinish', 'blockrule', (['Python', 'i[0]'],)], ['Python', "('block',) + i + (t, alt_t)"]]], ['And', [['Apply', 'start', 'blockrule', ()], ['Exactly', '^'], ['Bind', 'i', ['Apply', 'block_inner', 'blockrule', ()]], ['Bind', 't', ['Apply', 'template', 'blockrule', ()]], ['Apply', 'symbolfinish', 'blockrule', (['Python', 'i[0]'],)], ['Python', "('invertedblock',) + i + (t,)"]]]]]]]], ['Rule', 'alttemplate', ['And', [['And', []], ['Or', [['And', [['Bind', 'alt_t', ['Optional', ['Or', [['And', [['Apply', 'start', 'alttemplate', ()], ['Exactly', '^'], ['Apply', 'finish', 'alttemplate', ()], ['Apply', 'template', 'alttemplate', ()]]]]]]], ['Python', 'alt_t or []']]]]]]]]]]

        return moduleFromGrammar(tree, name, OMeta, globals)
Beispiel #6
0
    def __init__(self, key):
        gkey = ''.join(["'" + x + "'" for x in list(key)])

        grammar = r"""
start = (gettext:x | anything -> None)+:xs -> [x for x in xs if x != None]
gettext = '(' gettext_key space+ string:s ')' -> s
string  = space* '"' ('\\' '"' | ~'"' :x)*:xs '"' -> ''.join(xs)
gettext_key = """+gkey+"""
space = anything:c ?(c.isspace())  -> c
"""
        G = OMeta2Grammar(grammar)
        tree = G.parseGrammar('evaluator', TreeBuilder)
        self.Parser = moduleFromGrammar(tree, 'evaluator', OMetaBase, {})
Beispiel #7
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)
Beispiel #8
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)
Beispiel #9
0
 def test_bindingAccess(self):
     """
     Bound names in a rule can be accessed on the grammar's "locals" dict.
     """
     gg = self.classTested("stuff = '1':a ('2':b | '3':c)")
     t = gg.parseGrammar('TestGrammar', TreeBuilder)
     G = moduleFromGrammar(t, 'TestGrammar', OMetaBase, {})
     g = G("12")
     self.assertEqual(g.apply("stuff")[0], '2')
     self.assertEqual(g.locals['stuff']['a'], '1')
     self.assertEqual(g.locals['stuff']['b'], '2')
     g = G("13")
     self.assertEqual(g.apply("stuff")[0], '3')
     self.assertEqual(g.locals['stuff']['a'], '1')
     self.assertEqual(g.locals['stuff']['c'], '3')
Beispiel #10
0
 def test_bindingAccess(self):
     """
     Bound names in a rule can be accessed on the grammar's "locals" dict.
     """
     gg = self.classTested("stuff ::= '1':a ('2':b | '3':c)")
     t = gg.parseGrammar('TestGrammar', TreeBuilder)
     G = moduleFromGrammar(t, 'TestGrammar', OMetaBase, {})
     g = G("12")
     self.assertEqual(g.apply("stuff")[0], '2')
     self.assertEqual(g.locals['stuff']['a'], '1')
     self.assertEqual(g.locals['stuff']['b'], '2')
     g = G("13")
     self.assertEqual(g.apply("stuff")[0], '3')
     self.assertEqual(g.locals['stuff']['a'], '1')
     self.assertEqual(g.locals['stuff']['c'], '3')
Beispiel #11
0
 def test_bindingAccess(self):
     """
     Bound names in a rule can be accessed on the grammar's "locals" dict.
     """
     gg = self.classTested("stuff = '1':a ('2':b | '3':c)")
     t = gg.parseGrammar("TestGrammar", TreeBuilder)
     G = moduleFromGrammar(t, "TestGrammar", OMetaBase, {})
     g = G("12")
     self.assertEqual(g.apply("stuff")[0], "2")
     self.assertEqual(g.locals["stuff"]["a"], "1")
     self.assertEqual(g.locals["stuff"]["b"], "2")
     g = G("13")
     self.assertEqual(g.apply("stuff")[0], "3")
     self.assertEqual(g.locals["stuff"]["a"], "1")
     self.assertEqual(g.locals["stuff"]["c"], "3")
Beispiel #12
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))