feat: [python] make first annotaion

上级 58ecc373
......@@ -4,10 +4,10 @@ type CodeDataStruct struct {
Name string
ID string
MemberIds []string
Properties []CodeProperty
Extend string
Implements []string
Annotations []interface{}
Properties []CodeProperty
Extension interface{}
}
......
......@@ -4,6 +4,7 @@ import (
"fmt"
. "github.com/onsi/gomega"
"github.com/phodal/coca/pkg/adapter/cocafile"
"github.com/phodal/coca/pkg/domain/trial"
"io/ioutil"
"strings"
"testing"
......@@ -52,4 +53,5 @@ func Test_PythonClassWithDecorator(t *testing.T) {
codeFile := app.Analysis(string(file), "testdata/grammar/class_or_func_def_stmt.py")
g.Expect(len(codeFile.DataStructures)).To(Equal(1))
g.Expect(codeFile.DataStructures[0].Annotations[0].(*trial.PythonAnnotation).Name).To(Equal("decorator"))
}
package pyast
import (
"bytes"
"fmt"
parser "github.com/phodal/coca/languages/python"
"github.com/phodal/coca/pkg/domain/trial"
"io"
"os"
"reflect"
)
......@@ -12,13 +15,24 @@ type PythonIdentListener struct {
}
var currentCodeFile *trial.CodeFile
var debug = false
var output io.Writer
func NewPythonIdentListener(fileName string) *PythonIdentListener {
currentCodeFile = &trial.CodeFile{}
currentCodeFile.FullName = fileName
output = os.Stdout
return &PythonIdentListener{}
}
func (s *PythonIdentListener) SetDebugOutput(isDebug bool) io.Writer {
output = new(bytes.Buffer)
debug = isDebug
return output
}
func (s *PythonIdentListener) EnterClassdef(ctx *parser.ClassdefContext) {
dataStruct := trial.CodeDataStruct{
Name: ctx.Name().GetText(),
......@@ -29,14 +43,43 @@ func (s *PythonIdentListener) EnterClassdef(ctx *parser.ClassdefContext) {
switch x := ctx.GetParent().GetChild(0).(type) {
case *parser.DecoratorContext:
decorator := BuildDecorator(x)
dataStruct.Annotations = append(dataStruct.Annotations, decorator)
default:
fmt.Println(reflect.TypeOf(x))
fmt.Fprintf(output, "EnterClassdef: %s\n", reflect.TypeOf(x))
}
currentCodeFile.DataStructures = append(currentCodeFile.DataStructures, dataStruct)
}
func BuildDecorator(x *parser.DecoratorContext) *trial.PythonAnnotation {
text := x.Dotted_name().GetText()
annotation := &trial.PythonAnnotation{
Name: text,
}
if x.Arglist() != nil {
annotation.Properties = BuildArgList(x.Arglist().(*parser.ArglistContext))
}
return annotation
}
func BuildArgList(context *parser.ArglistContext) []trial.CodeProperty {
var arguments []trial.CodeProperty
for _, arg := range context.AllArgument() {
argContext := arg.(*parser.ArgumentContext)
argument := &trial.CodeProperty{
Name: "",
TypeName: argContext.GetText(),
}
arguments = append(arguments, *argument)
}
return arguments
}
func (s *PythonIdentListener) GetCodeFileInfo() *trial.CodeFile {
return currentCodeFile
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册