diff --git a/build.gradle b/build.gradle index a85cb17..9aad2a6 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ apply plugin: 'jacoco' apply plugin: "com.madvay.tools.build.gitbuildinfo" buildscript { - ext.kotlin_version = '1.0.5-2' + ext.kotlin_version = '1.0.6' repositories { jcenter() @@ -20,10 +20,8 @@ buildscript { } } -sourceCompatibility = 1.7 group = "com.github.sybila.ctl" -archivesBaseName = "ctl-parser" -version = '2.2.0' +version = '2.2.3' //antlr config generateGrammarSource { @@ -91,4 +89,5 @@ test { testLogging { events "skipped", "failed", "standardOut", "standardError" } + void } \ No newline at end of file diff --git a/src/main/kotlin/com/github/sybila/huctl/HUCTLParser.kt b/src/main/kotlin/com/github/sybila/huctl/HUCTLParser.kt index a5f8f83..702f23c 100644 --- a/src/main/kotlin/com/github/sybila/huctl/HUCTLParser.kt +++ b/src/main/kotlin/com/github/sybila/huctl/HUCTLParser.kt @@ -23,21 +23,21 @@ import java.util.* * and returns a final map of valid formula assignments */ -class HUCTLParser() { +class HUCTLParser { fun formula(input: String): Formula = parse("val = $input")["val"]!! fun dirFormula(input: String): DirectionFormula = (formula("{$input}EX True") as? Formula.Simple<*>)?.direction - ?: throw IllegalStateException("$input is not a direction formula") + ?: throw IllegalArgumentException("$input is not a direction formula") fun atom(input: String): Formula.Atom = formula(input) as? Formula.Atom - ?: throw IllegalStateException("$input is not an atom") + ?: throw IllegalArgumentException("$input is not an atom") fun dirAtom(input: String): DirectionFormula.Atom = dirFormula(input) as? DirectionFormula.Atom - ?: throw IllegalStateException("$input is not a direction atom") + ?: throw IllegalArgumentException("$input is not a direction atom") fun parse(input: String, onlyFlagged: Boolean = false): Map = process(FileParser().process(input), onlyFlagged) @@ -228,7 +228,7 @@ private class FileParser { lexer.removeErrorListeners() lexer.addErrorListener(errorListener) parser.removeErrorListeners() - lexer.addErrorListener(errorListener) + parser.addErrorListener(errorListener) val root = parser.root() val context = FileContext(location) ParseTreeWalker().walk(context, root) @@ -260,7 +260,7 @@ private data class ParserContext( .map { one -> assignments.filter { two -> one.name == two.name }.toSet().toList() } .filter { it.size > 1 } .any { - throw IllegalStateException( + throw IllegalArgumentException( "Duplicate assignment for ${it[0].name} defined in ${it[0].location} and ${it[1].location}" ) } @@ -306,7 +306,7 @@ private class FileContext(val location: String) : HUCTLBaseListener() { } override fun visitErrorNode(node: ErrorNode) { - throw IllegalStateException("Syntax error at '${node.text}' in $location:${node.symbol.line}") + throw IllegalArgumentException("Syntax error at '${node.text}' in $location:${node.symbol.line}") } /* ------ Formula Parsing ------ */ diff --git a/src/test/kotlin/com/github/sybila/huctl/ParserBasicTest.kt b/src/test/kotlin/com/github/sybila/huctl/ParserBasicTest.kt index 374768d..c900701 100644 --- a/src/test/kotlin/com/github/sybila/huctl/ParserBasicTest.kt +++ b/src/test/kotlin/com/github/sybila/huctl/ParserBasicTest.kt @@ -411,7 +411,7 @@ class Basic { @Test fun dirFormulaInvalidParse() { - assertFailsWith { + assertFailsWith { parser.dirFormula("True && EX False") } } @@ -425,7 +425,7 @@ class Basic { @Test fun atomInvalidParse() { - assertFailsWith { + assertFailsWith { parser.atom("EX x > 2.2") } } @@ -438,9 +438,17 @@ class Basic { @Test fun dirAtomInvalidParse() { - assertFailsWith { + assertFailsWith { parser.dirAtom("x+ && y-") } } + + @Test + fun garbageParse() { + assertFailsWith { + parser.parse(" foo ") + } + } + } \ No newline at end of file diff --git a/src/test/kotlin/com/github/sybila/huctl/ParserReferencesTest.kt b/src/test/kotlin/com/github/sybila/huctl/ParserReferencesTest.kt index 99033ae..99ec78f 100644 --- a/src/test/kotlin/com/github/sybila/huctl/ParserReferencesTest.kt +++ b/src/test/kotlin/com/github/sybila/huctl/ParserReferencesTest.kt @@ -93,7 +93,7 @@ class References { it.write("k = True") } - assertFailsWith(IllegalStateException::class) { + assertFailsWith(IllegalArgumentException::class) { parser.parse( ":include \"${ i1.absolutePath }\" \n" + "k = False" @@ -105,7 +105,7 @@ class References { @Test fun duplicateDeclarationInString() { - assertFailsWith(IllegalStateException::class) { + assertFailsWith(IllegalArgumentException::class) { parser.parse(""" k = True l = False @@ -116,7 +116,7 @@ class References { @Test fun duplicateDeclarationExpression() { - assertFailsWith(IllegalStateException::class) { + assertFailsWith(IllegalArgumentException::class) { parser.parse(""" k = 1 l = 2