Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* fix zeromicro#1318

* fix zeromicro#1318

* remove never used code

* fix unit tes

Co-authored-by: anqiansong <anqiansong@bytedance.com>
  • Loading branch information
kesonan and anqiansong authored Dec 13, 2021
1 parent 914692c commit 1b14de2
Show file tree
Hide file tree
Showing 18 changed files with 1,086 additions and 643 deletions.
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ service A-api {
@server(
handler: NoResponseHandler
)
get /greet/get(Request) returns
get /greet/get(Request)
}
`

Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/api/parser/g4/ApiParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ serviceApi: {match(p,"service")}serviceToken=ID serviceName lbrace='{' servi
serviceRoute: atDoc? (atServer|atHandler) route;
atDoc: ATDOC lp='('? ((kvLit+)|STRING) rp=')'?;
atHandler: ATHANDLER ID;
route: {checkHTTPMethod(p)}httpMethod=ID path request=body? returnToken=ID? response=replybody?;
route: {checkHTTPMethod(p)}httpMethod=ID path request=body? response=replybody?;
body: lp='(' (ID)? rp=')';
replybody: lp='(' dataType? rp=')';
replybody: returnToken='returns' lp='(' dataType? rp=')';
// kv
kvLit: key=ID {checkKeyValue(p)}value=LINE_VALUE;

Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/parser/g4/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type (
// ApiVisitor wraps api.BaseApiParserVisitor to call methods which has prefix Visit to
// visit node from the api syntax
ApiVisitor struct {
api.BaseApiParserVisitor
*api.BaseApiParserVisitor
debug bool
log console.Console
prefix string
Expand Down
36 changes: 21 additions & 15 deletions tools/goctl/api/parser/g4/ast/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ type Route struct {

// Body describes request,response body ast for api syntax
type Body struct {
Lp Expr
Rp Expr
Name DataType
ReturnExpr Expr
Lp Expr
Rp Expr
Name DataType
}

// VisitServiceSpec implements from api.BaseApiParserVisitor
Expand Down Expand Up @@ -175,7 +176,7 @@ func (v *ApiVisitor) VisitAtHandler(ctx *api.AtHandlerContext) interface{} {
return &atHandler
}

// VisitRoute implements from api.BaseApiParserVisitor
// serVisitRoute implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitRoute(ctx *api.RouteContext) interface{} {
var route Route
path := ctx.Path()
Expand All @@ -193,15 +194,11 @@ func (v *ApiVisitor) VisitRoute(ctx *api.RouteContext) interface{} {
if ctx.GetResponse() != nil {
reply := ctx.GetResponse().Accept(v)
if reply != nil {
route.Reply = reply.(*Body)
}
}
if ctx.GetReturnToken() != nil {
returnExpr := v.newExprWithToken(ctx.GetReturnToken())
if ctx.GetReturnToken().GetText() != "returns" {
v.panic(returnExpr, fmt.Sprintf("expecting returns, found input '%s'", ctx.GetReturnToken().GetText()))
resp := reply.(*Body)
route.ReturnToken = resp.ReturnExpr
resp.ReturnExpr = nil
route.Reply = resp
}
route.ReturnToken = returnExpr
}

route.DocExpr = v.getDoc(ctx)
Expand Down Expand Up @@ -249,6 +246,14 @@ func (v *ApiVisitor) VisitReplybody(ctx *api.ReplybodyContext) interface{} {
return nil
}

var returnExpr Expr
if ctx.GetReturnToken() != nil {
returnExpr = v.newExprWithToken(ctx.GetReturnToken())
if ctx.GetReturnToken().GetText() != "returns" {
v.panic(returnExpr, fmt.Sprintf("expecting returns, found input '%s'", ctx.GetReturnToken().GetText()))
}
}

dt := ctx.DataType().Accept(v).(DataType)
if dt == nil {
return nil
Expand All @@ -275,9 +280,10 @@ func (v *ApiVisitor) VisitReplybody(ctx *api.ReplybodyContext) interface{} {
}

return &Body{
Lp: v.newExprWithToken(ctx.GetLp()),
Rp: v.newExprWithToken(ctx.GetRp()),
Name: dt,
ReturnExpr: returnExpr,
Lp: v.newExprWithToken(ctx.GetLp()),
Rp: v.newExprWithToken(ctx.GetRp()),
Name: dt,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package api // ApiParser
import "github.com/zeromicro/antlr"

Expand Down
332 changes: 170 additions & 162 deletions tools/goctl/api/parser/g4/gen/api/apiparser_lexer.go

Large diffs are not rendered by default.

488 changes: 256 additions & 232 deletions tools/goctl/api/parser/g4/gen/api/apiparser_parser.go

Large diffs are not rendered by default.

57 changes: 52 additions & 5 deletions tools/goctl/api/parser/g4/gen/api/apiparser_parser1.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ func (s *SyntaxLitContext) GetAssign() antlr.Token { return s.assign }

func (s *SyntaxLitContext) GetVersion() antlr.Token { return s.version }


func (s *SyntaxLitContext) SetSyntaxToken(v antlr.Token) { s.syntaxToken = v }

func (s *SyntaxLitContext) SetAssign(v antlr.Token) { s.assign = v }

func (s *SyntaxLitContext) SetVersion(v antlr.Token) { s.version = v }


func (s *SyntaxLitContext) ID() antlr.TerminalNode {
return s.GetToken(ApiParserParserID, 0)
}
Expand All @@ -40,6 +42,7 @@ func (s *SyntaxLitContext) ToStringTree(ruleNames []string, recog antlr.Recogniz
return antlr.TreesStringTree(s, ruleNames, recog)
}


func (s *SyntaxLitContext) Accept(visitor antlr.ParseTreeVisitor) interface{} {
switch t := visitor.(type) {
case ApiParserVisitor:
Expand All @@ -50,6 +53,9 @@ func (s *SyntaxLitContext) Accept(visitor antlr.ParseTreeVisitor) interface{} {
}
}




func (p *ApiParserParser) SyntaxLit() (localctx ISyntaxLitContext) {
localctx = NewSyntaxLitContext(p, p.GetParserRuleContext(), p.GetState())
p.EnterRule(localctx, 4, ApiParserParserRULE_syntaxLit)
Expand All @@ -71,7 +77,7 @@ func (p *ApiParserParser) SyntaxLit() (localctx ISyntaxLitContext) {
}()

p.EnterOuterAlt(localctx, 1)
match(p, "syntax")
match(p,"syntax")
{
p.SetState(88)

Expand All @@ -95,9 +101,12 @@ func (p *ApiParserParser) SyntaxLit() (localctx ISyntaxLitContext) {
localctx.(*SyntaxLitContext).version = _m
}



return localctx
}


// IImportSpecContext is an interface to support dynamic dispatch.
type IImportSpecContext interface {
antlr.ParserRuleContext
Expand Down Expand Up @@ -164,6 +173,7 @@ func (s *ImportSpecContext) ToStringTree(ruleNames []string, recog antlr.Recogni
return antlr.TreesStringTree(s, ruleNames, recog)
}


func (s *ImportSpecContext) Accept(visitor antlr.ParseTreeVisitor) interface{} {
switch t := visitor.(type) {
case ApiParserVisitor:
Expand All @@ -174,6 +184,9 @@ func (s *ImportSpecContext) Accept(visitor antlr.ParseTreeVisitor) interface{} {
}
}




func (p *ApiParserParser) ImportSpec() (localctx IImportSpecContext) {
localctx = NewImportSpecContext(p, p.GetParserRuleContext(), p.GetState())
p.EnterRule(localctx, 6, ApiParserParserRULE_importSpec)
Expand Down Expand Up @@ -204,6 +217,7 @@ func (p *ApiParserParser) ImportSpec() (localctx IImportSpecContext) {
p.ImportLit()
}


case 2:
p.EnterOuterAlt(localctx, 2)
{
Expand All @@ -213,9 +227,11 @@ func (p *ApiParserParser) ImportSpec() (localctx IImportSpecContext) {

}


return localctx
}


// IImportLitContext is an interface to support dynamic dispatch.
type IImportLitContext interface {
antlr.ParserRuleContext
Expand All @@ -226,16 +242,18 @@ type IImportLitContext interface {
// GetImportToken returns the importToken token.
GetImportToken() antlr.Token


// SetImportToken sets the importToken token.
SetImportToken(antlr.Token)


// IsImportLitContext differentiates from other interfaces.
IsImportLitContext()
}

type ImportLitContext struct {
*antlr.BaseParserRuleContext
parser antlr.Parser
parser antlr.Parser
importToken antlr.Token
}

Expand Down Expand Up @@ -263,8 +281,10 @@ func (s *ImportLitContext) GetParser() antlr.Parser { return s.parser }

func (s *ImportLitContext) GetImportToken() antlr.Token { return s.importToken }


func (s *ImportLitContext) SetImportToken(v antlr.Token) { s.importToken = v }


func (s *ImportLitContext) ImportValue() IImportValueContext {
var t = s.GetTypedRuleContext(reflect.TypeOf((*IImportValueContext)(nil)).Elem(), 0)

Expand All @@ -287,6 +307,7 @@ func (s *ImportLitContext) ToStringTree(ruleNames []string, recog antlr.Recogniz
return antlr.TreesStringTree(s, ruleNames, recog)
}


func (s *ImportLitContext) Accept(visitor antlr.ParseTreeVisitor) interface{} {
switch t := visitor.(type) {
case ApiParserVisitor:
Expand All @@ -297,6 +318,9 @@ func (s *ImportLitContext) Accept(visitor antlr.ParseTreeVisitor) interface{} {
}
}




func (p *ApiParserParser) ImportLit() (localctx IImportLitContext) {
localctx = NewImportLitContext(p, p.GetParserRuleContext(), p.GetState())
p.EnterRule(localctx, 8, ApiParserParserRULE_importLit)
Expand All @@ -318,7 +342,7 @@ func (p *ApiParserParser) ImportLit() (localctx IImportLitContext) {
}()

p.EnterOuterAlt(localctx, 1)
match(p, "import")
match(p,"import")
{
p.SetState(98)

Expand All @@ -331,9 +355,12 @@ func (p *ApiParserParser) ImportLit() (localctx IImportLitContext) {
p.ImportValue()
}



return localctx
}


// IImportBlockContext is an interface to support dynamic dispatch.
type IImportBlockContext interface {
antlr.ParserRuleContext
Expand All @@ -344,16 +371,18 @@ type IImportBlockContext interface {
// GetImportToken returns the importToken token.
GetImportToken() antlr.Token


// SetImportToken sets the importToken token.
SetImportToken(antlr.Token)


// IsImportBlockContext differentiates from other interfaces.
IsImportBlockContext()
}

type ImportBlockContext struct {
*antlr.BaseParserRuleContext
parser antlr.Parser
parser antlr.Parser
importToken antlr.Token
}

Expand Down Expand Up @@ -381,8 +410,10 @@ func (s *ImportBlockContext) GetParser() antlr.Parser { return s.parser }

func (s *ImportBlockContext) GetImportToken() antlr.Token { return s.importToken }


func (s *ImportBlockContext) SetImportToken(v antlr.Token) { s.importToken = v }


func (s *ImportBlockContext) ID() antlr.TerminalNode {
return s.GetToken(ApiParserParserID, 0)
}
Expand Down Expand Up @@ -418,6 +449,7 @@ func (s *ImportBlockContext) ToStringTree(ruleNames []string, recog antlr.Recogn
return antlr.TreesStringTree(s, ruleNames, recog)
}


func (s *ImportBlockContext) Accept(visitor antlr.ParseTreeVisitor) interface{} {
switch t := visitor.(type) {
case ApiParserVisitor:
Expand All @@ -428,11 +460,15 @@ func (s *ImportBlockContext) Accept(visitor antlr.ParseTreeVisitor) interface{}
}
}




func (p *ApiParserParser) ImportBlock() (localctx IImportBlockContext) {
localctx = NewImportBlockContext(p, p.GetParserRuleContext(), p.GetState())
p.EnterRule(localctx, 10, ApiParserParserRULE_importBlock)
var _la int


defer func() {
p.ExitRule()
}()
Expand All @@ -450,7 +486,7 @@ func (p *ApiParserParser) ImportBlock() (localctx IImportBlockContext) {
}()

p.EnterOuterAlt(localctx, 1)
match(p, "import")
match(p,"import")
{
p.SetState(102)

Expand All @@ -471,6 +507,7 @@ func (p *ApiParserParser) ImportBlock() (localctx IImportBlockContext) {
p.ImportBlockValue()
}


p.SetState(107)
p.GetErrorHandler().Sync(p)
_la = p.GetTokenStream().LA(1)
Expand All @@ -480,9 +517,12 @@ func (p *ApiParserParser) ImportBlock() (localctx IImportBlockContext) {
p.Match(ApiParserParserT__2)
}



return localctx
}


// IImportBlockValueContext is an interface to support dynamic dispatch.
type IImportBlockValueContext interface {
antlr.ParserRuleContext
Expand Down Expand Up @@ -539,6 +579,7 @@ func (s *ImportBlockValueContext) ToStringTree(ruleNames []string, recog antlr.R
return antlr.TreesStringTree(s, ruleNames, recog)
}


func (s *ImportBlockValueContext) Accept(visitor antlr.ParseTreeVisitor) interface{} {
switch t := visitor.(type) {
case ApiParserVisitor:
Expand All @@ -549,6 +590,9 @@ func (s *ImportBlockValueContext) Accept(visitor antlr.ParseTreeVisitor) interfa
}
}




func (p *ApiParserParser) ImportBlockValue() (localctx IImportBlockValueContext) {
localctx = NewImportBlockValueContext(p, p.GetParserRuleContext(), p.GetState())
p.EnterRule(localctx, 12, ApiParserParserRULE_importBlockValue)
Expand All @@ -575,9 +619,12 @@ func (p *ApiParserParser) ImportBlockValue() (localctx IImportBlockValueContext)
p.ImportValue()
}



return localctx
}


// IImportValueContext is an interface to support dynamic dispatch.
type IImportValueContext interface {
antlr.ParserRuleContext
Expand Down
Loading

0 comments on commit 1b14de2

Please sign in to comment.