Skip to content

Commit

Permalink
internal comments (#268)
Browse files Browse the repository at this point in the history
* internal comments

* ignore comments starting with bang

---------

Co-authored-by: lukasjenicek <lukas.jenicek5@gmail.com>
  • Loading branch information
pkieltyka and LukasJenicek committed Apr 5, 2024
1 parent 20562c5 commit 80d62e2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion schema/ridl/_example/comments.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ enum Kind: uint32
- USER = 1 # user
- ADMIN = 2 # comment..

# or.. just..
#! or.. just..
enum Kind2: uint32
- USER # aka, = 0
- ADMIN # aka, = 1
Expand Down
4 changes: 4 additions & 0 deletions schema/ridl/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const (
tokenDot // "."
tokenQuestionMark // "?"
tokenRocket // "=>"
tokenBang // "!"
tokenWord // ..wordCharset..

tokenExtra // other
Expand Down Expand Up @@ -97,6 +98,7 @@ var tokenTypeName = map[tokenType]string{
tokenSlash: "[slash]",
tokenQuestionMark: "[question mark]",
tokenRocket: "[rocket]",
tokenBang: "[bang]",
tokenWord: "[word]",
tokenExtra: "[extra]",
tokenComposed: "[composed]",
Expand All @@ -123,6 +125,7 @@ var tokenTypeValue = map[tokenType][]rune{
tokenComma: {','},
tokenDot: {'.'},
tokenQuestionMark: {'?'},
tokenBang: {'!'},
}

var (
Expand All @@ -145,6 +148,7 @@ var (
isBackslash = isTokenType(tokenBackslash)
isSlash = isTokenType(tokenSlash)
isDot = isTokenType(tokenDot)
isBang = isTokenType(tokenBang)
)

func isTokenType(tt tokenType) func(r rune) bool {
Expand Down
4 changes: 3 additions & 1 deletion schema/ridl/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,9 @@ func parseComments(comments map[int]string, currentLine int) string {
for ; currentLine >= 0; currentLine-- {
comment, ok := comments[currentLine]
if ok {
c = append(c, comment)
if !strings.HasPrefix(comment, "!") {
c = append(c, comment)
}
delete(comments, currentLine)
iteration = 0

Expand Down
7 changes: 6 additions & 1 deletion schema/ridl/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -994,9 +994,14 @@ func TestParseServiceComments(t *testing.T) {
# Contacts service line 1
# Contacts service line 2
service ContactsService # Contacts service line 3
#! skip this line
# GetContact gives you contact for specific id
- GetContact(id: int) => (contact: Contact)
# Version returns you current deployed version
#
#! skip this line as its internal comment
#! skip more lines
#! more
- Version() => (details: any)
`)
assert.NoError(t, err)
Expand All @@ -1011,5 +1016,5 @@ func TestParseServiceComments(t *testing.T) {

assert.Equal(t, "Contacts service line 1\nContacts service line 2\nContacts service line 3", serviceNode.comment)
assert.Equal(t, "GetContact gives you contact for specific id", serviceNode.methods[0].comment)
assert.Equal(t, "Version returns you current deployed version", serviceNode.methods[1].comment)
assert.Equal(t, "Version returns you current deployed version\n", serviceNode.methods[1].comment)
}
1 change: 1 addition & 0 deletions schema/ridl/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func tokenize(src []byte) ([]token, map[int]string, error) {

commentLine := false
commentTokens := []string{}

for tok := range lx.tokens {
if tok.tt == tokenEOF {
break
Expand Down

0 comments on commit 80d62e2

Please sign in to comment.