From 7b4a5e3ec65dc7826cd46f0026969ca53886dd3e Mon Sep 17 00:00:00 2001 From: kingxt Date: Thu, 12 Nov 2020 16:57:28 +0800 Subject: [PATCH] api support for comment double slash // (#201) * add comment support * add comment support Co-authored-by: kim --- tools/goctl/api/gogen/gen_test.go | 13 ++++--- tools/goctl/api/parser/basestate.go | 56 ++++++++++++++++++++++++++++- tools/goctl/api/parser/entity.go | 8 ++--- tools/goctl/api/parser/rootstate.go | 10 ++++-- tools/goctl/api/parser/util.go | 4 +++ 5 files changed, 79 insertions(+), 12 deletions(-) diff --git a/tools/goctl/api/gogen/gen_test.go b/tools/goctl/api/gogen/gen_test.go index f118fd78..32682e4b 100644 --- a/tools/goctl/api/gogen/gen_test.go +++ b/tools/goctl/api/gogen/gen_test.go @@ -31,17 +31,20 @@ type Response struct { } @server( + // C0 group: greet ) +// C1 service A-api { - @server( + // C2 + @server( // C3 handler: GreetHandler ) - get /greet/from/:name(Request) returns (Response) - + get /greet/from/:name(Request) returns (Response) // hello + + // C4 @server( - handler: NoResponseHandler - + handler: NoResponseHandler // C5 ) get /greet/get(Request) returns } diff --git a/tools/goctl/api/parser/basestate.go b/tools/goctl/api/parser/basestate.go index f5a083da..547026a7 100644 --- a/tools/goctl/api/parser/basestate.go +++ b/tools/goctl/api/parser/basestate.go @@ -34,7 +34,7 @@ func (s *baseState) parseProperties() (map[string]string, error) { var st = startState for { - ch, err := s.read() + ch, err := s.readSkipComment() if err != nil { return nil, err } @@ -164,6 +164,60 @@ func (s *baseState) read() (rune, error) { return value, nil } +func (s *baseState) readSkipComment() (rune, error) { + ch, err := s.read() + if err != nil { + return 0, err + } + + if isSlash(ch) { + value, err := s.mayReadToEndOfLine() + if err != nil { + return 0, err + } + + if value > 0 { + ch = value + } + } + return ch, nil +} + +func (s *baseState) mayReadToEndOfLine() (rune, error) { + ch, err := s.read() + if err != nil { + return 0, err + } + + if isSlash(ch) { + for { + value, err := s.read() + if err != nil { + return 0, err + } + + if isNewline(value) { + return value, nil + } + } + } + err = s.unread() + return 0, err +} + +func (s *baseState) readLineSkipComment() (string, error) { + line, err := s.readLine() + if err != nil { + return "", err + } + + var commentIdx = strings.Index(line, "//") + if commentIdx >= 0 { + return line[:commentIdx], nil + } + return line, nil +} + func (s *baseState) readLine() (string, error) { line, _, err := s.r.ReadLine() if err != nil { diff --git a/tools/goctl/api/parser/entity.go b/tools/goctl/api/parser/entity.go index bb505eae..303fd815 100644 --- a/tools/goctl/api/parser/entity.go +++ b/tools/goctl/api/parser/entity.go @@ -30,7 +30,7 @@ func newEntity(state *baseState, api *spec.ApiSpec, parser entityParser) entity } func (s *entity) process() error { - line, err := s.state.readLine() + line, err := s.state.readLineSkipComment() if err != nil { return err } @@ -59,7 +59,7 @@ func (s *entity) process() error { var annos []spec.Annotation memberLoop: for { - ch, err := s.state.read() + ch, err := s.state.readSkipComment() if err != nil { return err } @@ -70,7 +70,7 @@ memberLoop: case ch == at: annotationLoop: for { - next, err := s.state.read() + next, err := s.state.readSkipComment() if err != nil { return err } @@ -127,7 +127,7 @@ memberLoop: } var line string - line, err = s.state.readLine() + line, err = s.state.readLineSkipComment() if err != nil { return err } diff --git a/tools/goctl/api/parser/rootstate.go b/tools/goctl/api/parser/rootstate.go index 994f70bd..2c8ba8e7 100644 --- a/tools/goctl/api/parser/rootstate.go +++ b/tools/goctl/api/parser/rootstate.go @@ -23,7 +23,7 @@ func (s rootState) process(api *spec.ApiSpec) (state, error) { var annos []spec.Annotation var builder strings.Builder for { - ch, err := s.read() + ch, err := s.readSkipComment() if err != nil { return nil, err } @@ -33,6 +33,7 @@ func (s rootState) process(api *spec.ApiSpec) (state, error) { if builder.Len() == 0 { continue } + token := builder.String() builder.Reset() return s.processToken(token, annos) @@ -44,10 +45,11 @@ func (s rootState) process(api *spec.ApiSpec) (state, error) { var annoName string annoLoop: for { - next, err := s.read() + next, err := s.readSkipComment() if err != nil { return nil, err } + switch { case isSpace(next): if builder.Len() > 0 { @@ -58,6 +60,7 @@ func (s rootState) process(api *spec.ApiSpec) (state, error) { if err := s.unread(); err != nil { return nil, err } + if builder.Len() > 0 { annoName = builder.String() builder.Reset() @@ -66,6 +69,7 @@ func (s rootState) process(api *spec.ApiSpec) (state, error) { if err != nil { return nil, err } + annos = append(annos, spec.Annotation{ Name: annoName, Properties: attrs, @@ -79,9 +83,11 @@ func (s rootState) process(api *spec.ApiSpec) (state, error) { if builder.Len() == 0 { return nil, fmt.Errorf("incorrect %q at the beginning of the line", leftParenthesis) } + if err := s.unread(); err != nil { return nil, err } + token := builder.String() builder.Reset() return s.processToken(token, annos) diff --git a/tools/goctl/api/parser/util.go b/tools/goctl/api/parser/util.go index 220e68af..48bdd6a0 100644 --- a/tools/goctl/api/parser/util.go +++ b/tools/goctl/api/parser/util.go @@ -36,6 +36,10 @@ func isSpace(r rune) bool { return r == ' ' || r == '\t' } +func isSlash(r rune) bool { + return r == '/' +} + func isNewline(r rune) bool { return r == '\n' || r == '\r' }