Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 8e2a2a0

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Parse mos-file statements using iteration
Recursion caused very long mos-files to crash during the parsing. Doing a listReverseInPlace is a small price to pay for not crashing. Note: There will be no testcase for this since you need ~45MB mos-files to trigger this on an -Os optimized omc (really only triggers for -O0 -g).
1 parent f9882c3 commit 8e2a2a0

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

Parser/Modelica.g

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,20 +1676,24 @@ interactive_stmt returns [void* ast]
16761676
@declarations { int last_sc = 0; }
16771677
@init{ ss = 0; } :
16781678
// A list of expressions or algorithms separated by semicolons and optionally ending with a semicolon
1679-
BOM? ss=interactive_stmt_list[&last_sc] EOF
1679+
BOM? ss=interactive_stmt_list (SEMICOLON {last_sc=1;})? EOF
16801680
{
16811681
ast = GlobalScript__ISTMTS(or_nil(ss), mmc_mk_bcon(last_sc));
16821682
}
16831683
;
16841684
1685-
interactive_stmt_list [int *last_sc] returns [void* ast]
1686-
@init { a.ast = 0; $ast = 0; void *val; } :
1687-
a=top_algorithm ( (SEMICOLON ss=interactive_stmt_list[last_sc]) | (SEMICOLON { *last_sc = 1; }) | /* empty */ )
1688-
{
1689-
$ast = mmc_mk_cons(a.ast, or_nil(ss));
1690-
}
1685+
interactive_stmt_list returns [void* ast]
1686+
@init { a.ast = 0; $ast = mmc_mk_nil(); void *val; } :
1687+
a=top_algorithm {$ast = mmc_mk_cons(a.ast, $ast);} (SEMICOLON a=top_algorithm {$ast = mmc_mk_cons(a.ast, $ast);})*
1688+
{
1689+
/* We build the list using iteration instead of recursion to save
1690+
* stack space, so we need to reverse the result. */
1691+
$ast = listReverseInPlace($ast);
1692+
}
16911693
;
16921694
1695+
1696+
16931697
/* MetaModelica */
16941698
match_expression returns [void* ast]
16951699
@init{ ty = 0; exp.ast = 0; cmt = 0; es = 0; cs = 0; } :

0 commit comments

Comments
 (0)