Skip to content

Commit ef2c0a7

Browse files
committed
fix(Bazel): Make StarlarkParser more verbose for Module.bazel issues
Signed-off-by: Mateusz Los <mateusz.los@extern.wenovate.de>
1 parent d965a78 commit ef2c0a7

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

plugins/package-managers/bazel/src/main/kotlin/StarlarkParser.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ internal data class Token(val type: TokenType, val value: String)
5757

5858
internal class Lexer(private val input: String) {
5959
private var pos = 0
60+
61+
fun locationInfo(): String {
62+
val lines = input.substring(0, pos).split('\n')
63+
val line = lines.size
64+
val column = lines.last().length + 1
65+
val lineText = input.lines().getOrElse(line - 1) { "" }
66+
return "line $line, column $column: '$lineText'"
67+
}
68+
6069
private val length = input.length
6170

6271
private fun peek(offset: Int = 0): Char = if (pos + offset < length) input[pos + offset] else '\u0000'
@@ -188,7 +197,9 @@ internal class Parser(input: String) {
188197
private var currentToken: Token = lexer.nextToken()
189198

190199
private fun eat(type: TokenType) {
191-
require(currentToken.type == type) { "Unexpected token: ${currentToken.type}, expected: $type" }
200+
require(currentToken.type == type) {
201+
"Unexpected token '${currentToken.value}' (${currentToken.type}) at ${lexer.locationInfo()}, expected: $type"
202+
}
192203
currentToken = lexer.nextToken()
193204
}
194205

0 commit comments

Comments
 (0)