Skip to content

Commit

Permalink
fix error handling problems
Browse files Browse the repository at this point in the history
  • Loading branch information
daemontus committed Jan 24, 2017
1 parent 406a83f commit 33085b0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
7 changes: 3 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -91,4 +89,5 @@ test {
testLogging {
events "skipped", "failed", "standardOut", "standardError"
}
void
}
14 changes: 7 additions & 7 deletions src/main/kotlin/com/github/sybila/huctl/HUCTLParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Formula>
= process(FileParser().process(input), onlyFlagged)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}"
)
}
Expand Down Expand Up @@ -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 ------ */
Expand Down
14 changes: 11 additions & 3 deletions src/test/kotlin/com/github/sybila/huctl/ParserBasicTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ class Basic {

@Test
fun dirFormulaInvalidParse() {
assertFailsWith<IllegalStateException> {
assertFailsWith<IllegalArgumentException> {
parser.dirFormula("True && EX False")
}
}
Expand All @@ -425,7 +425,7 @@ class Basic {

@Test
fun atomInvalidParse() {
assertFailsWith<IllegalStateException> {
assertFailsWith<IllegalArgumentException> {
parser.atom("EX x > 2.2")
}
}
Expand All @@ -438,9 +438,17 @@ class Basic {

@Test
fun dirAtomInvalidParse() {
assertFailsWith<IllegalStateException> {
assertFailsWith<IllegalArgumentException> {
parser.dirAtom("x+ && y-")
}
}


@Test
fun garbageParse() {
assertFailsWith<IllegalArgumentException> {
parser.parse(" foo ")
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class References {
it.write("k = True")
}

assertFailsWith(IllegalStateException::class) {
assertFailsWith(IllegalArgumentException::class) {
parser.parse(
":include \"${ i1.absolutePath }\" \n" +
"k = False"
Expand All @@ -105,7 +105,7 @@ class References {

@Test
fun duplicateDeclarationInString() {
assertFailsWith(IllegalStateException::class) {
assertFailsWith(IllegalArgumentException::class) {
parser.parse("""
k = True
l = False
Expand All @@ -116,7 +116,7 @@ class References {

@Test
fun duplicateDeclarationExpression() {
assertFailsWith(IllegalStateException::class) {
assertFailsWith(IllegalArgumentException::class) {
parser.parse("""
k = 1
l = 2
Expand Down

0 comments on commit 33085b0

Please sign in to comment.