Compare commits
No commits in common. "71b59302facdb62b03441da431a1b050d3d7d8dc" and "a1fa03615cbd6dbb960f7574b2f2596a11808e0b" have entirely different histories.
71b59302fa
...
a1fa03615c
3 changed files with 13 additions and 34 deletions
19
grammar.py
19
grammar.py
|
|
@ -25,8 +25,6 @@ class FineGrammar(Grammar):
|
||||||
|
|
||||||
trivia = ["BLANKS", "COMMENT"]
|
trivia = ["BLANKS", "COMMENT"]
|
||||||
|
|
||||||
pretty_indent = " "
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
precedence=[
|
precedence=[
|
||||||
|
|
@ -136,23 +134,14 @@ class FineGrammar(Grammar):
|
||||||
@rule("FunctionDecl")
|
@rule("FunctionDecl")
|
||||||
def function_declaration(self) -> Rule:
|
def function_declaration(self) -> Rule:
|
||||||
return seq(
|
return seq(
|
||||||
group(
|
|
||||||
group(
|
|
||||||
group(
|
group(
|
||||||
self.FUN,
|
self.FUN,
|
||||||
sp,
|
sp,
|
||||||
mark(
|
mark(self.IDENTIFIER, field="name", highlight=highlight.entity.name.function),
|
||||||
self.IDENTIFIER,
|
sp,
|
||||||
field="name",
|
group(
|
||||||
highlight=highlight.entity.name.function,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
nl,
|
|
||||||
mark(self.function_parameters, field="parameters"),
|
mark(self.function_parameters, field="parameters"),
|
||||||
),
|
mark(opt(sp, group(self.ARROW, sp, self.type_expression)), field="return_type"),
|
||||||
mark(
|
|
||||||
opt(indent(sp, group(self.ARROW, sp, self.type_expression))),
|
|
||||||
field="return_type",
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
sp,
|
sp,
|
||||||
|
|
|
||||||
|
|
@ -580,9 +580,7 @@ class Harness:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
return (
|
return (
|
||||||
wadler.layout_document(self.document, width, self.load_printer().indent())
|
wadler.layout_document(self.document, width).apply_to_source(self.source).splitlines()
|
||||||
.apply_to_source(self.source)
|
|
||||||
.splitlines()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,7 @@ class DocumentLayout:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def layout_document(doc: Document, width: int, indent: str) -> DocumentLayout:
|
def layout_document(doc: Document, width: int) -> DocumentLayout:
|
||||||
"""Lay out a document to fit within the given width.
|
"""Lay out a document to fit within the given width.
|
||||||
|
|
||||||
The result of this function is a DocumentLayout which can trivially be
|
The result of this function is a DocumentLayout which can trivially be
|
||||||
|
|
@ -213,13 +213,13 @@ def layout_document(doc: Document, width: int, indent: str) -> DocumentLayout:
|
||||||
column += len(replace)
|
column += len(replace)
|
||||||
else:
|
else:
|
||||||
# TODO: Custom newline expansion, custom indent segments.
|
# TODO: Custom newline expansion, custom indent segments.
|
||||||
output.append("\n" + (chunk.indent * indent))
|
output.append("\n" + (chunk.indent * " "))
|
||||||
column = chunk.indent * len(indent)
|
column = chunk.indent
|
||||||
|
|
||||||
case ForceBreak():
|
case ForceBreak():
|
||||||
# TODO: Custom newline expansion, custom indent segments.
|
# TODO: Custom newline expansion, custom indent segments.
|
||||||
output.append("\n" + (chunk.indent * indent))
|
output.append("\n" + (chunk.indent * " "))
|
||||||
column = chunk.indent * len(indent)
|
column = chunk.indent
|
||||||
|
|
||||||
case Cons(left, right):
|
case Cons(left, right):
|
||||||
chunks.append(chunk.with_document(right))
|
chunks.append(chunk.with_document(right))
|
||||||
|
|
@ -363,20 +363,12 @@ class Printer:
|
||||||
grammar: parser.Grammar
|
grammar: parser.Grammar
|
||||||
_matchers: dict[str, Matcher]
|
_matchers: dict[str, Matcher]
|
||||||
_nonterminals: dict[str, parser.NonTerminal]
|
_nonterminals: dict[str, parser.NonTerminal]
|
||||||
_indent: str
|
|
||||||
|
|
||||||
def __init__(self, grammar: parser.Grammar, indent: str | None = None):
|
def __init__(self, grammar: parser.Grammar):
|
||||||
self.grammar = grammar
|
self.grammar = grammar
|
||||||
self._nonterminals = {nt.name: nt for nt in grammar.non_terminals()}
|
self._nonterminals = {nt.name: nt for nt in grammar.non_terminals()}
|
||||||
self._matchers = {}
|
self._matchers = {}
|
||||||
|
|
||||||
if indent is None:
|
|
||||||
indent = getattr(self.grammar, "pretty_indent", " ")
|
|
||||||
self._indent = indent
|
|
||||||
|
|
||||||
def indent(self) -> str:
|
|
||||||
return self._indent
|
|
||||||
|
|
||||||
def lookup_nonterminal(self, name: str) -> parser.NonTerminal:
|
def lookup_nonterminal(self, name: str) -> parser.NonTerminal:
|
||||||
return self._nonterminals[name]
|
return self._nonterminals[name]
|
||||||
|
|
||||||
|
|
@ -560,4 +552,4 @@ class Printer:
|
||||||
|
|
||||||
def format_tree(self, tree: runtime.Tree, width: int) -> DocumentLayout:
|
def format_tree(self, tree: runtime.Tree, width: int) -> DocumentLayout:
|
||||||
doc = self.convert_tree_to_document(tree)
|
doc = self.convert_tree_to_document(tree)
|
||||||
return layout_document(doc, width, self._indent)
|
return layout_document(doc, width)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue