-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: <grammars> add basic comment todo support
- Loading branch information
Showing
11 changed files
with
823 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
plugins { | ||
java | ||
kotlin("jvm") | ||
} | ||
|
||
group = "com.phodal" | ||
version = "1.0-SNAPSHOT" | ||
|
||
repositories { | ||
mavenCentral() | ||
mavenLocal() | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
implementation(kotlin("stdlib-jdk8")) | ||
implementation(kotlin("reflect")) | ||
// Kotlin reflection. | ||
implementation(kotlin("test")) | ||
implementation(kotlin("test-junit")) | ||
|
||
// JUnit 5 | ||
testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0") | ||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.0") | ||
testRuntimeOnly("org.junit.platform:junit-platform-console:1.6.0") | ||
|
||
implementation("org.antlr:antlr4:4.8-1") | ||
implementation("org.antlr:antlr4-runtime:4.8-1") | ||
} | ||
|
||
configure<JavaPluginConvention> { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
tasks { | ||
compileKotlin { | ||
kotlinOptions.jvmTarget = "1.8" | ||
} | ||
compileTestKotlin { | ||
kotlinOptions.jvmTarget = "1.8" | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
} | ||
} |
213 changes: 213 additions & 0 deletions
213
chapi-ast-comments/src/main/java/chapi/ast/antlr/CommentLexer.interp
Large diffs are not rendered by default.
Oops, something went wrong.
347 changes: 347 additions & 0 deletions
347
chapi-ast-comments/src/main/java/chapi/ast/antlr/CommentLexer.java
Large diffs are not rendered by default.
Oops, something went wrong.
111 changes: 111 additions & 0 deletions
111
chapi-ast-comments/src/main/java/chapi/ast/antlr/CommentLexer.tokens
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,111 @@ | ||
COMMENT=1 | ||
LINE_COMMENT=2 | ||
PYTHON_COMMENT=3 | ||
DECIMAL_LITERAL=4 | ||
HEX_LITERAL=5 | ||
OCT_LITERAL=6 | ||
BINARY_LITERAL=7 | ||
FLOAT_LITERAL=8 | ||
HEX_FLOAT_LITERAL=9 | ||
BOOL_LITERAL=10 | ||
CHAR_LITERAL=11 | ||
STRING_LITERAL=12 | ||
NULL_LITERAL=13 | ||
LPAREN=14 | ||
RPAREN=15 | ||
LBRACE=16 | ||
RBRACE=17 | ||
LBRACK=18 | ||
RBRACK=19 | ||
SEMI=20 | ||
COMMA=21 | ||
DOT=22 | ||
ASSIGN=23 | ||
GT=24 | ||
LT=25 | ||
BANG=26 | ||
TILDE=27 | ||
QUESTION=28 | ||
COLON=29 | ||
EQUAL=30 | ||
LE=31 | ||
GE=32 | ||
NOTEQUAL=33 | ||
AND=34 | ||
OR=35 | ||
INC=36 | ||
DEC=37 | ||
ADD=38 | ||
SUB=39 | ||
MUL=40 | ||
DIV=41 | ||
BITAND=42 | ||
BITOR=43 | ||
CARET=44 | ||
MOD=45 | ||
ADD_ASSIGN=46 | ||
SUB_ASSIGN=47 | ||
MUL_ASSIGN=48 | ||
DIV_ASSIGN=49 | ||
AND_ASSIGN=50 | ||
OR_ASSIGN=51 | ||
XOR_ASSIGN=52 | ||
MOD_ASSIGN=53 | ||
LSHIFT_ASSIGN=54 | ||
RSHIFT_ASSIGN=55 | ||
URSHIFT_ASSIGN=56 | ||
TemplateStringLiteral=57 | ||
ARROW=58 | ||
COLONCOLON=59 | ||
AT=60 | ||
ELLIPSIS=61 | ||
WS=62 | ||
IDENTIFIER=63 | ||
'null'=13 | ||
'('=14 | ||
')'=15 | ||
'{'=16 | ||
'}'=17 | ||
'['=18 | ||
']'=19 | ||
';'=20 | ||
','=21 | ||
'.'=22 | ||
'='=23 | ||
'>'=24 | ||
'<'=25 | ||
'!'=26 | ||
'~'=27 | ||
'?'=28 | ||
':'=29 | ||
'=='=30 | ||
'<='=31 | ||
'>='=32 | ||
'!='=33 | ||
'&&'=34 | ||
'||'=35 | ||
'++'=36 | ||
'--'=37 | ||
'+'=38 | ||
'-'=39 | ||
'*'=40 | ||
'/'=41 | ||
'&'=42 | ||
'|'=43 | ||
'^'=44 | ||
'%'=45 | ||
'+='=46 | ||
'-='=47 | ||
'*='=48 | ||
'/='=49 | ||
'&='=50 | ||
'|='=51 | ||
'^='=52 | ||
'%='=53 | ||
'<<='=54 | ||
'>>='=55 | ||
'>>>='=56 | ||
'->'=58 | ||
'::'=59 | ||
'@'=60 | ||
'...'=61 |
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,10 @@ | ||
package chapi.ast.todo | ||
|
||
open class Todo( | ||
var Assignee: String = "", | ||
var Filename: String = "", | ||
var Message: String = "", | ||
var Line: Int = 0 | ||
) { | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
chapi-ast-comments/src/main/kotlin/chapi/ast/todo/TodoIdentApp.kt
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,70 @@ | ||
package chapi.ast.todo | ||
|
||
import chapi.ast.antlr.CommentLexer | ||
import org.antlr.v4.runtime.CharStreams | ||
import org.antlr.v4.runtime.Token | ||
|
||
private class TodoConstants { | ||
companion object { | ||
val TodoIdentifiers = arrayOf( | ||
"TODO", | ||
"FIXME" | ||
) | ||
} | ||
} | ||
|
||
open class TodoIdentApp { | ||
open fun Analysis(Str: String): Array<Todo> { | ||
var todos = arrayOf<Todo>() | ||
val lexer = this.lexerText(Str) | ||
|
||
for (token in lexer.allTokens) { | ||
val ONE_LINE_COMMENT = 1 | ||
val LINE_COMMENT = 2 | ||
val PYTHON_COMMENT = 3 | ||
|
||
if ( | ||
token.type == ONE_LINE_COMMENT || | ||
token.type == LINE_COMMENT || | ||
token.type == PYTHON_COMMENT | ||
) { | ||
val parsedComment = this.parseComment(token) | ||
|
||
if (parsedComment != null) { | ||
todos += parsedComment | ||
} | ||
} | ||
} | ||
|
||
return todos | ||
} | ||
|
||
private fun parseComment(token: Token): Todo? { | ||
val comment = token.text | ||
var trimText = comment.trim { it <= ' ' } | ||
if (trimText.startsWith("# ")) { | ||
trimText = trimText.substring(2) | ||
} | ||
|
||
if (isTodoIdentifier(trimText)) { | ||
val todo = Todo(Line = token.line) | ||
return todo | ||
} | ||
|
||
return null | ||
} | ||
|
||
private fun isTodoIdentifier(trimText: String): Boolean { | ||
for (todoIdentifier in TodoConstants.TodoIdentifiers) { | ||
if (trimText.toUpperCase().startsWith(todoIdentifier)) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
open fun lexerText(Str: String): CommentLexer { | ||
return CommentLexer(CharStreams.fromString(Str)) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
chapi-ast-comments/src/test/kotlin/chapi/ast/todo/TodoIdentAppTest.kt
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,15 @@ | ||
package chapi.ast.todo | ||
|
||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class TodoIdentAppTest { | ||
@Test | ||
internal fun shouldIdentifyTodoMessage() { | ||
var todoTxt = "# todo: should identify todo message" | ||
val todos = TodoIdentApp().Analysis(todoTxt) | ||
|
||
assertEquals(todos.size, 1) | ||
} | ||
} | ||
|
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,6 @@ | ||
# Grammars | ||
|
||
Grammars based on [https://github.com/antlr/grammars-v4](https://github.com/antlr/grammars-v4) | ||
|
||
TODO: use submodule to [https://github.com/antlr/grammars-v4](https://github.com/antlr/grammars-v4) | ||
|
This file was deleted.
Oops, something went wrong.
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