(f *testing.F)
| 259 | } |
| 260 | |
| 261 | func FuzzPrattParser(f *testing.F) { |
| 262 | f.Add([]byte("1 + 2 * 3")) |
| 263 | f.Add([]byte("a.?b[?0]")) |
| 264 | f.Add([]byte(".a.b(1, 2)")) |
| 265 | f.Fuzz(func(t *testing.T, data []byte) { |
| 266 | text := string(data) |
| 267 | if hasRawBytesRbPrefix(text) || hasUppercaseHexPrefix(text) { |
| 268 | return |
| 269 | } |
| 270 | |
| 271 | source := common.NewTextSource(text) |
| 272 | antlrAST, antlrErrs := antlrParser.Parse(source) |
| 273 | prattAST, prattErrs := prattParser.Parse(source) |
| 274 | |
| 275 | antlrHasErr := len(antlrErrs.GetErrors()) > 0 |
| 276 | prattHasErr := len(prattErrs.GetErrors()) > 0 |
| 277 | |
| 278 | if antlrHasErr != prattHasErr { |
| 279 | if !antlrHasErr && prattHasErr && hasEmptyCollectionWithComma(antlrAST, text) { |
| 280 | return |
| 281 | } |
| 282 | // For some expressions the two parsers allow different recursion depths; |
| 283 | // specifically, parens nested inside an operand are charged against the |
| 284 | // depth reached so far rather than accumulating on top of the enclosing parens, |
| 285 | // so the Pratt parser accepts inputs that ANTLR rejects. |
| 286 | if antlrHasErr && !prattHasErr && hasOnlyRecursionLimitErrors(antlrErrs) { |
| 287 | return |
| 288 | } |
| 289 | panic(fmt.Sprintf("Parser error mismatch for input %q:\nANTLR hasError=%t (%s)\nPratt hasError=%t (%s)", |
| 290 | text, antlrHasErr, antlrErrs.ToDisplayString(), prattHasErr, prattErrs.ToDisplayString())) |
| 291 | } |
| 292 | |
| 293 | if !antlrHasErr { |
| 294 | antlrProto, err := astToParsedExpr(antlrAST) |
| 295 | if err != nil { |
| 296 | panic(fmt.Sprintf("failed to convert ANTLR AST to proto: %v", err)) |
| 297 | } |
| 298 | prattProto, err := astToParsedExpr(prattAST) |
| 299 | if err != nil { |
| 300 | panic(fmt.Sprintf("failed to convert Pratt AST to proto: %v", err)) |
| 301 | } |
| 302 | |
| 303 | if !proto.Equal(antlrProto, prattProto) { |
| 304 | normalizeLeadingDotIdentPositions(prattProto, antlrProto) |
| 305 | if !proto.Equal(antlrProto, prattProto) { |
| 306 | panic(fmt.Sprintf("AST mismatch for input %q:\nANTLR Proto: %v\nPratt Proto: %v", |
| 307 | text, antlrProto, prattProto)) |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | }) |
| 312 | } |
| 313 |
nothing calls this directly
no test coverage detected