From bdfbbaca6ed7fb4616a312bbb9de320101311ff8 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 15 Aug 2019 18:46:23 +0200 Subject: Fixed token merging --- src/runMain/kotlin/Syntax.kt | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src/runMain/kotlin/Syntax.kt') 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>) { - 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>, index: Int, allowed: List) { - if (nextNonEmpty(statement, index).second !in allowed) { - throw SyntaxError(nextNonEmpty(statement, index).first) + private fun allowNext(statement: MutableList>, index: kotlin.Int, allowed: List) { + 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>, index: Int): Pair { + private fun nextNonEmpty(statement: MutableList>, index: kotlin.Int): Pair { 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 -- cgit v1.2.3