ast

package
v0.0.0-...-620b93c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrImportCycleNotAllowed = errors.New("import cycle not allowed")

ErrImportCycleNotAllowed defines an error for circular importing

Functions

func EqualDoc

func EqualDoc(spec1, spec2 Spec) bool

EqualDoc compares whether the element literals in two Spec are equal

func NewTextExpr

func NewTextExpr(v string) *defaultExpr

NewTextExpr creates a default instance for Expr

Types

type Api

type Api struct {
	LinePrefix string
	Syntax     *SyntaxExpr
	Import     []*ImportExpr

	Info *InfoExpr
	Type []TypeExpr

	Service []*Service
	// contains filtered or unexported fields
}

Api describes syntax for api

func (*Api) Equal

func (a *Api) Equal(v interface{}) bool

Equal compares whether the element literals in two Api are equal

func (*Api) Format

func (a *Api) Format() error

Format provides a formatter for api command, now nothing to do

type ApiVisitor

type ApiVisitor struct {
	*api.BaseApiParserVisitor
	// contains filtered or unexported fields
}

ApiVisitor wraps api.BaseApiParserVisitor to call methods which has prefix Visit to visit node from the api syntax

func NewApiVisitor

func NewApiVisitor(options ...VisitorOption) *ApiVisitor

NewApiVisitor creates an instance for ApiVisitor

func (*ApiVisitor) VisitAnonymousFiled

func (v *ApiVisitor) VisitAnonymousFiled(ctx *api.AnonymousFiledContext) interface{}

VisitAnonymousFiled implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitApi

func (v *ApiVisitor) VisitApi(ctx *api.ApiContext) interface{}

VisitApi implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitArrayType

func (v *ApiVisitor) VisitArrayType(ctx *api.ArrayTypeContext) interface{}

VisitArrayType implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitAtDoc

func (v *ApiVisitor) VisitAtDoc(ctx *api.AtDocContext) interface{}

VisitAtDoc implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitAtHandler

func (v *ApiVisitor) VisitAtHandler(ctx *api.AtHandlerContext) interface{}

VisitAtHandler implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitAtServer

func (v *ApiVisitor) VisitAtServer(ctx *api.AtServerContext) interface{}

VisitAtServer implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitBody

func (v *ApiVisitor) VisitBody(ctx *api.BodyContext) interface{}

VisitBody implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitDataType

func (v *ApiVisitor) VisitDataType(ctx *api.DataTypeContext) interface{}

VisitDataType implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitField

func (v *ApiVisitor) VisitField(ctx *api.FieldContext) interface{}

VisitField implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitImportBlock

func (v *ApiVisitor) VisitImportBlock(ctx *api.ImportBlockContext) interface{}

VisitImportBlock implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitImportBlockValue

func (v *ApiVisitor) VisitImportBlockValue(ctx *api.ImportBlockValueContext) interface{}

VisitImportBlockValue implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitImportLit

func (v *ApiVisitor) VisitImportLit(ctx *api.ImportLitContext) interface{}

VisitImportLit implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitImportSpec

func (v *ApiVisitor) VisitImportSpec(ctx *api.ImportSpecContext) interface{}

VisitImportSpec implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitImportValue

func (v *ApiVisitor) VisitImportValue(ctx *api.ImportValueContext) interface{}

VisitImportValue implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitInfoSpec

func (v *ApiVisitor) VisitInfoSpec(ctx *api.InfoSpecContext) interface{}

VisitInfoSpec implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitKvLit

func (v *ApiVisitor) VisitKvLit(ctx *api.KvLitContext) interface{}

VisitKvLit implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitMapType

func (v *ApiVisitor) VisitMapType(ctx *api.MapTypeContext) interface{}

VisitMapType implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitNormalField

func (v *ApiVisitor) VisitNormalField(ctx *api.NormalFieldContext) interface{}

VisitNormalField implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitPointerType

func (v *ApiVisitor) VisitPointerType(ctx *api.PointerTypeContext) interface{}

VisitPointerType implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitReplybody

func (v *ApiVisitor) VisitReplybody(ctx *api.ReplybodyContext) interface{}

VisitReplybody implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitRoute

func (v *ApiVisitor) VisitRoute(ctx *api.RouteContext) interface{}

serVisitRoute implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitServiceApi

func (v *ApiVisitor) VisitServiceApi(ctx *api.ServiceApiContext) interface{}

VisitServiceApi implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitServiceRoute

func (v *ApiVisitor) VisitServiceRoute(ctx *api.ServiceRouteContext) interface{}

