Fix bugs but still doesn't work for Fine

This commit is contained in:
John Doty 2024-08-30 09:14:01 -07:00
parent 066d2d8439
commit 98c4bb950f
2 changed files with 11 additions and 4 deletions

View file

@ -417,10 +417,14 @@ class FineGrammar(Grammar):
if __name__ == "__main__":
from parser.parser import compile_lexer, dump_lexer_table
from pathlib import Path
from parser.parser import dump_lexer_table
from parser.tree_sitter import emit_tree_sitter_grammar
grammar = FineGrammar()
grammar.build_table()
lexer = compile_lexer(grammar)
lexer = grammar.compile_lexer()
dump_lexer_table(lexer)
emit_tree_sitter_grammar(grammar, Path(__file__).parent / "tree-sitter-fine")

View file

@ -136,7 +136,7 @@ def convert_to_tree_sitter(rule: parser.Rule, grammar: parser.Grammar) -> str:
if len(final) > 1:
result = f"choice({result})"
if has_nothing:
result = f"opt({result})"
result = f"optional({result})"
return result
elif isinstance(rule, parser.SequenceRule):
@ -162,7 +162,10 @@ def convert_to_tree_sitter(rule: parser.Rule, grammar: parser.Grammar) -> str:
return result
elif isinstance(rule, parser.NonTerminal):
return f"$['{rule.name}']"
target_name = rule.name
if rule.transparent:
target_name = f"_{target_name}"
return f"$['{target_name}']"
elif isinstance(rule, parser.MetadataRule):
return convert_to_tree_sitter(rule.rule, grammar)