Skip to content

Commit 3814da7

Browse files
committed
feat(Bazel): Add variable parsing to bazel StarlarkParser
Signed-off-by: Mateusz Los <155952887+losmateusz1@users.noreply.github.com>
1 parent d1b2c02 commit 3814da7

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,15 @@ 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) {
200202
require(currentToken.type == type) {
201203
"Unexpected token '${currentToken.value}' (${currentToken.type}) at ${lexer.locationInfo()}, expected: $type"
202204
}
203-
currentToken = lexer.nextToken()
205+
currentToken = lookaheadToken
206+
lookaheadToken = lexer.nextToken()
204207
}
205208

206209
private fun parseKeyValue(): Pair<String, String> {
@@ -211,6 +214,9 @@ internal class Parser(input: String) {
211214

212215
if (currentToken.type in setOf(TokenType.STRING, TokenType.NUMBER, TokenType.BOOLEAN)) {
213216
eat(currentToken.type)
217+
} else if (currentToken.type == TokenType.IDENTIFIER) {
218+
value = variables[currentToken.value] ?: currentToken.value
219+
eat(TokenType.IDENTIFIER)
214220
} else if (currentToken.type == TokenType.LBRACKET) { // Skip lists for now.
215221
while (currentToken.type != TokenType.RBRACKET) {
216222
eat(currentToken.type)
@@ -223,6 +229,16 @@ internal class Parser(input: String) {
223229
return key to value
224230
}
225231

232+
private fun parseVariableAssignment() {
233+
val name = currentToken.value
234+
eat(TokenType.IDENTIFIER)
235+
eat(TokenType.EQUALS)
236+
if (currentToken.type in setOf(TokenType.STRING, TokenType.NUMBER, TokenType.BOOLEAN)) {
237+
variables[name] = currentToken.value
238+
eat(currentToken.type)
239+
}
240+
}
241+
226242
private fun parseBazelDepDirective(): BazelDepDirective {
227243
eat(TokenType.IDENTIFIER)
228244
eat(TokenType.LPAREN)
@@ -268,9 +284,12 @@ internal class Parser(input: String) {
268284
var moduleDirective: ModuleDirective? = null
269285

270286
while (currentToken.type != TokenType.EOF) {
271-
when (currentToken.value) {
272-
"module" -> moduleDirective = parseModuleDirective()
273-
"bazel_dep" -> dependencies.add(parseBazelDepDirective())
287+
when {
288+
currentToken.value == "module" -> moduleDirective = parseModuleDirective()
289+
currentToken.value == "bazel_dep" -> dependencies.add(parseBazelDepDirective())
290+
currentToken.type == TokenType.IDENTIFIER && lookaheadToken.type == TokenType.EQUALS ->
291+
parseVariableAssignment()
292+
274293
else -> eat(currentToken.type) // Skip unknown tokens.
275294
}
276295
}

0 commit comments

Comments
 (0)