Skip to content

Commit

Permalink
Fix bad indentation and additional breaks in multiline matrices (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErykMroczek authored Nov 29, 2023
2 parents be61853 + bea06c7 commit 54c500f
Show file tree
Hide file tree
Showing 10 changed files with 4,144 additions and 4,727 deletions.
95 changes: 22 additions & 73 deletions grammar/Modelica.g4
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,15 @@ equation
| for_equation
| connect_equation
| when_equation
| function_call
| component_reference function_call_args
)
description
;

statement
: (
component_reference ASSIGN expression
| function_call
| LPAREN output_expression_list RPAREN ASSIGN function_call
component_reference (ASSIGN expression | function_call_args)
| LPAREN output_expression_list RPAREN ASSIGN component_reference function_call_args
| BREAK
| RETURN
| if_statement
Expand Down Expand Up @@ -361,13 +360,13 @@ conditional_statements
;

for_equation
: for_initializer LOOP
: FOR for_indices LOOP
conditional_equations
END FOR
;

for_statement
: for_initializer LOOP
: FOR for_indices LOOP
conditional_statements
END FOR
;
Expand Down Expand Up @@ -535,11 +534,11 @@ primary
| STRING
| BOOL
| unary_expression
| function_call
| (component_reference | DER | INITIAL | PURE) function_call_args
| component_reference
| LPAREN output_expression_list RPAREN
| matrix
| array
| LBRACK expression_list (SEMICOLON expression_list)* RBRACK
| LCURLY array_arguments RCURLY
| END
;

Expand All @@ -550,37 +549,6 @@ name
: IDENT (DOT IDENT)*
;

// not present in spec
matrix
: LBRACK matrix_arguments RBRACK
;

// not present in spec
matrix_arguments
: matrix_row (SEMICOLON matrix_row)*
;

// not present in spec
matrix_row
: expression_list
;

// not present in spec
array
: LCURLY array_arguments RCURLY
;

// not present in spec
function_call
: (
component_reference
| DER
| INITIAL
| PURE
)
function_call_args
;

component_reference
: DOT? IDENT array_subscripts? (DOT IDENT array_subscripts?)*
;
Expand All @@ -590,8 +558,17 @@ function_call_args
;

function_arguments
: (function_argument | named_argument)
((COMMA (function_argument | named_argument))* | for_initializer)
: expression FOR for_indices
| function_argument (COMMA function_argument)* named_arguments?
| named_arguments
;

named_arguments
: named_argument (COMMA named_argument)*
;

named_argument
: IDENT EQUAL function_argument
;

function_argument
Expand All @@ -603,48 +580,20 @@ function_partial_application
: FUNCTION type_specifier LPAREN named_arguments? RPAREN
;

named_arguments
: named_argument (COMMA named_argument)*
;

named_argument
: IDENT EQUAL (function_partial_application | expression)
;

output_expression_list
: expression? (COMMA expression?)*
;

expression_list
: expression_list_member (COMMA expression_list_member)*
;

// not present in spec
expression_list_member
: expression
: expression (COMMA expression)*
;

array_arguments
: array_argument ((COMMA array_argument)* | for_initializer)
;

// not present in spec
for_initializer
: FOR for_indices
;

// not present in spec
array_argument
: expression
: expression ((COMMA expression)* | FOR for_indices)
;

array_subscripts
: LBRACK subscript_list RBRACK
;

// not present in spec
subscript_list
: subscript (COMMA subscript)*
: LBRACK subscript (COMMA subscript)* RBRACK
;

subscript
Expand Down
27 changes: 6 additions & 21 deletions mofmt/collecting/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ class Marker:
DEDENT = 4
IGNORE = 5
BLANK = 6
HARDBREAK = 7
SOFTBREAK = 8
WRAPPOINT = 9
BREAK = 7
WRAPPOINT = 8

__slots__ = ("typ", "val", "rep")

Expand All @@ -50,7 +49,6 @@ class Collector:

def __init__(self) -> None:
self.markers: list[Marker] = []
self.wrapped: bool = False

def add_marker(self, marker: Marker) -> None:
"""Add marker"""
Expand Down Expand Up @@ -86,6 +84,8 @@ def add_space(self) -> None:
return
if self.markers[-1].typ >= Marker.IGNORE:
return
if self.markers[-1].typ == Marker.SPACE:
return
if (
self.markers[-1].typ in {Marker.INDENT, Marker.DEDENT}
and self.markers[-2].typ >= Marker.BLANK
Expand All @@ -101,31 +101,16 @@ def add_blank(self) -> None:
"""Add a blank marker"""
if self.markers[-1].typ >= Marker.BLANK:
self.markers.pop()
if self.wrapped:
self.add_dedent()
self.wrapped = False
self.add_marker(Marker(Marker.BLANK, "\n\n", "BLANK"))

def add_softbreak(self) -> None:
"""Add a soft break marker"""
if self.markers[-1].typ >= Marker.BLANK:
return
self.add_marker(Marker(Marker.SOFTBREAK, "\n", "SBREAK"))

def add_hardbreak(self) -> None:
def add_break(self) -> None:
"""Add a hard break marker"""
if self.markers[-1].typ >= Marker.BLANK:
return
if self.wrapped:
self.add_dedent()
self.wrapped = False
self.add_marker(Marker(Marker.HARDBREAK, "\n", "HBREAK"))
self.add_marker(Marker(Marker.BREAK, "\n", "BREAK"))

def add_wrappoint(self) -> None:
"""Add a soft break marker"""
if not self.wrapped:
self.add_indent()
self.wrapped = True
self.add_marker(Marker(Marker.WRAPPOINT, "\n", "WRAP"))

def add_indent(self) -> None:
Expand Down
15 changes: 3 additions & 12 deletions mofmt/parsing/generated/Modelica.interp

Large diffs are not rendered by default.

Loading

0 comments on commit 54c500f

Please sign in to comment.