VisitServiceRoute implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitServiceSpec

func (v *ApiVisitor) VisitServiceSpec(ctx *api.ServiceSpecContext) interface{}

VisitServiceSpec implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitSpec

func (v *ApiVisitor) VisitSpec(ctx *api.SpecContext) interface{}

VisitSpec implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitSyntaxLit

func (v *ApiVisitor) VisitSyntaxLit(ctx *api.SyntaxLitContext) interface{}

VisitSyntaxLit implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitTypeAlias

func (v *ApiVisitor) VisitTypeAlias(ctx *api.TypeAliasContext) interface{}

VisitTypeAlias implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitTypeBlock

func (v *ApiVisitor) VisitTypeBlock(ctx *api.TypeBlockContext) interface{}

VisitTypeBlock implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitTypeBlockAlias

func (v *ApiVisitor) VisitTypeBlockAlias(ctx *api.TypeBlockAliasContext) interface{}

VisitTypeBlockAlias implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitTypeBlockBody

func (v *ApiVisitor) VisitTypeBlockBody(ctx *api.TypeBlockBodyContext) interface{}

VisitTypeBlockBody implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitTypeBlockStruct

func (v *ApiVisitor) VisitTypeBlockStruct(ctx *api.TypeBlockStructContext) interface{}

VisitTypeBlockStruct implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitTypeLit

func (v *ApiVisitor) VisitTypeLit(ctx *api.TypeLitContext) interface{}

VisitTypeLit implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitTypeLitBody

func (v *ApiVisitor) VisitTypeLitBody(ctx *api.TypeLitBodyContext) interface{}

VisitTypeLitBody implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitTypeSpec

func (v *ApiVisitor) VisitTypeSpec(ctx *api.TypeSpecContext) interface{}

VisitTypeSpec implements from api.BaseApiParserVisitor

func (*ApiVisitor) VisitTypeStruct

func (v *ApiVisitor) VisitTypeStruct(ctx *api.TypeStructContext) interface{}

VisitTypeStruct implements from api.BaseApiParserVisitor

type Array

type Array struct {
	ArrayExpr Expr
	LBrack    Expr
	RBrack    Expr
	Literal   DataType
}

Array describes the slice ast for api syntax

func (*Array) Equal

func (a *Array) Equal(dt DataType) bool

Equal compares whether the element literals in two Array are equal

func (*Array) Expr

func (a *Array) Expr() Expr

Expr returns the expression string of Array

func (*Array) Format

func (a *Array) Format() error

Format provides a formatter for api command, now nothing to do

func (*Array) IsNotNil

func (a *Array) IsNotNil() bool

IsNotNil returns whether the instance is nil or not

type AtDoc

type AtDoc struct {
	AtDocToken Expr
	Lp         Expr
	Rp         Expr
	LineDoc    Expr
	Kv         []*KvExpr
}

AtDoc describes service comments ast for api syntax

func (*AtDoc) Equal

func (a *AtDoc) Equal(v interface{}) bool

Equal compares whether the element literals in two AtDoc are equal

func (*AtDoc) Format

func (a *AtDoc) Format() error

Format provides a formatter for api command, now nothing to do

type AtHandler

type AtHandler struct {
	AtHandlerToken Expr
	Name           Expr
	DocExpr        []Expr
	CommentExpr    Expr
}

AtHandler describes service handler ast for api syntax

func (*AtHandler) Comment

func (a *AtHandler) Comment() Expr

Comment returns the comment of AtHandler, like // some text

func (*AtHandler) Doc

func (a *AtHandler) Doc() []Expr

Doc returns the document of AtHandler, like // some text

func (*AtHandler) Equal

func (a *AtHandler) Equal(v interface{}) bool

Equal compares whether the element literals in two AtHandler are equal

func (*AtHandler) Format

func (a *AtHandler) Format() error

Format provides a formatter for api command, now nothing to do

type AtServer

type AtServer struct {
	AtServerToken Expr
	Lp            Expr
	Rp            Expr
	Kv            KV
}

AtServer describes server metadata for api syntax

func (*AtServer) Equal

func (a *AtServer) Equal(v interface{}) bool

Equal compares whether the element literals in two AtServer are equal

func (*AtServer) Format

func (a *AtServer) Format() error

Format provides a formatter for api command, now nothing to do

type Body

type Body struct {
	ReturnExpr Expr
	Lp         Expr
	Rp         Expr
	Name       DataType
}

Body describes request,response body ast for api syntax

func (*Body) Equal

func (b *Body) Equal(v interface{}) bool

Equal compares whether the element literals in two Body are equal

