-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lua4jvm: Fix Antlr 4 syntax error handling
- Loading branch information
Showing
3 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
lua4jvm/src/main/java/fi/benjami/code4jvm/lua/compiler/LuaSyntaxException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package fi.benjami.code4jvm.lua.compiler; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import fi.benjami.code4jvm.lua.LuaVm; | ||
|
||
/** | ||
* Thrown by the {@link LuaVm Lua VM} when source code given to it contains | ||
* syntax errors. Although the VM will refuse to load such code, it will | ||
* attempt to parse through the entire chunk and report all identified syntax | ||
* errors. | ||
* | ||
*/ | ||
public class LuaSyntaxException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private final List<Error> errors; | ||
|
||
public LuaSyntaxException(List<Error> errors) { | ||
super(makeMsg(errors)); | ||
if (errors.isEmpty()) { | ||
throw new IllegalArgumentException("cannot to create syntax error without errors"); | ||
} | ||
this.errors = errors; | ||
} | ||
|
||
private static String makeMsg(List<Error> errors) { | ||
return errors.stream() | ||
.map(error -> error.chunkName + ":" + error.line + ":" + error.posInLine + " " + error.message) | ||
.collect(Collectors.joining("\n")); | ||
} | ||
|
||
public record Error( | ||
String chunkName, | ||
int line, | ||
int posInLine, | ||
String message | ||
) {} | ||
|
||
public List<Error> errors() { | ||
return errors; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
lua4jvm/src/test/java/fi/benjami/code4jvm/lua/test/CompilerErrorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package fi.benjami.code4jvm.lua.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import fi.benjami.code4jvm.lua.LuaVm; | ||
import fi.benjami.code4jvm.lua.compiler.LuaSyntaxException; | ||
|
||
public class CompilerErrorTest { | ||
|
||
private final LuaVm vm = new LuaVm(); | ||
|
||
@Test | ||
public void syntaxError() { | ||
try { | ||
vm.compile("\nfunction #() end"); | ||
} catch (LuaSyntaxException e) { | ||
var errors = e.errors(); | ||
assertEquals(1, errors.size()); | ||
var err = errors.get(0); | ||
assertEquals("unknown", err.chunkName()); | ||
assertEquals(2, err.line()); | ||
assertEquals(9, err.posInLine()); | ||
// Don't check the message; it is currently whatever antlr 4 sees fit | ||
return; | ||
} | ||
fail(); | ||
} | ||
|
||
@Test | ||
public void multipleErrors() { | ||
try { | ||
vm.compile(""" | ||
function #() end | ||
function #() end | ||
"""); | ||
} catch (LuaSyntaxException e) { | ||
var errors = e.errors(); | ||
assertEquals(2, errors.size()); | ||
var first = errors.get(0); | ||
assertEquals("unknown", first.chunkName()); | ||
assertEquals(1, first.line()); | ||
assertEquals(9, first.posInLine()); | ||
|
||
var second = errors.get(1); | ||
assertEquals("unknown", second.chunkName()); | ||
assertEquals(3, second.line()); | ||
assertEquals(9, second.posInLine()); | ||
return; | ||
} | ||
fail(); | ||
} | ||
} |