Exemplo n.º 1
0
def test_maybe_2():
    program = ("start: [foo]\n" "foo: NAME\n")
    rules = start(program).rules
    assert rules == [
        Rule('start', [Alt([Maybe('foo')])]),
        Rule('foo', [Alt(['NAME'])])
    ]
Exemplo n.º 2
0
def test_action_repr_str():
    alt = Alt(["one", "two"])
    assert repr(alt) == "Alt(['one', 'two'])"
    assert str(alt) == "one two"

    alt = Alt(["one", "two"], "foo + bar")
    assert repr(alt) == "Alt(['one', 'two'], 'foo + bar')"
    assert str(alt) == "one two { foo + bar }"
Exemplo n.º 3
0
def test_grammar():
    program = ("stmt: asmt | expr\n" "asmt: NAME '=' expr\n" "expr: NAME\n")
    rules = start(program).rules
    assert rules == [
        Rule('stmt', [Alt(['asmt']), Alt(['expr'])]),
        Rule('asmt', [Alt(['NAME', "'='", 'expr'])]),
        Rule('expr', [Alt(['NAME'])])
    ]
Exemplo n.º 4
0
def test_maybe_3():
    program = ("start: [foo foo | foo]\n" "foo: NAME\n")
    rules = start(program).rules
    assert rules == [
        Rule('start', [Alt([Maybe('_synthetic_rule_0')])]),
        Rule('foo', [Alt(['NAME'])]),
        Rule('_synthetic_rule_0',
             [Alt(['foo', 'foo']), Alt(['foo'])])
    ]
Exemplo n.º 5
0
def test_group():
    program = ("start: (foo foo | foo)\n" "foo: NAME\n")
    rules = start(program).rules
    assert rules == [
        Rule('start', [Alt(['_synthetic_rule_0'])]),
        Rule('foo', [Alt(['NAME'])]),
        Rule('_synthetic_rule_0',
             [Alt(['foo', 'foo']), Alt(['foo'])])
    ]
Exemplo n.º 6
0
def test_named_item():
    program = ("start: f=foo\n" "foo: n=NAME\n")
    file = StringIO(program)
    tokengen = generate_tokens(file.readline)
    tok = Tokenizer(tokengen)
    p = GrammarParser(tok)
    rules = p.start().rules
    assert rules == [
        Rule('start', [Alt([NamedItem('f', 'foo')])]),
        Rule('foo', [Alt([NamedItem('n', 'NAME')])])
    ]
Exemplo n.º 7
0
def test_action():
    program = "start: NAME { foo + bar } | NUMBER { -baz }\n"
    rules = start(program).rules
    assert rules == [
        Rule("start", [Alt(["NAME"], "foo + bar"),
                       Alt(["NUMBER"], "- baz")])
    ]
    assert rules != [
        Rule("start", [Alt(["NAME"], "foo + bar"),
                       Alt(["NUMBER"], "baz")])
    ]
Exemplo n.º 8
0
def test_indents():
    program = ("stmt: foo | bar\n" "    | baz\n" "    | booh | bah\n")
    rules = start(program).rules
    assert rules == [
        Rule('stmt', [
            Alt(['foo']),
            Alt(['bar']),
            Alt(['baz']),
            Alt(['booh']),
            Alt(['bah'])
        ])
    ]
Exemplo n.º 9
0
 def alt(self):
     self.show_rule('alt', [['items', 'action'], ['items']])
     pos = self.mark()
     if (True and self.show_index(0, 0)
             and (items := self.items()) is not None
             and self.show_index(0, 1)
             and (action := self.action()) is not None):
         self.show_index(0, 0, 2)
         retval = Alt(items, action)
         if retval is not None:
             return retval
Exemplo n.º 10
0
    def alt(self):
        self.show_rule('alt', [['items', 'action'], ['items']])
        pos = self.mark()
        if (True and self.show_index(0, 0)
                and (items := self.items()) is not None
                and self.show_index(0, 1)
                and (action := self.action()) is not None):
            self.show_index(0, 0, 2)
            retval = Alt(items, action)
            if retval is not None:
                return retval
        self.reset(pos)
        if (True and self.show_index(1, 0)
                and (items := self.items()) is not None):
            self.show_index(1, 0, 1)
            retval = Alt(items, None)
            if retval is not None:
                return retval
        self.reset(pos)
        self.show_index(0, 0, 0)
        return None

    @memoize
    def items(self):
        self.show_rule('items', [['item', 'items'], ['item']])
        pos = self.mark()
        if (True and self.show_index(0, 0)
                and (item := self.item()) is not None
                and self.show_index(0, 1)
                and (items := self.items()) is not None):
            self.show_index(0, 0, 2)
Exemplo n.º 11
0
def test_meta():
    program = ("@start 'start'\n" "@foo bar\n" "@bar\n" "stmt: foo\n")
    grammar = start(program)
    assert grammar
    assert grammar.rules == [Rule('stmt', [Alt(["foo"])])]
    assert grammar.metas == [('start', 'start'), ('foo', 'bar'), ('bar', None)]
Exemplo n.º 12
0
def test_cut():
    program = "start: NAME ~ NAME\n"
    rules = start(program).rules
    assert rules == [Rule('start', [Alt(['NAME', Cut(), 'NAME'])])]
Exemplo n.º 13
0
def test_lookahead_negative():
    program = "start: !NUMBER NAME\n"
    rules = start(program).rules
    assert rules == [
        Rule('start', [Alt([Lookahead('NUMBER', False), 'NAME'])])
    ]
Exemplo n.º 14
0
def test_lookahead():
    program = "start: &NUMBER NAME\n"
    rules = start(program).rules
    assert rules == [Rule('start', [Alt([Lookahead('NUMBER'), 'NAME'])])]
Exemplo n.º 15
0
def test_plus():
    program = "start: NAME+\n"
    rules = start(program).rules
    assert rules == [Rule('start', [Alt([Loop('NAME', True)])])]
Exemplo n.º 16
0
def test_star():
    program = "start: NAME*\n"
    rules = start(program).rules
    assert rules == [Rule('start', [Alt([Loop('NAME')])])]