Compare commits
2 commits
ca240906c9
...
46770419e6
| Author | SHA1 | Date | |
|---|---|---|---|
| 46770419e6 | |||
| 8845bcfdab |
2 changed files with 40 additions and 5 deletions
|
|
@ -348,7 +348,7 @@ class Printer:
|
||||||
def compile_rule(self, rule: parser.NonTerminal) -> Matcher:
|
def compile_rule(self, rule: parser.NonTerminal) -> Matcher:
|
||||||
generated_grammar: list[typing.Tuple[str, list[str]]] = []
|
generated_grammar: list[typing.Tuple[str, list[str]]] = []
|
||||||
visited: set[str] = set()
|
visited: set[str] = set()
|
||||||
group_count = 0
|
groups: dict[tuple[str, ...], str] = {}
|
||||||
indent_amounts: dict[str, int] = {}
|
indent_amounts: dict[str, int] = {}
|
||||||
newline_map: dict[str, str] = {}
|
newline_map: dict[str, str] = {}
|
||||||
done_forced_break = False
|
done_forced_break = False
|
||||||
|
|
@ -361,7 +361,7 @@ class Printer:
|
||||||
generated_grammar.append((name, trans_prod))
|
generated_grammar.append((name, trans_prod))
|
||||||
|
|
||||||
def compile_production(production: parser.FlattenedWithMetadata) -> list[str]:
|
def compile_production(production: parser.FlattenedWithMetadata) -> list[str]:
|
||||||
nonlocal group_count
|
nonlocal groups
|
||||||
nonlocal indent_amounts
|
nonlocal indent_amounts
|
||||||
nonlocal done_forced_break
|
nonlocal done_forced_break
|
||||||
|
|
||||||
|
|
@ -394,9 +394,13 @@ class Printer:
|
||||||
if isinstance(pretty, parser.FormatMeta):
|
if isinstance(pretty, parser.FormatMeta):
|
||||||
if pretty.group:
|
if pretty.group:
|
||||||
# Make a fake rule.
|
# Make a fake rule.
|
||||||
rule_name = f"g_{group_count}"
|
child_key = tuple(tx_children)
|
||||||
group_count += 1
|
rule_name = groups.get(child_key)
|
||||||
|
if rule_name is None:
|
||||||
|
rule_name = f"g_{len(groups)}"
|
||||||
|
groups[child_key] = rule_name
|
||||||
generated_grammar.append((rule_name, tx_children))
|
generated_grammar.append((rule_name, tx_children))
|
||||||
|
|
||||||
tx_children = [rule_name]
|
tx_children = [rule_name]
|
||||||
|
|
||||||
if pretty.indent:
|
if pretty.indent:
|
||||||
|
|
|
||||||
31
wrap.py
Normal file
31
wrap.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
import grammar
|
||||||
|
|
||||||
|
import parser.runtime as runtime
|
||||||
|
import parser.wadler as wadler
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
g = grammar.FineGrammar()
|
||||||
|
parser = runtime.Parser(g.build_table())
|
||||||
|
lexer = g.compile_lexer()
|
||||||
|
|
||||||
|
with open("test.fine", "r", encoding="utf-8") as f:
|
||||||
|
text = f.read()
|
||||||
|
|
||||||
|
tree, errors = parser.parse(runtime.GenericTokenStream(text, lexer))
|
||||||
|
if tree is None or len(errors) > 0:
|
||||||
|
print(f"{len(errors)} ERRORS")
|
||||||
|
for error in errors:
|
||||||
|
print(f"{error}")
|
||||||
|
return
|
||||||
|
|
||||||
|
WIDTH = 40
|
||||||
|
|
||||||
|
printer = wadler.Printer(g)
|
||||||
|
result = printer.format_tree(tree, WIDTH).apply_to_source(text)
|
||||||
|
for line in result.splitlines():
|
||||||
|
print(f"{line:<{WIDTH}}|")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue