aboutsummaryrefslogtreecommitdiff
path: root/src/runMain/kotlin/Syntax.kt
diff options
context:
space:
mode:
authorMarvin Borner2019-08-15 19:49:48 +0200
committerMarvin Borner2019-08-15 19:49:48 +0200
commit49db23f1c8c3bf6c858eb75b70f9ffd0a839fb3c (patch)
tree0c621eb66b2a4ea57b858e866764754fb7028579 /src/runMain/kotlin/Syntax.kt
parentbdfbbaca6ed7fb4616a312bbb9de320101311ff8 (diff)
Abstracted pair to token class
Diffstat (limited to 'src/runMain/kotlin/Syntax.kt')
-rw-r--r--src/runMain/kotlin/Syntax.kt37
1 files changed, 20 insertions, 17 deletions
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