ANTLR grammar allows a standalone comma in empty lists, maps, and structs (e.g., '[,]', '{,}', 'Msg{,}'), whereas Pratt parser requires at least one element/entry before a trailing comma.
(antlrAST *ast.AST, text string)
| 112 | // (e.g., '[,]', '{,}', 'Msg{,}'), whereas Pratt parser requires at least one element/entry |
| 113 | // before a trailing comma. |
| 114 | func hasEmptyCollectionWithComma(antlrAST *ast.AST, text string) bool { |
| 115 | if antlrAST == nil { |
| 116 | return false |
| 117 | } |
| 118 | sourceInfo := antlrAST.SourceInfo() |
| 119 | rbuf := runes.NewBuffer(text) |
| 120 | for _, node := range getAllNodes(antlrAST) { |
| 121 | expr := node |
| 122 | isEmptyCollection := false |
| 123 | closeChar := '}' |
| 124 | switch expr.Kind() { |
| 125 | case ast.ListKind: |
| 126 | if expr.AsList().Size() == 0 { |
| 127 | isEmptyCollection = true |
| 128 | closeChar = ']' |
| 129 | } |
| 130 | case ast.MapKind: |
| 131 | if expr.AsMap().Size() == 0 { |
| 132 | isEmptyCollection = true |
| 133 | } |
| 134 | case ast.StructKind: |
| 135 | if len(expr.AsStruct().Fields()) == 0 { |
| 136 | isEmptyCollection = true |
| 137 | } |
| 138 | } |
| 139 | if !isEmptyCollection { |
| 140 | continue |
| 141 | } |
| 142 | offsetRange, found := sourceInfo.GetOffsetRange(expr.ID()) |
| 143 | if !found || offsetRange.Start < 0 || int(offsetRange.Start) >= rbuf.Len() { |
| 144 | continue |
| 145 | } |
| 146 | openPos := int(offsetRange.Start) |
| 147 | hasComma := false |
| 148 | for i := openPos + 1; i < rbuf.Len(); i++ { |
| 149 | r := rbuf.Get(i) |
| 150 | if r == '/' && i+1 < rbuf.Len() && rbuf.Get(i+1) == '/' { |
| 151 | for i += 2; i < rbuf.Len() && rbuf.Get(i) != '\n'; i++ { |
| 152 | } |
| 153 | continue |
| 154 | } |
| 155 | if r == closeChar { |
| 156 | if hasComma { |
| 157 | return true |
| 158 | } |
| 159 | break |
| 160 | } |
| 161 | if r == ',' { |
| 162 | hasComma = true |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | return false |
| 167 | } |
| 168 | |
| 169 | // hasOnlyRecursionLimitErrors returns true if all parse errors are recursion limit exceeded errors. |
| 170 | func hasOnlyRecursionLimitErrors(errs *common.Errors) bool { |
no test coverage detected