diff options
author | Marvin Borner | 2019-08-15 19:49:48 +0200 |
---|---|---|
committer | Marvin Borner | 2019-08-15 19:49:48 +0200 |
commit | 49db23f1c8c3bf6c858eb75b70f9ffd0a839fb3c (patch) | |
tree | 0c621eb66b2a4ea57b858e866764754fb7028579 | |
parent | bdfbbaca6ed7fb4616a312bbb9de320101311ff8 (diff) |
Abstracted pair to token class
-rw-r--r-- | src/runMain/kotlin/Lexical.kt | 12 | ||||
-rw-r--r-- | src/runMain/kotlin/Syntax.kt | 37 | ||||
-rw-r--r-- | src/runMain/kotlin/Token.kt | 5 | ||||
-rw-r--r-- | src/runMain/kotlin/exceptions/SyntaxError.kt | 4 |
4 files changed, 35 insertions, 23 deletions
diff --git a/src/runMain/kotlin/Lexical.kt b/src/runMain/kotlin/Lexical.kt index dbf2bad..73b15aa 100644 --- a/src/runMain/kotlin/Lexical.kt +++ b/src/runMain/kotlin/Lexical.kt @@ -6,19 +6,23 @@ class Lexical { * Analyzes the given [source] and returns the tokens divided into an array of statements * TODO: Add line number to token (abstract to class?) */ - fun analyze(source: String): MutableList<MutableList<Pair<String, TokenType>>> { + fun analyze(source: String): MutableList<MutableList<Token>> { var buffer = "" var skipStatementEnd = false var statementEnd: Boolean - val statements = mutableListOf<MutableList<Pair<String, TokenType>>>() - val currentStatement = mutableListOf<Pair<String, TokenType>>() + val statements = mutableListOf<MutableList<Token>>() + val currentStatement = mutableListOf<Token>() for (i in source.indices) { buffer += source[i] if (source[i] == '"') skipStatementEnd = !skipStatementEnd statementEnd = source[i] == ';' && !skipStatementEnd val tokenType = getTokenType(buffer, if (source.length > i + 1) source[i + 1] else ' ') if (tokenType != Skip && !statementEnd) { - currentStatement.add(buffer to tokenType) + val newToken = Token() + newToken.content = buffer + newToken.type = tokenType + newToken.lineNumber = 13 + currentStatement.add(newToken) buffer = "" } else if (statementEnd) { statements.add(currentStatement.toMutableList()) diff --git a/src/runMain/kotlin/Syntax.kt b/src/runMain/kotlin/Syntax.kt index d479356..19d8505 100644 --- a/src/runMain/kotlin/Syntax.kt +++ b/src/runMain/kotlin/Syntax.kt @@ -5,14 +5,14 @@ class Syntax { /** * Checks and validates whether the code complies with the syntax/grammar rules */ - fun check(statements: MutableList<MutableList<Pair<String, TokenType>>>): Boolean { + fun check(statements: MutableList<MutableList<Token>>): Boolean { statements.forEach { statement -> removePadding(statement) mergeStrings(statement) mergeTokens(statement) - print(statement) + // print(statement) statement.forEachIndexed { j, token -> - when (token.second) { + when (token.type) { Keyword -> allowNext(statement, j, listOf(Assignment, Constant)) Constant -> allowNext(statement, j, listOf(Comparison, Arithmetic, Logical, Bracket)) Assignment -> allowNext(statement, j, listOf(Constant)) @@ -24,7 +24,7 @@ class Syntax { Bracket -> allowNext(statement, j, listOf(Constant, Keyword, Logical, Assignment)) Classifier -> Unit Empty -> Unit - Skip -> throw UnknownType(token.first) + Skip -> throw UnknownType(token.content) } } } @@ -34,34 +34,37 @@ class Syntax { /** * Removes empty characters from the start and end of statements */ - private fun removePadding(statement: MutableList<Pair<String, TokenType>>) { - while (statement[0].second == Empty) + private fun removePadding(statement: MutableList<Token>) { + while (statement[0].type == Empty) statement.removeAt(0) - while (statement[statement.size - 1].second == Empty) + while (statement[statement.size - 1].type == Empty) statement.removeAt(statement.size - 1) } /** * Merges classified strings to constants */ - private fun mergeStrings(statement: MutableList<Pair<String, TokenType>>) { + private fun mergeStrings(statement: MutableList<Token>) { var stringing = false statement.forEachIndexed { i, token -> - if (token.second == Classifier) + if (token.type == Classifier) stringing = !stringing - if (stringing || token.second == Classifier) statement[i] = token.first to Constant + if (stringing || token.type == Classifier) { + token.type = Constant + statement[i] = token + } } } /** * Merges tokens by same type */ - private fun mergeTokens(statement: MutableList<Pair<String, TokenType>>) { + private fun mergeTokens(statement: MutableList<Token>) { 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 + if (statement[i].type == statement[i + 1].type) { // TODO: Differentiate int and string + statement[i].content = statement[i].content + statement[i + 1].content statement.removeAt(i + 1) i-- } @@ -72,8 +75,8 @@ class Syntax { /** * Throws [SyntaxError] if the next token is not in [allowed] */ - 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) { + 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)) } } @@ -81,9 +84,9 @@ class Syntax { /** * Finds the next non empty token by [index] */ - private fun nextNonEmpty(statement: MutableList<Pair<String, TokenType>>, index: kotlin.Int): Pair<String, TokenType> { + private fun nextNonEmpty(statement: MutableList<Token>, index: kotlin.Int): Token { var i = index + 1 - while (statement[i].second == Empty) i++ + while (statement[i].type == Empty) i++ return statement[i] } }
\ No newline at end of file diff --git a/src/runMain/kotlin/Token.kt b/src/runMain/kotlin/Token.kt new file mode 100644 index 0000000..9b7e622 --- /dev/null +++ b/src/runMain/kotlin/Token.kt @@ -0,0 +1,5 @@ +class Token { + lateinit var content: String + lateinit var type: TokenType + var lineNumber: kotlin.Int = 0 +} diff --git a/src/runMain/kotlin/exceptions/SyntaxError.kt b/src/runMain/kotlin/exceptions/SyntaxError.kt index 0596f52..952187e 100644 --- a/src/runMain/kotlin/exceptions/SyntaxError.kt +++ b/src/runMain/kotlin/exceptions/SyntaxError.kt @@ -1,8 +1,8 @@ package exceptions -import TokenType +import Token /** * Gets thrown if the syntax is wrong */ -class SyntaxError(context: Pair<String, TokenType>) : Exception("Unexpected ${context.second}: ${context.first}")
\ No newline at end of file +class SyntaxError(context: Token) : Exception("Unexpected ${context.type}: ${context.content}")
\ No newline at end of file |