diff --git a/src/main/kotlin/dsl/ast/parser/Parser.kt b/src/main/kotlin/dsl/ast/parser/Parser.kt index 218dd0b..2715b45 100644 --- a/src/main/kotlin/dsl/ast/parser/Parser.kt +++ b/src/main/kotlin/dsl/ast/parser/Parser.kt @@ -8,6 +8,7 @@ import dsl.token.model.entity.Token import dsl.token.model.entity.TokenList import dsl.token.model.entity.TokenPosition import dsl.token.model.enumeration.TokenType +import error.InvalidSyntaxException abstract class Parser { fun parse(input: TokenList): ParserOutput { @@ -177,7 +178,7 @@ abstract class Parser { val output = parser.parse(input) if (output.result == ParsingResult.FAILED) { - throw RuntimeException("Expected $expected") + throw InvalidSyntaxException("Expected $expected, got '${input.current.rawValue}'", input.currentPos()) } return output diff --git a/src/main/kotlin/dsl/token/tokenizer/DefaultTokenizer.kt b/src/main/kotlin/dsl/token/tokenizer/DefaultTokenizer.kt index 6ddeedf..af54959 100644 --- a/src/main/kotlin/dsl/token/tokenizer/DefaultTokenizer.kt +++ b/src/main/kotlin/dsl/token/tokenizer/DefaultTokenizer.kt @@ -2,6 +2,7 @@ package dsl.token.tokenizer import dsl.token.model.entity.Token import dsl.token.model.entity.TokenList +import dsl.token.model.entity.TokenPosition import dsl.token.model.entity.TokenizerOutput import dsl.token.model.enumeration.TokenType import dsl.token.tokenizer.Tokenizer.Companion.default @@ -9,6 +10,7 @@ import dsl.token.tokenizer.Tokenizer.Companion.keywords import dsl.token.tokenizer.Tokenizer.Companion.mapValue import dsl.token.tokenizer.Tokenizer.Companion.regex import dsl.token.tokenizer.Tokenizer.Companion.separated +import error.InvalidSyntaxException class DefaultTokenizer : Tokenizer { private val tokenizers = listOf( @@ -89,7 +91,7 @@ class DefaultTokenizer : Tokenizer { val output = tokenize(line, current, index) if (!output.consumed()) { - throw RuntimeException("Unknown symbol ${line[current]}") + throw InvalidSyntaxException("Unknown symbol ${line[current]}", TokenPosition(index, current, -1)) } current += output.consumedChars diff --git a/src/main/kotlin/error/InvalidSyntaxException.kt b/src/main/kotlin/error/InvalidSyntaxException.kt new file mode 100644 index 0000000..f6cef2e --- /dev/null +++ b/src/main/kotlin/error/InvalidSyntaxException.kt @@ -0,0 +1,8 @@ +package error + +import dsl.token.model.entity.TokenPosition + +class InvalidSyntaxException(message: String?, val position: TokenPosition?) : Exception(message) { + override val message: String? + get() = super.message + if(position != null) " $position" else "" +} \ No newline at end of file