Skip to content

Commit

Permalink
Improve config syntax errors (#91)
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Sherman <[email protected]>
  • Loading branch information
bentsherman authored Jan 7, 2025
1 parent 2a0fa3c commit 96e73bc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
33 changes: 31 additions & 2 deletions modules/compiler/src/main/antlr/ConfigParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ configStatement
| configBlock #configBlockStmtAlt
| configAppendBlock #configAppendBlockStmtAlt
| configIncomplete #configIncompleteStmtAlt
| invalidStatement #configInvalidStmtAlt
;

// -- include statement
Expand Down Expand Up @@ -127,6 +128,7 @@ configBlockStatement
| configAppendBlock #configAppendBlockBlockStmtAlt
| configSelector #configSelectorBlockStmtAlt
| configIncomplete #configIncompleteBlockStmtAlt
| invalidStatement #configInvalidBlockStmtAlt
;

configSelector
Expand All @@ -147,6 +149,15 @@ configIncomplete
: configPrimary (DOT configPrimary)* DOT?
;

//
// -- invalid statements
//
invalidStatement
: ifElseStatement
| tryCatchStatement
| variableDeclaration
;


//
// statements
Expand Down Expand Up @@ -198,7 +209,7 @@ assertStatement

// -- variable declaration
variableDeclaration
: DEF identifier (nls ASSIGN nls initializer=expression)?
: (DEF | legacyType | DEF legacyType) identifier (nls ASSIGN nls initializer=expression)?
| DEF variableNames nls ASSIGN nls initializer=expression
;

Expand Down Expand Up @@ -338,6 +349,7 @@ pathElement

// method call expression (with closure)
| closure #closurePathExprAlt
| closureWithLabels #closureWithLabelsPathExprAlt

// method call expression
| arguments #argumentsPathExprAlt
Expand Down Expand Up @@ -419,7 +431,20 @@ formalParameterList
;

formalParameter
: identifier (nls ASSIGN nls expression)?
: DEF? legacyType? identifier (nls ASSIGN nls expression)?
;

closureWithLabels
: LBRACE (nls (formalParameterList nls)? ARROW)? nls blockStatementsWithLabels RBRACE
;

blockStatementsWithLabels
: statementOrLabeled (sep statementOrLabeled)* sep?
;

statementOrLabeled
: identifier COLON nls statementOrLabeled
| statement
;

// -- list expression
Expand Down Expand Up @@ -508,6 +533,10 @@ typeArguments
: LT type (COMMA type)* GT
;

legacyType
: type (LBRACK RBRACK)*
;


//
// keywords, whitespace
Expand Down
30 changes: 27 additions & 3 deletions modules/compiler/src/main/java/config/parser/ConfigAstBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public ModuleNode buildAST() {

private ModuleNode compilationUnit(CompilationUnitContext ctx) {
for( var stmt : ctx.configStatement() )
moduleNode.addConfigStatement(configStatement(stmt));
configStatement(stmt);

var scriptClassNode = moduleNode.getScriptClassDummy();
var statements = moduleNode.getConfigStatements();
Expand All @@ -201,7 +201,7 @@ private ModuleNode compilationUnit(CompilationUnitContext ctx) {
return moduleNode;
}

private ConfigStatement configStatement(ConfigStatementContext ctx) {
private void configStatement(ConfigStatementContext ctx) {
ConfigStatement result;

if( ctx instanceof ConfigIncludeStmtAltContext ciac )
Expand All @@ -219,11 +219,16 @@ else if( ctx instanceof ConfigAppendBlockStmtAltContext cbac )
else if( ctx instanceof ConfigIncompleteStmtAltContext ciac )
result = ast( configIncomplete(ciac.configIncomplete()), ciac );

else if( ctx instanceof ConfigInvalidStmtAltContext ciac ) {
invalidStatement(ciac.invalidStatement());
return;
}

else
throw createParsingFailedException("Invalid statement: " + ctx.getText(), ctx);

saveLeadingComments(result, ctx);
return result;
moduleNode.addConfigStatement(result);
}

private ConfigStatement configInclude(ConfigIncludeContext ctx) {
Expand Down Expand Up @@ -256,6 +261,7 @@ private ConfigStatement configBlock(ConfigBlockContext ctx) {
var name = configPrimary(ctx.configPrimary());
var statements = ctx.configBlockStatement().stream()
.map(this::configBlockStatement)
.filter(stmt -> stmt != null)
.collect(Collectors.toList());
return new ConfigBlockNode(name, statements);
}
Expand All @@ -281,6 +287,11 @@ else if( ctx instanceof ConfigSelectorBlockStmtAltContext csac )
else if( ctx instanceof ConfigIncompleteBlockStmtAltContext ciac )
result = ast( configIncomplete(ciac.configIncomplete()), ciac );

else if( ctx instanceof ConfigInvalidBlockStmtAltContext ciac ) {
invalidStatement(ciac.invalidStatement());
return null;
}

else
throw createParsingFailedException("Invalid statement in config block: " + ctx.getText(), ctx);

Expand Down Expand Up @@ -320,6 +331,19 @@ private ConfigStatement configIncomplete(ConfigIncompleteContext ctx) {
return result;
}

private void invalidStatement(InvalidStatementContext ctx) {
String message;
if( ctx.ifElseStatement() != null )
message = "If statements cannot be mixed with config statements";
else if( ctx.tryCatchStatement() != null )
message = "Try-catch blocks cannot be mixed with config statements";
else if( ctx.variableDeclaration() != null )
message = "Variable declarations cannot be mixed with config statements";
else
message = "Invalid config statement";
collectSyntaxError(new SyntaxException(message, ast( new EmptyStatement(), ctx )));
}

/// STATEMENTS

private Statement statement(StatementContext ctx) {
Expand Down

0 comments on commit 96e73bc

Please sign in to comment.