Skip to content

Commit

Permalink
feat: [python] identify basic import
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 15, 2020
1 parent 6bd5da2 commit d08da6c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
8 changes: 7 additions & 1 deletion pkg/domain/trial/code_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ type CodeFile struct {
Members []*CodeMember
DataStructures []CodeDataStruct
// Deprecated: support for migration only
ClassNodes []domain.JClassNode
ClassNodes []domain.JClassNode
}

type CodeImport struct {
Source string
AsName string
UsageName []string
}
13 changes: 12 additions & 1 deletion trial/pkg/application/pyapp/py_ident_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"testing"
)


func Test_AllPythonGrammar(t *testing.T) {
g := NewGomegaWithT(t)

Expand Down Expand Up @@ -58,3 +57,15 @@ func Test_PythonClassWithDecorator(t *testing.T) {
g.Expect(codeFile.Members[0].MethodNodes[0].Name).To(Equal("bar"))
g.Expect(len(codeFile.Members[0].MethodNodes[0].Annotations.([]trial.PythonAnnotation))).To(Equal(2))
}

func Test_PythonImport(t *testing.T) {
g := NewGomegaWithT(t)

app := new(PythonApiApp)

file, _ := ioutil.ReadFile("testdata/grammar/import_stmt.py")
codeFile := app.Analysis(string(file), "import_stmt")

fmt.Println(codeFile)
g.Expect(true).To(Equal(true))
}
30 changes: 30 additions & 0 deletions trial/pkg/ast/pyast/python_ident_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pyast

import (
"bytes"
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
parser "github.com/phodal/coca/languages/python"
"github.com/phodal/coca/pkg/domain/trial"
Expand All @@ -16,6 +17,7 @@ type PythonIdentListener struct {
var currentCodeFile *trial.CodeFile
var debug = false
var output io.Writer
var hasEnterMember = false

func NewPythonIdentListener(fileName string) *PythonIdentListener {
currentCodeFile = &trial.CodeFile{}
Expand All @@ -32,7 +34,26 @@ func (s *PythonIdentListener) SetDebugOutput(isDebug bool) io.Writer {
return output
}

func (s *PythonIdentListener) EnterImport_stmt(ctx *parser.Import_stmtContext) {
for _, asName := range ctx.Dotted_as_names().(*parser.Dotted_as_namesContext).AllDotted_as_name() {
nameContext := asName.(*parser.Dotted_as_nameContext)
asNameText := nameContext.Dotted_name().GetText()
name := ""
if nameContext.Name() != nil {
name = nameContext.Name().GetText()
}

codeImport := &trial.CodeImport{
Source: asNameText,
AsName: name,
}

fmt.Println(codeImport)
}
}

func (s *PythonIdentListener) EnterClassdef(ctx *parser.ClassdefContext) {
hasEnterMember = true
dataStruct := trial.CodeDataStruct{
Name: ctx.Name().GetText(),
ID: "",
Expand All @@ -49,7 +70,12 @@ func (s *PythonIdentListener) EnterClassdef(ctx *parser.ClassdefContext) {
currentCodeFile.DataStructures = append(currentCodeFile.DataStructures, dataStruct)
}

func (s *PythonIdentListener) ExitClassdef(ctx *parser.ClassdefContext) {
hasEnterMember = false
}

func (s *PythonIdentListener) EnterFuncdef(ctx *parser.FuncdefContext) {
hasEnterMember = true
function := trial.CodeFunction{
Name: ctx.Name().GetText(),
}
Expand All @@ -68,6 +94,10 @@ func (s *PythonIdentListener) EnterFuncdef(ctx *parser.FuncdefContext) {
currentCodeFile.Members = append(currentCodeFile.Members, member)
}

func (s *PythonIdentListener) ExitFuncdef(ctx *parser.FuncdefContext) {
hasEnterMember = false
}

func BuildDecoratorsByIndex(node antlr.ParseTree, index int) []trial.PythonAnnotation {
var nodes []parser.DecoratorContext
for i := 0; i < index; i++ {
Expand Down

0 comments on commit d08da6c

Please sign in to comment.