diff options
author | Marvin Borner | 2019-08-12 18:41:05 +0200 |
---|---|---|
committer | Marvin Borner | 2019-08-12 18:41:05 +0200 |
commit | d9d9fbdd9d13a5eadc5c57ac19405983cfe11b2e (patch) | |
tree | bd0df880a66ae490384aa48ab2688632e73fbf1e | |
parent | bf5882f74f79a15dc5688e813e7d42ae62a44263 (diff) |
Improved exception handling
-rw-r--r-- | src/Lexical.kt | 21 | ||||
-rw-r--r-- | src/Testing.kt | 2 | ||||
-rw-r--r-- | src/exceptions/UnknownType.kt | 3 |
3 files changed, 21 insertions, 5 deletions
diff --git a/src/Lexical.kt b/src/Lexical.kt index 7b92755..1655805 100644 --- a/src/Lexical.kt +++ b/src/Lexical.kt @@ -1,12 +1,16 @@ +import exceptions.UnknownType + class Lexical { fun analyze(source: String): MutableList<Pair<String, TokenType>> { var buffer = "" + var stringMode = false val tokens = mutableListOf<Pair<String, TokenType>>() for (i in source.indices) { buffer += source[i] - val tokenType = getTokenType(buffer, if (source.length > i + 1) source[i + 1] else ' ') + val tokenType = getTokenType(buffer, if (source.length > i + 1) source[i + 1] else ' ', stringMode) if (tokenType != TokenType.Skip) { tokens += buffer to tokenType + if (buffer == "\"") stringMode = true buffer = "" } } @@ -16,7 +20,7 @@ class Lexical { /** * Matches the tokens to a [TokenType] */ - private fun getTokenType(token: String, next: Char): TokenType { + private fun getTokenType(token: String, next: Char, stringMode: Boolean): TokenType { return when { token + next in keyword -> TokenType.Skip token in keyword -> TokenType.Keyword @@ -41,7 +45,12 @@ class Lexical { token in punctuation -> TokenType.Punctuation - token == " " -> TokenType.Empty + token in brackets -> TokenType.Bracket + + token in classifier -> TokenType.Classifier + + token.contains(" ") && token.length > 1 && !stringMode -> throw UnknownType(token) + token == " " && !stringMode -> TokenType.Empty else -> TokenType.Skip } @@ -53,6 +62,8 @@ class Lexical { private val comparison = listOf("==", "!=", "<", "<=", ">", ">=") private val logical = listOf("&&", "||", "!") private val punctuation = listOf(",", ":", ".") + private val brackets = listOf("(", ")", "[", "]", "{", "}") // TODO: Use brackets for functions + private val classifier = listOf("\"") // TODO: Add char mode e.g 'a' } enum class TokenType { @@ -61,9 +72,11 @@ enum class TokenType { Arithmetic, Comparison, Logical, - Punctuation, Identifier, Constant, + Punctuation, + Bracket, + Classifier, Empty, Skip }
\ No newline at end of file diff --git a/src/Testing.kt b/src/Testing.kt index 0b16145..4e4ec0d 100644 --- a/src/Testing.kt +++ b/src/Testing.kt @@ -4,7 +4,7 @@ class Testing { val tokens = Lexical().analyze(source) for (token in tokens) { print(token.first) - print("\n") + print(" => ") print(token.second) print("\n") } diff --git a/src/exceptions/UnknownType.kt b/src/exceptions/UnknownType.kt new file mode 100644 index 0000000..4c348a5 --- /dev/null +++ b/src/exceptions/UnknownType.kt @@ -0,0 +1,3 @@ +package exceptions + +class UnknownType(token: String = "") : Exception("Token type not known: $token")
\ No newline at end of file |