Dave Leeds
01/09/2018, 6:50 PMprivate enum class NestingCharacters(val open: String, val close: String) {
BRACE("{", "}"),
PAREN("(", ")"),
BRACKET("[", "]");
fun apply(char: String, stack: Stack<String>) {
when (char) {
open -> stack.push(char)
close -> {
val top = stack.peek()
if (stack.isStackEmpty()) throw SyntaxErrorTwoException(char)
if (top == open) stack.pop() else throw SyntaxErrorThreeException(top, char)
}
}
}
}
fun linter2(line: String): Boolean {
with(Stack<String>()) {
line.split("").forEach { char ->
NestingCharacters.values().forEach { it.apply(char, this) }
}
if (isNotEmpty()) throw SyntaxErrorOneException(peek())
}
return true
}
chi
01/09/2018, 9:08 PMBRACE
, PAREN
and BRACKET
?Dave Leeds
01/09/2018, 10:01 PMenum
, so that’s the full definition in the code listing above.BRACE("{", "}")
is invoking the constructor on NestingCharacters
, setting open
to {
and close
to }
.