Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Phodal
Chapi
提交
cf50e98a
C
Chapi
项目概览
Phodal
/
Chapi
通知
5
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Chapi
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
cf50e98a
编写于
2月 25, 2020
作者:
P
Phodal Huang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: <python> add basic local vars support
上级
b354b675
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
50 addition
and
8 deletion
+50
-8
chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonAstBaseListener.kt
.../main/kotlin/chapi/ast/pythonast/PythonAstBaseListener.kt
+34
-8
chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonFullIdentListener.kt
...ain/kotlin/chapi/ast/pythonast/PythonFullIdentListener.kt
+1
-0
chapi-ast-python/src/test/kotlin/chapi/ast/pythonast/PythonAnalyserTest.kt
...src/test/kotlin/chapi/ast/pythonast/PythonAnalyserTest.kt
+15
-0
未找到文件。
chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonAstBaseListener.kt
浏览文件 @
cf50e98a
...
...
@@ -7,7 +7,7 @@ import org.antlr.v4.runtime.tree.ParseTree
open
class
PythonAstBaseListener
:
PythonParserBaseListener
()
{
var
currentFunction
:
CodeFunction
=
CodeFunction
()
private
var
localVars
=
mutableMapOf
<
String
,
String
>()
var
localVars
=
mutableMapOf
<
String
,
String
>()
fun
buildParameters
(
listCtx
:
PythonParser
.
TypedargslistContext
?):
Array
<
CodeProperty
>
{
var
parameters
:
Array
<
CodeProperty
>
=
arrayOf
()
...
...
@@ -103,29 +103,53 @@ open class PythonAstBaseListener : PythonParserBaseListener() {
val
assignPartCtx
=
exprCtx
.
assign_part
()
if
(
assignPartCtx
!=
null
)
{
if
(
assignPartCtx
.
ASSIGN
()
!=
null
)
{
// println(assignPartCtx.getChild(1).text)
val
rightObj
=
buildAssignPart
(
assignPartCtx
)
localVars
[
leftPart
]
=
rightObj
}
}
}
private
fun
buildTestContext
(
testContext
:
PythonParser
.
TestContext
)
{
private
fun
buildAssignPart
(
assignPartCtx
:
PythonParser
.
Assign_partContext
):
String
{
var
returnAtom
=
""
for
(
starExprCtx
in
assignPartCtx
.
testlist_star_expr
())
{
val
child
=
starExprCtx
.
getChild
(
0
)
when
(
child
::
class
.
java
.
simpleName
)
{
"TestlistContext"
->
{
val
testlistCtx
=
child
as
PythonParser
.
TestlistContext
for
(
testContext
in
testlistCtx
.
test
())
{
returnAtom
=
this
.
buildTestContext
(
testContext
)
}
}
}
}
return
returnAtom
}
private
fun
buildTestContext
(
testContext
:
PythonParser
.
TestContext
):
String
{
var
returnType
=
""
val
exprCtx
=
testContext
.
getChild
(
0
).
getChild
(
0
).
getChild
(
0
)
val
expr
=
exprCtx
::
class
.
java
.
simpleName
when
(
expr
)
{
val
expr
Type
=
exprCtx
::
class
.
java
.
simpleName
when
(
exprType
)
{
"ExprContext"
->
{
val
exprCtx
=
exprCtx
as
PythonParser
.
ExprContext
val
exprChild
=
exprCtx
.
getChild
(
0
)
::
class
.
java
.
simpleName
when
(
exprChild
)
{
"AtomContext"
->
{
buildAtomExpr
(
exprCtx
)
returnType
=
buildAtomExpr
(
exprCtx
)
}
}
}
else
->
{
println
(
"buildTestContext -> "
+
exprType
)
}
}
return
returnType
}
private
fun
buildAtomExpr
(
exprCtx
:
PythonParser
.
ExprContext
)
{
var
funcCalls
:
Array
<
CodeCall
>
=
arrayOf
()
private
fun
buildAtomExpr
(
exprCtx
:
PythonParser
.
ExprContext
)
:
String
{
var
funcCalls
:
Array
<
CodeCall
>
=
arrayOf
()
val
atomName
=
exprCtx
.
atom
().
text
for
(
trailerContext
in
exprCtx
.
trailer
())
{
if
(
trailerContext
.
DOT
()
!=
null
)
{
...
...
@@ -146,5 +170,7 @@ open class PythonAstBaseListener : PythonParserBaseListener() {
}
currentFunction
.
FunctionCalls
=
funcCalls
return
atomName
}
}
chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonFullIdentListener.kt
浏览文件 @
cf50e98a
...
...
@@ -102,6 +102,7 @@ class PythonFullIdentListener(var fileName: String) : PythonAstBaseListener() {
}
override
fun
exitFuncdef
(
ctx
:
PythonParser
.
FuncdefContext
?)
{
currentFunction
.
addVarsFromMap
(
this
.
localVars
)
if
(
currentNode
.
NodeName
==
""
)
{
defaultNode
.
Functions
+=
currentFunction
}
else
{
...
...
chapi-ast-python/src/test/kotlin/chapi/ast/pythonast/PythonAnalyserTest.kt
浏览文件 @
cf50e98a
...
...
@@ -32,4 +32,19 @@ async def show(str):
assertEquals
(
firstFunc
.
FunctionCalls
[
0
].
FunctionName
,
"print"
)
assertEquals
(
firstFunc
.
FunctionCalls
[
0
].
NodeName
,
""
)
}
@Test
internal
fun
shouldIdentLocalVarsForFuncCall
()
{
val
code
=
"""
def build():
p = Person()
p.setName("Hunter").setAge(24).setSSN("111-22-3333")
"""
val
codeFile
=
PythonAnalyser
().
analysis
(
code
,
""
)
val
firstFunc
=
codeFile
.
DataStructures
[
0
].
Functions
[
0
]
assertEquals
(
firstFunc
.
LocalVariables
.
size
,
1
)
assertEquals
(
firstFunc
.
LocalVariables
[
0
].
TypeValue
,
"p"
)
assertEquals
(
firstFunc
.
LocalVariables
[
0
].
TypeType
,
"Person"
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录