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 12, 2024
1 parent 8cb1a79 commit 41e6f98
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 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
9 changes: 4 additions & 5 deletions utils/spdx/src/test/kotlin/SpdxExpressionParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -176,32 +176,31 @@ class SpdxExpressionParserTest : WordSpec({
SpdxExpression.parse("license WITH exception+")
}

exception.message shouldBe "extraneous input '+' expecting <EOF>"
exception.message shouldBe "Unexpected token 'PLUS(position=23)'."
}

"fail if a compound expression is used before WITH" {
val exception = shouldThrow<SpdxException> {
SpdxExpression.parse("(license1 AND license2) WITH exception")
}

exception.message shouldBe "mismatched input 'WITH' expecting {AND, OR, ')', '+'}"
exception.message shouldBe "Unexpected token 'WITH(position=25)'."
}

"fail on an invalid symbol" {
val exception = shouldThrow<SpdxException> {
SpdxExpression.parse("/")
}

exception.message shouldBe "token recognition error at: '/'"
exception.message shouldBe "Unexpected character '/' at position 1."
}

"fail on a syntax error" {
val exception = shouldThrow<SpdxException> {
SpdxExpression.parse("((")
}

exception.message shouldBe
"mismatched input '<EOF>' expecting {'(', DOCUMENTREFERENCE, LICENSEREFERENCE, IDSTRING}"
exception.message shouldBe "Unexpected token 'null'."
}
}
})

0 comments on commit 41e6f98

Please sign in to comment.