Skip to content

Commit

Permalink
Recursively rework precedence.
Browse files Browse the repository at this point in the history
  • Loading branch information
waynebeaton committed Jun 9, 2020
1 parent f1b039a commit 3512922
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public int getPrecedence() {

@Override
public boolean matchesApproved(SpdxBinaryOperation operation, Collection<String> approved) {
// TODO Implement this.
return false;
}

Expand Down Expand Up @@ -115,9 +116,9 @@ public static SpdxBinaryOperation create(Operator operator, SpdxExpression left,
}

public static SpdxBinaryOperation create(Operator operator, SpdxExpression left, SpdxBinaryOperation right) {
if (operator.getPrecedence() > right.operator.getPrecedence()) {
SpdxBinaryOperation primary = new SpdxBinaryOperation(operator, left, right.left);
return new SpdxBinaryOperation(right.operator, primary, right.right);
if (operator.getPrecedence() >= right.operator.getPrecedence()) {
SpdxBinaryOperation primary = create(operator, left, right.left);
return create(right.operator, primary, right.right);
}
return new SpdxBinaryOperation(operator, left, right);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void testConjunction() {

@Test
void testMultipleConjunction() {
assertEquals("(EPL-1.0 AND (Apache-2.0 AND MIT))",
assertEquals("((EPL-1.0 AND Apache-2.0) AND MIT)",
new SpdxExpressionParser().parse("EPL-1.0 AND Apache-2.0 AND MIT").toString());
}

Expand Down Expand Up @@ -127,6 +127,18 @@ void testExceptionPrecedence2() {
new SpdxExpressionParser().parse("EPL-1.0 WITH Exception OR MIT").toString());
}

@Test
void testExceptionPrecedence3() {
assertEquals("(((EPL-1.0 AND Apache-2.0) OR MIT) OR BSD0)",
new SpdxExpressionParser().parse("EPL-1.0 AND Apache-2.0 OR MIT OR BSD0").toString());
}

@Test
void testExceptionPrecedence4() {
assertEquals("(((EPL-1.0 AND Apache-2.0) OR (MIT WITH STUFF)) OR BSD0)",
new SpdxExpressionParser().parse("EPL-1.0 AND Apache-2.0 OR MIT WITH STUFF OR BSD0").toString());
}

@Test
void testMatchSimple() {
assertTrue(new SpdxExpressionParser().parse("EPL-2.0").matchesApproved(Collections.singleton("EPL-2.0")));
Expand Down

0 comments on commit 3512922

Please sign in to comment.