@Test public void files() { AstScanner<LexerlessGrammar> scanner = LuaAstScanner.create(new LuaConfiguration(Charsets.UTF_8), Collections.emptyList()); scanner.scanFiles(ImmutableList.of(new File("src/test/resources/metrics/comments.lua"), new File("src/test/resources/metrics/lines.lua"))); SourceProject project = (SourceProject) scanner.getIndex().search(new QueryByType(SourceProject.class)).iterator().next(); assertThat(project.getInt(LuaMetric.FILES)).isEqualTo(2); }
public static AstScanner<LexerlessGrammar> create(LuaConfiguration conf, List<SquidAstVisitor<LexerlessGrammar>> visitors) { final SquidAstVisitorContextImpl<LexerlessGrammar> context = new SquidAstVisitorContextImpl<>(new SourceProject("Lua Project")); final Parser<LexerlessGrammar> parser = LuaParser.create(conf); AstScanner.Builder<LexerlessGrammar> builder = new ProgressAstScanner.Builder(context).setBaseParser(parser); builder.withMetrics(LuaMetric.values()); builder.setFilesMetric(LuaMetric.FILES); builder.setCommentAnalyser(new LuaCommentAnalyser()); / builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceClass cls = new SourceClass("table:" + seq); cls.setStartAtLine(astNode.getTokenLine()); return cls; } },LuaGrammar.TABLECONSTRUCTOR)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.TABLECONSTRUCTORS) .subscribeTo(LuaGrammar.TABLECONSTRUCTOR) .build()); builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceFunction function = new SourceFunction("function" + seq); function.setStartAtLine(astNode.getTokenLine()); return function; } }, LuaGrammar.FUNCTION,LuaGrammar.FUNCSTAT,LuaGrammar.LOCALFUNCSTAT)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.FUNCTIONS) .subscribeTo(LuaGrammar.FUNCTION,LuaGrammar.FUNCSTAT,LuaGrammar.LOCALFUNCSTAT) .build()); builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceFuncCall functionCall = new SourceFuncCall("functionCall" + seq); functionCall.setStartAtLine(astNode.getTokenLine()); return functionCall; } },LuaGrammar.FUNCTIONCALL)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.FUNCTIONCALL) .subscribeTo(LuaGrammar.FUNCTIONCALL) .build()); builder.withSquidAstVisitor(new LinesVisitor<LexerlessGrammar>(LuaMetric.LINES)); builder.withSquidAstVisitor(new LinesOfCodeVisitor<LexerlessGrammar>(LuaMetric.LINES_OF_CODE)); builder.withSquidAstVisitor(CommentsVisitor.<LexerlessGrammar>builder().withCommentMetric(LuaMetric.COMMENT_LINES) .withNoSonar(true) .withIgnoreHeaderComment(conf.getIgnoreHeaderComments()) .build()); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.STATEMENTS) .subscribeTo( LuaGrammar.IF_STATEMENT, LuaGrammar.WHILE_STATEMENT, LuaGrammar.FOR_STATEMENT, LuaGrammar.DO_STATEMENT, LuaGrammar.WHILE_STATEMENT, LuaGrammar.REPEAT_STATEMENT, LuaGrammar.Keyword.AND, LuaGrammar.Keyword.OR ).build()); builder.withSquidAstVisitor(new ComplexityVisitor()); for (SquidAstVisitor<LexerlessGrammar> visitor : visitors) { if (visitor instanceof CharsetAwareVisitor) { ((CharsetAwareVisitor) visitor).setCharset(conf.getCharset()); } builder.withSquidAstVisitor(visitor); } return builder.build(); }
LuaAstScanner { public static AstScanner<LexerlessGrammar> create(LuaConfiguration conf, List<SquidAstVisitor<LexerlessGrammar>> visitors) { final SquidAstVisitorContextImpl<LexerlessGrammar> context = new SquidAstVisitorContextImpl<>(new SourceProject("Lua Project")); final Parser<LexerlessGrammar> parser = LuaParser.create(conf); AstScanner.Builder<LexerlessGrammar> builder = new ProgressAstScanner.Builder(context).setBaseParser(parser); builder.withMetrics(LuaMetric.values()); builder.setFilesMetric(LuaMetric.FILES); builder.setCommentAnalyser(new LuaCommentAnalyser()); / builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceClass cls = new SourceClass("table:" + seq); cls.setStartAtLine(astNode.getTokenLine()); return cls; } },LuaGrammar.TABLECONSTRUCTOR)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.TABLECONSTRUCTORS) .subscribeTo(LuaGrammar.TABLECONSTRUCTOR) .build()); builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceFunction function = new SourceFunction("function" + seq); function.setStartAtLine(astNode.getTokenLine()); return function; } }, LuaGrammar.FUNCTION,LuaGrammar.FUNCSTAT,LuaGrammar.LOCALFUNCSTAT)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.FUNCTIONS) .subscribeTo(LuaGrammar.FUNCTION,LuaGrammar.FUNCSTAT,LuaGrammar.LOCALFUNCSTAT) .build()); builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceFuncCall functionCall = new SourceFuncCall("functionCall" + seq); functionCall.setStartAtLine(astNode.getTokenLine()); return functionCall; } },LuaGrammar.FUNCTIONCALL)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.FUNCTIONCALL) .subscribeTo(LuaGrammar.FUNCTIONCALL) .build()); builder.withSquidAstVisitor(new LinesVisitor<LexerlessGrammar>(LuaMetric.LINES)); builder.withSquidAstVisitor(new LinesOfCodeVisitor<LexerlessGrammar>(LuaMetric.LINES_OF_CODE)); builder.withSquidAstVisitor(CommentsVisitor.<LexerlessGrammar>builder().withCommentMetric(LuaMetric.COMMENT_LINES) .withNoSonar(true) .withIgnoreHeaderComment(conf.getIgnoreHeaderComments()) .build()); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.STATEMENTS) .subscribeTo( LuaGrammar.IF_STATEMENT, LuaGrammar.WHILE_STATEMENT, LuaGrammar.FOR_STATEMENT, LuaGrammar.DO_STATEMENT, LuaGrammar.WHILE_STATEMENT, LuaGrammar.REPEAT_STATEMENT, LuaGrammar.Keyword.AND, LuaGrammar.Keyword.OR ).build()); builder.withSquidAstVisitor(new ComplexityVisitor()); for (SquidAstVisitor<LexerlessGrammar> visitor : visitors) { if (visitor instanceof CharsetAwareVisitor) { ((CharsetAwareVisitor) visitor).setCharset(conf.getCharset()); } builder.withSquidAstVisitor(visitor); } return builder.build(); } }
LuaAstScanner { public static AstScanner<LexerlessGrammar> create(LuaConfiguration conf, List<SquidAstVisitor<LexerlessGrammar>> visitors) { final SquidAstVisitorContextImpl<LexerlessGrammar> context = new SquidAstVisitorContextImpl<>(new SourceProject("Lua Project")); final Parser<LexerlessGrammar> parser = LuaParser.create(conf); AstScanner.Builder<LexerlessGrammar> builder = new ProgressAstScanner.Builder(context).setBaseParser(parser); builder.withMetrics(LuaMetric.values()); builder.setFilesMetric(LuaMetric.FILES); builder.setCommentAnalyser(new LuaCommentAnalyser()); / builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceClass cls = new SourceClass("table:" + seq); cls.setStartAtLine(astNode.getTokenLine()); return cls; } },LuaGrammar.TABLECONSTRUCTOR)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.TABLECONSTRUCTORS) .subscribeTo(LuaGrammar.TABLECONSTRUCTOR) .build()); builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceFunction function = new SourceFunction("function" + seq); function.setStartAtLine(astNode.getTokenLine()); return function; } }, LuaGrammar.FUNCTION,LuaGrammar.FUNCSTAT,LuaGrammar.LOCALFUNCSTAT)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.FUNCTIONS) .subscribeTo(LuaGrammar.FUNCTION,LuaGrammar.FUNCSTAT,LuaGrammar.LOCALFUNCSTAT) .build()); builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceFuncCall functionCall = new SourceFuncCall("functionCall" + seq); functionCall.setStartAtLine(astNode.getTokenLine()); return functionCall; } },LuaGrammar.FUNCTIONCALL)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.FUNCTIONCALL) .subscribeTo(LuaGrammar.FUNCTIONCALL) .build()); builder.withSquidAstVisitor(new LinesVisitor<LexerlessGrammar>(LuaMetric.LINES)); builder.withSquidAstVisitor(new LinesOfCodeVisitor<LexerlessGrammar>(LuaMetric.LINES_OF_CODE)); builder.withSquidAstVisitor(CommentsVisitor.<LexerlessGrammar>builder().withCommentMetric(LuaMetric.COMMENT_LINES) .withNoSonar(true) .withIgnoreHeaderComment(conf.getIgnoreHeaderComments()) .build()); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.STATEMENTS) .subscribeTo( LuaGrammar.IF_STATEMENT, LuaGrammar.WHILE_STATEMENT, LuaGrammar.FOR_STATEMENT, LuaGrammar.DO_STATEMENT, LuaGrammar.WHILE_STATEMENT, LuaGrammar.REPEAT_STATEMENT, LuaGrammar.Keyword.AND, LuaGrammar.Keyword.OR ).build()); builder.withSquidAstVisitor(new ComplexityVisitor()); for (SquidAstVisitor<LexerlessGrammar> visitor : visitors) { if (visitor instanceof CharsetAwareVisitor) { ((CharsetAwareVisitor) visitor).setCharset(conf.getCharset()); } builder.withSquidAstVisitor(visitor); } return builder.build(); } private  LuaAstScanner(); }
LuaAstScanner { public static AstScanner<LexerlessGrammar> create(LuaConfiguration conf, List<SquidAstVisitor<LexerlessGrammar>> visitors) { final SquidAstVisitorContextImpl<LexerlessGrammar> context = new SquidAstVisitorContextImpl<>(new SourceProject("Lua Project")); final Parser<LexerlessGrammar> parser = LuaParser.create(conf); AstScanner.Builder<LexerlessGrammar> builder = new ProgressAstScanner.Builder(context).setBaseParser(parser); builder.withMetrics(LuaMetric.values()); builder.setFilesMetric(LuaMetric.FILES); builder.setCommentAnalyser(new LuaCommentAnalyser()); / builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceClass cls = new SourceClass("table:" + seq); cls.setStartAtLine(astNode.getTokenLine()); return cls; } },LuaGrammar.TABLECONSTRUCTOR)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.TABLECONSTRUCTORS) .subscribeTo(LuaGrammar.TABLECONSTRUCTOR) .build()); builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceFunction function = new SourceFunction("function" + seq); function.setStartAtLine(astNode.getTokenLine()); return function; } }, LuaGrammar.FUNCTION,LuaGrammar.FUNCSTAT,LuaGrammar.LOCALFUNCSTAT)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.FUNCTIONS) .subscribeTo(LuaGrammar.FUNCTION,LuaGrammar.FUNCSTAT,LuaGrammar.LOCALFUNCSTAT) .build()); builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceFuncCall functionCall = new SourceFuncCall("functionCall" + seq); functionCall.setStartAtLine(astNode.getTokenLine()); return functionCall; } },LuaGrammar.FUNCTIONCALL)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.FUNCTIONCALL) .subscribeTo(LuaGrammar.FUNCTIONCALL) .build()); builder.withSquidAstVisitor(new LinesVisitor<LexerlessGrammar>(LuaMetric.LINES)); builder.withSquidAstVisitor(new LinesOfCodeVisitor<LexerlessGrammar>(LuaMetric.LINES_OF_CODE)); builder.withSquidAstVisitor(CommentsVisitor.<LexerlessGrammar>builder().withCommentMetric(LuaMetric.COMMENT_LINES) .withNoSonar(true) .withIgnoreHeaderComment(conf.getIgnoreHeaderComments()) .build()); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.STATEMENTS) .subscribeTo( LuaGrammar.IF_STATEMENT, LuaGrammar.WHILE_STATEMENT, LuaGrammar.FOR_STATEMENT, LuaGrammar.DO_STATEMENT, LuaGrammar.WHILE_STATEMENT, LuaGrammar.REPEAT_STATEMENT, LuaGrammar.Keyword.AND, LuaGrammar.Keyword.OR ).build()); builder.withSquidAstVisitor(new ComplexityVisitor()); for (SquidAstVisitor<LexerlessGrammar> visitor : visitors) { if (visitor instanceof CharsetAwareVisitor) { ((CharsetAwareVisitor) visitor).setCharset(conf.getCharset()); } builder.withSquidAstVisitor(visitor); } return builder.build(); } private  LuaAstScanner(); static SourceFile scanSingleFile(File file, SquidAstVisitor<LexerlessGrammar>... visitors); static AstScanner<LexerlessGrammar> create(LuaConfiguration conf, List<SquidAstVisitor<LexerlessGrammar>> visitors); }
LuaAstScanner { public static AstScanner<LexerlessGrammar> create(LuaConfiguration conf, List<SquidAstVisitor<LexerlessGrammar>> visitors) { final SquidAstVisitorContextImpl<LexerlessGrammar> context = new SquidAstVisitorContextImpl<>(new SourceProject("Lua Project")); final Parser<LexerlessGrammar> parser = LuaParser.create(conf); AstScanner.Builder<LexerlessGrammar> builder = new ProgressAstScanner.Builder(context).setBaseParser(parser); builder.withMetrics(LuaMetric.values()); builder.setFilesMetric(LuaMetric.FILES); builder.setCommentAnalyser(new LuaCommentAnalyser()); / builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceClass cls = new SourceClass("table:" + seq); cls.setStartAtLine(astNode.getTokenLine()); return cls; } },LuaGrammar.TABLECONSTRUCTOR)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.TABLECONSTRUCTORS) .subscribeTo(LuaGrammar.TABLECONSTRUCTOR) .build()); builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceFunction function = new SourceFunction("function" + seq); function.setStartAtLine(astNode.getTokenLine()); return function; } }, LuaGrammar.FUNCTION,LuaGrammar.FUNCSTAT,LuaGrammar.LOCALFUNCSTAT)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.FUNCTIONS) .subscribeTo(LuaGrammar.FUNCTION,LuaGrammar.FUNCSTAT,LuaGrammar.LOCALFUNCSTAT) .build()); builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() { private int seq = 0; @Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceFuncCall functionCall = new SourceFuncCall("functionCall" + seq); functionCall.setStartAtLine(astNode.getTokenLine()); return functionCall; } },LuaGrammar.FUNCTIONCALL)); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.FUNCTIONCALL) .subscribeTo(LuaGrammar.FUNCTIONCALL) .build()); builder.withSquidAstVisitor(new LinesVisitor<LexerlessGrammar>(LuaMetric.LINES)); builder.withSquidAstVisitor(new LinesOfCodeVisitor<LexerlessGrammar>(LuaMetric.LINES_OF_CODE)); builder.withSquidAstVisitor(CommentsVisitor.<LexerlessGrammar>builder().withCommentMetric(LuaMetric.COMMENT_LINES) .withNoSonar(true) .withIgnoreHeaderComment(conf.getIgnoreHeaderComments()) .build()); builder.withSquidAstVisitor(CounterVisitor.<LexerlessGrammar>builder() .setMetricDef(LuaMetric.STATEMENTS) .subscribeTo( LuaGrammar.IF_STATEMENT, LuaGrammar.WHILE_STATEMENT, LuaGrammar.FOR_STATEMENT, LuaGrammar.DO_STATEMENT, LuaGrammar.WHILE_STATEMENT, LuaGrammar.REPEAT_STATEMENT, LuaGrammar.Keyword.AND, LuaGrammar.Keyword.OR ).build()); builder.withSquidAstVisitor(new ComplexityVisitor()); for (SquidAstVisitor<LexerlessGrammar> visitor : visitors) { if (visitor instanceof CharsetAwareVisitor) { ((CharsetAwareVisitor) visitor).setCharset(conf.getCharset()); } builder.withSquidAstVisitor(visitor); } return builder.build(); } private  LuaAstScanner(); static SourceFile scanSingleFile(File file, SquidAstVisitor<LexerlessGrammar>... visitors); static AstScanner<LexerlessGrammar> create(LuaConfiguration conf, List<SquidAstVisitor<LexerlessGrammar>> visitors);  }