Skip to content

Commit

Permalink
Fix script text should not end if the rbrace ('}') is not the only on…
Browse files Browse the repository at this point in the history
…e in line

According to structurizr dsl parser, the script ends once a line with a single rbrace ('}') is met.

See https://github.com/structurizr/java/blob/39bf3d0890afc4caa52cf3302269a12fc2a4236b/structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java#L208
  • Loading branch information
goto1134 committed Apr 8, 2024
1 parent 98bf9a4 commit d6e30ce
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
41 changes: 14 additions & 27 deletions src/main/grammar/StructurizrDSL.flex
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,6 @@ import static nl.dirkgroot.structurizr.dsl.psi.SDTypes.*;
public StructurizrDSLLexer() {
this(null);
}

private void startScript() {
braces = 0;
yybegin(EXPECT_SCRIPT);
}

private void endScript() {
braces = 0;
yybegin(YYINITIAL);
}
%}

%{
private int braces = 0;
%}

%ignorecase
Expand Down Expand Up @@ -61,7 +47,7 @@ LineComment=("/"{MultiLineSeparatorsWithSpaces}?"/"|"#") {NonCrLf}*
BlockCommentStart="/"{MultiLineSeparatorsWithSpaces}?"*"
BlockCommentEnd="*"{MultiLineSeparatorsWithSpaces}?"/"

ScriptText=[{NonEOL}--[{}]]+
ScriptText=({EscapeOrMultiLineSeparator}|{NonEOL})

Arrow=-{MultiLineSeparatorsWithSpaces}?>
%%
Expand All @@ -85,17 +71,18 @@ Arrow=-{MultiLineSeparatorsWithSpaces}?>
}

<EXPECT_SCRIPT> {
"{" { braces++; }
"}" {
if (braces == 0) {
endScript();
yypushback(1);
return SCRIPT_TEXT;
}
braces--;
}
{CrLf} { }
{ScriptText} { }
{SpaceOrMultiLineSeparator}*\}{SpaceOrMultiLineSeparator}*{CrLf}? {
int rbracePosExclusive = StringUtil.lastIndexOf(yytext(), '}', 0, yylength());
yypushback(yylength() - rbracePosExclusive);
yybegin(EXPECT_NON_COMMENT);
return SCRIPT_TEXT;
}
{ScriptText}*{CrLf} { }
{ScriptText}+ {
// EOF met
yybegin(YYINITIAL);
return SCRIPT_TEXT;
}
}

// STATES
Expand Down Expand Up @@ -126,7 +113,7 @@ Arrow=-{MultiLineSeparatorsWithSpaces}?>
}

<EXPECT_SCRIPT_ARGUMENTS> {
"{" { startScript(); return BRACE1; }
"{" { yybegin(EXPECT_SCRIPT); return BRACE1; }
{QuotedText} { return QUOTED_TEXT; }
{UnquotedText} { return UNQUOTED_TEXT; }
[^] { yybegin(YYINITIAL); yypushback(yylength()); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,57 @@ class ScriptBlockTest {
)
}

@Test
fun `ends if multiline separator before rbrace`() {
"""
!script kotlin {
some arbitrary code
\
}
""".trimIndent().tokenize() shouldContainExactly listOf(
UNQUOTED_TEXT to "!script",
WHITE_SPACE to " ",
UNQUOTED_TEXT to "kotlin",
WHITE_SPACE to " ",
BRACE1 to "{",
SCRIPT_TEXT to "\n some arbitrary code\n\\\n",
BRACE2 to "}"
)
}

@Test
fun `does not end if the line contains other symbols before rbrace`() {
"""
!script kotlin {
some arbitrary code
test }
""".trimIndent().tokenize() shouldContainExactly listOf(
UNQUOTED_TEXT to "!script",
WHITE_SPACE to " ",
UNQUOTED_TEXT to "kotlin",
WHITE_SPACE to " ",
BRACE1 to "{",
SCRIPT_TEXT to "\n some arbitrary code\ntest }",
)
}

@Test
fun `does not end if the line contains other symbols after rbrace`() {
"""
!script kotlin {
some arbitrary code
} \
test
""".trimIndent().tokenize() shouldContainExactly listOf(
UNQUOTED_TEXT to "!script",
WHITE_SPACE to " ",
UNQUOTED_TEXT to "kotlin",
WHITE_SPACE to " ",
BRACE1 to "{",
SCRIPT_TEXT to "\n some arbitrary code\n} \\\ntest",
)
}

@Test
fun `indented block`() {
"""
Expand Down

0 comments on commit d6e30ce

Please sign in to comment.