-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from seart-group/feature/quantifier
- Loading branch information
Showing
6 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ch.usi.si.seart.treesitter; | ||
|
||
/** | ||
* Represents the {@link Capture} quantifier in a {@link Pattern}, | ||
* i.e. the number of nodes that a capture should contain. Within a | ||
* query, a capture can have different quantifiers for each pattern. | ||
* | ||
* @since 1.12.0 | ||
* @author Ozren Dabić | ||
*/ | ||
public enum Quantifier { | ||
|
||
/** | ||
* The capture will not match any nodes, | ||
* as said capture is not present in a | ||
* specific pattern. | ||
*/ | ||
ZERO, | ||
/** | ||
* The capture will match at most one node. | ||
* Example: | ||
* <pre>{@code | ||
* ((_)? @capture) | ||
* }</pre> | ||
*/ | ||
ZERO_OR_ONE, | ||
/** | ||
* The capture will match any number of nodes. | ||
* Example: | ||
* <pre>{@code | ||
* ((_)* @capture) | ||
* }</pre> | ||
*/ | ||
ZERO_OR_MORE, | ||
/** | ||
* The capture will match exactly one node. | ||
* Example: | ||
* <pre>{@code | ||
* ((_) @capture) | ||
* }</pre> | ||
*/ | ||
ONE, | ||
/** | ||
* The capture will match at least one node. | ||
* Example: | ||
* <pre>{@code | ||
* ((_)+ @capture) | ||
* }</pre> | ||
*/ | ||
ONE_OR_MORE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/test/java/ch/usi/si/seart/treesitter/QuantifierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package ch.usi.si.seart.treesitter; | ||
|
||
import lombok.Cleanup; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.ArgumentsProvider; | ||
import org.junit.jupiter.params.provider.ArgumentsSource; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
class QuantifierTest extends BaseTest { | ||
|
||
private static final Language language = Language.JAVA; | ||
|
||
private static final Map<Quantifier, String> symbols = Map.of( | ||
Quantifier.ONE, "", | ||
Quantifier.ONE_OR_MORE, "+", | ||
Quantifier.ZERO_OR_ONE, "?", | ||
Quantifier.ZERO_OR_MORE, "*" | ||
); | ||
|
||
private static final class QuantifierArgumentsProvider implements ArgumentsProvider { | ||
|
||
@Override | ||
public Stream<? extends Arguments> provideArguments(ExtensionContext context) { | ||
return symbols.entrySet().stream() | ||
.map(entry -> { | ||
Quantifier quantifier = entry.getKey(); | ||
String symbol = entry.getValue(); | ||
String pattern = "((_)" + symbol + " @capture)"; | ||
Query query = Query.getFor(language, pattern); | ||
List<Capture> captures = query.getCaptures(); | ||
Capture capture = captures.stream().findFirst().orElseThrow(); | ||
return Arguments.of(quantifier, capture, query); | ||
}); | ||
} | ||
} | ||
|
||
@ParameterizedTest(name = "[{index}] {0}") | ||
@ArgumentsSource(QuantifierArgumentsProvider.class) | ||
void testGetQuantifiers(Quantifier expected, Capture capture, Query query) { | ||
List<Quantifier> quantifiers = capture.getQuantifiers(); | ||
Assertions.assertNotNull(quantifiers); | ||
Assertions.assertFalse(quantifiers.isEmpty()); | ||
Assertions.assertEquals(1, quantifiers.size()); | ||
Quantifier actual = quantifiers.stream() | ||
.findFirst() | ||
.orElseGet(Assertions::fail); | ||
Assertions.assertEquals(expected, actual); | ||
query.close(); | ||
} | ||
|
||
@Test | ||
void testGetQuantifier() { | ||
@Cleanup Query query = Query.builder() | ||
.language(language) | ||
.pattern("((_) @capture)") | ||
.pattern("(identifier)") | ||
.build(); | ||
List<Capture> captures = query.getCaptures(); | ||
List<Pattern> patterns = query.getPatterns(); | ||
Capture capture = captures.stream().findFirst().orElseThrow(); | ||
Pattern pattern = patterns.stream().skip(1).findFirst().orElseThrow(); | ||
Quantifier quantifier = capture.getQuantifier(pattern); | ||
Assertions.assertEquals(Quantifier.ZERO, quantifier); | ||
} | ||
|
||
@Test | ||
void testGetQuantifierThrows() { | ||
@Cleanup Query original = Query.getFor(language, "((_) @capture)"); | ||
@Cleanup Query copy = Query.getFor(language, "((_) @capture)"); | ||
List<Capture> captures = original.getCaptures(); | ||
Capture capture = captures.stream().findFirst().orElseThrow(); | ||
List<Pattern> patterns = copy.getPatterns(); | ||
Pattern pattern = patterns.stream().findFirst().orElseThrow(); | ||
Assertions.assertThrows(NullPointerException.class, () -> capture.getQuantifier(null)); | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> capture.getQuantifier(pattern)); | ||
} | ||
} |