Code optimized (#523)

* optimized markdown generator

* optimized markdown generator

* optimized markdown generator

* add more comment

* add comment

* add comment

* add comments for rpc tool

* add comments for model tool

* add comments for model tool

* add comments for model tool

* add comments for config tool

* add comments for config tool

* add comments

* add comments

* add comments

* add comments

* add comment

* remove rpc main head info

* add comment

* optimized

Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
This commit is contained in:
kingxt
2021-02-26 16:11:47 +08:00
committed by GitHub
parent ef146cf5ba
commit e6ef1fca12
104 changed files with 651 additions and 375 deletions

View File

@@ -7,6 +7,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
)
// Api describes syntax for api
type Api struct {
LinePrefix string
Syntax *SyntaxExpr
@@ -21,6 +22,7 @@ type Api struct {
routeM map[string]PlaceHolder
}
// VisitApi implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitApi(ctx *api.ApiContext) interface{} {
var final Api
final.importM = map[string]PlaceHolder{}
@@ -152,6 +154,7 @@ func (v *ApiVisitor) acceptSyntax(root *Api, final *Api) {
}
}
// VisitSpec implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitSpec(ctx *api.SpecContext) interface{} {
var root Api
if ctx.SyntaxLit() != nil {
@@ -178,11 +181,13 @@ func (v *ApiVisitor) VisitSpec(ctx *api.SpecContext) interface{} {
return &root
}
// Format provides a formatter for api command, now nothing to do
func (a *Api) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two Api are equal
func (a *Api) Equal(v interface{}) bool {
if v == nil {
return false

View File

@@ -12,6 +12,7 @@ import (
)
type (
// Parser provides api parsing capabilities
Parser struct {
linePrefix string
debug bool
@@ -19,9 +20,11 @@ type (
antlr.DefaultErrorListener
}
// ParserOption defines an function with argument Parser
ParserOption func(p *Parser)
)
// NewParser creates an instance for Parser
func NewParser(options ...ParserOption) *Parser {
p := &Parser{
log: console.NewColorConsole(),
@@ -425,6 +428,7 @@ func (p *Parser) readContent(filename string) (string, error) {
return string(data), nil
}
// SyntaxError accepts errors and panic it
func (p *Parser) SyntaxError(_ antlr.Recognizer, _ interface{}, line, column int, msg string, _ antlr.RecognitionException) {
str := fmt.Sprintf(`%s line %d:%d %s`, p.linePrefix, line, column, msg)
if p.debug {
@@ -433,12 +437,14 @@ func (p *Parser) SyntaxError(_ antlr.Recognizer, _ interface{}, line, column int
panic(str)
}
// WithParserDebug returns a debug ParserOption
func WithParserDebug() ParserOption {
return func(p *Parser) {
p.debug = true
}
}
// WithParserPrefix returns a prefix ParserOption
func WithParserPrefix(prefix string) ParserOption {
return func(p *Parser) {
p.linePrefix = prefix

View File

@@ -12,11 +12,15 @@ import (
)
type (
// TokenStream defines a token
TokenStream interface {
GetStart() antlr.Token
GetStop() antlr.Token
GetParser() antlr.Parser
}
// ApiVisitor wraps api.BaseApiParserVisitor to call methods which has prefix Visit to
// visit node from the api syntax
ApiVisitor struct {
api.BaseApiParserVisitor
debug bool
@@ -25,8 +29,10 @@ type (
infoFlag bool
}
// VisitorOption defines a function with argument ApiVisitor
VisitorOption func(v *ApiVisitor)
// Spec describes api spec
Spec interface {
Doc() []Expr
Comment() Expr
@@ -34,6 +40,7 @@ type (
Equal(v interface{}) bool
}
// Expr describes ast expression
Expr interface {
Prefix() string
Line() int
@@ -47,6 +54,7 @@ type (
}
)
// NewApiVisitor creates an instance for ApiVisitor
func NewApiVisitor(options ...VisitorOption) *ApiVisitor {
v := &ApiVisitor{
log: console.NewColorConsole(),
@@ -66,12 +74,14 @@ func (v *ApiVisitor) panic(expr Expr, msg string) {
panic(errString)
}
// WithVisitorPrefix returns a VisitorOption wrap with specified prefix
func WithVisitorPrefix(prefix string) VisitorOption {
return func(v *ApiVisitor) {
v.prefix = prefix
}
}
// WithVisitorDebug returns a debug VisitorOption
func WithVisitorDebug() VisitorOption {
return func(v *ApiVisitor) {
v.debug = true
@@ -84,6 +94,7 @@ type defaultExpr struct {
start, stop int
}
// NewTextExpr creates a default instance for Expr
func NewTextExpr(v string) *defaultExpr {
return &defaultExpr{
v: v,
@@ -201,6 +212,7 @@ func (e *defaultExpr) IsNotNil() bool {
return e != nil
}
// EqualDoc compares whether the element literals in two Spec are equal
func EqualDoc(spec1, spec2 Spec) bool {
if spec1 == nil {
return spec2 == nil

View File

@@ -4,6 +4,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
)
// ImportExpr defines import syntax for api
type ImportExpr struct {
Import Expr
Value Expr
@@ -11,6 +12,7 @@ type ImportExpr struct {
CommentExpr Expr
}
// VisitImportSpec implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitImportSpec(ctx *api.ImportSpecContext) interface{} {
var list []*ImportExpr
if ctx.ImportLit() != nil {
@@ -25,6 +27,7 @@ func (v *ApiVisitor) VisitImportSpec(ctx *api.ImportSpecContext) interface{} {
return list
}
// VisitImportLit implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitImportLit(ctx *api.ImportLitContext) interface{} {
importToken := v.newExprWithToken(ctx.GetImportToken())
valueExpr := ctx.ImportValue().Accept(v).(Expr)
@@ -38,6 +41,7 @@ func (v *ApiVisitor) VisitImportLit(ctx *api.ImportLitContext) interface{} {
}
}
// VisitImportBlock implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitImportBlock(ctx *api.ImportBlockContext) interface{} {
importToken := v.newExprWithToken(ctx.GetImportToken())
values := ctx.AllImportBlockValue()
@@ -52,6 +56,7 @@ func (v *ApiVisitor) VisitImportBlock(ctx *api.ImportBlockContext) interface{} {
return list
}
// VisitImportBlockValue implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitImportBlockValue(ctx *api.ImportBlockValueContext) interface{} {
value := ctx.ImportValue().Accept(v).(Expr)
return &ImportExpr{
@@ -61,15 +66,18 @@ func (v *ApiVisitor) VisitImportBlockValue(ctx *api.ImportBlockValueContext) int
}
}
// VisitImportValue implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitImportValue(ctx *api.ImportValueContext) interface{} {
return v.newExprWithTerminalNode(ctx.STRING())
}
// Format provides a formatter for api command, now nothing to do
func (i *ImportExpr) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two ImportExpr are equal
func (i *ImportExpr) Equal(v interface{}) bool {
if v == nil {
return false
@@ -87,10 +95,12 @@ func (i *ImportExpr) Equal(v interface{}) bool {
return i.Import.Equal(imp.Import) && i.Value.Equal(imp.Value)
}
// Doc returns the document of ImportExpr, like // some text
func (i *ImportExpr) Doc() []Expr {
return i.DocExpr
}
// Comment returns the comment of ImportExpr, like // some text
func (i *ImportExpr) Comment() Expr {
return i.CommentExpr
}

View File

@@ -4,6 +4,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
)
// InfoExpr defines info syntax for api
type InfoExpr struct {
Info Expr
Lp Expr
@@ -11,6 +12,7 @@ type InfoExpr struct {
Kvs []*KvExpr
}
// VisitInfoSpec implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitInfoSpec(ctx *api.InfoSpecContext) interface{} {
var expr InfoExpr
expr.Info = v.newExprWithToken(ctx.GetInfoToken())
@@ -29,11 +31,13 @@ func (v *ApiVisitor) VisitInfoSpec(ctx *api.InfoSpecContext) interface{} {
return &expr
}
// Format provides a formatter for api command, now nothing to do
func (i *InfoExpr) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two InfoExpr are equal
func (i *InfoExpr) Equal(v interface{}) bool {
if v == nil {
return false

View File

@@ -6,6 +6,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
)
// KvExpr describes key-value for api
type KvExpr struct {
Key Expr
Value Expr
@@ -13,6 +14,7 @@ type KvExpr struct {
CommentExpr Expr
}
// VisitKvLit implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitKvLit(ctx *api.KvLitContext) interface{} {
var kvExpr KvExpr
kvExpr.Key = v.newExprWithToken(ctx.GetKey())
@@ -48,11 +50,13 @@ func (v *ApiVisitor) VisitKvLit(ctx *api.KvLitContext) interface{} {
return &kvExpr
}
// Format provides a formatter for api command, now nothing to do
func (k *KvExpr) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two KvExpr are equal
func (k *KvExpr) Equal(v interface{}) bool {
if v == nil {
return false
@@ -70,10 +74,12 @@ func (k *KvExpr) Equal(v interface{}) bool {
return k.Key.Equal(kv.Key) && k.Value.Equal(kv.Value)
}
// Doc returns the document of KvExpr, like // some text
func (k *KvExpr) Doc() []Expr {
return k.DocExpr
}
// Comment returns the comment of KvExpr, like // some text
func (k *KvExpr) Comment() Expr {
return k.CommentExpr
}

View File

@@ -1,5 +1,7 @@
package ast
// Holder defines a default instance for PlaceHolder
var Holder PlaceHolder
// PlaceHolder defines an empty struct
type PlaceHolder struct{}

View File

@@ -7,13 +7,16 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
)
// Service describes service for api syntax
type Service struct {
AtServer *AtServer
ServiceApi *ServiceApi
}
// KV defines a slice for KvExpr
type KV []*KvExpr
// AtServer describes server metadata for api syntax
type AtServer struct {
AtServerToken Expr
Lp Expr
@@ -21,6 +24,7 @@ type AtServer struct {
Kv KV
}
// ServiceApi describes service ast for api syntax
type ServiceApi struct {
ServiceToken Expr
Name Expr
@@ -29,6 +33,7 @@ type ServiceApi struct {
ServiceRoute []*ServiceRoute
}
// ServiceRoute describes service route ast for api syntax
type ServiceRoute struct {
AtDoc *AtDoc
AtServer *AtServer
@@ -36,6 +41,7 @@ type ServiceRoute struct {
Route *Route
}
// AtDoc describes service comments ast for api syntax
type AtDoc struct {
AtDocToken Expr
Lp Expr
@@ -44,6 +50,7 @@ type AtDoc struct {
Kv []*KvExpr
}
// AtHandler describes service hander ast for api syntax
type AtHandler struct {
AtHandlerToken Expr
Name Expr
@@ -51,6 +58,7 @@ type AtHandler struct {
CommentExpr Expr
}
// Route describes route ast for api syntax
type Route struct {
Method Expr
Path Expr
@@ -61,12 +69,14 @@ type Route struct {
CommentExpr Expr
}
// Body describes request,response body ast for api syntax
type Body struct {
Lp Expr
Rp Expr
Name DataType
}
// VisitServiceSpec implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitServiceSpec(ctx *api.ServiceSpecContext) interface{} {
var serviceSpec Service
if ctx.AtServer() != nil {
@@ -77,6 +87,7 @@ func (v *ApiVisitor) VisitServiceSpec(ctx *api.ServiceSpecContext) interface{} {
return &serviceSpec
}
// VisitAtServer implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitAtServer(ctx *api.AtServerContext) interface{} {
var atServer AtServer
atServer.AtServerToken = v.newExprWithTerminalNode(ctx.ATSERVER())
@@ -90,6 +101,7 @@ func (v *ApiVisitor) VisitAtServer(ctx *api.AtServerContext) interface{} {
return &atServer
}
// VisitServiceApi implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitServiceApi(ctx *api.ServiceApiContext) interface{} {
var serviceApi ServiceApi
serviceApi.ServiceToken = v.newExprWithToken(ctx.GetServiceToken())
@@ -105,6 +117,7 @@ func (v *ApiVisitor) VisitServiceApi(ctx *api.ServiceApiContext) interface{} {
return &serviceApi
}
// VisitServiceRoute implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitServiceRoute(ctx *api.ServiceRouteContext) interface{} {
var serviceRoute ServiceRoute
if ctx.AtDoc() != nil {
@@ -121,6 +134,7 @@ func (v *ApiVisitor) VisitServiceRoute(ctx *api.ServiceRouteContext) interface{}
return &serviceRoute
}
// VisitAtDoc implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitAtDoc(ctx *api.AtDocContext) interface{} {
var atDoc AtDoc
atDoc.AtDocToken = v.newExprWithTerminalNode(ctx.ATDOC())
@@ -150,6 +164,7 @@ func (v *ApiVisitor) VisitAtDoc(ctx *api.AtDocContext) interface{} {
return &atDoc
}
// VisitAtHandler implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitAtHandler(ctx *api.AtHandlerContext) interface{} {
var atHandler AtHandler
astHandlerExpr := v.newExprWithTerminalNode(ctx.ATHANDLER())
@@ -160,6 +175,7 @@ func (v *ApiVisitor) VisitAtHandler(ctx *api.AtHandlerContext) interface{} {
return &atHandler
}
// VisitRoute implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitRoute(ctx *api.RouteContext) interface{} {
var route Route
path := ctx.Path()
@@ -193,6 +209,7 @@ func (v *ApiVisitor) VisitRoute(ctx *api.RouteContext) interface{} {
return &route
}
// VisitBody implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitBody(ctx *api.BodyContext) interface{} {
if ctx.ID() == nil {
return nil
@@ -211,7 +228,7 @@ func (v *ApiVisitor) VisitBody(ctx *api.BodyContext) interface{} {
}
}
// note: forward compatible
// VisitReplybody implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitReplybody(ctx *api.ReplybodyContext) interface{} {
if ctx.DataType() == nil {
return nil
@@ -253,10 +270,13 @@ func (v *ApiVisitor) VisitReplybody(ctx *api.ReplybodyContext) interface{} {
}
}
// Format provides a formatter for api command, now nothing to do
func (b *Body) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two Body are equal
func (b *Body) Equal(v interface{}) bool {
if v == nil {
return false
@@ -278,19 +298,23 @@ func (b *Body) Equal(v interface{}) bool {
return b.Name.Equal(body.Name)
}
// Format provides a formatter for api command, now nothing to do
func (r *Route) Format() error {
// todo
return nil
}
// Doc returns the document of Route, like // some text
func (r *Route) Doc() []Expr {
return r.DocExpr
}
// Comment returns the comment of Route, like // some text
func (r *Route) Comment() Expr {
return r.CommentExpr
}
// Equal compares whether the element literals in two Route are equal
func (r *Route) Equal(v interface{}) bool {
if v == nil {
return false
@@ -330,19 +354,23 @@ func (r *Route) Equal(v interface{}) bool {
return EqualDoc(r, route)
}
// Doc returns the document of AtHandler, like // some text
func (a *AtHandler) Doc() []Expr {
return a.DocExpr
}
// Comment returns the comment of AtHandler, like // some text
func (a *AtHandler) Comment() Expr {
return a.CommentExpr
}
// Format provides a formatter for api command, now nothing to do
func (a *AtHandler) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two AtHandler are equal
func (a *AtHandler) Equal(v interface{}) bool {
if v == nil {
return false
@@ -364,11 +392,13 @@ func (a *AtHandler) Equal(v interface{}) bool {
return EqualDoc(a, atHandler)
}
// Format provides a formatter for api command, now nothing to do
func (a *AtDoc) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two AtDoc are equal
func (a *AtDoc) Equal(v interface{}) bool {
if v == nil {
return false
@@ -419,11 +449,13 @@ func (a *AtDoc) Equal(v interface{}) bool {
return true
}
// Format provides a formatter for api command, now nothing to do
func (a *AtServer) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two AtServer are equal
func (a *AtServer) Equal(v interface{}) bool {
if v == nil {
return false
@@ -471,6 +503,7 @@ func (a *AtServer) Equal(v interface{}) bool {
return true
}
// Equal compares whether the element literals in two ServiceRoute are equal
func (s *ServiceRoute) Equal(v interface{}) bool {
if v == nil {
return false
@@ -500,11 +533,13 @@ func (s *ServiceRoute) Equal(v interface{}) bool {
return s.Route.Equal(sr.Route)
}
// Format provides a formatter for api command, now nothing to do
func (s *ServiceRoute) Format() error {
// todo
return nil
}
// GetHandler returns handler name of api route
func (s *ServiceRoute) GetHandler() Expr {
if s.AtHandler != nil {
return s.AtHandler.Name
@@ -513,11 +548,13 @@ func (s *ServiceRoute) GetHandler() Expr {
return s.AtServer.Kv.Get("handler")
}
// Format provides a formatter for api command, now nothing to do
func (a *ServiceApi) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two ServiceApi are equal
func (a *ServiceApi) Equal(v interface{}) bool {
if v == nil {
return false
@@ -569,11 +606,13 @@ func (a *ServiceApi) Equal(v interface{}) bool {
return true
}
// Format provides a formatter for api command, now nothing to do
func (s *Service) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two Service are equal
func (s *Service) Equal(v interface{}) bool {
if v == nil {
return false
@@ -593,6 +632,7 @@ func (s *Service) Equal(v interface{}) bool {
return s.ServiceApi.Equal(service.ServiceApi)
}
// Get returns the tergate KV by specified key
func (kv KV) Get(key string) Expr {
for _, each := range kv {
if each.Key.Text() == key {

View File

@@ -4,6 +4,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
)
// SyntaxExpr describes syntax for api
type SyntaxExpr struct {
Syntax Expr
Assign Expr
@@ -12,6 +13,7 @@ type SyntaxExpr struct {
CommentExpr Expr
}
// VisitSyntaxLit implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitSyntaxLit(ctx *api.SyntaxLitContext) interface{} {
syntax := v.newExprWithToken(ctx.GetSyntaxToken())
assign := v.newExprWithToken(ctx.GetAssign())
@@ -25,11 +27,13 @@ func (v *ApiVisitor) VisitSyntaxLit(ctx *api.SyntaxLitContext) interface{} {
}
}
// Format provides a formatter for api command, now nothing to do
func (s *SyntaxExpr) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two SyntaxExpr are equal
func (s *SyntaxExpr) Equal(v interface{}) bool {
if v == nil {
return false
@@ -49,10 +53,12 @@ func (s *SyntaxExpr) Equal(v interface{}) bool {
s.Version.Equal(syntax.Version)
}
// Doc returns the document of SyntaxExpr, like // some text
func (s *SyntaxExpr) Doc() []Expr {
return s.DocExpr
}
// Comment returns the comment of SyntaxExpr, like // some text
func (s *SyntaxExpr) Comment() Expr {
return s.CommentExpr
}

View File

@@ -9,13 +9,15 @@ import (
)
type (
// TypeAlias TypeStruct
// TypeExpr describes an expression for TypeAlias and TypeStruct
TypeExpr interface {
Doc() []Expr
Format() error
Equal(v interface{}) bool
NameExpr() Expr
}
// TypeAlias describes alias ast for api syatax
TypeAlias struct {
Name Expr
Assign Expr
@@ -24,6 +26,7 @@ type (
CommentExpr Expr
}
// TypeStruct describes structure ast for api syatax
TypeStruct struct {
Name Expr
Struct Expr
@@ -33,6 +36,7 @@ type (
Fields []*TypeField
}
// TypeField describes field ast for api syntax
TypeField struct {
IsAnonymous bool
// Name is nil if IsAnonymous
@@ -43,6 +47,7 @@ type (
CommentExpr Expr
}
// DataType describes datatype for api syntax, the default implementation expressions are
// Literal, Interface, Map, Array, Time, Pointer
DataType interface {
Expr() Expr
@@ -51,15 +56,18 @@ type (
IsNotNil() bool
}
// int, bool, Foo,...
// Literal describes the basic types of golang, non-reference types,
// such as int, bool, Foo,...
Literal struct {
Literal Expr
}
// Interface describes the interface type of golang,Its fixed value is interface{}
Interface struct {
Literal Expr
}
// Map describes the map ast for api syntax
Map struct {
MapExpr Expr
Map Expr
@@ -69,6 +77,7 @@ type (
Value DataType
}
// Array describes the slice ast for api syntax
Array struct {
ArrayExpr Expr
LBrack Expr
@@ -76,10 +85,12 @@ type (
Literal DataType
}
// Time describes the time ast for api syntax
Time struct {
Literal Expr
}
// Pointer describes the pointer ast for api syntax
Pointer struct {
PointerExpr Expr
Star Expr
@@ -87,6 +98,7 @@ type (
}
)
// VisitTypeSpec implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitTypeSpec(ctx *api.TypeSpecContext) interface{} {
if ctx.TypeLit() != nil {
return []TypeExpr{ctx.TypeLit().Accept(v).(TypeExpr)}
@@ -94,6 +106,7 @@ func (v *ApiVisitor) VisitTypeSpec(ctx *api.TypeSpecContext) interface{} {
return ctx.TypeBlock().Accept(v)
}
// VisitTypeLit implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitTypeLit(ctx *api.TypeLitContext) interface{} {
typeLit := ctx.TypeLitBody().Accept(v)
alias, ok := typeLit.(*TypeAlias)
@@ -109,6 +122,7 @@ func (v *ApiVisitor) VisitTypeLit(ctx *api.TypeLitContext) interface{} {
return typeLit
}
// VisitTypeBlock implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitTypeBlock(ctx *api.TypeBlockContext) interface{} {
list := ctx.AllTypeBlockBody()
var types []TypeExpr
@@ -119,6 +133,7 @@ func (v *ApiVisitor) VisitTypeBlock(ctx *api.TypeBlockContext) interface{} {
return types
}
// VisitTypeLitBody implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitTypeLitBody(ctx *api.TypeLitBodyContext) interface{} {
if ctx.TypeAlias() != nil {
return ctx.TypeAlias().Accept(v)
@@ -126,6 +141,7 @@ func (v *ApiVisitor) VisitTypeLitBody(ctx *api.TypeLitBodyContext) interface{} {
return ctx.TypeStruct().Accept(v)
}
// VisitTypeBlockBody implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitTypeBlockBody(ctx *api.TypeBlockBodyContext) interface{} {
if ctx.TypeBlockAlias() != nil {
return ctx.TypeBlockAlias().Accept(v).(*TypeAlias)
@@ -133,6 +149,7 @@ func (v *ApiVisitor) VisitTypeBlockBody(ctx *api.TypeBlockBodyContext) interface
return ctx.TypeBlockStruct().Accept(v).(*TypeStruct)
}
// VisitTypeStruct implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitTypeStruct(ctx *api.TypeStructContext) interface{} {
var st TypeStruct
st.Name = v.newExprWithToken(ctx.GetStructName())
@@ -168,6 +185,7 @@ func (v *ApiVisitor) VisitTypeStruct(ctx *api.TypeStructContext) interface{} {
return &st
}
// VisitTypeBlockStruct implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitTypeBlockStruct(ctx *api.TypeBlockStructContext) interface{} {
var st TypeStruct
st.Name = v.newExprWithToken(ctx.GetStructName())
@@ -200,6 +218,7 @@ func (v *ApiVisitor) VisitTypeBlockStruct(ctx *api.TypeBlockStructContext) inter
return &st
}
// VisitTypeBlockAlias implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitTypeBlockAlias(ctx *api.TypeBlockAliasContext) interface{} {
var alias TypeAlias
alias.Name = v.newExprWithToken(ctx.GetAlias())
@@ -212,6 +231,7 @@ func (v *ApiVisitor) VisitTypeBlockAlias(ctx *api.TypeBlockAliasContext) interfa
return &alias
}
// VisitTypeAlias implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitTypeAlias(ctx *api.TypeAliasContext) interface{} {
var alias TypeAlias
alias.Name = v.newExprWithToken(ctx.GetAlias())
@@ -224,6 +244,7 @@ func (v *ApiVisitor) VisitTypeAlias(ctx *api.TypeAliasContext) interface{} {
return &alias
}
// VisitField implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitField(ctx *api.FieldContext) interface{} {
iAnonymousFiled := ctx.AnonymousFiled()
iNormalFieldContext := ctx.NormalField()
@@ -236,6 +257,7 @@ func (v *ApiVisitor) VisitField(ctx *api.FieldContext) interface{} {
return nil
}
// VisitNormalField implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitNormalField(ctx *api.NormalFieldContext) interface{} {
var field TypeField
field.Name = v.newExprWithToken(ctx.GetFieldName())
@@ -259,6 +281,7 @@ func (v *ApiVisitor) VisitNormalField(ctx *api.NormalFieldContext) interface{} {
return &field
}
// VisitAnonymousFiled implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitAnonymousFiled(ctx *api.AnonymousFiledContext) interface{} {
start := ctx.GetStart()
stop := ctx.GetStop()
@@ -282,6 +305,7 @@ func (v *ApiVisitor) VisitAnonymousFiled(ctx *api.AnonymousFiledContext) interfa
return &field
}
// VisitDataType implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitDataType(ctx *api.DataTypeContext) interface{} {
if ctx.ID() != nil {
idExpr := v.newExprWithTerminalNode(ctx.ID())
@@ -310,6 +334,7 @@ func (v *ApiVisitor) VisitDataType(ctx *api.DataTypeContext) interface{} {
return ctx.TypeStruct().Accept(v)
}
// VisitPointerType implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitPointerType(ctx *api.PointerTypeContext) interface{} {
nameExpr := v.newExprWithTerminalNode(ctx.ID())
v.exportCheck(nameExpr)
@@ -320,6 +345,7 @@ func (v *ApiVisitor) VisitPointerType(ctx *api.PointerTypeContext) interface{} {
}
}
// VisitMapType implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitMapType(ctx *api.MapTypeContext) interface{} {
return &Map{
MapExpr: v.newExprWithText(ctx.GetText(), ctx.GetMapToken().GetLine(), ctx.GetMapToken().GetColumn(),
@@ -332,6 +358,7 @@ func (v *ApiVisitor) VisitMapType(ctx *api.MapTypeContext) interface{} {
}
}
// VisitArrayType implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitArrayType(ctx *api.ArrayTypeContext) interface{} {
return &Array{
ArrayExpr: v.newExprWithText(ctx.GetText(), ctx.GetLbrack().GetLine(), ctx.GetLbrack().GetColumn(), ctx.GetLbrack().GetStart(), ctx.DataType().GetStop().GetStop()),
@@ -341,22 +368,27 @@ func (v *ApiVisitor) VisitArrayType(ctx *api.ArrayTypeContext) interface{} {
}
}
// NameExpr returns the expression string of TypeAlias
func (a *TypeAlias) NameExpr() Expr {
return a.Name
}
// Doc returns the document of TypeAlias, like // some text
func (a *TypeAlias) Doc() []Expr {
return a.DocExpr
}
// Comment returns the comment of TypeAlias, like // some text
func (a *TypeAlias) Comment() Expr {
return a.CommentExpr
}
// Format provides a formatter for api command, now nothing to do
func (a *TypeAlias) Format() error {
return nil
}
// Equal compares whether the element literals in two TypeAlias are equal
func (a *TypeAlias) Equal(v interface{}) bool {
if v == nil {
return false
@@ -378,15 +410,18 @@ func (a *TypeAlias) Equal(v interface{}) bool {
return EqualDoc(a, alias)
}
// Expr returns the expression string of Literal
func (l *Literal) Expr() Expr {
return l.Literal
}
// Format provides a formatter for api command, now nothing to do
func (l *Literal) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two Literal are equal
func (l *Literal) Equal(dt DataType) bool {
if dt == nil {
return false
@@ -400,19 +435,23 @@ func (l *Literal) Equal(dt DataType) bool {
return l.Literal.Equal(v.Literal)
}
// IsNotNil returns whether the instance is nil or not
func (l *Literal) IsNotNil() bool {
return l != nil
}
// Expr returns the expression string of Interface
func (i *Interface) Expr() Expr {
return i.Literal
}
// Format provides a formatter for api command, now nothing to do
func (i *Interface) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two Interface are equal
func (i *Interface) Equal(dt DataType) bool {
if dt == nil {
return false
@@ -426,19 +465,23 @@ func (i *Interface) Equal(dt DataType) bool {
return i.Literal.Equal(v.Literal)
}
// IsNotNil returns whether the instance is nil or not
func (i *Interface) IsNotNil() bool {
return i != nil
}
// Expr returns the expression string of Map
func (m *Map) Expr() Expr {
return m.MapExpr
}
// Format provides a formatter for api command, now nothing to do
func (m *Map) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two Map are equal
func (m *Map) Equal(dt DataType) bool {
if dt == nil {
return false
@@ -464,19 +507,23 @@ func (m *Map) Equal(dt DataType) bool {
return m.Map.Equal(v.Map)
}
// IsNotNil returns whether the instance is nil or not
func (m *Map) IsNotNil() bool {
return m != nil
}
// Expr returns the expression string of Array
func (a *Array) Expr() Expr {
return a.ArrayExpr
}
// Format provides a formatter for api command, now nothing to do
func (a *Array) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two Array are equal
func (a *Array) Equal(dt DataType) bool {
if dt == nil {
return false
@@ -494,19 +541,23 @@ func (a *Array) Equal(dt DataType) bool {
return a.Literal.Equal(v.Literal)
}
// IsNotNil returns whether the instance is nil or not
func (a *Array) IsNotNil() bool {
return a != nil
}
// Expr returns the expression string of Time
func (t *Time) Expr() Expr {
return t.Literal
}
// Format provides a formatter for api command, now nothing to do
func (t *Time) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two Time are equal
func (t *Time) Equal(dt DataType) bool {
if dt == nil {
return false
@@ -520,18 +571,22 @@ func (t *Time) Equal(dt DataType) bool {
return t.Literal.Equal(v.Literal)
}
// IsNotNil returns whether the instance is nil or not
func (t *Time) IsNotNil() bool {
return t != nil
}
// Expr returns the expression string of Pointer
func (p *Pointer) Expr() Expr {
return p.PointerExpr
}
// Format provides a formatter for api command, now nothing to do
func (p *Pointer) Format() error {
return nil
}
// Equal compares whether the element literals in two Pointer are equal
func (p *Pointer) Equal(dt DataType) bool {
if dt == nil {
return false
@@ -553,14 +608,17 @@ func (p *Pointer) Equal(dt DataType) bool {
return p.Name.Equal(v.Name)
}
// IsNotNil returns whether the instance is nil or not
func (p *Pointer) IsNotNil() bool {
return p != nil
}
// NameExpr returns the expression string of TypeStruct
func (s *TypeStruct) NameExpr() Expr {
return s.Name
}
// Equal compares whether the element literals in two TypeStruct are equal
func (s *TypeStruct) Equal(dt interface{}) bool {
if dt == nil {
return false
@@ -621,15 +679,18 @@ func (s *TypeStruct) Equal(dt interface{}) bool {
return true
}
// Doc returns the document of TypeStruct, like // some text
func (s *TypeStruct) Doc() []Expr {
return s.DocExpr
}
// Format provides a formatter for api command, now nothing to do
func (s *TypeStruct) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two TypeField are equal
func (t *TypeField) Equal(v interface{}) bool {
if v == nil {
return false
@@ -663,14 +724,17 @@ func (t *TypeField) Equal(v interface{}) bool {
return EqualDoc(t, f)
}
// Doc returns the document of TypeField, like // some text
func (t *TypeField) Doc() []Expr {
return t.DocExpr
}
// Comment returns the comment of TypeField, like // some text
func (t *TypeField) Comment() Expr {
return t.CommentExpr
}
// Format provides a formatter for api command, now nothing to do
func (t *TypeField) Format() error {
// todo
return nil

View File

@@ -4931,7 +4931,7 @@ func (p *ApiParserParser) Route() (localctx IRouteContext) {
}()
p.EnterOuterAlt(localctx, 1)
checkHttpMethod(p)
checkHTTPMethod(p)
{
p.SetState(291)

View File

@@ -74,7 +74,7 @@ func checkKeyValue(p *ApiParserParser) {
setCurrentTokenText(p, v)
}
func checkHttpMethod(p *ApiParserParser) {
func checkHTTPMethod(p *ApiParserParser) {
method := getCurrentTokenText(p)
uppler := strings.ToUpper(method)
switch uppler {
@@ -107,11 +107,13 @@ func checkKey(p *ApiParserParser) {
}
}
// IsBasicType returns true if the input argument is basic golang type
func IsBasicType(text string) bool {
_, ok := kind[text]
return ok
}
// IsGolangKeyWord returns true if input argument is golang keyword, but it will be ignored which in excepts
func IsGolangKeyWord(text string, excepts ...string) bool {
for _, each := range excepts {
if text == each {
@@ -171,6 +173,7 @@ func isNormal(p *ApiParserParser) bool {
return len(list) > 1
}
// MatchTag returns a Boolean value, which returns true if it does matched, otherwise returns fase
func MatchTag(v string) bool {
return matchRegex(v, tagRegex)
}

View File

@@ -12,7 +12,7 @@ import (
)
var (
normalApi = `
normalAPI = `
syntax="v1"
info (
@@ -32,7 +32,7 @@ var (
post /foo (Foo) returns ([]int)
}
`
missDeclarationApi = `
missDeclarationAPI = `
@server(
foo: bar
)
@@ -43,7 +43,7 @@ var (
}
`
missDeclarationInArrayApi = `
missDeclarationInArrayAPI = `
@server(
foo: bar
)
@@ -54,7 +54,7 @@ var (
}
`
missDeclarationInArrayApi2 = `
missDeclarationInArrayAPI2 = `
@server(
foo: bar
)
@@ -65,7 +65,7 @@ var (
}
`
nestedApiImport = `
nestedAPIImport = `
import "foo.api"
`
@@ -99,27 +99,27 @@ var (
)
func TestApiParser(t *testing.T) {
t.Run("missDeclarationApi", func(t *testing.T) {
_, err := parser.ParseContent(missDeclarationApi)
t.Run("missDeclarationAPI", func(t *testing.T) {
_, err := parser.ParseContent(missDeclarationAPI)
assert.Error(t, err)
fmt.Printf("%+v\n", err)
})
t.Run("missDeclarationApi", func(t *testing.T) {
_, err := parser.ParseContent(missDeclarationInArrayApi)
t.Run("missDeclarationAPI", func(t *testing.T) {
_, err := parser.ParseContent(missDeclarationInArrayAPI)
assert.Error(t, err)
fmt.Printf("%+v\n", err)
})
t.Run("missDeclarationApi", func(t *testing.T) {
_, err := parser.ParseContent(missDeclarationInArrayApi2)
t.Run("missDeclarationAPI", func(t *testing.T) {
_, err := parser.ParseContent(missDeclarationInArrayAPI2)
assert.Error(t, err)
fmt.Printf("%+v\n", err)
})
t.Run("nestedImport", func(t *testing.T) {
file := filepath.Join(t.TempDir(), "foo.api")
err := ioutil.WriteFile(file, []byte(nestedApiImport), os.ModePerm)
err := ioutil.WriteFile(file, []byte(nestedAPIImport), os.ModePerm)
if err != nil {
return
}
@@ -275,7 +275,7 @@ func TestApiParser(t *testing.T) {
})
t.Run("normal", func(t *testing.T) {
v, err := parser.ParseContent(normalApi)
v, err := parser.ParseContent(normalAPI)
assert.Nil(t, err)
body := &ast.Body{
Lp: ast.NewTextExpr("("),

View File

@@ -15,6 +15,7 @@ type parser struct {
spec *spec.ApiSpec
}
// Parse parses the api file
func Parse(filename string) (*spec.ApiSpec, error) {
astParser := ast.NewParser(ast.WithParserPrefix(filepath.Base(filename)))
ast, err := astParser.Parse(filename)
@@ -32,6 +33,7 @@ func Parse(filename string) (*spec.ApiSpec, error) {
return spec, nil
}
// ParseContent parses the api content
func ParseContent(content string) (*spec.ApiSpec, error) {
astParser := ast.NewParser()
ast, err := astParser.ParseContent(content)