forked from adrienmo/sonar-elixir
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for @TypeDoc. Separate parser.
- Loading branch information
Showing
4 changed files
with
169 additions
and
102 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
142 changes: 142 additions & 0 deletions
142
...r-elixir-plugin/src/main/java/eu/arthepsy/sonar/plugins/elixir/language/ElixirParser.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,142 @@ | ||
/* | ||
* SonarQube Elixir plugin | ||
* Copyright (C) 2015 Andris Raugulis | ||
* [email protected] | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package eu.arthepsy.sonar.plugins.elixir.language; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
|
||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class ElixirParser { | ||
private int lineCount = 0; | ||
private int emptyLineCount = 0; | ||
private int commentLineCount = 0; | ||
private int classCount = 0; | ||
private int publicFunctionCount = 0; | ||
private int privateFunctionCount = 0; | ||
private int documentedClassCount = 0; | ||
private int documentedPrivateFunctionCount = 0; | ||
private int documentedPublicFunctionCount = 0; | ||
|
||
private final Pattern defPattern = Pattern.compile("^\\s*def(|p|module|struct)\\s(.*)$"); | ||
private final Matcher defMatcher = defPattern.matcher(""); | ||
private final Pattern heredocPattern = Pattern.compile("^.*\"\"\"\\s*$"); | ||
private final Matcher heredocMatcher = heredocPattern.matcher(""); | ||
private final Pattern docPattern = Pattern.compile("^\\s*@(doc|moduledoc|typedoc)([\"\\s].*)$"); | ||
private final Matcher docMatcher = docPattern.matcher(""); | ||
|
||
private boolean hasDoc = false; | ||
private boolean inClass = false; | ||
|
||
public ElixirParser() { } | ||
|
||
public int getLineCount() { return lineCount; } | ||
public int getEmptyLineCount() { return emptyLineCount; } | ||
public int getCommentLineCount() { return commentLineCount; } | ||
public int getClassCount() { return classCount; } | ||
public int getPublicFunctionCount() { return publicFunctionCount; } | ||
public int getPrivateFunctionCount() { return privateFunctionCount; } | ||
public int getDocumentedClassCount() { return documentedClassCount; } | ||
public int getDocumentedPublicFunctionCount() { return documentedPublicFunctionCount; } | ||
public int getDocumentedPrivateFunctionCount() { return documentedPrivateFunctionCount; } | ||
|
||
public void parse(List<String> lines) { | ||
this.parseLines(lines); | ||
} | ||
|
||
private void parseLines(List<String> lines) { | ||
hasDoc = false; | ||
inClass = false; | ||
|
||
lineCount = lines.size(); | ||
for (int i = 0; i < lineCount; i++) { | ||
String line = lines.get(i); | ||
if (StringUtils.isBlank(line)) { | ||
emptyLineCount++; | ||
} | ||
docMatcher.reset(line); | ||
boolean inDoc = docMatcher.find(); | ||
if (inDoc) { | ||
commentLineCount++; | ||
String docHead = docMatcher.group(2).trim(); | ||
if (! (StringUtils.equalsIgnoreCase(docHead, "false") || StringUtils.equalsIgnoreCase(docHead, "nil"))) { | ||
switch (docMatcher.group(1)) { | ||
case "doc": | ||
hasDoc = true; | ||
break; | ||
case "moduledoc": | ||
if (inClass) { | ||
documentedClassCount++; | ||
} | ||
break; | ||
case "typedoc": | ||
break; | ||
} | ||
} | ||
} | ||
heredocMatcher.reset(line); | ||
if (heredocMatcher.find()) { | ||
while (i < lineCount - 1) { | ||
if (inDoc) { | ||
commentLineCount++; | ||
} | ||
i++; | ||
line = lines.get(i); | ||
if (line.matches("^\\s*\"\"\"\\s*$")) { | ||
break; | ||
} | ||
} | ||
continue; | ||
} | ||
|
||
defMatcher.reset(line); | ||
if (defMatcher.find()) { | ||
switch (defMatcher.group(1)) { | ||
case "module": | ||
classCount++; | ||
inClass = true; | ||
break; | ||
case "": | ||
publicFunctionCount++; | ||
if (hasDoc) { | ||
documentedPublicFunctionCount++; | ||
} | ||
break; | ||
case "p": | ||
privateFunctionCount++; | ||
if (hasDoc) { | ||
documentedPrivateFunctionCount++; | ||
} | ||
break; | ||
} | ||
hasDoc = false; | ||
} | ||
if (line.matches("^\\s*#.*$")) { | ||
commentLineCount++; | ||
} | ||
} | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -30,4 +30,8 @@ defmodule Docs do | |
# private function doc6 | ||
defp doc6() do | ||
end | ||
|
||
@typedoc "type doc example" | ||
@type doc7 :: any | ||
|
||
end |