Skip to content

Commit 19fbdee

Browse files
committed
feat(Bazel): Add variable parsing to bazel StarlarkParser
Signed-off-by: Mateusz Los <mateusz.los@extern.wenovate.de>
1 parent ef2c0a7 commit 19fbdee

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,16 @@ internal class Lexer(private val input: String) {
195195
internal class Parser(input: String) {
196196
private val lexer = Lexer(input)
197197
private var currentToken: Token = lexer.nextToken()
198+
private var lookaheadToken: Token = lexer.nextToken()
199+
private val variables = mutableMapOf<String, String>()
198200

199201
private fun eat(type: TokenType) {
200-
require(currentToken.type == type) {
201-
"Unexpected token '${currentToken.value}' (${currentToken.type}) at ${lexer.locationInfo()}, expected: $type"
202-
}
203-
currentToken = lexer.nextToken()
202+
val message = "Unexpected token '${currentToken.value}' (${currentToken.type})" +
203+
" at ${lexer.locationInfo()}, expected: $type"
204+
require(currentToken.type == type) { message }
205+
206+
currentToken = lookaheadToken
207+
lookaheadToken = lexer.nextToken()
204208
}
205209

206210
private fun parseKeyValue(): Pair<String, String> {
@@ -211,6 +215,9 @@ internal class Parser(input: String) {
211215

212216
if (currentToken.type in setOf(TokenType.STRING, TokenType.NUMBER, TokenType.BOOLEAN)) {
213217
eat(currentToken.type)
218+
} else if (currentToken.type == TokenType.IDENTIFIER) {
219+
value = variables[currentToken.value] ?: currentToken.value
220+
eat(TokenType.IDENTIFIER)
214221
} else if (currentToken.type == TokenType.LBRACKET) { // Skip lists for now.
215222
while (currentToken.type != TokenType.RBRACKET) {
216223
eat(currentToken.type)
@@ -223,6 +230,16 @@ internal class Parser(input: String) {
223230
return key to value
224231
}
225232

233+
private fun parseVariableAssignment() {
234+
val name = currentToken.value
235+
eat(TokenType.IDENTIFIER)
236+
eat(TokenType.EQUALS)
237+
if (currentToken.type in setOf(TokenType.STRING, TokenType.NUMBER, TokenType.BOOLEAN)) {
238+
variables[name] = currentToken.value
239+
eat(currentToken.type)
240+
}
241+
}
242+
226243
private fun parseBazelDepDirective(): BazelDepDirective {
227244
eat(TokenType.IDENTIFIER)
228245
eat(TokenType.LPAREN)
@@ -268,9 +285,14 @@ internal class Parser(input: String) {
268285
var moduleDirective: ModuleDirective? = null
269286

270287
while (currentToken.type != TokenType.EOF) {
271-
when (currentToken.value) {
272-
"module" -> moduleDirective = parseModuleDirective()
273-
"bazel_dep" -> dependencies.add(parseBazelDepDirective())
288+
when {
289+
currentToken.value == "module" -> moduleDirective = parseModuleDirective()
290+
291+
currentToken.value == "bazel_dep" -> dependencies.add(parseBazelDepDirective())
292+
293+
currentToken.type == TokenType.IDENTIFIER && lookaheadToken.type == TokenType.EQUALS ->
294+
parseVariableAssignment()
295+
274296
else -> eat(currentToken.type) // Skip unknown tokens.
275297
}
276298
}

0 commit comments

Comments
 (0)