Skip to content

Commit

Permalink
feat(spdx-utils): Take the new parser implementation into use
Browse files Browse the repository at this point in the history
Replace the ANTLR based parser with the new implementation in
`SpdxExpression.parse()`.

Signed-off-by: Martin Nonnenmacher <[email protected]>
  • Loading branch information
mnonnenmacher committed Feb 6, 2024
1 parent 212d1ef commit 9e2e861
Showing 1 changed file with 3 additions and 18 deletions.
21 changes: 3 additions & 18 deletions utils/spdx/src/main/kotlin/SpdxExpression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer

import org.antlr.v4.runtime.CharStreams
import org.antlr.v4.runtime.CommonTokenStream

import org.ossreviewtoolkit.utils.spdx.SpdxConstants.DOCUMENT_REF_PREFIX
import org.ossreviewtoolkit.utils.spdx.SpdxConstants.LICENSE_REF_PREFIX
import org.ossreviewtoolkit.utils.spdx.parser.SpdxExpressionParser

/**
* An SPDX expression as defined by version 2.1 of the [SPDX specification, appendix IV][1].
Expand Down Expand Up @@ -86,21 +84,8 @@ sealed class SpdxExpression {
* allowed ([ALLOW_DEPRECATED][Strictness.ALLOW_DEPRECATED]) or only current license identifiers are allowed
* ([ALLOW_CURRENT][Strictness.ALLOW_CURRENT]). Throws an [SpdxException] if the string cannot be parsed.
*/
fun parse(expression: String, strictness: Strictness): SpdxExpression {
val charStream = CharStreams.fromString(expression)
val lexer = SpdxExpressionLexer(charStream).apply {
removeErrorListeners()
addErrorListener(SpdxErrorListener())
}

val tokenStream = CommonTokenStream(lexer)
val parser = SpdxExpressionParser(tokenStream).apply {
removeErrorListeners()
addErrorListener(SpdxErrorListener())
}

return SpdxExpressionDefaultVisitor(strictness).visit(parser.licenseExpression())
}
fun parse(expression: String, strictness: Strictness): SpdxExpression =
SpdxExpressionParser(expression, strictness).parse()
}

/**
Expand Down

0 comments on commit 9e2e861

Please sign in to comment.