feature: refactor api parse to g4 (#365)
* feature: refactor api parse to g4 * new g4 parser * add CHANGE_LOG.MD * refactor * fix byte bug * refactor * optimized * optimized * revert * update readme.md * update readme.md * update readme.md * update readme.md * remove no need * fix java gen * add upgrade * resolve confilits Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
This commit is contained in:
79
tools/goctl/api/parser/g4/ast/kv.go
Normal file
79
tools/goctl/api/parser/g4/ast/kv.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package ast
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
|
||||
)
|
||||
|
||||
type KvExpr struct {
|
||||
Key Expr
|
||||
Value Expr
|
||||
DocExpr []Expr
|
||||
CommentExpr Expr
|
||||
}
|
||||
|
||||
func (v *ApiVisitor) VisitKvLit(ctx *api.KvLitContext) interface{} {
|
||||
var kvExpr KvExpr
|
||||
kvExpr.Key = v.newExprWithToken(ctx.GetKey())
|
||||
commentExpr := v.getComment(ctx)
|
||||
if ctx.GetValue() != nil {
|
||||
valueText := ctx.GetValue().GetText()
|
||||
valueExpr := v.newExprWithToken(ctx.GetValue())
|
||||
if strings.Contains(valueText, "//") {
|
||||
if commentExpr == nil {
|
||||
commentExpr = v.newExprWithToken(ctx.GetValue())
|
||||
commentExpr.SetText("")
|
||||
}
|
||||
|
||||
index := strings.Index(valueText, "//")
|
||||
commentExpr.SetText(valueText[index:])
|
||||
valueExpr.SetText(strings.TrimSpace(valueText[:index]))
|
||||
} else if strings.Contains(valueText, "/*") {
|
||||
if commentExpr == nil {
|
||||
commentExpr = v.newExprWithToken(ctx.GetValue())
|
||||
commentExpr.SetText("")
|
||||
}
|
||||
|
||||
index := strings.Index(valueText, "/*")
|
||||
commentExpr.SetText(valueText[index:])
|
||||
valueExpr.SetText(strings.TrimSpace(valueText[:index]))
|
||||
}
|
||||
|
||||
kvExpr.Value = valueExpr
|
||||
}
|
||||
|
||||
kvExpr.DocExpr = v.getDoc(ctx)
|
||||
kvExpr.CommentExpr = commentExpr
|
||||
return &kvExpr
|
||||
}
|
||||
|
||||
func (k *KvExpr) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *KvExpr) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
kv, ok := v.(*KvExpr)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if !EqualDoc(k, kv) {
|
||||
return false
|
||||
}
|
||||
|
||||
return k.Key.Equal(kv.Key) && k.Value.Equal(kv.Value)
|
||||
}
|
||||
|
||||
func (k *KvExpr) Doc() []Expr {
|
||||
return k.DocExpr
|
||||
}
|
||||
|
||||
func (k *KvExpr) Comment() Expr {
|
||||
return k.CommentExpr
|
||||
}
|
||||
Reference in New Issue
Block a user