Skip to content

Commit

Permalink
feat: <grammars> add basic comment todo support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 2, 2020
1 parent d4332d1 commit c4095e4
Show file tree
Hide file tree
Showing 11 changed files with 823 additions and 9 deletions.
46 changes: 46 additions & 0 deletions chapi-ast-comments/build.gradle.kts
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 chapi-ast-comments/src/main/java/chapi/ast/antlr/CommentLexer.interp

Large diffs are not rendered by default.

347 changes: 347 additions & 0 deletions chapi-ast-comments/src/main/java/chapi/ast/antlr/CommentLexer.java

Large diffs are not rendered by default.

111 changes: 111 additions & 0 deletions chapi-ast-comments/src/main/java/chapi/ast/antlr/CommentLexer.tokens
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
10 changes: 10 additions & 0 deletions chapi-ast-comments/src/main/kotlin/chapi/ast/todo/Todo.kt
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 chapi-ast-comments/src/main/kotlin/chapi/ast/todo/TodoIdentApp.kt
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))
}
}
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)
}
}

6 changes: 6 additions & 0 deletions grammars/README.md
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)

9 changes: 0 additions & 9 deletions grammars/RefactorMethodSignatureParser.g4

This file was deleted.

4 changes: 4 additions & 0 deletions scripts/compile-antlr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ cd ../..
cd grammars/c

antlr -Dlanguage=Java -listener C.g4 -o ../../chapi-ast-c/src/main/java/chapi/ast/antlr -package chapi.ast.antlr

cd ..

antlr -Dlanguage=Java -listener CommentLexer.g4 -o ../chapi-ast-comments/src/main/java/chapi/ast/antlr -package chapi.ast.antlr
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ include("chapi-ast-go")
include("chapi-ast-python")
include("chapi-ast-c")
include("chapi-ast-kotlin")
include("chapi-ast-comments")

0 comments on commit c4095e4

Please # to comment.