use of org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement in project sonar-java by SonarSource.
the class RegexTreeHelper method getGraphemeInList.
public static List<RegexCheck.RegexIssueLocation> getGraphemeInList(List<? extends RegexSyntaxElement> trees) {
List<RegexCheck.RegexIssueLocation> result = new ArrayList<>();
List<RegexSyntaxElement> codePoints = new ArrayList<>();
for (RegexSyntaxElement child : trees) {
if (child instanceof CharacterTree) {
CharacterTree currentCharacter = (CharacterTree) child;
if (!currentCharacter.isEscapeSequence()) {
if (!isMark(currentCharacter)) {
addCurrentGrapheme(result, codePoints);
codePoints.clear();
codePoints.add(currentCharacter);
} else if (!codePoints.isEmpty()) {
codePoints.add(currentCharacter);
}
continue;
}
}
addCurrentGrapheme(result, codePoints);
codePoints.clear();
}
addCurrentGrapheme(result, codePoints);
return result;
}
use of org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement in project sonar-php by SonarSource.
the class InvalidRegexCheck method reportSyntaxErrors.
private void reportSyntaxErrors(List<SyntaxError> syntaxErrors) {
// report on the first issue
RegexSyntaxElement tree = syntaxErrors.get(0).getOffendingSyntaxElement();
List<RegexIssueLocation> secondaries = syntaxErrors.stream().map(error -> new RegexIssueLocation(error.getOffendingSyntaxElement(), error.getMessage())).collect(Collectors.toList());
String msg = String.format(MESSAGE_FORMAT, secondaries.size() > 1 ? "s" : "");
newIssue(tree, msg, null, secondaries);
}
use of org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement in project sonar-java by SonarSource.
the class InvalidRegexCheck method reportSyntaxErrors.
private void reportSyntaxErrors(List<SyntaxError> syntaxErrors) {
// report on the first issue
RegexSyntaxElement tree = syntaxErrors.get(0).getOffendingSyntaxElement();
List<RegexIssueLocation> secondaries = syntaxErrors.stream().map(error -> new RegexCheck.RegexIssueLocation(error.getOffendingSyntaxElement(), error.getMessage())).collect(Collectors.toList());
reportIssue(tree, secondaries);
}
use of org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement in project sonar-java by SonarSource.
the class JavaAnalyzerRegexSourceTest method invalid_regex.
@Test
void invalid_regex() {
RegexSource source = RegexParserTestUtils.makeSource("(");
RegexParseResult result = new RegexParser(source, new FlagSet()).parse();
if (result.getSyntaxErrors().isEmpty()) {
fail("should have encountered syntax error");
}
RegexSyntaxElement offendingSyntaxElement = result.getSyntaxErrors().get(0).getOffendingSyntaxElement();
List<TextSpan> items = ((JavaAnalyzerRegexSource) offendingSyntaxElement.getSource()).textSpansFor(offendingSyntaxElement.getRange());
assertThat(items).hasSize(1);
assertTextSpan(3, 2, 3, items.get(0));
}
use of org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement in project sonar-java by SonarSource.
the class SimplifiedRegexCharacterClass method findIntersections.
private List<RegexSyntaxElement> findIntersections(SimplifiedRegexCharacterClass that, boolean stopAtFirst) {
Iterator<Map.Entry<Integer, RegexSyntaxElement>> iter = that.contents.entrySet().iterator();
List<RegexSyntaxElement> intersections = new ArrayList<>();
if (!iter.hasNext()) {
return intersections;
}
Map.Entry<Integer, RegexSyntaxElement> entry = iter.next();
while (iter.hasNext()) {
Map.Entry<Integer, RegexSyntaxElement> nextEntry = iter.next();
int to = (nextEntry.getValue() == null) ? (nextEntry.getKey() - 1) : nextEntry.getKey();
RegexSyntaxElement value = entry.getValue();
if (value != null && hasEntryBetween(entry.getKey(), to)) {
intersections.add(value);
if (stopAtFirst) {
return intersections;
}
}
entry = nextEntry;
}
RegexSyntaxElement value = entry.getValue();
if (value != null && hasEntryBetween(entry.getKey(), Character.MAX_CODE_POINT)) {
intersections.add(value);
}
return intersections;
}
Aggregations