Search in sources :

Example 1 with RegexSyntaxElement

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;
}
Also used : ArrayList(java.util.ArrayList) CharacterTree(org.sonarsource.analyzer.commons.regex.ast.CharacterTree) RegexSyntaxElement(org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement)

Example 2 with RegexSyntaxElement

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);
}
Also used : RegexIssueLocation(org.sonarsource.analyzer.commons.regex.RegexIssueLocation) List(java.util.List) RegexParseResult(org.sonarsource.analyzer.commons.regex.RegexParseResult) RegexSyntaxElement(org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement) SyntaxError(org.sonarsource.analyzer.commons.regex.SyntaxError) FunctionCallTree(org.sonar.plugins.php.api.tree.expression.FunctionCallTree) RegexIssueLocation(org.sonarsource.analyzer.commons.regex.RegexIssueLocation) Rule(org.sonar.check.Rule) Collectors(java.util.stream.Collectors) RegexSyntaxElement(org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement)

Example 3 with RegexSyntaxElement

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);
}
Also used : List(java.util.List) RegexCheck(org.sonar.java.regex.RegexCheck) RegexParseResult(org.sonarsource.analyzer.commons.regex.RegexParseResult) RegexSyntaxElement(org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement) SyntaxError(org.sonarsource.analyzer.commons.regex.SyntaxError) Rule(org.sonar.check.Rule) Collectors(java.util.stream.Collectors) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) RegexSyntaxElement(org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement)

Example 4 with RegexSyntaxElement

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));
}
Also used : RegexParser(org.sonarsource.analyzer.commons.regex.RegexParser) FlagSet(org.sonarsource.analyzer.commons.regex.ast.FlagSet) TextSpan(org.sonar.java.reporting.AnalyzerMessage.TextSpan) RegexParseResult(org.sonarsource.analyzer.commons.regex.RegexParseResult) RegexSource(org.sonarsource.analyzer.commons.regex.RegexSource) RegexSyntaxElement(org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement) Test(org.junit.jupiter.api.Test)

Example 5 with RegexSyntaxElement

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;
}
Also used : ArrayList(java.util.ArrayList) RegexSyntaxElement(org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

RegexSyntaxElement (org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement)7 RegexParseResult (org.sonarsource.analyzer.commons.regex.RegexParseResult)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Rule (org.sonar.check.Rule)3 SyntaxError (org.sonarsource.analyzer.commons.regex.SyntaxError)3 ArrayList (java.util.ArrayList)2 RegexIssueLocation (org.sonarsource.analyzer.commons.regex.RegexIssueLocation)2 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Test (org.junit.jupiter.api.Test)1 RegexCheck (org.sonar.java.regex.RegexCheck)1 TextSpan (org.sonar.java.reporting.AnalyzerMessage.TextSpan)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 FunctionCallTree (org.sonar.plugins.php.api.tree.expression.FunctionCallTree)1 CallExpression (org.sonar.plugins.python.api.tree.CallExpression)1 RegexParser (org.sonarsource.analyzer.commons.regex.RegexParser)1 RegexSource (org.sonarsource.analyzer.commons.regex.RegexSource)1 CharacterTree (org.sonarsource.analyzer.commons.regex.ast.CharacterTree)1 FlagSet (org.sonarsource.analyzer.commons.regex.ast.FlagSet)1