func (*Body) Format

func (b *Body) Format() error

Format provides a formatter for api command, now nothing to do

type DataType

type DataType interface {
	Expr() Expr
	Equal(dt DataType) bool
	Format() error
	IsNotNil() bool
}

DataType describes datatype for api syntax, the default implementation expressions are Literal, Interface, Map, Array, Time, Pointer

type Expr

type Expr interface {
	Prefix() string
	Line() int
	Column() int
	Text() string
	SetText(text string)
	Start() int
	Stop() int
	Equal(expr Expr) bool
	IsNotNil() bool
}

Expr describes ast expression

type ImportExpr

type ImportExpr struct {
	Import      Expr
	Value       Expr
	DocExpr     []Expr
	CommentExpr Expr
}

ImportExpr defines import syntax for api

func (*ImportExpr) Comment

func (i *ImportExpr) Comment() Expr

Comment returns the comment of ImportExpr, like // some text

func (*ImportExpr) Doc

func (i *ImportExpr) Doc() []Expr

Doc returns the document of ImportExpr, like // some text

func (*ImportExpr) Equal

func (i *ImportExpr) Equal(v interface{}) bool

Equal compares whether the element literals in two ImportExpr are equal

func (*ImportExpr) Format

func (i *ImportExpr) Format() error

Format provides a formatter for api command, now nothing to do

type InfoExpr

type InfoExpr struct {
	Info Expr
	Lp   Expr
	Rp   Expr
	Kvs  []*KvExpr
}

InfoExpr defines info syntax for api

func (*InfoExpr) Equal

func (i *InfoExpr) Equal(v interface{}) bool

Equal compares whether the element literals in two InfoExpr are equal

func (*InfoExpr) Format

func (i *InfoExpr) Format() error

Format provides a formatter for api command, now nothing to do

type Interface

type Interface struct {
	Literal Expr
}

Interface describes the interface type of golang,Its fixed value is interface{}

func (*Interface) Equal

func (i *Interface) Equal(dt DataType) bool

Equal compares whether the element literals in two Interface are equal

func (*Interface) Expr

func (i *Interface) Expr() Expr

Expr returns the expression string of Interface

func (*Interface) Format

func (i *Interface) Format() error

Format provides a formatter for api command, now nothing to do

func (*Interface) IsNotNil

func (i *Interface) IsNotNil() bool

IsNotNil returns whether the instance is nil or not

type KV

type KV []*KvExpr

KV defines a slice for KvExpr

func (KV) Get

func (kv KV) Get(key string) Expr

Get returns the target KV by specified key

type KvExpr

type KvExpr struct {
	Key         Expr
	Value       Expr
	DocExpr     []Expr
	CommentExpr Expr
}

KvExpr describes key-value for api

func (*KvExpr) Comment

func (k *KvExpr) Comment() Expr

Comment returns the comment of KvExpr, like // some text

func (*KvExpr) Doc

func (k *KvExpr) Doc() []Expr

Doc returns the document of KvExpr, like // some text

func (*KvExpr) Equal

func (k *KvExpr) Equal(v interface{}) bool

Equal compares whether the element literals in two KvExpr are equal

func (*KvExpr) Format

func (k *KvExpr) Format() error

Format provides a formatter for api command, now nothing to do

type Literal

type Literal struct {
	Literal Expr
}

Literal describes the basic types of golang, non-reference types, such as int, bool, Foo,...

func (*Literal) Equal

func (l *Literal) Equal(dt DataType) bool

Equal compares whether the element literals in two Literal are equal

func (*Literal) Expr

func (l *Literal) Expr() Expr

Expr returns the expression string of Literal

func (*Literal) Format

func (l *Literal) Format() error

Format provides a formatter for api command, now nothing to do

func (*Literal) IsNotNil

func (l *Literal) IsNotNil() bool

IsNotNil returns whether the instance is nil or not

type Map

type Map struct {
	MapExpr Expr
	Map     Expr
	LBrack  Expr
	RBrack  Expr
	Key     Expr
	Value   DataType
}

Map describes the map ast for api syntax

func (*Map) Equal

func (m *Map) Equal(dt DataType) bool

Equal compares whether the element literals in two Map are equal

func (*Map) Expr

func (m *Map) Expr() Expr

Expr returns the expression string of Map

func (*Map) Format

func (m *Map) Format() error

Format provides a formatter for api command, now nothing to do

func (*Map) IsNotNil

func (m *Map) IsNotNil() bool

IsNotNil returns whether the instance is nil or not

type Parser

type Parser struct {
	antlr.DefaultErrorListener
	// contains filtered or unexported fields
}

