Dave Leeds
private 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
BRACE
PAREN
BRACKET
enum
BRACE("{", "}")
NestingCharacters
open
{
close
}
A modern programming language that makes developers happier.