@@ -4,6 +4,7 @@ import io.github.danielnaczo.python3parser.Python3Lexer
4
4
import io.github.danielnaczo.python3parser.Python3Parser
5
5
import io.github.danielnaczo.python3parser.model.AST
6
6
import io.github.danielnaczo.python3parser.model.expr.Expression
7
+ import io.github.danielnaczo.python3parser.model.expr.atoms.Atom
7
8
import io.github.danielnaczo.python3parser.model.expr.atoms.Name
8
9
import io.github.danielnaczo.python3parser.model.expr.atoms.Num
9
10
import io.github.danielnaczo.python3parser.model.expr.atoms.Str
@@ -45,12 +46,7 @@ class PythonCode(private val body: Module, val filename: String? = null) {
45
46
46
47
companion object {
47
48
fun getFromString (code : String , filename : String? = null): PythonCode {
48
- val lexer = Python3Lexer (fromString(code))
49
- val tokens = CommonTokenStream (lexer)
50
- val parser = Python3Parser (tokens)
51
- val moduleVisitor = ModuleVisitor ()
52
- val ast = moduleVisitor.visit(parser.file_input()) as Module
53
-
49
+ val ast = textToModule(code)
54
50
return PythonCode (ast, filename)
55
51
}
56
52
}
@@ -106,3 +102,36 @@ fun astToString(stmt: Statement): String {
106
102
val modulePrettyPrintVisitor = ModulePrettyPrintVisitor ()
107
103
return modulePrettyPrintVisitor.visitModule(Module (listOf (stmt)), IndentationPrettyPrint (0 ))
108
104
}
105
+
106
+ fun textToModule (code : String ): Module {
107
+ val lexer = Python3Lexer (fromString(code))
108
+ val tokens = CommonTokenStream (lexer)
109
+ val parser = Python3Parser (tokens)
110
+ val moduleVisitor = ModuleVisitor ()
111
+ return moduleVisitor.visit(parser.file_input()) as Module
112
+ }
113
+
114
+ object AnnotationProcessor {
115
+ // get only types with modules in prefixes
116
+ fun getTypesFromAnnotation (annotation : String ): Set <String > {
117
+ val annotationAST = textToModule(annotation)
118
+ val visitor = Visitor ()
119
+ val result = mutableSetOf<String >()
120
+ visitor.visitModule(annotationAST, result)
121
+ return result
122
+ }
123
+
124
+ private class Visitor : ModifierVisitor <MutableSet <String >>() {
125
+ override fun visitAtom (atom : Atom , param : MutableSet <String >): AST {
126
+ parse(
127
+ nameWithPrefixFromAtom(apply ()),
128
+ onError = null ,
129
+ atom
130
+ ) { it } ?.let { typeName ->
131
+ param.add(typeName)
132
+ }
133
+
134
+ return super .visitAtom(atom, param)
135
+ }
136
+ }
137
+ }
0 commit comments