Parser provides api parsing capabilities

func NewParser

func NewParser(options ...ParserOption) *Parser

NewParser creates an instance for Parser

func (*Parser) Accept

func (p *Parser) Accept(fn func(p *api.ApiParserParser, visitor *ApiVisitor) interface{}, content string) (v interface{}, err error)

Accept can parse any terminalNode of api tree by fn. -- for debug

func (*Parser) Parse

func (p *Parser) Parse(filename string) (*Api, error)

Parse is used to parse the api from the specified file name

func (*Parser) ParseContent

func (p *Parser) ParseContent(content string, filename ...string) (*Api, error)

ParseContent is used to parse the api from the specified content

func (*Parser) SyntaxError

func (p *Parser) SyntaxError(_ antlr.Recognizer, _ interface{}, line, column int, msg string, _ antlr.RecognitionException)

SyntaxError accepts errors and panic it

type ParserOption

type ParserOption func(p *Parser)

ParserOption defines an function with argument Parser

func WithParserDebug

func WithParserDebug() ParserOption

WithParserDebug returns a debug ParserOption

func WithParserPrefix

func WithParserPrefix(prefix string) ParserOption

WithParserPrefix returns a prefix ParserOption

func WithParserSkipCheckTypeDeclaration

func WithParserSkipCheckTypeDeclaration() ParserOption

type PlaceHolder

type PlaceHolder struct{}

PlaceHolder defines an empty struct

var Holder PlaceHolder

Holder defines a default instance for PlaceHolder

type Pointer

type Pointer struct {
	PointerExpr Expr
	Star        Expr
	Name        Expr
}

Pointer describes the pointer ast for api syntax

func (*Pointer) Equal

func (p *Pointer) Equal(dt DataType) bool

Equal compares whether the element literals in two Pointer are equal

func (*Pointer) Expr

func (p *Pointer) Expr() Expr

Expr returns the expression string of Pointer

func (*Pointer) Format

func (p *Pointer) Format() error

Format provides a formatter for api command, now nothing to do

func (*Pointer) IsNotNil

func (p *Pointer) IsNotNil() bool

IsNotNil returns whether the instance is nil or not

type Route

type Route struct {
	Method      Expr
	Path        Expr
	Req         *Body
	ReturnToken Expr
	Reply       *Body
	DocExpr     []Expr
	CommentExpr Expr
}

Route describes route ast for api syntax

func (*Route) Comment

func (r *Route) Comment() Expr

Comment returns the comment of Route, like // some text

func (*Route) Doc

func (r *Route) Doc() []Expr

Doc returns the document of Route, like // some text

func (*Route) Equal

func (r *Route) Equal(v interface{}) bool

Equal compares whether the element literals in two Route are equal

func (*Route) Format

func (r *Route) Format() error

Format provides a formatter for api command, now nothing to do

type Service

type Service struct {
	AtServer   *AtServer
	ServiceApi *ServiceApi
}

Service describes service for api syntax

func (*Service) Equal

func (s *Service) Equal(v interface{}) bool

Equal compares whether the element literals in two Service are equal

func (*Service) Format

func (s *Service) Format() error

Format provides a formatter for api command, now nothing to do

type ServiceApi

type ServiceApi struct {
	ServiceToken Expr
	Name         Expr
	Lbrace       Expr
	Rbrace       Expr
	ServiceRoute []*ServiceRoute
}

ServiceApi describes service ast for api syntax

func (*ServiceApi) Equal

func (a *ServiceApi) Equal(v interface{}) bool

Equal compares whether the element literals in two ServiceApi are equal

func (*ServiceApi) Format

func (a *ServiceApi) Format() error

Format provides a formatter for api command, now nothing to do

type ServiceRoute

type ServiceRoute struct {
	AtDoc     *AtDoc
	AtServer  *AtServer
	AtHandler *AtHandler
	Route     *Route
}

ServiceRoute describes service route ast for api syntax

func (*ServiceRoute) Equal

func (s *ServiceRoute) Equal(v interface{}) bool

Equal compares whether the element literals in two ServiceRoute are equal

func (*ServiceRoute) Format

func (s *ServiceRoute) Format() error

Format provides a formatter for api command, now nothing to do

func (*ServiceRoute) GetHandler

func (s *ServiceRoute) GetHandler() Expr

GetHandler returns handler name of api route

type Spec

type Spec interface {
	Doc() []Expr
	Comment() Expr
	Format() error
	Equal(v interface{}) bool
}

Spec describes api spec

type SyntaxExpr

