Skip to content

Commit 81dafb8

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

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,17 @@ internal class Lexer(private val input: String) {
193193
internal class Parser(input: String) {
194194
private val lexer = Lexer(input)
195195
private var currentToken: Token = lexer.nextToken()
196+
private var lookaheadToken: Token = lexer.nextToken()
197+
private val variables = mutableMapOf<String, String>()
196198

197199
private fun eat(type: TokenType) {
198200
require(currentToken.type == type) {
199201
"Unexpected token '${currentToken.value}' (${currentToken.type})" +
200202
" at ${lexer.locationInfo()}, expected: $type"
201203
}
202-
currentToken = lexer.nextToken()
204+
205+
currentToken = lookaheadToken
206+
lookaheadToken = lexer.nextToken()
203207
}
204208

205209
private fun parseKeyValue(): Pair<String, String> {
@@ -210,6 +214,9 @@ internal class Parser(input: String) {
210214

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

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

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

0 commit comments

Comments
 (0)