You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/api/parser/g4/ast/import.go

97 lines
2.0 KiB
Go

package ast
import (
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
)
type ImportExpr struct {
Import Expr
Value Expr
DocExpr []Expr
CommentExpr Expr
}
func (v *ApiVisitor) VisitImportSpec(ctx *api.ImportSpecContext) interface{} {
var list []*ImportExpr
if ctx.ImportLit() != nil {
lits := ctx.ImportLit().Accept(v).([]*ImportExpr)
list = append(list, lits...)
}
if ctx.ImportBlock() != nil {
blocks := ctx.ImportBlock().Accept(v).([]*ImportExpr)
list = append(list, blocks...)
}
return list
}
func (v *ApiVisitor) VisitImportLit(ctx *api.ImportLitContext) interface{} {
importToken := v.newExprWithToken(ctx.GetImportToken())
valueExpr := ctx.ImportValue().Accept(v).(Expr)
return []*ImportExpr{
{
Import: importToken,
Value: valueExpr,
DocExpr: v.getDoc(ctx),
CommentExpr: v.getComment(ctx),
},
}
}
func (v *ApiVisitor) VisitImportBlock(ctx *api.ImportBlockContext) interface{} {
importToken := v.newExprWithToken(ctx.GetImportToken())
values := ctx.AllImportBlockValue()
var list []*ImportExpr
for _, value := range values {
importExpr := value.Accept(v).(*ImportExpr)
importExpr.Import = importToken
list = append(list, importExpr)
}
return list
}
func (v *ApiVisitor) VisitImportBlockValue(ctx *api.ImportBlockValueContext) interface{} {
value := ctx.ImportValue().Accept(v).(Expr)
return &ImportExpr{
Value: value,
DocExpr: v.getDoc(ctx),
CommentExpr: v.getComment(ctx),
}
}
func (v *ApiVisitor) VisitImportValue(ctx *api.ImportValueContext) interface{} {
return v.newExprWithTerminalNode(ctx.STRING())
}
func (i *ImportExpr) Format() error {
// todo
return nil
}
func (i *ImportExpr) Equal(v interface{}) bool {
if v == nil {
return false
}
imp, ok := v.(*ImportExpr)
if !ok {
return false
}
if !EqualDoc(i, imp) {
return false
}
return i.Import.Equal(imp.Import) && i.Value.Equal(imp.Value)
}
func (i *ImportExpr) Doc() []Expr {
return i.DocExpr
}
func (i *ImportExpr) Comment() Expr {
return i.CommentExpr
}