type SyntaxExpr struct {
	Syntax      Expr
	Assign      Expr
	Version     Expr
	DocExpr     []Expr
	CommentExpr Expr
}

SyntaxExpr describes syntax for api

func (*SyntaxExpr) Comment

func (s *SyntaxExpr) Comment() Expr

Comment returns the comment of SyntaxExpr, like // some text

func (*SyntaxExpr) Doc

func (s *SyntaxExpr) Doc() []Expr

Doc returns the document of SyntaxExpr, like // some text

func (*SyntaxExpr) Equal

func (s *SyntaxExpr) Equal(v interface{}) bool

Equal compares whether the element literals in two SyntaxExpr are equal

func (*SyntaxExpr) Format

func (s *SyntaxExpr) Format() error

Format provides a formatter for api command, now nothing to do

type Time

type Time struct {
	Literal Expr
}

Time describes the time ast for api syntax

func (*Time) Equal

func (t *Time) Equal(dt DataType) bool

Equal compares whether the element literals in two Time are equal

func (*Time) Expr

func (t *Time) Expr() Expr

Expr returns the expression string of Time

func (*Time) Format

func (t *Time) Format() error

Format provides a formatter for api command, now nothing to do

func (*Time) IsNotNil

func (t *Time) IsNotNil() bool

IsNotNil returns whether the instance is nil or not

type TokenStream

type TokenStream interface {
	GetStart() antlr.Token
	GetStop() antlr.Token
	GetParser() antlr.Parser
}

TokenStream defines a token

type TypeAlias

type TypeAlias struct {
	Name        Expr
	Assign      Expr
	DataType    DataType
	DocExpr     []Expr
	CommentExpr Expr
}

TypeAlias describes alias ast for api syntax

func (*TypeAlias) Comment

func (a *TypeAlias) Comment() Expr

Comment returns the comment of TypeAlias, like // some text

func (*TypeAlias) Doc

func (a *TypeAlias) Doc() []Expr

Doc returns the document of TypeAlias, like // some text

func (*TypeAlias) Equal

func (a *TypeAlias) Equal(v interface{}) bool

Equal compares whether the element literals in two TypeAlias are equal

func (*TypeAlias) Format

func (a *TypeAlias) Format() error

Format provides a formatter for api command, now nothing to do

func (*TypeAlias) NameExpr

func (a *TypeAlias) NameExpr() Expr

NameExpr returns the expression string of TypeAlias

type TypeExpr

type TypeExpr interface {
	Doc() []Expr
	Format() error
	Equal(v interface{}) bool
	NameExpr() Expr
}

TypeExpr describes an expression for TypeAlias and TypeStruct

type TypeField

type TypeField struct {
	IsAnonymous bool
	// Name is nil if IsAnonymous
	Name        Expr
	DataType    DataType
	Tag         Expr
	DocExpr     []Expr
	CommentExpr Expr
}

TypeField describes field ast for api syntax

func (*TypeField) Comment

func (t *TypeField) Comment() Expr

Comment returns the comment of TypeField, like // some text

func (*TypeField) Doc

func (t *TypeField) Doc() []Expr

Doc returns the document of TypeField, like // some text

func (*TypeField) Equal

func (t *TypeField) Equal(v interface{}) bool

Equal compares whether the element literals in two TypeField are equal

func (*TypeField) Format

func (t *TypeField) Format() error

Format provides a formatter for api command, now nothing to do

type TypeStruct

type TypeStruct struct {
	Name    Expr
	Struct  Expr
	LBrace  Expr
	RBrace  Expr
	DocExpr []Expr
	Fields  []*TypeField
}

TypeStruct describes structure ast for api syntax

func (*TypeStruct) Doc

func (s *TypeStruct) Doc() []Expr

Doc returns the document of TypeStruct, like // some text

func (*TypeStruct) Equal

func (s *TypeStruct) Equal(dt interface{}) bool

Equal compares whether the element literals in two TypeStruct are equal

func (*TypeStruct) Format

func (s *TypeStruct) Format() error

Format provides a formatter for api command, now nothing to do

func (*TypeStruct) NameExpr

func (s *TypeStruct) NameExpr() Expr

NameExpr returns the expression string of TypeStruct

type VisitorOption

type VisitorOption func(v *ApiVisitor)

VisitorOption defines a function with argument ApiVisitor

func WithVisitorDebug

func WithVisitorDebug() VisitorOption

WithVisitorDebug returns a debug VisitorOption

func WithVisitorPrefix

func WithVisitorPrefix(prefix string) VisitorOption

WithVisitorPrefix returns a VisitorOption wrap with specified prefix

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL