diff options
author | Marvin Borner | 2019-08-15 22:05:55 +0200 |
---|---|---|
committer | Marvin Borner | 2019-08-15 22:05:55 +0200 |
commit | cc8c02d780f703e0ea547e79dacf6a571686e1df (patch) | |
tree | 9937b657405332abacc1eec580077041b9da5708 /src/runMain/kotlin/Syntax.kt | |
parent | 49db23f1c8c3bf6c858eb75b70f9ffd0a839fb3c (diff) |
Improved syntax analyser
Diffstat (limited to 'src/runMain/kotlin/Syntax.kt')
-rw-r--r-- | src/runMain/kotlin/Syntax.kt | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/src/runMain/kotlin/Syntax.kt b/src/runMain/kotlin/Syntax.kt index 19d8505..938814d 100644 --- a/src/runMain/kotlin/Syntax.kt +++ b/src/runMain/kotlin/Syntax.kt @@ -10,18 +10,22 @@ class Syntax { removePadding(statement) mergeStrings(statement) mergeTokens(statement) - // print(statement) + statement.forEach { println("${it.content} ${it.type}") } statement.forEachIndexed { j, token -> + if (statement.count { it.content == "(" } != statement.count { it.content == ")" }) + throw SyntaxError("Bracket", "Brackets are not even") + when (token.type) { - Keyword -> allowNext(statement, j, listOf(Assignment, Constant)) + Keyword -> allowNext(statement, j, listOf(Bracket)) + Function -> allowNext(statement, j, listOf(Bracket)) Constant -> allowNext(statement, j, listOf(Comparison, Arithmetic, Logical, Bracket)) - Assignment -> allowNext(statement, j, listOf(Constant)) - Arithmetic -> allowNext(statement, j, listOf(Constant)) - Comparison -> allowNext(statement, j, listOf(Constant)) - Logical -> allowNext(statement, j, listOf(Constant)) - Identifier -> allowNext(statement, j, listOf(Keyword, Assignment)) + Assignment -> allowNext(statement, j, listOf(Constant, Function)) + Arithmetic -> allowNext(statement, j, listOf(Constant, Function)) + Comparison -> allowNext(statement, j, listOf(Constant, Function)) + Logical -> allowNext(statement, j, listOf(Constant, Function)) + Variable -> allowNext(statement, j, listOf(Assignment, Bracket, Arithmetic, Logical, Comparison)) Punctuation -> allowNext(statement, j, listOf(Constant)) - Bracket -> allowNext(statement, j, listOf(Constant, Keyword, Logical, Assignment)) + Bracket -> allowNext(statement, j, listOf(Constant, Keyword, Logical, Assignment, Variable)) Classifier -> Unit Empty -> Unit Skip -> throw UnknownType(token.content) @@ -77,7 +81,8 @@ class Syntax { */ private fun allowNext(statement: MutableList<Token>, index: kotlin.Int, allowed: List<TokenType>) { if (statement.size > index + 1 && nextNonEmpty(statement, index).type !in allowed) { - throw SyntaxError(nextNonEmpty(statement, index)) + val token = nextNonEmpty(statement, index) + throw SyntaxError(token.type.toString(), token.content) } } |