MCPcopy Create free account
hub / github.com/cel-expr/cel-go / FuzzPrattParser

Function FuzzPrattParser

fuzzing/fuzz_pratt_parser_test.go:261–312  ·  view source on GitHub ↗
(f *testing.F)

Source from the content-addressed store, hash-verified

259}
260
261func 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

Callers

nothing calls this directly

Calls 12

NewTextSourceFunction · 0.92
hasRawBytesRbPrefixFunction · 0.85
hasUppercaseHexPrefixFunction · 0.85
astToParsedExprFunction · 0.85
GetErrorsMethod · 0.80
AddMethod · 0.65
ParseMethod · 0.65
EqualMethod · 0.65
ToDisplayStringMethod · 0.45

Tested by

no test coverage detected