diff options
author | Marvin Borner | 2019-08-15 18:46:23 +0200 |
---|---|---|
committer | Marvin Borner | 2019-08-15 18:46:23 +0200 |
commit | bdfbbaca6ed7fb4616a312bbb9de320101311ff8 (patch) | |
tree | d3d3dccb04ae2660077ede46ab6c9ee7be10f87e | |
parent | 5707ecbabfe3b9b580fcc7b2ebdfef6a60786c40 (diff) |
Fixed token merging
-rw-r--r-- | src/runMain/kotlin/Syntax.kt | 28 | ||||
-rw-r--r-- | src/runMain/kotlin/exceptions/SyntaxError.kt | 4 |
2 files changed, 17 insertions, 15 deletions
diff --git a/src/runMain/kotlin/Syntax.kt b/src/runMain/kotlin/Syntax.kt index a950e77..d479356 100644 --- a/src/runMain/kotlin/Syntax.kt +++ b/src/runMain/kotlin/Syntax.kt @@ -22,11 +22,9 @@ class Syntax { Identifier -> allowNext(statement, j, listOf(Keyword, Assignment)) Punctuation -> allowNext(statement, j, listOf(Constant)) Bracket -> allowNext(statement, j, listOf(Constant, Keyword, Logical, Assignment)) - Classifier -> { - } - Empty -> { - } - else -> throw UnknownType(token.first) + Classifier -> Unit + Empty -> Unit + Skip -> throw UnknownType(token.first) } } } @@ -60,30 +58,32 @@ class Syntax { * Merges tokens by same type */ private fun mergeTokens(statement: MutableList<Pair<String, TokenType>>) { - statement.forEachIndexed { i, token -> - if (statement.size > i + 1 && statement[i + 1].second == token.second) { - statement[i] = statement[i].first + statement[i + 1].first to token.second + var i = 0 + while (statement.size > i + 1) { + if (statement[i].second == statement[i + 1].second) { // TODO: Differentiate int and string + statement[i] = statement[i].first + statement[i + 1].first to statement[i].second statement.removeAt(i + 1) + i-- } + i++ } } /** * Throws [SyntaxError] if the next token is not in [allowed] */ - private fun allowNext(statement: MutableList<Pair<String, TokenType>>, index: Int, allowed: List<TokenType>) { - if (nextNonEmpty(statement, index).second !in allowed) { - throw SyntaxError(nextNonEmpty(statement, index).first) + private fun allowNext(statement: MutableList<Pair<String, TokenType>>, index: kotlin.Int, allowed: List<TokenType>) { + if (statement.size > index + 1 && nextNonEmpty(statement, index).second !in allowed) { + throw SyntaxError(nextNonEmpty(statement, index)) } } /** * Finds the next non empty token by [index] */ - private fun nextNonEmpty(statement: MutableList<Pair<String, TokenType>>, index: Int): Pair<String, TokenType> { + private fun nextNonEmpty(statement: MutableList<Pair<String, TokenType>>, index: kotlin.Int): Pair<String, TokenType> { var i = index + 1 - while (statement[i].second == Empty) - i++ + while (statement[i].second == Empty) i++ return statement[i] } }
\ No newline at end of file diff --git a/src/runMain/kotlin/exceptions/SyntaxError.kt b/src/runMain/kotlin/exceptions/SyntaxError.kt index 3fc2a69..0596f52 100644 --- a/src/runMain/kotlin/exceptions/SyntaxError.kt +++ b/src/runMain/kotlin/exceptions/SyntaxError.kt @@ -1,6 +1,8 @@ package exceptions +import TokenType + /** * Gets thrown if the syntax is wrong */ -class SyntaxError(context: String = "") : Exception("Syntax error: $context")
\ No newline at end of file +class SyntaxError(context: Pair<String, TokenType>) : Exception("Unexpected ${context.second}: ${context.first}")
\ No newline at end of file |