Compare commits
No commits in common. "5e12af9f31629c31a49b69d4d192e931336f7b2a" and "591da0c97180bbdc339ee57da9ec0ef66a7a3e53" have entirely different histories.
5e12af9f31
...
591da0c971
3 changed files with 15 additions and 30 deletions
12
grammar.py
12
grammar.py
|
|
@ -52,8 +52,8 @@ class FineGrammar(Grammar):
|
||||||
def class_declaration(self) -> Rule:
|
def class_declaration(self) -> Rule:
|
||||||
return seq(
|
return seq(
|
||||||
self.CLASS,
|
self.CLASS,
|
||||||
mark(self.IDENTIFIER, field="name", highlight=highlight.entity.name.type),
|
mark(self.IDENTIFIER, highlight=highlight.entity.name.type),
|
||||||
mark(self._class_body, field="body"),
|
self._class_body,
|
||||||
)
|
)
|
||||||
|
|
||||||
@rule
|
@rule
|
||||||
|
|
@ -107,10 +107,10 @@ class FineGrammar(Grammar):
|
||||||
def function_declaration(self) -> Rule:
|
def function_declaration(self) -> Rule:
|
||||||
return seq(
|
return seq(
|
||||||
self.FUN,
|
self.FUN,
|
||||||
mark(self.IDENTIFIER, field="name", highlight=highlight.entity.name.function),
|
mark(self.IDENTIFIER, highlight=highlight.entity.name.function),
|
||||||
mark(self.function_parameters, field="parameters"),
|
self.function_parameters,
|
||||||
mark(opt(self.ARROW, self.type_expression), field="return_type"),
|
opt(self.ARROW, self.type_expression),
|
||||||
mark(self.block, field="body"),
|
self.block,
|
||||||
)
|
)
|
||||||
|
|
||||||
@rule("ParamList")
|
@rule("ParamList")
|
||||||
|
|
|
||||||
|
|
@ -2729,9 +2729,6 @@ class Grammar:
|
||||||
def terminals(self) -> list[Terminal]:
|
def terminals(self) -> list[Terminal]:
|
||||||
return self._terminals
|
return self._terminals
|
||||||
|
|
||||||
def trivia_terminals(self) -> list[Terminal]:
|
|
||||||
return self._trivia
|
|
||||||
|
|
||||||
def non_terminals(self) -> list[NonTerminal]:
|
def non_terminals(self) -> list[NonTerminal]:
|
||||||
return [nt for _, nt in inspect.getmembers(self, lambda x: isinstance(x, NonTerminal))]
|
return [nt for _, nt in inspect.getmembers(self, lambda x: isinstance(x, NonTerminal))]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -99,17 +99,6 @@ def to_javascript_regex(re: parser.Re) -> str:
|
||||||
raise Exception(f"Regex node {re} not supported for tree-sitter")
|
raise Exception(f"Regex node {re} not supported for tree-sitter")
|
||||||
|
|
||||||
|
|
||||||
def terminal_to_tree_sitter(rule: parser.Terminal) -> str:
|
|
||||||
if isinstance(rule.pattern, parser.Re):
|
|
||||||
regex = to_javascript_regex(rule.pattern)
|
|
||||||
regex = regex.replace("/", "\\/")
|
|
||||||
result = f"/{regex}/"
|
|
||||||
else:
|
|
||||||
string = to_js_string(rule.pattern)
|
|
||||||
result = f'"{string}"'
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def apply_precedence(js: str, name: str, grammar: parser.Grammar) -> str:
|
def apply_precedence(js: str, name: str, grammar: parser.Grammar) -> str:
|
||||||
prec = grammar.get_precedence(name)
|
prec = grammar.get_precedence(name)
|
||||||
if prec is not None:
|
if prec is not None:
|
||||||
|
|
@ -130,7 +119,14 @@ def convert_to_tree_sitter(rule: parser.Rule, grammar: parser.Grammar) -> str:
|
||||||
return method(grammar)
|
return method(grammar)
|
||||||
|
|
||||||
if isinstance(rule, parser.Terminal):
|
if isinstance(rule, parser.Terminal):
|
||||||
return terminal_to_tree_sitter(rule)
|
if isinstance(rule.pattern, parser.Re):
|
||||||
|
regex = to_javascript_regex(rule.pattern)
|
||||||
|
result = f"/{regex}/"
|
||||||
|
else:
|
||||||
|
string = to_js_string(rule.pattern)
|
||||||
|
result = f'"{string}"'
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
elif isinstance(rule, parser.AlternativeRule):
|
elif isinstance(rule, parser.AlternativeRule):
|
||||||
final = []
|
final = []
|
||||||
|
|
@ -209,11 +205,7 @@ def convert_to_tree_sitter(rule: parser.Rule, grammar: parser.Grammar) -> str:
|
||||||
return f"$['{target_name}']"
|
return f"$['{target_name}']"
|
||||||
|
|
||||||
elif isinstance(rule, parser.MetadataRule):
|
elif isinstance(rule, parser.MetadataRule):
|
||||||
result = convert_to_tree_sitter(rule.rule, grammar)
|
return convert_to_tree_sitter(rule.rule, grammar)
|
||||||
field = rule.metadata.get("field")
|
|
||||||
if field is not None:
|
|
||||||
result = f"field('{field}', {result})"
|
|
||||||
return result
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Rule {rule} not supported for tree-sitter")
|
raise ValueError(f"Rule {rule} not supported for tree-sitter")
|
||||||
|
|
@ -229,10 +221,6 @@ def emit_tree_sitter_grammar(grammar: parser.Grammar, path: pathlib.Path | str):
|
||||||
f.write("\n")
|
f.write("\n")
|
||||||
f.write("module.exports = grammar({\n")
|
f.write("module.exports = grammar({\n")
|
||||||
f.write(f" name: '{grammar.name}',\n")
|
f.write(f" name: '{grammar.name}',\n")
|
||||||
|
|
||||||
extras = ", ".join([terminal_to_tree_sitter(t) for t in grammar.trivia_terminals()])
|
|
||||||
f.write(f" extras: $ => [{extras}],\n")
|
|
||||||
|
|
||||||
f.write(" rules: {\n")
|
f.write(" rules: {\n")
|
||||||
f.write(f" source_file: $ => $['{grammar.start}'],\n")
|
f.write(f" source_file: $ => $['{grammar.start}'],\n")
|
||||||
for rule in grammar.non_terminals():
|
for rule in grammar.non_terminals():
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue