Skip to content

Commit 392feed

Browse files
committed
Inline PropositionalFormulaParser
The interface `PropositionalFormulaParser` only ever had the one default instance. Furthermore, there are no real use cases to change its behaviour without changing the only user of it, `PreprocessorAnnotationParser`, because the string syntax couples them very extremely. Note that the `WontfixTestCases` are actually parsed correctly by now.
1 parent 07756a1 commit 392feed

9 files changed

Lines changed: 168 additions & 291 deletions

File tree

src/main/java/org/variantsync/diffdetective/datasets/predefined/Marlin.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.variantsync.diffdetective.datasets.PatchDiffParseOptions;
44
import org.variantsync.diffdetective.datasets.Repository;
55
import org.variantsync.diffdetective.feature.PreprocessorAnnotationParser;
6-
import org.variantsync.diffdetective.feature.PropositionalFormulaParser;
76

87
import java.nio.file.Path;
98

@@ -15,7 +14,6 @@
1514
public class Marlin {
1615
public static final PreprocessorAnnotationParser ANNOTATION_PARSER =
1716
PreprocessorAnnotationParser.CreateCppAnnotationParser(
18-
PropositionalFormulaParser.Default,
1917
new MarlinCPPDiffLineFormulaExtractor()
2018
);
2119

src/main/java/org/variantsync/diffdetective/feature/AbstractingFormulaExtractor.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.variantsync.diffdetective.feature;
22

3+
import org.prop4j.Literal;
4+
import org.prop4j.Node;
5+
import org.prop4j.NodeReader;
36
import org.tinylog.Logger;
47
import org.variantsync.diffdetective.error.UncheckedUnParseableFormulaException;
58
import org.variantsync.diffdetective.error.UnparseableFormulaException;
6-
import org.variantsync.diffdetective.feature.cpp.AbstractingCExpressionVisitor;
7-
import org.variantsync.diffdetective.feature.cpp.ControllingCExpressionVisitor;
9+
import org.variantsync.diffdetective.util.fide.FixTrueFalse;
810

911
import java.util.function.Supplier;
1012
import java.util.regex.Matcher;
@@ -43,7 +45,7 @@ public AbstractingFormulaExtractor(Pattern annotationPattern) {
4345
* @return The extracted and abstracted formula
4446
*/
4547
@Override
46-
public String extractFormula(final String text) throws UnparseableFormulaException {
48+
public Node extractFormula(final String text) throws UnparseableFormulaException {
4749
final Matcher matcher = annotationPattern.matcher(text);
4850
final Supplier<UnparseableFormulaException> couldNotExtractFormula = () ->
4951
new UnparseableFormulaException("Could not extract formula from line \"" + text + "\".");
@@ -74,7 +76,33 @@ public String extractFormula(final String text) throws UnparseableFormulaExcepti
7476
throw couldNotExtractFormula.get();
7577
}
7678

77-
return fm;
79+
return parseFormula(fm);
80+
}
81+
82+
/**
83+
* Parser that uses the {@link NodeReader} from FeatureIDE and uses its
84+
* {@link NodeReader#activateJavaSymbols() java symbols} to match operators.
85+
*/
86+
protected Node parseFormula(String text) {
87+
final NodeReader nodeReader = new NodeReader();
88+
nodeReader.activateJavaSymbols();
89+
90+
Node node = nodeReader.stringToNode(text);
91+
92+
// if parsing succeeded
93+
if (node != null) {
94+
// TODO: Is this our desired behaviour?
95+
// If so, should we document it by not using get here
96+
// and instead keeping the witness that this call happened?
97+
node = FixTrueFalse.EliminateTrueAndFalseInplace(node).get();
98+
}
99+
100+
if (node == null) {
101+
// Logger.warn("Could not parse expression '{}' to feature mapping. Usin>
102+
node = new Literal(text);
103+
}
104+
105+
return node;
78106
}
79107

80108
/**

src/main/java/org/variantsync/diffdetective/feature/DiffLineFormulaExtractor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.variantsync.diffdetective.feature;
22

3+
import org.prop4j.Node;
34
import org.variantsync.diffdetective.error.UnparseableFormulaException;
45

56
/**
@@ -21,7 +22,7 @@ public interface DiffLineFormulaExtractor {
2122
* @param line The line of which to get the feature mapping
2223
* @return The feature mapping as a String of the given line
2324
*/
24-
String extractFormula(final String line) throws UnparseableFormulaException;
25+
Node extractFormula(final String line) throws UnparseableFormulaException;
2526

2627
/**
2728
* Resolves any macros in the given formula that are relevant for feature annotations.

src/main/java/org/variantsync/diffdetective/feature/PreprocessorAnnotationParser.java

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,64 +34,49 @@ public class PreprocessorAnnotationParser implements AnnotationParser {
3434

3535
/**
3636
* Default parser for C preprocessor annotations.
37-
* Created by invoking {@link #PreprocessorAnnotationParser(Pattern, PropositionalFormulaParser, DiffLineFormulaExtractor)}.
37+
* Created by invoking {@link #PreprocessorAnnotationParser(Pattern, DiffLineFormulaExtractor)}.
3838
*/
3939
public static final PreprocessorAnnotationParser CPPAnnotationParser =
40-
new PreprocessorAnnotationParser(CPP_PATTERN, PropositionalFormulaParser.Default, new CPPDiffLineFormulaExtractor());
40+
new PreprocessorAnnotationParser(CPP_PATTERN, new CPPDiffLineFormulaExtractor());
4141

4242
/**
4343
* Default parser for <a href="https://www.slashdev.ca/javapp/">JavaPP (Java PreProcessor)</a> annotations.
44-
* Created by invoking {@link #PreprocessorAnnotationParser(Pattern, PropositionalFormulaParser, DiffLineFormulaExtractor)}.
44+
* Created by invoking {@link #PreprocessorAnnotationParser(Pattern, DiffLineFormulaExtractor)}.
4545
*/
4646
public static final PreprocessorAnnotationParser JPPAnnotationParser =
47-
new PreprocessorAnnotationParser(JPP_PATTERN, PropositionalFormulaParser.Default, new JPPDiffLineFormulaExtractor());
47+
new PreprocessorAnnotationParser(JPP_PATTERN, new JPPDiffLineFormulaExtractor());
4848

4949
// Pattern that is used to identify the AnnotationType of a given annotation.
5050
private final Pattern annotationPattern;
51-
private final PropositionalFormulaParser formulaParser;
5251
private final DiffLineFormulaExtractor extractor;
5352

54-
/**
55-
* Invokes {@link #PreprocessorAnnotationParser(Pattern, PropositionalFormulaParser, DiffLineFormulaExtractor)} with
56-
* the {@link PropositionalFormulaParser#Default default formula parser} and a new {@link DiffLineFormulaExtractor}.
57-
*
58-
* @param annotationPattern Pattern that is used to identify the AnnotationType of a given annotation; {@link #CPP_PATTERN} provides an example
59-
*/
60-
public PreprocessorAnnotationParser(final Pattern annotationPattern, final DiffLineFormulaExtractor formulaExtractor) {
61-
this(annotationPattern, PropositionalFormulaParser.Default, formulaExtractor);
62-
}
63-
6453
/**
6554
* Creates a new preprocessor annotation parser.
6655
*
6756
* @param annotationPattern Pattern that is used to identify the AnnotationType of a given annotation; {@link #CPP_PATTERN} provides an example
68-
* @param formulaParser Parser that is used to parse propositional formulas in conditional annotations (e.g., the formula <code>f</code> in <code>#if f</code>).
69-
* @param formulaExtractor An extractor that extracts the formula part of a preprocessor annotation that is then given to the formulaParser.
57+
* @param formulaExtractor An extractor that extracts the formula part of a preprocessor annotation
7058
*/
71-
public PreprocessorAnnotationParser(final Pattern annotationPattern, final PropositionalFormulaParser formulaParser, DiffLineFormulaExtractor formulaExtractor) {
59+
public PreprocessorAnnotationParser(final Pattern annotationPattern, DiffLineFormulaExtractor formulaExtractor) {
7260
this.annotationPattern = annotationPattern;
73-
this.formulaParser = formulaParser;
7461
this.extractor = formulaExtractor;
7562
}
7663

7764
/**
7865
* Creates a new preprocessor annotation parser for C preprocessor annotations.
7966
*
80-
* @param formulaParser Parser that is used to parse propositional formulas in conditional annotations (e.g., the formula <code>f</code> in <code>#if f</code>).
81-
* @param formulaExtractor An extractor that extracts the formula part of a preprocessor annotation that is then given to the formulaParser.
67+
* @param formulaExtractor An extractor that extracts the formula part of a preprocessor annotation
8268
*/
83-
public static PreprocessorAnnotationParser CreateCppAnnotationParser(final PropositionalFormulaParser formulaParser, DiffLineFormulaExtractor formulaExtractor) {
84-
return new PreprocessorAnnotationParser(CPP_PATTERN, formulaParser, formulaExtractor);
69+
public static PreprocessorAnnotationParser CreateCppAnnotationParser(DiffLineFormulaExtractor formulaExtractor) {
70+
return new PreprocessorAnnotationParser(CPP_PATTERN, formulaExtractor);
8571
}
8672

8773
/**
8874
* Creates a new preprocessor annotation parser for <a href="https://www.slashdev.ca/javapp/">JavaPP (Java PreProcessor)</a> annotations.
8975
*
90-
* @param formulaParser Parser that is used to parse propositional formulas in conditional annotations (e.g., the formula <code>f</code> in <code>#if f</code>).
91-
* @param formulaExtractor An extractor that extracts the formula part of a preprocessor annotation that is then given to the formulaParser.
76+
* @param formulaExtractor An extractor that extracts the formula part of a preprocessor annotation
9277
*/
93-
public static PreprocessorAnnotationParser CreateJppAnnotationParser(final PropositionalFormulaParser formulaParser, DiffLineFormulaExtractor formulaExtractor) {
94-
return new PreprocessorAnnotationParser(JPP_PATTERN, formulaParser, formulaExtractor);
78+
public static PreprocessorAnnotationParser CreateJppAnnotationParser(DiffLineFormulaExtractor formulaExtractor) {
79+
return new PreprocessorAnnotationParser(JPP_PATTERN, formulaExtractor);
9580
}
9681

9782
/**
@@ -103,7 +88,7 @@ public static PreprocessorAnnotationParser CreateJppAnnotationParser(final Propo
10388
* @throws UnparseableFormulaException when {@link DiffLineFormulaExtractor#extractFormula(String)} throws.
10489
*/
10590
public Node parseAnnotation(String line) throws UnparseableFormulaException {
106-
return this.formulaParser.parse(extractor.extractFormula(line));
91+
return extractor.extractFormula(line);
10792
}
10893

10994
@Override

src/main/java/org/variantsync/diffdetective/feature/PropositionalFormulaParser.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/main/java/org/variantsync/diffdetective/feature/cpp/CPPDiffLineFormulaExtractor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.antlr.v4.runtime.CharStreams;
44
import org.antlr.v4.runtime.CommonTokenStream;
5+
import org.prop4j.Node;
6+
import org.prop4j.Not;
57
import org.variantsync.diffdetective.error.UnparseableFormulaException;
68
import org.variantsync.diffdetective.feature.AbstractingFormulaExtractor;
79
import org.variantsync.diffdetective.feature.ParseErrorListener;
@@ -36,14 +38,14 @@ public CPPDiffLineFormulaExtractor() {
3638
* @return The feature mapping as a String of the given line
3739
*/
3840
@Override
39-
public String extractFormula(final String line) throws UnparseableFormulaException {
41+
public Node extractFormula(final String line) throws UnparseableFormulaException {
4042
// Delegate the formula extraction to AbstractingFormulaExtractor
41-
String fm = super.extractFormula(line);
43+
Node fm = super.extractFormula(line);
4244

4345
// negate for ifndef
4446
final Matcher matcher = CPP_ANNOTATION_PATTERN.matcher(line);
4547
if (matcher.find() && "ifndef".equals(matcher.group(1))) {
46-
fm = "!(" + fm + ")";
48+
fm = new Not(fm);
4749
}
4850

4951
return fm;

0 commit comments

Comments
 (0)