chi
01/09/2018, 1:48 PMfun linter(line: String): Boolean {
val myStack = Stack<String>()
line.split("").forEach { char ->
when (char) {
"{", "(", "[" -> myStack.push(char)
"}", ")", "]" -> {
if (!myStack.isStackEmpty()) {
val topStackItem = myStack.peek()
if (topStackItem != null) {
when (char) {
"}" -> {
if (topStackItem == "{")myStack.pop()
else throw SyntaxErrorThreeException(topStackItem, char)
}
"]" -> {
if (topStackItem == "[") myStack.pop()
else throw SyntaxErrorThreeException(topStackItem, char)
}
")" -> {
if (topStackItem == "(") myStack.pop()
else throw SyntaxErrorThreeException(topStackItem, char)
}
}
}
} else {
throw SyntaxErrorTwoException(char)
}
}
}
}
if (!myStack.isStackEmpty()) throw SyntaxErrorOneException(myStack.peek()!!)
else return true
}