Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Adds a better error message for unexpected tokens after the workspace…
Browse files Browse the repository at this point in the history
… definition.
  • Loading branch information
simonbrowndotje committed Jan 26, 2023
1 parent 9a6a719 commit c79a127
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/main/java/com/structurizr/dsl/StructurizrDslParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -762,9 +762,14 @@ void parse(List<String> lines, File dslFile) throws StructurizrDslParserExceptio
} else {
String[] expectedTokens;
if (getContext() == null) {
expectedTokens = new String[] {
StructurizrDslTokens.WORKSPACE_TOKEN
};
if (getWorkspace() == null) {
// the workspace hasn't yet been created
expectedTokens = new String[]{
StructurizrDslTokens.WORKSPACE_TOKEN
};
} else {
expectedTokens = new String[0];
}
} else {
expectedTokens = getContext().getPermittedTokens();
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/dsl/unexpected-tokens-after-workspace.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
workspace {
}

hello world
4 changes: 4 additions & 0 deletions src/test/dsl/unexpected-tokens-before-workspace.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
hello world

workspace {
}
1 change: 0 additions & 1 deletion src/test/dsl/unexpected-tokens-in-dsl.dsl

This file was deleted.

15 changes: 13 additions & 2 deletions src/test/java/com/structurizr/dsl/DslTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -927,16 +927,27 @@ void test_excludeRelationships() throws Exception {
}

@Test
void test_unexpectedTokensInDsl() throws Exception {
void test_unexpectedTokensBeforeWorkspace() throws Exception {
try {
StructurizrDslParser parser = new StructurizrDslParser();
parser.parse(new File("src/test/dsl/unexpected-tokens-in-dsl.dsl"));
parser.parse(new File("src/test/dsl/unexpected-tokens-before-workspace.dsl"));
fail();
} catch (StructurizrDslParserException e) {
assertEquals("Unexpected tokens (expected: workspace) at line 1: hello world", e.getMessage());
}
}

@Test
void test_unexpectedTokensAfterWorkspace() throws Exception {
try {
StructurizrDslParser parser = new StructurizrDslParser();
parser.parse(new File("src/test/dsl/unexpected-tokens-after-workspace.dsl"));
fail();
} catch (StructurizrDslParserException e) {
assertEquals("Unexpected tokens at line 4: hello world", e.getMessage());
}
}

@Test
void test_unexpectedTokensInWorkspace() throws Exception {
try {
Expand Down

0 comments on commit c79a127

Please sign in to comment.