api support for comment double slash // (#201)

* add comment support

* add comment support

Co-authored-by: kim <xutao@xiaoheiban.cn>
master
kingxt 4 years ago committed by GitHub
parent e6df21e0d2
commit 7b4a5e3ec6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -31,17 +31,20 @@ type Response struct {
} }
@server( @server(
// C0
group: greet group: greet
) )
// C1
service A-api { service A-api {
@server( // C2
@server( // C3
handler: GreetHandler handler: GreetHandler
) )
get /greet/from/:name(Request) returns (Response) get /greet/from/:name(Request) returns (Response) // hello
// C4
@server( @server(
handler: NoResponseHandler handler: NoResponseHandler // C5
) )
get /greet/get(Request) returns get /greet/get(Request) returns
} }

@ -34,7 +34,7 @@ func (s *baseState) parseProperties() (map[string]string, error) {
var st = startState var st = startState
for { for {
ch, err := s.read() ch, err := s.readSkipComment()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -164,6 +164,60 @@ func (s *baseState) read() (rune, error) {
return value, nil 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) { func (s *baseState) readLine() (string, error) {
line, _, err := s.r.ReadLine() line, _, err := s.r.ReadLine()
if err != nil { if err != nil {

@ -30,7 +30,7 @@ func newEntity(state *baseState, api *spec.ApiSpec, parser entityParser) entity
} }
func (s *entity) process() error { func (s *entity) process() error {
line, err := s.state.readLine() line, err := s.state.readLineSkipComment()
if err != nil { if err != nil {
return err return err
} }
@ -59,7 +59,7 @@ func (s *entity) process() error {
var annos []spec.Annotation var annos []spec.Annotation
memberLoop: memberLoop:
for { for {
ch, err := s.state.read() ch, err := s.state.readSkipComment()
if err != nil { if err != nil {
return err return err
} }
@ -70,7 +70,7 @@ memberLoop:
case ch == at: case ch == at:
annotationLoop: annotationLoop:
for { for {
next, err := s.state.read() next, err := s.state.readSkipComment()
if err != nil { if err != nil {
return err return err
} }
@ -127,7 +127,7 @@ memberLoop:
} }
var line string var line string
line, err = s.state.readLine() line, err = s.state.readLineSkipComment()
if err != nil { if err != nil {
return err return err
} }

@ -23,7 +23,7 @@ func (s rootState) process(api *spec.ApiSpec) (state, error) {
var annos []spec.Annotation var annos []spec.Annotation
var builder strings.Builder var builder strings.Builder
for { for {
ch, err := s.read() ch, err := s.readSkipComment()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -33,6 +33,7 @@ func (s rootState) process(api *spec.ApiSpec) (state, error) {
if builder.Len() == 0 { if builder.Len() == 0 {
continue continue
} }
token := builder.String() token := builder.String()
builder.Reset() builder.Reset()
return s.processToken(token, annos) return s.processToken(token, annos)
@ -44,10 +45,11 @@ func (s rootState) process(api *spec.ApiSpec) (state, error) {
var annoName string var annoName string
annoLoop: annoLoop:
for { for {
next, err := s.read() next, err := s.readSkipComment()
if err != nil { if err != nil {
return nil, err return nil, err
} }
switch { switch {
case isSpace(next): case isSpace(next):
if builder.Len() > 0 { if builder.Len() > 0 {
@ -58,6 +60,7 @@ func (s rootState) process(api *spec.ApiSpec) (state, error) {
if err := s.unread(); err != nil { if err := s.unread(); err != nil {
return nil, err return nil, err
} }
if builder.Len() > 0 { if builder.Len() > 0 {
annoName = builder.String() annoName = builder.String()
builder.Reset() builder.Reset()
@ -66,6 +69,7 @@ func (s rootState) process(api *spec.ApiSpec) (state, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
annos = append(annos, spec.Annotation{ annos = append(annos, spec.Annotation{
Name: annoName, Name: annoName,
Properties: attrs, Properties: attrs,
@ -79,9 +83,11 @@ func (s rootState) process(api *spec.ApiSpec) (state, error) {
if builder.Len() == 0 { if builder.Len() == 0 {
return nil, fmt.Errorf("incorrect %q at the beginning of the line", leftParenthesis) return nil, fmt.Errorf("incorrect %q at the beginning of the line", leftParenthesis)
} }
if err := s.unread(); err != nil { if err := s.unread(); err != nil {
return nil, err return nil, err
} }
token := builder.String() token := builder.String()
builder.Reset() builder.Reset()
return s.processToken(token, annos) return s.processToken(token, annos)

@ -36,6 +36,10 @@ func isSpace(r rune) bool {
return r == ' ' || r == '\t' return r == ' ' || r == '\t'
} }
func isSlash(r rune) bool {
return r == '/'
}
func isNewline(r rune) bool { func isNewline(r rune) bool {
return r == '\n' || r == '\r' return r == '\n' || r == '\r'
} }

Loading…
Cancel
Save