2222
2323package org.ossreviewtoolkit.plugins.packagemanagers.bazel
2424
25+ import org.ossreviewtoolkit.utils.common.enumSetOf
26+
2527internal data class ModuleMetadata (
2628 val module : ModuleDirective ? = null ,
2729 val dependencies : List <BazelDepDirective >
@@ -193,13 +195,17 @@ internal class Lexer(private val input: String) {
193195internal class Parser (input : String ) {
194196 private val lexer = Lexer (input)
195197 private var currentToken: Token = lexer.nextToken()
198+ private var lookaheadToken: Token = lexer.nextToken()
199+ private val variables = mutableMapOf<String , String >()
196200
197201 private fun eat (type : TokenType ) {
198202 require(currentToken.type == type) {
199203 " Unexpected token '${currentToken.value} ' (${currentToken.type} )" +
200204 " at ${lexer.locationInfo()} , expected: $type "
201205 }
202- currentToken = lexer.nextToken()
206+
207+ currentToken = lookaheadToken
208+ lookaheadToken = lexer.nextToken()
203209 }
204210
205211 private fun parseKeyValue (): Pair <String , String > {
@@ -210,6 +216,9 @@ internal class Parser(input: String) {
210216
211217 if (currentToken.type in setOf (TokenType .STRING , TokenType .NUMBER , TokenType .BOOLEAN )) {
212218 eat(currentToken.type)
219+ } else if (currentToken.type == TokenType .IDENTIFIER ) {
220+ value = variables[currentToken.value] ? : currentToken.value
221+ eat(TokenType .IDENTIFIER )
213222 } else if (currentToken.type == TokenType .LBRACKET ) { // Skip lists for now.
214223 while (currentToken.type != TokenType .RBRACKET ) {
215224 eat(currentToken.type)
@@ -222,6 +231,16 @@ internal class Parser(input: String) {
222231 return key to value
223232 }
224233
234+ private fun parseVariableAssignment () {
235+ val name = currentToken.value
236+ eat(TokenType .IDENTIFIER )
237+ eat(TokenType .EQUALS )
238+ if (currentToken.type in enumSetOf(TokenType .STRING , TokenType .NUMBER , TokenType .BOOLEAN )) {
239+ variables[name] = currentToken.value
240+ eat(currentToken.type)
241+ }
242+ }
243+
225244 private fun parseBazelDepDirective (): BazelDepDirective {
226245 eat(TokenType .IDENTIFIER )
227246 eat(TokenType .LPAREN )
@@ -267,9 +286,14 @@ internal class Parser(input: String) {
267286 var moduleDirective: ModuleDirective ? = null
268287
269288 while (currentToken.type != TokenType .EOF ) {
270- when (currentToken.value) {
271- " module" -> moduleDirective = parseModuleDirective()
272- " bazel_dep" -> dependencies.add(parseBazelDepDirective())
289+ when {
290+ currentToken.value == " module" -> moduleDirective = parseModuleDirective()
291+
292+ currentToken.value == " bazel_dep" -> dependencies + = parseBazelDepDirective()
293+
294+ currentToken.type == TokenType .IDENTIFIER && lookaheadToken.type == TokenType .EQUALS ->
295+ parseVariableAssignment()
296+
273297 else -> eat(currentToken.type) // Skip unknown tokens.
274298 }
275299 }
0 commit comments