@@ -248,7 +248,7 @@ func (l *ParseTreeListener) parseRedefinedType(ctx *Type_defContext, t *Type) {
248248 t .Redefined = & RedefinedType {
249249 Name : ctx .IDENTIFIER (1 ).GetText (),
250250 }
251- g := ctx .Generic_type ()
251+ g := ctx .Value_type ()
252252 if g .Base_type () != nil {
253253 t .Redefined .GenericType = BaseType {
254254 Name : strings .TrimRight (g .Base_type ().GetText (), "?" ),
@@ -374,7 +374,7 @@ func (l *ParseTreeListener) ExitOneof_def(ctx *Oneof_defContext) {
374374func (l * ParseTreeListener ) parseOneOfType (ctx * Oneof_defContext , o * OneOf ) {
375375
376376 // Process all oneof fields
377- for _ , f := range ctx .AllOneof_field () {
377+ for _ , f := range ctx .AllCommon_type_field () {
378378 typeField := TypeField {
379379 Position : Position {
380380 Start : f .GetStart ().GetLine (),
@@ -387,18 +387,18 @@ func (l *ParseTreeListener) parseOneOfType(ctx *Oneof_defContext, o *OneOf) {
387387 }
388388
389389 // Regular field
390- typeField .FieldType = l .parseCommonFieldType (f .Common_type_field (). Common_field_type ())
391- typeField .Name = f .Common_type_field (). IDENTIFIER ().GetText ()
390+ typeField .FieldType = l .parseCommonFieldType (f .Common_field_type ())
391+ typeField .Name = f .IDENTIFIER ().GetText ()
392392
393393 // Default value
394- if f .Common_type_field (). Const_value () != nil {
395- s := f .Common_type_field (). Const_value ().GetText ()
394+ if f .Const_value () != nil {
395+ s := f .Const_value ().GetText ()
396396 typeField .Default = & s
397397 }
398398
399399 // Annotations
400- if f .Common_type_field (). Type_annotations () != nil {
401- for _ , aCtx := range f .Common_type_field (). Type_annotations ().AllAnnotation () {
400+ if f .Type_annotations () != nil {
401+ for _ , aCtx := range f .Type_annotations ().AllAnnotation () {
402402 a := Annotation {
403403 Key : aCtx .IDENTIFIER ().GetText (),
404404 Position : Position {
@@ -473,6 +473,96 @@ func (l *ParseTreeListener) ExitRpc_def(ctx *Rpc_defContext) {
473473 l .Document .RPCs = append (l .Document .RPCs , r )
474474}
475475
476+ // isTerminatorToken returns true if the token is a terminator token.
477+ func isTerminatorToken (t antlr.Token ) bool {
478+ return t .GetTokenType () == TLexerNEWLINE || t .GetTokenType () == TLexerSEMI
479+
480+ }
481+
482+ // previousTokenOnChannel returns the previous token on the specified channel.
483+ func (l * ParseTreeListener ) previousTokenOnChannel (i int ) int {
484+ tokens := l .Tokens .GetAllTokens ()
485+ for i >= 0 && (isTerminatorToken (tokens [i ]) || tokens [i ].GetChannel () != antlr .LexerDefaultTokenChannel ) {
486+ i --
487+ }
488+ return i
489+ }
490+
491+ // filterForChannel filters tokens for a specific channel.
492+ func (l * ParseTreeListener ) filterForChannel (left , right , channel int ) []antlr.Token {
493+ tokens := l .Tokens .GetAllTokens ()
494+ hidden := make ([]antlr.Token , 0 )
495+ for i := left ; i < right + 1 ; i ++ {
496+ t := tokens [i ]
497+ if channel == - 1 {
498+ if t .GetChannel () != antlr .LexerDefaultTokenChannel {
499+ hidden = append (hidden , t )
500+ }
501+ } else if t .GetChannel () == channel {
502+ hidden = append (hidden , t )
503+ }
504+ }
505+ if len (hidden ) == 0 {
506+ return nil
507+ }
508+ return hidden
509+ }
510+
511+ // GetHiddenTokensToLeft returns all hidden tokens to the left of a token.
512+ func (l * ParseTreeListener ) GetHiddenTokensToLeft (tokenIndex , channel int ) []antlr.Token {
513+ tokens := l .Tokens .GetAllTokens ()
514+ if tokenIndex < 0 || tokenIndex >= len (tokens ) {
515+ panic (strconv .Itoa (tokenIndex ) + " not in 0.." + strconv .Itoa (len (tokens )- 1 ))
516+ }
517+
518+ prevOnChannel := l .previousTokenOnChannel (tokenIndex - 1 )
519+ if prevOnChannel == tokenIndex - 1 {
520+ return nil
521+ }
522+
523+ // If there are none on channel to the left and prevOnChannel == -1 then from = 0
524+ from := prevOnChannel + 1
525+ to := tokenIndex - 1
526+ return l .filterForChannel (from , to , channel )
527+ }
528+
529+ // nextTokenOnChannel returns the next token on the specified channel.
530+ func (l * ParseTreeListener ) nextTokenOnChannel (i int ) int {
531+ tokens := l .Tokens .GetAllTokens ()
532+ if i >= len (tokens ) {
533+ return - 1
534+ }
535+ token := tokens [i ]
536+ for isTerminatorToken (tokens [i ]) || token .GetChannel () != antlr .LexerDefaultTokenChannel {
537+ if token .GetTokenType () == antlr .TokenEOF {
538+ return - 1
539+ }
540+ i ++
541+ token = tokens [i ]
542+ }
543+ return i
544+ }
545+
546+ // GetHiddenTokensToRight returns all hidden tokens to the right of a token.
547+ func (l * ParseTreeListener ) GetHiddenTokensToRight (tokenIndex , channel int ) []antlr.Token {
548+ tokens := l .Tokens .GetAllTokens ()
549+ if tokenIndex < 0 || tokenIndex >= len (tokens ) {
550+ panic (strconv .Itoa (tokenIndex ) + " not in 0.." + strconv .Itoa (len (tokens )- 1 ))
551+ }
552+
553+ nextOnChannel := l .nextTokenOnChannel (tokenIndex + 1 )
554+ from := tokenIndex + 1
555+
556+ // If no onChannel to the right, then nextOnChannel == -1, so set 'to' to the last token
557+ var to int
558+ if nextOnChannel == - 1 {
559+ to = len (tokens ) - 1
560+ } else {
561+ to = nextOnChannel
562+ }
563+ return l .filterForChannel (from , to , channel )
564+ }
565+
476566// topComment extracts comments immediately above a token.
477567// It supports both single-line (//) and multi-line (/* */) comments.
478568func (l * ParseTreeListener ) topComment (token antlr.Token ) []Comment {
@@ -482,7 +572,7 @@ func (l *ParseTreeListener) topComment(token antlr.Token) []Comment {
482572 )
483573
484574 // Collect single-line comments
485- comments := l .Tokens . GetHiddenTokensToLeft (token .GetTokenIndex (), TLexerSL_COMMENT_CHAN )
575+ comments := l .GetHiddenTokensToLeft (token .GetTokenIndex (), TLexerSL_COMMENT_CHAN )
486576 for _ , c := range comments {
487577 if _ , ok := l .Attached [c .GetLine ()]; ok {
488578 continue
@@ -498,7 +588,7 @@ func (l *ParseTreeListener) topComment(token antlr.Token) []Comment {
498588 }
499589
500590 // Collect multi-line comments
501- comments = l .Tokens . GetHiddenTokensToLeft (token .GetTokenIndex (), TLexerML_COMMENT_CHAN )
591+ comments = l .GetHiddenTokensToLeft (token .GetTokenIndex (), TLexerML_COMMENT_CHAN )
502592 for _ , c := range comments {
503593 if _ , ok := l .Attached [c .GetLine ()]; ok {
504594 continue
0 commit comments