File: test_grammar_visualizer.py

package info (click to toggle)
python-pegen 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,980 kB
  • sloc: python: 15,064; makefile: 89
file content (116 lines) | stat: -rw-r--r-- 3,187 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import textwrap
from typing import List

from pegen.grammar_parser import GeneratedParser as GrammarParser
from pegen.grammar_visualizer import ASTGrammarPrinter
from pegen.utils import parse_string


def test_simple_rule() -> None:
    grammar = """
    start: 'a' 'b'
    """
    rules = parse_string(grammar, GrammarParser)

    printer = ASTGrammarPrinter()
    lines: List[str] = []
    printer.print_grammar_ast(rules, printer=lines.append)

    output = "\n".join(lines)
    expected_output = textwrap.dedent(
        """\
    └──Rule
       └──Rhs
          └──Alt
             ├──NamedItem
             │  └──StringLeaf("'a'")
             └──NamedItem
                └──StringLeaf("'b'")
    """
    )

    assert output == expected_output


def test_multiple_rules() -> None:
    grammar = """
    start: a b
    a: 'a'
    b: 'b'
    """
    rules = parse_string(grammar, GrammarParser)

    printer = ASTGrammarPrinter()
    lines: List[str] = []
    printer.print_grammar_ast(rules, printer=lines.append)

    output = "\n".join(lines)
    expected_output = textwrap.dedent(
        """\
    └──Rule
       └──Rhs
          └──Alt
             ├──NamedItem
             │  └──NameLeaf('a')
             └──NamedItem
                └──NameLeaf('b')

    └──Rule
       └──Rhs
          └──Alt
             └──NamedItem
                └──StringLeaf("'a'")

    └──Rule
       └──Rhs
          └──Alt
             └──NamedItem
                └──StringLeaf("'b'")
                    """
    )

    assert output == expected_output


def test_deep_nested_rule() -> None:
    grammar = """
    start: 'a' ['b'['c'['d']]]
    """
    rules = parse_string(grammar, GrammarParser)

    printer = ASTGrammarPrinter()
    lines: List[str] = []
    printer.print_grammar_ast(rules, printer=lines.append)

    output = "\n".join(lines)
    print()
    print(output)
    expected_output = textwrap.dedent(
        """\
    └──Rule
       └──Rhs
          └──Alt
             ├──NamedItem
             │  └──StringLeaf("'a'")
             └──NamedItem
                └──Opt
                   └──Rhs
                      └──Alt
                         ├──NamedItem
                         │  └──StringLeaf("'b'")
                         └──NamedItem
                            └──Opt
                               └──Rhs
                                  └──Alt
                                     ├──NamedItem
                                     │  └──StringLeaf("'c'")
                                     └──NamedItem
                                        └──Opt
                                           └──Rhs
                                              └──Alt
                                                 └──NamedItem
                                                    └──StringLeaf("'d'")
                            """
    )

    assert output == expected_output