abhijay94 / sonar-gosu

SonarQube plugin for Gosu language
0 stars 1 forks source link

Groovy Lexer fails to recognize the "\" character in a gosu lambda expression #2

Open abhijay94 opened 5 years ago

abhijay94 commented 5 years ago

This plugin still uses the Groovy Lexer, which fails to recognize the backslash literal. An example would be:

var result = Query.make(Policy).select().where(\elt -> elt.UWReferralReasons.IsEmpty)

While lexing the file into tokens, the lexer throws the below exception:

ERROR: Unexpected token when lexing file : GosuSonarTest.gs groovyjarjarantlr.TokenStreamRecognitionException: unexpected char: '\' at org.codehaus.groovy.antlr.parser.GroovyLexer.nextToken(GroovyLexer.java:728) at org.codehaus.groovy.antlr.parser.GroovyLexer$1.nextToken(GroovyLexer.java:262) at org.sonar.plugins.gosu.GosuSensor.computeBaseMetrics(GosuSensor.java:229) at org.sonar.plugins.gosu.GosuSensor.computeBaseMetrics(GosuSensor.java:207) at org.sonar.plugins.gosu.GosuSensor.execute(GosuSensor.java:110) at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:48) at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:85) at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:59) at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:77) at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:59) at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:82) at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136) at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122) at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:359) at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:354) at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:317) at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136) at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122) at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:128) at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136) at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122) at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:73) at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:67) at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60) at com.sun.proxy.$Proxy0.execute(Unknown Source) at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:185) at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:137) at org.sonarsource.scanner.cli.Main.execute(Main.java:111) at org.sonarsource.scanner.cli.Main.execute(Main.java:75) at org.sonarsource.scanner.cli.Main.main(Main.java:61) This project has a dependency on groovy 2.4.4 jar which has this Groovy lexer. Need to find a way to override this lexer to add support for gosu literals or use a Gosu Lexer instead, if available.

DevSett commented 4 years ago

@abhijay94 hi, could you defeat this mistake?

abhijay94 commented 4 years ago

@DevSett Not yet. Looks like the forked project was abandoned too soon and it doesn't even have a working gosu lexer. We currently have the groovy lexer only. Will need to make massive modifications to make it gosu compatible or implement one from scratch.

DevSett commented 4 years ago

@abhijay94 Yeah, that's what I figured. But while you were answering me, I wrote my primitive gosu lexer.

DevSett commented 4 years ago

@abhijay94 Here's an example of a steering wheel using my parser:

@Data public class TFPropertyMethodRule extends TinkoffAbstractRule { String name = "TFPropertyMethod"; int priority = 2;

private static final String START = "get";
private static final String END = "()";
private static final String MESSAGE = "It is recommended to use Property.";

@Override
public void applyConstructor(List<MethodGosu> constructorGosus) {
    for (MethodGosu method : constructorGosus) {
        checkMethod(method);
    }
}

@Override
public void applyMethods(List<MethodGosu> methodGosus) {
    for (MethodGosu method : methodGosus) {
        checkMethod(method);
    }
}

private void checkMethod(MethodGosu method) {
    String[] lines = method.getCode().split("\n");
    for (int i = 0; i < lines.length; i++) {
        String line = lines[i];
        String words[] = line.split("\\s+");
        for (String word : words) {
            String[] splitDot = word.split(Pattern.quote("."));
            for (String s : splitDot) {
                if (s.startsWith(START) && s.endsWith(END)) {
                    addAlert(method.getNumRow() + i, line, MESSAGE);
                }
            }
        }

    }
}

} `