Exemple #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'])])
    ]
Exemple #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 }"
Exemple #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'])])
    ]
Exemple #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'])])
    ]
Exemple #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'])])
    ]
Exemple #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')])])
    ]
Exemple #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")])
    ]
Exemple #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'])
        ])
    ]
Exemple #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
Exemple #10
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)]
Exemple #11
0
def test_cut():
    program = "start: NAME ~ NAME\n"
    rules = start(program).rules
    assert rules == [Rule('start', [Alt(['NAME', Cut(), 'NAME'])])]
Exemple #12
0
def test_lookahead_negative():
    program = "start: !NUMBER NAME\n"
    rules = start(program).rules
    assert rules == [
        Rule('start', [Alt([Lookahead('NUMBER', False), 'NAME'])])
    ]
Exemple #13
0
def test_lookahead():
    program = "start: &NUMBER NAME\n"
    rules = start(program).rules
    assert rules == [Rule('start', [Alt([Lookahead('NUMBER'), 'NAME'])])]
Exemple #14
0
def test_plus():
    program = "start: NAME+\n"
    rules = start(program).rules
    assert rules == [Rule('start', [Alt([Loop('NAME', True)])])]
Exemple #15
0
def test_star():
    program = "start: NAME*\n"
    rules = start(program).rules
    assert rules == [Rule('start', [Alt([Loop('NAME')])])]
Exemple #16
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)