github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/ast/logql/logql.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Code generated by pigeon; DO NOT EDIT. 18 19 package logql 20 21 import ( 22 "bytes" 23 "encoding/json" 24 "errors" 25 "fmt" 26 "io" 27 "math" 28 "os" 29 "regexp" 30 "sort" 31 "strconv" 32 "strings" 33 "sync" 34 "unicode" 35 "unicode/utf8" 36 37 "github.com/siglens/siglens/pkg/ast" 38 structs "github.com/siglens/siglens/pkg/segment/structs" 39 "github.com/siglens/siglens/pkg/segment/utils" 40 ) 41 42 func getParseError(err error) error { 43 switch ev := err.(type) { 44 case errList: 45 if pe, ok := ev[0].(*parserError); ok { 46 return &ast.ParseError{ 47 Inner: pe.Inner, 48 Line: pe.pos.line, 49 Column: pe.pos.col, 50 Offset: pe.pos.offset, 51 Prefix: pe.prefix, 52 Expected: pe.expected, 53 } 54 } 55 } 56 return err 57 } 58 59 var g = &grammar{ 60 rules: []*rule{ 61 { 62 name: "Start", 63 pos: position{line: 35, col: 1, offset: 630}, 64 expr: &choiceExpr{ 65 pos: position{line: 35, col: 10, offset: 639}, 66 alternatives: []interface{}{ 67 &actionExpr{ 68 pos: position{line: 35, col: 10, offset: 639}, 69 run: (*parser).callonStart2, 70 expr: &seqExpr{ 71 pos: position{line: 35, col: 10, offset: 639}, 72 exprs: []interface{}{ 73 &labeledExpr{ 74 pos: position{line: 35, col: 10, offset: 639}, 75 label: "query", 76 expr: &zeroOrOneExpr{ 77 pos: position{line: 35, col: 16, offset: 645}, 78 expr: &ruleRefExpr{ 79 pos: position{line: 35, col: 16, offset: 645}, 80 name: "Stream", 81 }, 82 }, 83 }, 84 &labeledExpr{ 85 pos: position{line: 35, col: 24, offset: 653}, 86 label: "jf", 87 expr: &zeroOrOneExpr{ 88 pos: position{line: 35, col: 27, offset: 656}, 89 expr: &ruleRefExpr{ 90 pos: position{line: 35, col: 27, offset: 656}, 91 name: "JSONFilter", 92 }, 93 }, 94 }, 95 &labeledExpr{ 96 pos: position{line: 35, col: 39, offset: 668}, 97 label: "fs", 98 expr: &zeroOrOneExpr{ 99 pos: position{line: 35, col: 42, offset: 671}, 100 expr: &ruleRefExpr{ 101 pos: position{line: 35, col: 42, offset: 671}, 102 name: "FilterStream", 103 }, 104 }, 105 }, 106 &ruleRefExpr{ 107 pos: position{line: 35, col: 56, offset: 685}, 108 name: "EOF", 109 }, 110 }, 111 }, 112 }, 113 &actionExpr{ 114 pos: position{line: 62, col: 5, offset: 1337}, 115 run: (*parser).callonStart14, 116 expr: &seqExpr{ 117 pos: position{line: 62, col: 5, offset: 1337}, 118 exprs: []interface{}{ 119 &ruleRefExpr{ 120 pos: position{line: 62, col: 5, offset: 1337}, 121 name: "COUNT_OVER_TIME", 122 }, 123 &litMatcher{ 124 pos: position{line: 62, col: 21, offset: 1353}, 125 val: "(", 126 ignoreCase: false, 127 want: "\"(\"", 128 }, 129 &labeledExpr{ 130 pos: position{line: 62, col: 25, offset: 1357}, 131 label: "query", 132 expr: &ruleRefExpr{ 133 pos: position{line: 62, col: 31, offset: 1363}, 134 name: "Stream", 135 }, 136 }, 137 &labeledExpr{ 138 pos: position{line: 62, col: 38, offset: 1370}, 139 label: "duration", 140 expr: &ruleRefExpr{ 141 pos: position{line: 62, col: 47, offset: 1379}, 142 name: "Duration", 143 }, 144 }, 145 &litMatcher{ 146 pos: position{line: 62, col: 56, offset: 1388}, 147 val: ")", 148 ignoreCase: false, 149 want: "\")\"", 150 }, 151 }, 152 }, 153 }, 154 }, 155 }, 156 }, 157 { 158 name: "Stream", 159 pos: position{line: 75, col: 1, offset: 1940}, 160 expr: &actionExpr{ 161 pos: position{line: 75, col: 11, offset: 1950}, 162 run: (*parser).callonStream1, 163 expr: &seqExpr{ 164 pos: position{line: 75, col: 11, offset: 1950}, 165 exprs: []interface{}{ 166 &ruleRefExpr{ 167 pos: position{line: 75, col: 11, offset: 1950}, 168 name: "Delimiter", 169 }, 170 &labeledExpr{ 171 pos: position{line: 75, col: 21, offset: 1960}, 172 label: "q1", 173 expr: &ruleRefExpr{ 174 pos: position{line: 75, col: 24, offset: 1963}, 175 name: "Query", 176 }, 177 }, 178 &labeledExpr{ 179 pos: position{line: 75, col: 30, offset: 1969}, 180 label: "rest", 181 expr: &zeroOrMoreExpr{ 182 pos: position{line: 75, col: 35, offset: 1974}, 183 expr: &ruleRefExpr{ 184 pos: position{line: 75, col: 36, offset: 1975}, 185 name: "Query", 186 }, 187 }, 188 }, 189 &ruleRefExpr{ 190 pos: position{line: 75, col: 44, offset: 1983}, 191 name: "Delimiter", 192 }, 193 &zeroOrOneExpr{ 194 pos: position{line: 75, col: 54, offset: 1993}, 195 expr: &ruleRefExpr{ 196 pos: position{line: 75, col: 54, offset: 1993}, 197 name: "space", 198 }, 199 }, 200 }, 201 }, 202 }, 203 }, 204 { 205 name: "FilterStream", 206 pos: position{line: 104, col: 1, offset: 2685}, 207 expr: &actionExpr{ 208 pos: position{line: 104, col: 17, offset: 2701}, 209 run: (*parser).callonFilterStream1, 210 expr: &seqExpr{ 211 pos: position{line: 104, col: 17, offset: 2701}, 212 exprs: []interface{}{ 213 &labeledExpr{ 214 pos: position{line: 104, col: 17, offset: 2701}, 215 label: "rest", 216 expr: &zeroOrMoreExpr{ 217 pos: position{line: 104, col: 22, offset: 2706}, 218 expr: &choiceExpr{ 219 pos: position{line: 104, col: 23, offset: 2707}, 220 alternatives: []interface{}{ 221 &ruleRefExpr{ 222 pos: position{line: 104, col: 23, offset: 2707}, 223 name: "LogFilter", 224 }, 225 &ruleRefExpr{ 226 pos: position{line: 104, col: 35, offset: 2719}, 227 name: "LabelFilter", 228 }, 229 }, 230 }, 231 }, 232 }, 233 &zeroOrOneExpr{ 234 pos: position{line: 104, col: 49, offset: 2733}, 235 expr: &ruleRefExpr{ 236 pos: position{line: 104, col: 49, offset: 2733}, 237 name: "space", 238 }, 239 }, 240 }, 241 }, 242 }, 243 }, 244 { 245 name: "Query", 246 pos: position{line: 134, col: 1, offset: 3460}, 247 expr: &actionExpr{ 248 pos: position{line: 134, col: 11, offset: 3470}, 249 run: (*parser).callonQuery1, 250 expr: &seqExpr{ 251 pos: position{line: 134, col: 11, offset: 3470}, 252 exprs: []interface{}{ 253 &labeledExpr{ 254 pos: position{line: 134, col: 11, offset: 3470}, 255 label: "field", 256 expr: &ruleRefExpr{ 257 pos: position{line: 134, col: 17, offset: 3476}, 258 name: "Field", 259 }, 260 }, 261 &zeroOrOneExpr{ 262 pos: position{line: 134, col: 23, offset: 3482}, 263 expr: &ruleRefExpr{ 264 pos: position{line: 134, col: 23, offset: 3482}, 265 name: "space", 266 }, 267 }, 268 &litMatcher{ 269 pos: position{line: 134, col: 30, offset: 3489}, 270 val: "=", 271 ignoreCase: false, 272 want: "\"=\"", 273 }, 274 &zeroOrOneExpr{ 275 pos: position{line: 134, col: 34, offset: 3493}, 276 expr: &ruleRefExpr{ 277 pos: position{line: 134, col: 34, offset: 3493}, 278 name: "space", 279 }, 280 }, 281 &labeledExpr{ 282 pos: position{line: 134, col: 41, offset: 3500}, 283 label: "field1", 284 expr: &ruleRefExpr{ 285 pos: position{line: 134, col: 48, offset: 3507}, 286 name: "Field", 287 }, 288 }, 289 &zeroOrOneExpr{ 290 pos: position{line: 134, col: 54, offset: 3513}, 291 expr: &litMatcher{ 292 pos: position{line: 134, col: 54, offset: 3513}, 293 val: ",", 294 ignoreCase: false, 295 want: "\",\"", 296 }, 297 }, 298 &zeroOrOneExpr{ 299 pos: position{line: 134, col: 59, offset: 3518}, 300 expr: &ruleRefExpr{ 301 pos: position{line: 134, col: 59, offset: 3518}, 302 name: "space", 303 }, 304 }, 305 }, 306 }, 307 }, 308 }, 309 { 310 name: "SingleField", 311 pos: position{line: 145, col: 1, offset: 3729}, 312 expr: &actionExpr{ 313 pos: position{line: 145, col: 17, offset: 3745}, 314 run: (*parser).callonSingleField1, 315 expr: &seqExpr{ 316 pos: position{line: 145, col: 17, offset: 3745}, 317 exprs: []interface{}{ 318 &zeroOrOneExpr{ 319 pos: position{line: 145, col: 17, offset: 3745}, 320 expr: &litMatcher{ 321 pos: position{line: 145, col: 17, offset: 3745}, 322 val: ",", 323 ignoreCase: false, 324 want: "\",\"", 325 }, 326 }, 327 &zeroOrOneExpr{ 328 pos: position{line: 145, col: 22, offset: 3750}, 329 expr: &ruleRefExpr{ 330 pos: position{line: 145, col: 22, offset: 3750}, 331 name: "space", 332 }, 333 }, 334 &labeledExpr{ 335 pos: position{line: 145, col: 29, offset: 3757}, 336 label: "field", 337 expr: &ruleRefExpr{ 338 pos: position{line: 145, col: 35, offset: 3763}, 339 name: "Field", 340 }, 341 }, 342 &zeroOrOneExpr{ 343 pos: position{line: 145, col: 41, offset: 3769}, 344 expr: &litMatcher{ 345 pos: position{line: 145, col: 41, offset: 3769}, 346 val: ",", 347 ignoreCase: false, 348 want: "\",\"", 349 }, 350 }, 351 }, 352 }, 353 }, 354 }, 355 { 356 name: "LogFilter", 357 pos: position{line: 155, col: 1, offset: 3956}, 358 expr: &actionExpr{ 359 pos: position{line: 155, col: 14, offset: 3969}, 360 run: (*parser).callonLogFilter1, 361 expr: &seqExpr{ 362 pos: position{line: 155, col: 14, offset: 3969}, 363 exprs: []interface{}{ 364 &zeroOrOneExpr{ 365 pos: position{line: 155, col: 14, offset: 3969}, 366 expr: &ruleRefExpr{ 367 pos: position{line: 155, col: 14, offset: 3969}, 368 name: "space", 369 }, 370 }, 371 &labeledExpr{ 372 pos: position{line: 155, col: 21, offset: 3976}, 373 label: "grep", 374 expr: &ruleRefExpr{ 375 pos: position{line: 155, col: 26, offset: 3981}, 376 name: "GrepFilter", 377 }, 378 }, 379 &zeroOrOneExpr{ 380 pos: position{line: 155, col: 37, offset: 3992}, 381 expr: &ruleRefExpr{ 382 pos: position{line: 155, col: 37, offset: 3992}, 383 name: "space", 384 }, 385 }, 386 &labeledExpr{ 387 pos: position{line: 155, col: 44, offset: 3999}, 388 label: "field", 389 expr: &ruleRefExpr{ 390 pos: position{line: 155, col: 50, offset: 4005}, 391 name: "Field", 392 }, 393 }, 394 &zeroOrOneExpr{ 395 pos: position{line: 155, col: 56, offset: 4011}, 396 expr: &ruleRefExpr{ 397 pos: position{line: 155, col: 56, offset: 4011}, 398 name: "space", 399 }, 400 }, 401 }, 402 }, 403 }, 404 }, 405 { 406 name: "LabelFilter", 407 pos: position{line: 166, col: 1, offset: 4228}, 408 expr: &actionExpr{ 409 pos: position{line: 166, col: 16, offset: 4243}, 410 run: (*parser).callonLabelFilter1, 411 expr: &seqExpr{ 412 pos: position{line: 166, col: 16, offset: 4243}, 413 exprs: []interface{}{ 414 &zeroOrOneExpr{ 415 pos: position{line: 166, col: 16, offset: 4243}, 416 expr: &ruleRefExpr{ 417 pos: position{line: 166, col: 16, offset: 4243}, 418 name: "space", 419 }, 420 }, 421 &litMatcher{ 422 pos: position{line: 166, col: 23, offset: 4250}, 423 val: "|", 424 ignoreCase: false, 425 want: "\"|\"", 426 }, 427 &zeroOrOneExpr{ 428 pos: position{line: 166, col: 27, offset: 4254}, 429 expr: &ruleRefExpr{ 430 pos: position{line: 166, col: 27, offset: 4254}, 431 name: "space", 432 }, 433 }, 434 &labeledExpr{ 435 pos: position{line: 166, col: 34, offset: 4261}, 436 label: "field", 437 expr: &ruleRefExpr{ 438 pos: position{line: 166, col: 40, offset: 4267}, 439 name: "Field", 440 }, 441 }, 442 &zeroOrOneExpr{ 443 pos: position{line: 166, col: 46, offset: 4273}, 444 expr: &ruleRefExpr{ 445 pos: position{line: 166, col: 46, offset: 4273}, 446 name: "space", 447 }, 448 }, 449 &labeledExpr{ 450 pos: position{line: 166, col: 53, offset: 4280}, 451 label: "op", 452 expr: &ruleRefExpr{ 453 pos: position{line: 166, col: 56, offset: 4283}, 454 name: "opCOMP", 455 }, 456 }, 457 &zeroOrOneExpr{ 458 pos: position{line: 166, col: 63, offset: 4290}, 459 expr: &ruleRefExpr{ 460 pos: position{line: 166, col: 63, offset: 4290}, 461 name: "space", 462 }, 463 }, 464 &labeledExpr{ 465 pos: position{line: 166, col: 70, offset: 4297}, 466 label: "field1", 467 expr: &ruleRefExpr{ 468 pos: position{line: 166, col: 77, offset: 4304}, 469 name: "Field", 470 }, 471 }, 472 &zeroOrOneExpr{ 473 pos: position{line: 166, col: 83, offset: 4310}, 474 expr: &litMatcher{ 475 pos: position{line: 166, col: 83, offset: 4310}, 476 val: ",", 477 ignoreCase: false, 478 want: "\",\"", 479 }, 480 }, 481 &zeroOrOneExpr{ 482 pos: position{line: 166, col: 88, offset: 4315}, 483 expr: &ruleRefExpr{ 484 pos: position{line: 166, col: 88, offset: 4315}, 485 name: "space", 486 }, 487 }, 488 }, 489 }, 490 }, 491 }, 492 { 493 name: "JSONFilter", 494 pos: position{line: 177, col: 1, offset: 4533}, 495 expr: &choiceExpr{ 496 pos: position{line: 177, col: 15, offset: 4547}, 497 alternatives: []interface{}{ 498 &actionExpr{ 499 pos: position{line: 177, col: 15, offset: 4547}, 500 run: (*parser).callonJSONFilter2, 501 expr: &seqExpr{ 502 pos: position{line: 177, col: 15, offset: 4547}, 503 exprs: []interface{}{ 504 &zeroOrOneExpr{ 505 pos: position{line: 177, col: 15, offset: 4547}, 506 expr: &ruleRefExpr{ 507 pos: position{line: 177, col: 15, offset: 4547}, 508 name: "space", 509 }, 510 }, 511 &litMatcher{ 512 pos: position{line: 177, col: 22, offset: 4554}, 513 val: "|", 514 ignoreCase: false, 515 want: "\"|\"", 516 }, 517 &ruleRefExpr{ 518 pos: position{line: 177, col: 26, offset: 4558}, 519 name: "space", 520 }, 521 &litMatcher{ 522 pos: position{line: 177, col: 32, offset: 4564}, 523 val: "json", 524 ignoreCase: false, 525 want: "\"json\"", 526 }, 527 &zeroOrOneExpr{ 528 pos: position{line: 177, col: 39, offset: 4571}, 529 expr: &ruleRefExpr{ 530 pos: position{line: 177, col: 39, offset: 4571}, 531 name: "space", 532 }, 533 }, 534 &labeledExpr{ 535 pos: position{line: 177, col: 46, offset: 4578}, 536 label: "rest", 537 expr: &zeroOrMoreExpr{ 538 pos: position{line: 177, col: 51, offset: 4583}, 539 expr: &ruleRefExpr{ 540 pos: position{line: 177, col: 52, offset: 4584}, 541 name: "Query", 542 }, 543 }, 544 }, 545 }, 546 }, 547 }, 548 &actionExpr{ 549 pos: position{line: 208, col: 5, offset: 5974}, 550 run: (*parser).callonJSONFilter14, 551 expr: &seqExpr{ 552 pos: position{line: 208, col: 5, offset: 5974}, 553 exprs: []interface{}{ 554 &zeroOrOneExpr{ 555 pos: position{line: 208, col: 5, offset: 5974}, 556 expr: &ruleRefExpr{ 557 pos: position{line: 208, col: 5, offset: 5974}, 558 name: "space", 559 }, 560 }, 561 &litMatcher{ 562 pos: position{line: 208, col: 12, offset: 5981}, 563 val: "|", 564 ignoreCase: false, 565 want: "\"|\"", 566 }, 567 &ruleRefExpr{ 568 pos: position{line: 208, col: 16, offset: 5985}, 569 name: "space", 570 }, 571 &litMatcher{ 572 pos: position{line: 208, col: 22, offset: 5991}, 573 val: "logfmt", 574 ignoreCase: false, 575 want: "\"logfmt\"", 576 }, 577 &zeroOrOneExpr{ 578 pos: position{line: 208, col: 31, offset: 6000}, 579 expr: &ruleRefExpr{ 580 pos: position{line: 208, col: 31, offset: 6000}, 581 name: "space", 582 }, 583 }, 584 &labeledExpr{ 585 pos: position{line: 208, col: 38, offset: 6007}, 586 label: "rest", 587 expr: &zeroOrMoreExpr{ 588 pos: position{line: 208, col: 43, offset: 6012}, 589 expr: &choiceExpr{ 590 pos: position{line: 208, col: 44, offset: 6013}, 591 alternatives: []interface{}{ 592 &ruleRefExpr{ 593 pos: position{line: 208, col: 44, offset: 6013}, 594 name: "Query", 595 }, 596 &ruleRefExpr{ 597 pos: position{line: 208, col: 52, offset: 6021}, 598 name: "SingleField", 599 }, 600 }, 601 }, 602 }, 603 }, 604 }, 605 }, 606 }, 607 }, 608 }, 609 }, 610 { 611 name: "Duration", 612 pos: position{line: 221, col: 1, offset: 6683}, 613 expr: &actionExpr{ 614 pos: position{line: 221, col: 13, offset: 6695}, 615 run: (*parser).callonDuration1, 616 expr: &seqExpr{ 617 pos: position{line: 221, col: 13, offset: 6695}, 618 exprs: []interface{}{ 619 &litMatcher{ 620 pos: position{line: 221, col: 13, offset: 6695}, 621 val: "[", 622 ignoreCase: false, 623 want: "\"[\"", 624 }, 625 &labeledExpr{ 626 pos: position{line: 221, col: 17, offset: 6699}, 627 label: "val", 628 expr: &ruleRefExpr{ 629 pos: position{line: 221, col: 21, offset: 6703}, 630 name: "Integer", 631 }, 632 }, 633 &labeledExpr{ 634 pos: position{line: 221, col: 29, offset: 6711}, 635 label: "timeUnit", 636 expr: &ruleRefExpr{ 637 pos: position{line: 221, col: 38, offset: 6720}, 638 name: "TIME_UNIT", 639 }, 640 }, 641 &litMatcher{ 642 pos: position{line: 221, col: 48, offset: 6730}, 643 val: "]", 644 ignoreCase: false, 645 want: "\"]\"", 646 }, 647 }, 648 }, 649 }, 650 }, 651 { 652 name: "TIME_UNIT", 653 pos: position{line: 234, col: 1, offset: 7055}, 654 expr: &choiceExpr{ 655 pos: position{line: 234, col: 14, offset: 7068}, 656 alternatives: []interface{}{ 657 &actionExpr{ 658 pos: position{line: 234, col: 14, offset: 7068}, 659 run: (*parser).callonTIME_UNIT2, 660 expr: &litMatcher{ 661 pos: position{line: 234, col: 14, offset: 7068}, 662 val: "ms", 663 ignoreCase: false, 664 want: "\"ms\"", 665 }, 666 }, 667 &actionExpr{ 668 pos: position{line: 236, col: 5, offset: 7105}, 669 run: (*parser).callonTIME_UNIT4, 670 expr: &litMatcher{ 671 pos: position{line: 236, col: 5, offset: 7105}, 672 val: "s", 673 ignoreCase: false, 674 want: "\"s\"", 675 }, 676 }, 677 &actionExpr{ 678 pos: position{line: 238, col: 5, offset: 7144}, 679 run: (*parser).callonTIME_UNIT6, 680 expr: &litMatcher{ 681 pos: position{line: 238, col: 5, offset: 7144}, 682 val: "m", 683 ignoreCase: false, 684 want: "\"m\"", 685 }, 686 }, 687 &actionExpr{ 688 pos: position{line: 240, col: 5, offset: 7184}, 689 run: (*parser).callonTIME_UNIT8, 690 expr: &litMatcher{ 691 pos: position{line: 240, col: 5, offset: 7184}, 692 val: "h", 693 ignoreCase: false, 694 want: "\"h\"", 695 }, 696 }, 697 &actionExpr{ 698 pos: position{line: 242, col: 5, offset: 7229}, 699 run: (*parser).callonTIME_UNIT10, 700 expr: &litMatcher{ 701 pos: position{line: 242, col: 5, offset: 7229}, 702 val: "d", 703 ignoreCase: false, 704 want: "\"d\"", 705 }, 706 }, 707 &actionExpr{ 708 pos: position{line: 244, col: 5, offset: 7279}, 709 run: (*parser).callonTIME_UNIT12, 710 expr: &litMatcher{ 711 pos: position{line: 244, col: 5, offset: 7279}, 712 val: "w", 713 ignoreCase: false, 714 want: "\"w\"", 715 }, 716 }, 717 &actionExpr{ 718 pos: position{line: 246, col: 5, offset: 7333}, 719 run: (*parser).callonTIME_UNIT14, 720 expr: &litMatcher{ 721 pos: position{line: 246, col: 5, offset: 7333}, 722 val: "y", 723 ignoreCase: false, 724 want: "\"y\"", 725 }, 726 }, 727 }, 728 }, 729 }, 730 { 731 name: "opCOMP", 732 pos: position{line: 250, col: 1, offset: 7388}, 733 expr: &choiceExpr{ 734 pos: position{line: 250, col: 11, offset: 7398}, 735 alternatives: []interface{}{ 736 &ruleRefExpr{ 737 pos: position{line: 250, col: 11, offset: 7398}, 738 name: "opCustom", 739 }, 740 &actionExpr{ 741 pos: position{line: 251, col: 3, offset: 7409}, 742 run: (*parser).callonopCOMP3, 743 expr: &litMatcher{ 744 pos: position{line: 251, col: 3, offset: 7409}, 745 val: "==", 746 ignoreCase: false, 747 want: "\"==\"", 748 }, 749 }, 750 &actionExpr{ 751 pos: position{line: 253, col: 5, offset: 7440}, 752 run: (*parser).callonopCOMP5, 753 expr: &litMatcher{ 754 pos: position{line: 253, col: 5, offset: 7440}, 755 val: "=~", 756 ignoreCase: false, 757 want: "\"=~\"", 758 }, 759 }, 760 &actionExpr{ 761 pos: position{line: 255, col: 5, offset: 7471}, 762 run: (*parser).callonopCOMP7, 763 expr: &litMatcher{ 764 pos: position{line: 255, col: 5, offset: 7471}, 765 val: "<=", 766 ignoreCase: false, 767 want: "\"<=\"", 768 }, 769 }, 770 &actionExpr{ 771 pos: position{line: 257, col: 5, offset: 7513}, 772 run: (*parser).callonopCOMP9, 773 expr: &litMatcher{ 774 pos: position{line: 257, col: 5, offset: 7513}, 775 val: ">=", 776 ignoreCase: false, 777 want: "\">=\"", 778 }, 779 }, 780 &actionExpr{ 781 pos: position{line: 259, col: 5, offset: 7555}, 782 run: (*parser).callonopCOMP11, 783 expr: &litMatcher{ 784 pos: position{line: 259, col: 5, offset: 7555}, 785 val: "=", 786 ignoreCase: false, 787 want: "\"=\"", 788 }, 789 }, 790 &actionExpr{ 791 pos: position{line: 261, col: 5, offset: 7596}, 792 run: (*parser).callonopCOMP13, 793 expr: &litMatcher{ 794 pos: position{line: 261, col: 5, offset: 7596}, 795 val: "<", 796 ignoreCase: false, 797 want: "\"<\"", 798 }, 799 }, 800 &actionExpr{ 801 pos: position{line: 263, col: 5, offset: 7637}, 802 run: (*parser).callonopCOMP15, 803 expr: &litMatcher{ 804 pos: position{line: 263, col: 5, offset: 7637}, 805 val: ">", 806 ignoreCase: false, 807 want: "\">\"", 808 }, 809 }, 810 &actionExpr{ 811 pos: position{line: 265, col: 5, offset: 7678}, 812 run: (*parser).callonopCOMP17, 813 expr: &litMatcher{ 814 pos: position{line: 265, col: 5, offset: 7678}, 815 val: "!=", 816 ignoreCase: false, 817 want: "\"!=\"", 818 }, 819 }, 820 &actionExpr{ 821 pos: position{line: 267, col: 5, offset: 7720}, 822 run: (*parser).callonopCOMP19, 823 expr: &litMatcher{ 824 pos: position{line: 267, col: 5, offset: 7720}, 825 val: "!~", 826 ignoreCase: false, 827 want: "\"!~\"", 828 }, 829 }, 830 }, 831 }, 832 }, 833 { 834 name: "GrepFilter", 835 pos: position{line: 271, col: 1, offset: 7752}, 836 expr: &choiceExpr{ 837 pos: position{line: 271, col: 15, offset: 7766}, 838 alternatives: []interface{}{ 839 &actionExpr{ 840 pos: position{line: 271, col: 15, offset: 7766}, 841 run: (*parser).callonGrepFilter2, 842 expr: &litMatcher{ 843 pos: position{line: 271, col: 15, offset: 7766}, 844 val: "|=", 845 ignoreCase: false, 846 want: "\"|=\"", 847 }, 848 }, 849 &actionExpr{ 850 pos: position{line: 273, col: 5, offset: 7797}, 851 run: (*parser).callonGrepFilter4, 852 expr: &litMatcher{ 853 pos: position{line: 273, col: 5, offset: 7797}, 854 val: "!=", 855 ignoreCase: false, 856 want: "\"!=\"", 857 }, 858 }, 859 &actionExpr{ 860 pos: position{line: 275, col: 5, offset: 7839}, 861 run: (*parser).callonGrepFilter6, 862 expr: &litMatcher{ 863 pos: position{line: 275, col: 5, offset: 7839}, 864 val: "|~", 865 ignoreCase: false, 866 want: "\"|~\"", 867 }, 868 }, 869 &actionExpr{ 870 pos: position{line: 277, col: 5, offset: 7871}, 871 run: (*parser).callonGrepFilter8, 872 expr: &litMatcher{ 873 pos: position{line: 277, col: 5, offset: 7871}, 874 val: "!~", 875 ignoreCase: false, 876 want: "\"!~\"", 877 }, 878 }, 879 }, 880 }, 881 }, 882 { 883 name: "opCustom", 884 pos: position{line: 282, col: 1, offset: 7904}, 885 expr: &actionExpr{ 886 pos: position{line: 282, col: 13, offset: 7916}, 887 run: (*parser).callonopCustom1, 888 expr: &seqExpr{ 889 pos: position{line: 282, col: 13, offset: 7916}, 890 exprs: []interface{}{ 891 &litMatcher{ 892 pos: position{line: 282, col: 13, offset: 7916}, 893 val: "=", 894 ignoreCase: false, 895 want: "\"=\"", 896 }, 897 &labeledExpr{ 898 pos: position{line: 282, col: 17, offset: 7920}, 899 label: "opname", 900 expr: &oneOrMoreExpr{ 901 pos: position{line: 282, col: 24, offset: 7927}, 902 expr: &charClassMatcher{ 903 pos: position{line: 282, col: 24, offset: 7927}, 904 val: "[a-z]i", 905 ranges: []rune{'a', 'z'}, 906 ignoreCase: true, 907 inverted: false, 908 }, 909 }, 910 }, 911 &litMatcher{ 912 pos: position{line: 282, col: 32, offset: 7935}, 913 val: "=", 914 ignoreCase: false, 915 want: "\"=\"", 916 }, 917 }, 918 }, 919 }, 920 }, 921 { 922 name: "LetOpr", 923 pos: position{line: 287, col: 1, offset: 7982}, 924 expr: &choiceExpr{ 925 pos: position{line: 287, col: 11, offset: 7992}, 926 alternatives: []interface{}{ 927 &actionExpr{ 928 pos: position{line: 287, col: 11, offset: 7992}, 929 run: (*parser).callonLetOpr2, 930 expr: &seqExpr{ 931 pos: position{line: 287, col: 11, offset: 7992}, 932 exprs: []interface{}{ 933 &charClassMatcher{ 934 pos: position{line: 287, col: 11, offset: 7992}, 935 val: "[>]", 936 chars: []rune{'>'}, 937 ignoreCase: false, 938 inverted: false, 939 }, 940 &litMatcher{ 941 pos: position{line: 287, col: 15, offset: 7996}, 942 val: "=", 943 ignoreCase: false, 944 want: "\"=\"", 945 }, 946 }, 947 }, 948 }, 949 &actionExpr{ 950 pos: position{line: 289, col: 5, offset: 8052}, 951 run: (*parser).callonLetOpr6, 952 expr: &litMatcher{ 953 pos: position{line: 289, col: 5, offset: 8052}, 954 val: ">", 955 ignoreCase: false, 956 want: "\">\"", 957 }, 958 }, 959 &actionExpr{ 960 pos: position{line: 291, col: 5, offset: 8099}, 961 run: (*parser).callonLetOpr8, 962 expr: &seqExpr{ 963 pos: position{line: 291, col: 5, offset: 8099}, 964 exprs: []interface{}{ 965 &charClassMatcher{ 966 pos: position{line: 291, col: 5, offset: 8099}, 967 val: "[<]", 968 chars: []rune{'<'}, 969 ignoreCase: false, 970 inverted: false, 971 }, 972 &litMatcher{ 973 pos: position{line: 291, col: 9, offset: 8103}, 974 val: "=", 975 ignoreCase: false, 976 want: "\"=\"", 977 }, 978 }, 979 }, 980 }, 981 &actionExpr{ 982 pos: position{line: 293, col: 5, offset: 8156}, 983 run: (*parser).callonLetOpr12, 984 expr: &litMatcher{ 985 pos: position{line: 293, col: 5, offset: 8156}, 986 val: "<", 987 ignoreCase: false, 988 want: "\"<\"", 989 }, 990 }, 991 &actionExpr{ 992 pos: position{line: 295, col: 5, offset: 8200}, 993 run: (*parser).callonLetOpr14, 994 expr: &seqExpr{ 995 pos: position{line: 295, col: 5, offset: 8200}, 996 exprs: []interface{}{ 997 &charClassMatcher{ 998 pos: position{line: 295, col: 5, offset: 8200}, 999 val: "[=]", 1000 chars: []rune{'='}, 1001 ignoreCase: false, 1002 inverted: false, 1003 }, 1004 &litMatcher{ 1005 pos: position{line: 295, col: 9, offset: 8204}, 1006 val: "=", 1007 ignoreCase: false, 1008 want: "\"=\"", 1009 }, 1010 }, 1011 }, 1012 }, 1013 &actionExpr{ 1014 pos: position{line: 297, col: 5, offset: 8246}, 1015 run: (*parser).callonLetOpr18, 1016 expr: &seqExpr{ 1017 pos: position{line: 297, col: 5, offset: 8246}, 1018 exprs: []interface{}{ 1019 &charClassMatcher{ 1020 pos: position{line: 297, col: 5, offset: 8246}, 1021 val: "[!]", 1022 chars: []rune{'!'}, 1023 ignoreCase: false, 1024 inverted: false, 1025 }, 1026 &litMatcher{ 1027 pos: position{line: 297, col: 9, offset: 8250}, 1028 val: "=", 1029 ignoreCase: false, 1030 want: "\"=\"", 1031 }, 1032 }, 1033 }, 1034 }, 1035 &actionExpr{ 1036 pos: position{line: 299, col: 5, offset: 8295}, 1037 run: (*parser).callonLetOpr22, 1038 expr: &litMatcher{ 1039 pos: position{line: 299, col: 5, offset: 8295}, 1040 val: "+", 1041 ignoreCase: false, 1042 want: "\"+\"", 1043 }, 1044 }, 1045 &actionExpr{ 1046 pos: position{line: 301, col: 5, offset: 8334}, 1047 run: (*parser).callonLetOpr24, 1048 expr: &litMatcher{ 1049 pos: position{line: 301, col: 5, offset: 8334}, 1050 val: "-", 1051 ignoreCase: false, 1052 want: "\"-\"", 1053 }, 1054 }, 1055 &actionExpr{ 1056 pos: position{line: 303, col: 5, offset: 8378}, 1057 run: (*parser).callonLetOpr26, 1058 expr: &litMatcher{ 1059 pos: position{line: 303, col: 5, offset: 8378}, 1060 val: "/", 1061 ignoreCase: false, 1062 want: "\"/\"", 1063 }, 1064 }, 1065 &actionExpr{ 1066 pos: position{line: 305, col: 5, offset: 8420}, 1067 run: (*parser).callonLetOpr28, 1068 expr: &litMatcher{ 1069 pos: position{line: 305, col: 5, offset: 8420}, 1070 val: "*", 1071 ignoreCase: false, 1072 want: "\"*\"", 1073 }, 1074 }, 1075 &actionExpr{ 1076 pos: position{line: 307, col: 5, offset: 8464}, 1077 run: (*parser).callonLetOpr30, 1078 expr: &litMatcher{ 1079 pos: position{line: 307, col: 5, offset: 8464}, 1080 val: "%", 1081 ignoreCase: false, 1082 want: "\"%\"", 1083 }, 1084 }, 1085 }, 1086 }, 1087 }, 1088 { 1089 name: "LetIdentifier", 1090 pos: position{line: 311, col: 1, offset: 8505}, 1091 expr: &choiceExpr{ 1092 pos: position{line: 311, col: 18, offset: 8522}, 1093 alternatives: []interface{}{ 1094 &actionExpr{ 1095 pos: position{line: 311, col: 18, offset: 8522}, 1096 run: (*parser).callonLetIdentifier2, 1097 expr: &seqExpr{ 1098 pos: position{line: 311, col: 18, offset: 8522}, 1099 exprs: []interface{}{ 1100 &zeroOrOneExpr{ 1101 pos: position{line: 311, col: 18, offset: 8522}, 1102 expr: &litMatcher{ 1103 pos: position{line: 311, col: 18, offset: 8522}, 1104 val: "-", 1105 ignoreCase: false, 1106 want: "\"-\"", 1107 }, 1108 }, 1109 &ruleRefExpr{ 1110 pos: position{line: 311, col: 23, offset: 8527}, 1111 name: "Float", 1112 }, 1113 }, 1114 }, 1115 }, 1116 &actionExpr{ 1117 pos: position{line: 317, col: 5, offset: 8722}, 1118 run: (*parser).callonLetIdentifier7, 1119 expr: &seqExpr{ 1120 pos: position{line: 317, col: 5, offset: 8722}, 1121 exprs: []interface{}{ 1122 &oneOrMoreExpr{ 1123 pos: position{line: 317, col: 5, offset: 8722}, 1124 expr: &litMatcher{ 1125 pos: position{line: 317, col: 5, offset: 8722}, 1126 val: "-", 1127 ignoreCase: false, 1128 want: "\"-\"", 1129 }, 1130 }, 1131 &ruleRefExpr{ 1132 pos: position{line: 317, col: 10, offset: 8727}, 1133 name: "Integer", 1134 }, 1135 }, 1136 }, 1137 }, 1138 &actionExpr{ 1139 pos: position{line: 323, col: 6, offset: 8932}, 1140 run: (*parser).callonLetIdentifier12, 1141 expr: &ruleRefExpr{ 1142 pos: position{line: 323, col: 6, offset: 8932}, 1143 name: "Integer", 1144 }, 1145 }, 1146 &actionExpr{ 1147 pos: position{line: 329, col: 5, offset: 9141}, 1148 run: (*parser).callonLetIdentifier14, 1149 expr: &ruleRefExpr{ 1150 pos: position{line: 329, col: 5, offset: 9141}, 1151 name: "QuotedValue", 1152 }, 1153 }, 1154 }, 1155 }, 1156 }, 1157 { 1158 name: "BoolValue", 1159 pos: position{line: 337, col: 1, offset: 9302}, 1160 expr: &choiceExpr{ 1161 pos: position{line: 337, col: 14, offset: 9315}, 1162 alternatives: []interface{}{ 1163 &litMatcher{ 1164 pos: position{line: 337, col: 14, offset: 9315}, 1165 val: "false", 1166 ignoreCase: false, 1167 want: "\"false\"", 1168 }, 1169 &litMatcher{ 1170 pos: position{line: 337, col: 24, offset: 9325}, 1171 val: "true", 1172 ignoreCase: false, 1173 want: "\"true\"", 1174 }, 1175 }, 1176 }, 1177 }, 1178 { 1179 name: "COUNT_OVER_TIME", 1180 pos: position{line: 339, col: 1, offset: 9333}, 1181 expr: &litMatcher{ 1182 pos: position{line: 339, col: 20, offset: 9352}, 1183 val: "count_over_time", 1184 ignoreCase: false, 1185 want: "\"count_over_time\"", 1186 }, 1187 }, 1188 { 1189 name: "Field", 1190 pos: position{line: 341, col: 1, offset: 9372}, 1191 expr: &choiceExpr{ 1192 pos: position{line: 341, col: 10, offset: 9381}, 1193 alternatives: []interface{}{ 1194 &ruleRefExpr{ 1195 pos: position{line: 341, col: 10, offset: 9381}, 1196 name: "Value", 1197 }, 1198 &actionExpr{ 1199 pos: position{line: 341, col: 18, offset: 9389}, 1200 run: (*parser).callonField3, 1201 expr: &labeledExpr{ 1202 pos: position{line: 341, col: 18, offset: 9389}, 1203 label: "pieces", 1204 expr: &ruleRefExpr{ 1205 pos: position{line: 341, col: 25, offset: 9396}, 1206 name: "FieldPiece", 1207 }, 1208 }, 1209 }, 1210 }, 1211 }, 1212 }, 1213 { 1214 name: "Identifier", 1215 pos: position{line: 349, col: 1, offset: 9488}, 1216 expr: &actionExpr{ 1217 pos: position{line: 350, col: 4, offset: 9505}, 1218 run: (*parser).callonIdentifier1, 1219 expr: &oneOrMoreExpr{ 1220 pos: position{line: 350, col: 4, offset: 9505}, 1221 expr: &charClassMatcher{ 1222 pos: position{line: 350, col: 4, offset: 9505}, 1223 val: "[a-zA-Z0-9_@./*]i", 1224 chars: []rune{'_', '@', '.', '/', '*'}, 1225 ranges: []rune{'a', 'z', 'a', 'z', '0', '9'}, 1226 ignoreCase: true, 1227 inverted: false, 1228 }, 1229 }, 1230 }, 1231 }, 1232 { 1233 name: "Value", 1234 pos: position{line: 354, col: 1, offset: 9563}, 1235 expr: &actionExpr{ 1236 pos: position{line: 354, col: 10, offset: 9572}, 1237 run: (*parser).callonValue1, 1238 expr: &labeledExpr{ 1239 pos: position{line: 354, col: 10, offset: 9572}, 1240 label: "val", 1241 expr: &choiceExpr{ 1242 pos: position{line: 355, col: 5, offset: 9582}, 1243 alternatives: []interface{}{ 1244 &ruleRefExpr{ 1245 pos: position{line: 355, col: 5, offset: 9582}, 1246 name: "Float", 1247 }, 1248 &ruleRefExpr{ 1249 pos: position{line: 356, col: 7, offset: 9594}, 1250 name: "Integer", 1251 }, 1252 }, 1253 }, 1254 }, 1255 }, 1256 }, 1257 { 1258 name: "Integer", 1259 pos: position{line: 362, col: 1, offset: 9649}, 1260 expr: &actionExpr{ 1261 pos: position{line: 362, col: 12, offset: 9660}, 1262 run: (*parser).callonInteger1, 1263 expr: &seqExpr{ 1264 pos: position{line: 362, col: 12, offset: 9660}, 1265 exprs: []interface{}{ 1266 &zeroOrOneExpr{ 1267 pos: position{line: 362, col: 12, offset: 9660}, 1268 expr: &charClassMatcher{ 1269 pos: position{line: 362, col: 12, offset: 9660}, 1270 val: "[+-]", 1271 chars: []rune{'+', '-'}, 1272 ignoreCase: false, 1273 inverted: false, 1274 }, 1275 }, 1276 &oneOrMoreExpr{ 1277 pos: position{line: 362, col: 18, offset: 9666}, 1278 expr: &charClassMatcher{ 1279 pos: position{line: 362, col: 18, offset: 9666}, 1280 val: "[0-9]", 1281 ranges: []rune{'0', '9'}, 1282 ignoreCase: false, 1283 inverted: false, 1284 }, 1285 }, 1286 }, 1287 }, 1288 }, 1289 }, 1290 { 1291 name: "Float", 1292 pos: position{line: 366, col: 1, offset: 9724}, 1293 expr: &actionExpr{ 1294 pos: position{line: 366, col: 10, offset: 9733}, 1295 run: (*parser).callonFloat1, 1296 expr: &seqExpr{ 1297 pos: position{line: 366, col: 10, offset: 9733}, 1298 exprs: []interface{}{ 1299 &zeroOrOneExpr{ 1300 pos: position{line: 366, col: 10, offset: 9733}, 1301 expr: &charClassMatcher{ 1302 pos: position{line: 366, col: 10, offset: 9733}, 1303 val: "[+-]", 1304 chars: []rune{'+', '-'}, 1305 ignoreCase: false, 1306 inverted: false, 1307 }, 1308 }, 1309 &seqExpr{ 1310 pos: position{line: 366, col: 17, offset: 9740}, 1311 exprs: []interface{}{ 1312 &zeroOrMoreExpr{ 1313 pos: position{line: 366, col: 17, offset: 9740}, 1314 expr: &charClassMatcher{ 1315 pos: position{line: 366, col: 17, offset: 9740}, 1316 val: "[0-9]", 1317 ranges: []rune{'0', '9'}, 1318 ignoreCase: false, 1319 inverted: false, 1320 }, 1321 }, 1322 &litMatcher{ 1323 pos: position{line: 366, col: 24, offset: 9747}, 1324 val: ".", 1325 ignoreCase: false, 1326 want: "\".\"", 1327 }, 1328 &oneOrMoreExpr{ 1329 pos: position{line: 366, col: 28, offset: 9751}, 1330 expr: &charClassMatcher{ 1331 pos: position{line: 366, col: 28, offset: 9751}, 1332 val: "[0-9]", 1333 ranges: []rune{'0', '9'}, 1334 ignoreCase: false, 1335 inverted: false, 1336 }, 1337 }, 1338 }, 1339 }, 1340 }, 1341 }, 1342 }, 1343 }, 1344 { 1345 name: "FieldPiece", 1346 pos: position{line: 370, col: 1, offset: 9815}, 1347 expr: &choiceExpr{ 1348 pos: position{line: 370, col: 15, offset: 9829}, 1349 alternatives: []interface{}{ 1350 &ruleRefExpr{ 1351 pos: position{line: 370, col: 15, offset: 9829}, 1352 name: "QuotedFieldPiece", 1353 }, 1354 &ruleRefExpr{ 1355 pos: position{line: 370, col: 34, offset: 9848}, 1356 name: "UnquotedFieldPiece", 1357 }, 1358 &ruleRefExpr{ 1359 pos: position{line: 370, col: 55, offset: 9869}, 1360 name: "Star", 1361 }, 1362 }, 1363 }, 1364 }, 1365 { 1366 name: "UnquotedFieldPiece", 1367 pos: position{line: 372, col: 1, offset: 9875}, 1368 expr: &actionExpr{ 1369 pos: position{line: 372, col: 23, offset: 9897}, 1370 run: (*parser).callonUnquotedFieldPiece1, 1371 expr: &oneOrMoreExpr{ 1372 pos: position{line: 372, col: 23, offset: 9897}, 1373 expr: &charClassMatcher{ 1374 pos: position{line: 372, col: 23, offset: 9897}, 1375 val: "[-a-zA-Z0-9$&,?#%_@;[\\]{}+-./*:]i", 1376 chars: []rune{'-', '$', '&', ',', '?', '#', '%', '_', '@', ';', '[', ']', '{', '}', '/', '*', ':'}, 1377 ranges: []rune{'a', 'z', 'a', 'z', '0', '9', '+', '.'}, 1378 ignoreCase: true, 1379 inverted: false, 1380 }, 1381 }, 1382 }, 1383 }, 1384 { 1385 name: "QuotedFieldPiece", 1386 pos: position{line: 376, col: 1, offset: 9968}, 1387 expr: &ruleRefExpr{ 1388 pos: position{line: 376, col: 21, offset: 9988}, 1389 name: "QuotedValue", 1390 }, 1391 }, 1392 { 1393 name: "Star", 1394 pos: position{line: 378, col: 1, offset: 10001}, 1395 expr: &actionExpr{ 1396 pos: position{line: 378, col: 9, offset: 10009}, 1397 run: (*parser).callonStar1, 1398 expr: &litMatcher{ 1399 pos: position{line: 378, col: 9, offset: 10009}, 1400 val: "*", 1401 ignoreCase: false, 1402 want: "\"*\"", 1403 }, 1404 }, 1405 }, 1406 { 1407 name: "QuotedValue", 1408 pos: position{line: 381, col: 1, offset: 10037}, 1409 expr: &actionExpr{ 1410 pos: position{line: 381, col: 16, offset: 10052}, 1411 run: (*parser).callonQuotedValue1, 1412 expr: &seqExpr{ 1413 pos: position{line: 381, col: 16, offset: 10052}, 1414 exprs: []interface{}{ 1415 &litMatcher{ 1416 pos: position{line: 381, col: 16, offset: 10052}, 1417 val: "\"", 1418 ignoreCase: false, 1419 want: "\"\\\"\"", 1420 }, 1421 &zeroOrMoreExpr{ 1422 pos: position{line: 381, col: 20, offset: 10056}, 1423 expr: &choiceExpr{ 1424 pos: position{line: 381, col: 22, offset: 10058}, 1425 alternatives: []interface{}{ 1426 &seqExpr{ 1427 pos: position{line: 381, col: 22, offset: 10058}, 1428 exprs: []interface{}{ 1429 ¬Expr{ 1430 pos: position{line: 381, col: 22, offset: 10058}, 1431 expr: &ruleRefExpr{ 1432 pos: position{line: 381, col: 23, offset: 10059}, 1433 name: "EscapedChar", 1434 }, 1435 }, 1436 &anyMatcher{ 1437 line: 381, col: 35, offset: 10071, 1438 }, 1439 }, 1440 }, 1441 &seqExpr{ 1442 pos: position{line: 381, col: 39, offset: 10075}, 1443 exprs: []interface{}{ 1444 &litMatcher{ 1445 pos: position{line: 381, col: 39, offset: 10075}, 1446 val: "\\", 1447 ignoreCase: false, 1448 want: "\"\\\\\"", 1449 }, 1450 &ruleRefExpr{ 1451 pos: position{line: 381, col: 44, offset: 10080}, 1452 name: "EscapeSequence", 1453 }, 1454 }, 1455 }, 1456 }, 1457 }, 1458 }, 1459 &litMatcher{ 1460 pos: position{line: 381, col: 62, offset: 10098}, 1461 val: "\"", 1462 ignoreCase: false, 1463 want: "\"\\\"\"", 1464 }, 1465 }, 1466 }, 1467 }, 1468 }, 1469 { 1470 name: "EscapedChar", 1471 pos: position{line: 385, col: 1, offset: 10215}, 1472 expr: &charClassMatcher{ 1473 pos: position{line: 385, col: 16, offset: 10230}, 1474 val: "[\\x00-\\x1f\"\\\\]", 1475 chars: []rune{'"', '\\'}, 1476 ranges: []rune{'\x00', '\x1f'}, 1477 ignoreCase: false, 1478 inverted: false, 1479 }, 1480 }, 1481 { 1482 name: "EscapeSequence", 1483 pos: position{line: 387, col: 1, offset: 10246}, 1484 expr: &choiceExpr{ 1485 pos: position{line: 387, col: 19, offset: 10264}, 1486 alternatives: []interface{}{ 1487 &ruleRefExpr{ 1488 pos: position{line: 387, col: 19, offset: 10264}, 1489 name: "SingleCharEscape", 1490 }, 1491 &ruleRefExpr{ 1492 pos: position{line: 387, col: 38, offset: 10283}, 1493 name: "UnicodeEscape", 1494 }, 1495 }, 1496 }, 1497 }, 1498 { 1499 name: "SingleCharEscape", 1500 pos: position{line: 389, col: 1, offset: 10298}, 1501 expr: &charClassMatcher{ 1502 pos: position{line: 389, col: 21, offset: 10318}, 1503 val: "[\"\\\\/bfnrt]", 1504 chars: []rune{'"', '\\', '/', 'b', 'f', 'n', 'r', 't'}, 1505 ignoreCase: false, 1506 inverted: false, 1507 }, 1508 }, 1509 { 1510 name: "UnicodeEscape", 1511 pos: position{line: 391, col: 1, offset: 10331}, 1512 expr: &seqExpr{ 1513 pos: position{line: 391, col: 18, offset: 10348}, 1514 exprs: []interface{}{ 1515 &litMatcher{ 1516 pos: position{line: 391, col: 18, offset: 10348}, 1517 val: "u", 1518 ignoreCase: false, 1519 want: "\"u\"", 1520 }, 1521 &ruleRefExpr{ 1522 pos: position{line: 391, col: 22, offset: 10352}, 1523 name: "HexDigit", 1524 }, 1525 &ruleRefExpr{ 1526 pos: position{line: 391, col: 31, offset: 10361}, 1527 name: "HexDigit", 1528 }, 1529 &ruleRefExpr{ 1530 pos: position{line: 391, col: 40, offset: 10370}, 1531 name: "HexDigit", 1532 }, 1533 &ruleRefExpr{ 1534 pos: position{line: 391, col: 49, offset: 10379}, 1535 name: "HexDigit", 1536 }, 1537 }, 1538 }, 1539 }, 1540 { 1541 name: "HexDigit", 1542 pos: position{line: 393, col: 1, offset: 10389}, 1543 expr: &charClassMatcher{ 1544 pos: position{line: 393, col: 13, offset: 10401}, 1545 val: "[0-9a-f]i", 1546 ranges: []rune{'0', '9', 'a', 'f'}, 1547 ignoreCase: true, 1548 inverted: false, 1549 }, 1550 }, 1551 { 1552 name: "EOF", 1553 pos: position{line: 398, col: 1, offset: 10485}, 1554 expr: ¬Expr{ 1555 pos: position{line: 398, col: 7, offset: 10491}, 1556 expr: &anyMatcher{ 1557 line: 398, col: 8, offset: 10492, 1558 }, 1559 }, 1560 }, 1561 { 1562 name: "_", 1563 displayName: "\"whitespace\"", 1564 pos: position{line: 400, col: 1, offset: 10495}, 1565 expr: &zeroOrMoreExpr{ 1566 pos: position{line: 400, col: 19, offset: 10513}, 1567 expr: &charClassMatcher{ 1568 pos: position{line: 400, col: 19, offset: 10513}, 1569 val: "[ \\n\\t\\r]", 1570 chars: []rune{' ', '\n', '\t', '\r'}, 1571 ignoreCase: false, 1572 inverted: false, 1573 }, 1574 }, 1575 }, 1576 { 1577 name: "space", 1578 pos: position{line: 402, col: 1, offset: 10525}, 1579 expr: &oneOrMoreExpr{ 1580 pos: position{line: 402, col: 10, offset: 10534}, 1581 expr: &charClassMatcher{ 1582 pos: position{line: 402, col: 10, offset: 10534}, 1583 val: "[ \\n\\t\\r]", 1584 chars: []rune{' ', '\n', '\t', '\r'}, 1585 ignoreCase: false, 1586 inverted: false, 1587 }, 1588 }, 1589 }, 1590 { 1591 name: "Delimiter", 1592 pos: position{line: 404, col: 1, offset: 10546}, 1593 expr: &choiceExpr{ 1594 pos: position{line: 404, col: 14, offset: 10559}, 1595 alternatives: []interface{}{ 1596 &litMatcher{ 1597 pos: position{line: 404, col: 14, offset: 10559}, 1598 val: "{", 1599 ignoreCase: false, 1600 want: "\"{\"", 1601 }, 1602 &litMatcher{ 1603 pos: position{line: 404, col: 20, offset: 10565}, 1604 val: "}", 1605 ignoreCase: false, 1606 want: "\"}\"", 1607 }, 1608 }, 1609 }, 1610 }, 1611 }, 1612 } 1613 1614 func (c *current) onStart2(query, jf, fs interface{}) (interface{}, error) { 1615 var q ast.QueryStruct 1616 if jf != nil { 1617 q.PipeCommands = jf.(*structs.QueryAggregators) 1618 q.PipeCommands.PipeCommandType = structs.OutputTransformType 1619 } 1620 if query != nil && fs != nil { 1621 finalNode := &ast.Node{ 1622 NodeType: ast.NodeAnd, 1623 Left: query.(*ast.Node), 1624 Right: fs.(*ast.Node), 1625 } 1626 q.SearchFilter = finalNode 1627 return q, nil 1628 1629 } 1630 if fs != nil && query == nil { 1631 q.SearchFilter = fs.(*ast.Node) 1632 return q, nil 1633 } 1634 1635 if query != nil && fs == nil { 1636 q.SearchFilter = query.(*ast.Node) 1637 return q, nil 1638 } 1639 1640 return q, nil 1641 } 1642 1643 func (p *parser) callonStart2() (interface{}, error) { 1644 stack := p.vstack[len(p.vstack)-1] 1645 _ = stack 1646 return p.cur.onStart2(stack["query"], stack["jf"], stack["fs"]) 1647 } 1648 1649 func (c *current) onStart14(query, duration interface{}) (interface{}, error) { 1650 var q ast.QueryStruct 1651 q.SearchFilter = query.(*ast.Node) 1652 aggs := &structs.QueryAggregators{} 1653 countCols := []*structs.MeasureAggregator{{MeasureCol: "*", MeasureFunc: utils.Count}} 1654 aggs.GroupByRequest = &structs.GroupByRequest{MeasureOperations: countCols, GroupByColumns: []string{"*"}} 1655 timeNow := utils.GetCurrentTimeMillis() 1656 aggs.TimeHistogram = &structs.TimeBucket{StartTime: timeNow - duration.(uint64), EndTime: timeNow} 1657 q.PipeCommands = aggs 1658 q.PipeCommands.PipeCommandType = structs.GroupByType 1659 return q, nil 1660 } 1661 1662 func (p *parser) callonStart14() (interface{}, error) { 1663 stack := p.vstack[len(p.vstack)-1] 1664 _ = stack 1665 return p.cur.onStart14(stack["query"], stack["duration"]) 1666 } 1667 1668 func (c *current) onStream1(q1, rest interface{}) (interface{}, error) { 1669 startNode, ok := q1.(*ast.Node) 1670 if !ok { 1671 return nil, nil 1672 } 1673 if len(rest.([]interface{})) == 0 { 1674 return startNode, nil 1675 } 1676 finalNode := &ast.Node{ 1677 NodeType: ast.NodeAnd, 1678 Left: startNode, 1679 Right: &ast.Node{}, 1680 } 1681 ptr := finalNode 1682 for index, query := range rest.([]interface{}) { 1683 if index == len(rest.([]interface{}))-1 { 1684 ptr.Right = query.(*ast.Node) 1685 } else { 1686 ptr.Right = &ast.Node{ 1687 NodeType: ast.NodeAnd, 1688 Left: query.(*ast.Node), 1689 Right: &ast.Node{}, 1690 } 1691 ptr = ptr.Right 1692 } 1693 } 1694 return finalNode, nil 1695 } 1696 1697 func (p *parser) callonStream1() (interface{}, error) { 1698 stack := p.vstack[len(p.vstack)-1] 1699 _ = stack 1700 return p.cur.onStream1(stack["q1"], stack["rest"]) 1701 } 1702 1703 func (c *current) onFilterStream1(rest interface{}) (interface{}, error) { 1704 if rest == nil || len(rest.([]interface{})) == 0 { 1705 return nil, nil 1706 } 1707 list := rest.([]interface{}) 1708 if len(list) == 1 { 1709 return list[0].(*ast.Node), nil 1710 } 1711 finalNode := &ast.Node{ 1712 NodeType: ast.NodeAnd, 1713 Left: list[0].(*ast.Node), 1714 Right: &ast.Node{}, 1715 } 1716 ptr := finalNode 1717 list = list[1:] 1718 for index, filter := range list { 1719 if index == len(list)-1 { 1720 ptr.Right = filter.(*ast.Node) 1721 } else { 1722 ptr.Right = &ast.Node{ 1723 NodeType: ast.NodeAnd, 1724 Left: filter.(*ast.Node), 1725 Right: &ast.Node{}, 1726 } 1727 ptr = ptr.Right 1728 } 1729 } 1730 return finalNode, nil 1731 } 1732 1733 func (p *parser) callonFilterStream1() (interface{}, error) { 1734 stack := p.vstack[len(p.vstack)-1] 1735 _ = stack 1736 return p.cur.onFilterStream1(stack["rest"]) 1737 } 1738 1739 func (c *current) onQuery1(field, field1 interface{}) (interface{}, error) { 1740 return &ast.Node{ 1741 NodeType: ast.NodeTerminal, 1742 Comparison: ast.Comparison{ 1743 Op: "=", 1744 Field: field.(string), 1745 Values: field1, 1746 }, 1747 }, nil 1748 } 1749 1750 func (p *parser) callonQuery1() (interface{}, error) { 1751 stack := p.vstack[len(p.vstack)-1] 1752 _ = stack 1753 return p.cur.onQuery1(stack["field"], stack["field1"]) 1754 } 1755 1756 func (c *current) onSingleField1(field interface{}) (interface{}, error) { 1757 return &ast.Node{ 1758 NodeType: ast.NodeTerminal, 1759 Comparison: ast.Comparison{ 1760 Field: field.(string), 1761 Values: field, 1762 }, 1763 }, nil 1764 } 1765 1766 func (p *parser) callonSingleField1() (interface{}, error) { 1767 stack := p.vstack[len(p.vstack)-1] 1768 _ = stack 1769 return p.cur.onSingleField1(stack["field"]) 1770 } 1771 1772 func (c *current) onLogFilter1(grep, field interface{}) (interface{}, error) { 1773 return &ast.Node{ 1774 NodeType: ast.NodeTerminal, 1775 Comparison: ast.Comparison{ 1776 Op: grep.(string), 1777 Values: ast.GrepValue{Field: field.(string)}, 1778 }, 1779 }, nil 1780 1781 } 1782 1783 func (p *parser) callonLogFilter1() (interface{}, error) { 1784 stack := p.vstack[len(p.vstack)-1] 1785 _ = stack 1786 return p.cur.onLogFilter1(stack["grep"], stack["field"]) 1787 } 1788 1789 func (c *current) onLabelFilter1(field, op, field1 interface{}) (interface{}, error) { 1790 return &ast.Node{ 1791 NodeType: ast.NodeTerminal, 1792 Comparison: ast.Comparison{ 1793 Op: op.(string), 1794 Field: field.(string), 1795 Values: field1, 1796 }, 1797 }, nil 1798 } 1799 1800 func (p *parser) callonLabelFilter1() (interface{}, error) { 1801 stack := p.vstack[len(p.vstack)-1] 1802 _ = stack 1803 return p.cur.onLabelFilter1(stack["field"], stack["op"], stack["field1"]) 1804 } 1805 1806 func (c *current) onJSONFilter2(rest interface{}) (interface{}, error) { 1807 if len(rest.([]interface{})) == 0 { 1808 return nil, nil 1809 } 1810 rawIncludeValues := make([]*structs.IncludeValue, 0) 1811 mapLabels := make(map[string]string, 0) 1812 aggNode := &structs.QueryAggregators{OutputTransforms: &structs.OutputTransforms{}} 1813 columsArray := make([]string, 0) 1814 for _, query := range rest.([]interface{}) { 1815 label := query.(*ast.Node).Comparison.Field 1816 expression := strings.Trim(query.(*ast.Node).Comparison.Values.(string), "\"") 1817 r := regexp.MustCompile(`^(\w+)\[(\d+)\]$`) 1818 isIndexed := r.MatchString(expression) 1819 if isIndexed { 1820 match := r.FindStringSubmatch(expression) 1821 cname := match[1] 1822 index, err := strconv.Atoi(match[2]) 1823 if err != nil { 1824 return nil, fmt.Errorf("ParseJSONRequest: error parsing json array expression") 1825 } 1826 rawIncludeValues = append(rawIncludeValues, &structs.IncludeValue{Index: index, ColName: cname, Label: label}) 1827 columsArray = append(columsArray, cname) 1828 } else { 1829 mapLabels[expression] = label 1830 columsArray = append(columsArray, expression) 1831 } 1832 } 1833 aggNode.OutputTransforms = &structs.OutputTransforms{OutputColumns: &structs.ColumnsRequest{IncludeColumns: columsArray}} 1834 aggNode.OutputTransforms.OutputColumns.RenameColumns = mapLabels 1835 aggNode.OutputTransforms.OutputColumns.IncludeValues = rawIncludeValues 1836 return aggNode, nil 1837 } 1838 1839 func (p *parser) callonJSONFilter2() (interface{}, error) { 1840 stack := p.vstack[len(p.vstack)-1] 1841 _ = stack 1842 return p.cur.onJSONFilter2(stack["rest"]) 1843 } 1844 1845 func (c *current) onJSONFilter14(rest interface{}) (interface{}, error) { 1846 aggNode := &structs.QueryAggregators{OutputTransforms: &structs.OutputTransforms{OutputColumns: &structs.ColumnsRequest{}}} 1847 rawIncludeValues := make([]*structs.IncludeValue, 0) 1848 for _, query := range rest.([]interface{}) { 1849 expression := strings.Trim(query.(*ast.Node).Comparison.Values.(string), "\"") 1850 newValue := &structs.IncludeValue{ColName: expression, Label: query.(*ast.Node).Comparison.Field} 1851 rawIncludeValues = append(rawIncludeValues, newValue) 1852 } 1853 aggNode.OutputTransforms.OutputColumns.IncludeValues = rawIncludeValues 1854 aggNode.OutputTransforms.OutputColumns.Logfmt = true 1855 return aggNode, nil 1856 } 1857 1858 func (p *parser) callonJSONFilter14() (interface{}, error) { 1859 stack := p.vstack[len(p.vstack)-1] 1860 _ = stack 1861 return p.cur.onJSONFilter14(stack["rest"]) 1862 } 1863 1864 func (c *current) onDuration1(val, timeUnit interface{}) (interface{}, error) { 1865 switch rawVal := val.(type) { 1866 case json.Number: 1867 interval, err := rawVal.Int64() 1868 if err != nil { 1869 return nil, fmt.Errorf("input is not an integer number") 1870 } 1871 return uint64(interval) * timeUnit.(uint64), nil 1872 default: 1873 return nil, fmt.Errorf("input is not a JSON number") 1874 } 1875 } 1876 1877 func (p *parser) callonDuration1() (interface{}, error) { 1878 stack := p.vstack[len(p.vstack)-1] 1879 _ = stack 1880 return p.cur.onDuration1(stack["val"], stack["timeUnit"]) 1881 } 1882 1883 func (c *current) onTIME_UNIT2() (interface{}, error) { 1884 return uint64(1), nil 1885 } 1886 1887 func (p *parser) callonTIME_UNIT2() (interface{}, error) { 1888 stack := p.vstack[len(p.vstack)-1] 1889 _ = stack 1890 return p.cur.onTIME_UNIT2() 1891 } 1892 1893 func (c *current) onTIME_UNIT4() (interface{}, error) { 1894 return uint64(1000), nil 1895 } 1896 1897 func (p *parser) callonTIME_UNIT4() (interface{}, error) { 1898 stack := p.vstack[len(p.vstack)-1] 1899 _ = stack 1900 return p.cur.onTIME_UNIT4() 1901 } 1902 1903 func (c *current) onTIME_UNIT6() (interface{}, error) { 1904 return uint64(60000), nil 1905 } 1906 1907 func (p *parser) callonTIME_UNIT6() (interface{}, error) { 1908 stack := p.vstack[len(p.vstack)-1] 1909 _ = stack 1910 return p.cur.onTIME_UNIT6() 1911 } 1912 1913 func (c *current) onTIME_UNIT8() (interface{}, error) { 1914 return uint64(60 * 60000), nil 1915 } 1916 1917 func (p *parser) callonTIME_UNIT8() (interface{}, error) { 1918 stack := p.vstack[len(p.vstack)-1] 1919 _ = stack 1920 return p.cur.onTIME_UNIT8() 1921 } 1922 1923 func (c *current) onTIME_UNIT10() (interface{}, error) { 1924 return uint64(24 * 60 * 60000), nil 1925 } 1926 1927 func (p *parser) callonTIME_UNIT10() (interface{}, error) { 1928 stack := p.vstack[len(p.vstack)-1] 1929 _ = stack 1930 return p.cur.onTIME_UNIT10() 1931 } 1932 1933 func (c *current) onTIME_UNIT12() (interface{}, error) { 1934 return uint64(7 * 24 * 60 * 60000), nil 1935 } 1936 1937 func (p *parser) callonTIME_UNIT12() (interface{}, error) { 1938 stack := p.vstack[len(p.vstack)-1] 1939 _ = stack 1940 return p.cur.onTIME_UNIT12() 1941 } 1942 1943 func (c *current) onTIME_UNIT14() (interface{}, error) { 1944 return uint64(365 * 24 * 60 * 60000), nil 1945 } 1946 1947 func (p *parser) callonTIME_UNIT14() (interface{}, error) { 1948 stack := p.vstack[len(p.vstack)-1] 1949 _ = stack 1950 return p.cur.onTIME_UNIT14() 1951 } 1952 1953 func (c *current) onopCOMP3() (interface{}, error) { 1954 return "=", nil 1955 } 1956 1957 func (p *parser) callonopCOMP3() (interface{}, error) { 1958 stack := p.vstack[len(p.vstack)-1] 1959 _ = stack 1960 return p.cur.onopCOMP3() 1961 } 1962 1963 func (c *current) onopCOMP5() (interface{}, error) { 1964 return "=", nil 1965 } 1966 1967 func (p *parser) callonopCOMP5() (interface{}, error) { 1968 stack := p.vstack[len(p.vstack)-1] 1969 _ = stack 1970 return p.cur.onopCOMP5() 1971 } 1972 1973 func (c *current) onopCOMP7() (interface{}, error) { 1974 return string(c.text), nil 1975 } 1976 1977 func (p *parser) callonopCOMP7() (interface{}, error) { 1978 stack := p.vstack[len(p.vstack)-1] 1979 _ = stack 1980 return p.cur.onopCOMP7() 1981 } 1982 1983 func (c *current) onopCOMP9() (interface{}, error) { 1984 return string(c.text), nil 1985 } 1986 1987 func (p *parser) callonopCOMP9() (interface{}, error) { 1988 stack := p.vstack[len(p.vstack)-1] 1989 _ = stack 1990 return p.cur.onopCOMP9() 1991 } 1992 1993 func (c *current) onopCOMP11() (interface{}, error) { 1994 return string(c.text), nil 1995 } 1996 1997 func (p *parser) callonopCOMP11() (interface{}, error) { 1998 stack := p.vstack[len(p.vstack)-1] 1999 _ = stack 2000 return p.cur.onopCOMP11() 2001 } 2002 2003 func (c *current) onopCOMP13() (interface{}, error) { 2004 return string(c.text), nil 2005 } 2006 2007 func (p *parser) callonopCOMP13() (interface{}, error) { 2008 stack := p.vstack[len(p.vstack)-1] 2009 _ = stack 2010 return p.cur.onopCOMP13() 2011 } 2012 2013 func (c *current) onopCOMP15() (interface{}, error) { 2014 return string(c.text), nil 2015 } 2016 2017 func (p *parser) callonopCOMP15() (interface{}, error) { 2018 stack := p.vstack[len(p.vstack)-1] 2019 _ = stack 2020 return p.cur.onopCOMP15() 2021 } 2022 2023 func (c *current) onopCOMP17() (interface{}, error) { 2024 return string(c.text), nil 2025 } 2026 2027 func (p *parser) callonopCOMP17() (interface{}, error) { 2028 stack := p.vstack[len(p.vstack)-1] 2029 _ = stack 2030 return p.cur.onopCOMP17() 2031 } 2032 2033 func (c *current) onopCOMP19() (interface{}, error) { 2034 return "!=", nil 2035 } 2036 2037 func (p *parser) callonopCOMP19() (interface{}, error) { 2038 stack := p.vstack[len(p.vstack)-1] 2039 _ = stack 2040 return p.cur.onopCOMP19() 2041 } 2042 2043 func (c *current) onGrepFilter2() (interface{}, error) { 2044 return "=", nil 2045 } 2046 2047 func (p *parser) callonGrepFilter2() (interface{}, error) { 2048 stack := p.vstack[len(p.vstack)-1] 2049 _ = stack 2050 return p.cur.onGrepFilter2() 2051 } 2052 2053 func (c *current) onGrepFilter4() (interface{}, error) { 2054 return string(c.text), nil 2055 } 2056 2057 func (p *parser) callonGrepFilter4() (interface{}, error) { 2058 stack := p.vstack[len(p.vstack)-1] 2059 _ = stack 2060 return p.cur.onGrepFilter4() 2061 } 2062 2063 func (c *current) onGrepFilter6() (interface{}, error) { 2064 return "=", nil 2065 } 2066 2067 func (p *parser) callonGrepFilter6() (interface{}, error) { 2068 stack := p.vstack[len(p.vstack)-1] 2069 _ = stack 2070 return p.cur.onGrepFilter6() 2071 } 2072 2073 func (c *current) onGrepFilter8() (interface{}, error) { 2074 return "!=", nil 2075 } 2076 2077 func (p *parser) callonGrepFilter8() (interface{}, error) { 2078 stack := p.vstack[len(p.vstack)-1] 2079 _ = stack 2080 return p.cur.onGrepFilter8() 2081 } 2082 2083 func (c *current) onopCustom1(opname interface{}) (interface{}, error) { 2084 return ast.OpNameToString(c.text) 2085 } 2086 2087 func (p *parser) callonopCustom1() (interface{}, error) { 2088 stack := p.vstack[len(p.vstack)-1] 2089 _ = stack 2090 return p.cur.onopCustom1(stack["opname"]) 2091 } 2092 2093 func (c *current) onLetOpr2() (interface{}, error) { 2094 return utils.LetGreaterThanOrEqualTo, nil 2095 } 2096 2097 func (p *parser) callonLetOpr2() (interface{}, error) { 2098 stack := p.vstack[len(p.vstack)-1] 2099 _ = stack 2100 return p.cur.onLetOpr2() 2101 } 2102 2103 func (c *current) onLetOpr6() (interface{}, error) { 2104 return utils.LetGreaterThan, nil 2105 } 2106 2107 func (p *parser) callonLetOpr6() (interface{}, error) { 2108 stack := p.vstack[len(p.vstack)-1] 2109 _ = stack 2110 return p.cur.onLetOpr6() 2111 } 2112 2113 func (c *current) onLetOpr8() (interface{}, error) { 2114 return utils.LetLessThanOrEqualTo, nil 2115 } 2116 2117 func (p *parser) callonLetOpr8() (interface{}, error) { 2118 stack := p.vstack[len(p.vstack)-1] 2119 _ = stack 2120 return p.cur.onLetOpr8() 2121 } 2122 2123 func (c *current) onLetOpr12() (interface{}, error) { 2124 return utils.LetLessThan, nil 2125 } 2126 2127 func (p *parser) callonLetOpr12() (interface{}, error) { 2128 stack := p.vstack[len(p.vstack)-1] 2129 _ = stack 2130 return p.cur.onLetOpr12() 2131 } 2132 2133 func (c *current) onLetOpr14() (interface{}, error) { 2134 return utils.LetEquals, nil 2135 } 2136 2137 func (p *parser) callonLetOpr14() (interface{}, error) { 2138 stack := p.vstack[len(p.vstack)-1] 2139 _ = stack 2140 return p.cur.onLetOpr14() 2141 } 2142 2143 func (c *current) onLetOpr18() (interface{}, error) { 2144 return utils.LetNotEquals, nil 2145 } 2146 2147 func (p *parser) callonLetOpr18() (interface{}, error) { 2148 stack := p.vstack[len(p.vstack)-1] 2149 _ = stack 2150 return p.cur.onLetOpr18() 2151 } 2152 2153 func (c *current) onLetOpr22() (interface{}, error) { 2154 return utils.LetAdd, nil 2155 } 2156 2157 func (p *parser) callonLetOpr22() (interface{}, error) { 2158 stack := p.vstack[len(p.vstack)-1] 2159 _ = stack 2160 return p.cur.onLetOpr22() 2161 } 2162 2163 func (c *current) onLetOpr24() (interface{}, error) { 2164 return utils.LetSubtract, nil 2165 } 2166 2167 func (p *parser) callonLetOpr24() (interface{}, error) { 2168 stack := p.vstack[len(p.vstack)-1] 2169 _ = stack 2170 return p.cur.onLetOpr24() 2171 } 2172 2173 func (c *current) onLetOpr26() (interface{}, error) { 2174 return utils.LetDivide, nil 2175 } 2176 2177 func (p *parser) callonLetOpr26() (interface{}, error) { 2178 stack := p.vstack[len(p.vstack)-1] 2179 _ = stack 2180 return p.cur.onLetOpr26() 2181 } 2182 2183 func (c *current) onLetOpr28() (interface{}, error) { 2184 return utils.LetMultiply, nil 2185 } 2186 2187 func (p *parser) callonLetOpr28() (interface{}, error) { 2188 stack := p.vstack[len(p.vstack)-1] 2189 _ = stack 2190 return p.cur.onLetOpr28() 2191 } 2192 2193 func (c *current) onLetOpr30() (interface{}, error) { 2194 return utils.LetModulo, nil 2195 } 2196 2197 func (p *parser) callonLetOpr30() (interface{}, error) { 2198 stack := p.vstack[len(p.vstack)-1] 2199 _ = stack 2200 return p.cur.onLetOpr30() 2201 } 2202 2203 func (c *current) onLetIdentifier2() (interface{}, error) { 2204 var dte utils.DtypeEnclosure 2205 dte.Dtype = utils.SS_DT_FLOAT 2206 dte.FloatVal, _ = strconv.ParseFloat(string(c.text), 64) 2207 dte.StringVal = string(c.text) 2208 return &dte, nil 2209 } 2210 2211 func (p *parser) callonLetIdentifier2() (interface{}, error) { 2212 stack := p.vstack[len(p.vstack)-1] 2213 _ = stack 2214 return p.cur.onLetIdentifier2() 2215 } 2216 2217 func (c *current) onLetIdentifier7() (interface{}, error) { 2218 var dte utils.DtypeEnclosure 2219 dte.Dtype = utils.SS_DT_SIGNED_NUM 2220 dte.SignedVal, _ = strconv.ParseInt(string(c.text), 10, 64) 2221 dte.StringVal = string(c.text) 2222 return &dte, nil 2223 } 2224 2225 func (p *parser) callonLetIdentifier7() (interface{}, error) { 2226 stack := p.vstack[len(p.vstack)-1] 2227 _ = stack 2228 return p.cur.onLetIdentifier7() 2229 } 2230 2231 func (c *current) onLetIdentifier12() (interface{}, error) { 2232 var dte utils.DtypeEnclosure 2233 dte.Dtype = utils.SS_DT_UNSIGNED_NUM 2234 dte.UnsignedVal, _ = strconv.ParseUint(string(c.text), 10, 64) 2235 dte.StringVal = string(c.text) 2236 return &dte, nil 2237 } 2238 2239 func (p *parser) callonLetIdentifier12() (interface{}, error) { 2240 stack := p.vstack[len(p.vstack)-1] 2241 _ = stack 2242 return p.cur.onLetIdentifier12() 2243 } 2244 2245 func (c *current) onLetIdentifier14() (interface{}, error) { 2246 var dte utils.DtypeEnclosure 2247 dte.Dtype = utils.SS_DT_STRING 2248 dte.BoolVal = 1 2249 dte.StringVal = string(c.text) 2250 return &dte, nil 2251 } 2252 2253 func (p *parser) callonLetIdentifier14() (interface{}, error) { 2254 stack := p.vstack[len(p.vstack)-1] 2255 _ = stack 2256 return p.cur.onLetIdentifier14() 2257 } 2258 2259 func (c *current) onField3(pieces interface{}) (interface{}, error) { 2260 if pieces == nil { 2261 return "*", nil 2262 } 2263 2264 return string(c.text), nil 2265 } 2266 2267 func (p *parser) callonField3() (interface{}, error) { 2268 stack := p.vstack[len(p.vstack)-1] 2269 _ = stack 2270 return p.cur.onField3(stack["pieces"]) 2271 } 2272 2273 func (c *current) onIdentifier1() (interface{}, error) { 2274 2275 return string(c.text), nil 2276 } 2277 2278 func (p *parser) callonIdentifier1() (interface{}, error) { 2279 stack := p.vstack[len(p.vstack)-1] 2280 _ = stack 2281 return p.cur.onIdentifier1() 2282 } 2283 2284 func (c *current) onValue1(val interface{}) (interface{}, error) { 2285 return ast.MakeValue(val) 2286 2287 } 2288 2289 func (p *parser) callonValue1() (interface{}, error) { 2290 stack := p.vstack[len(p.vstack)-1] 2291 _ = stack 2292 return p.cur.onValue1(stack["val"]) 2293 } 2294 2295 func (c *current) onInteger1() (interface{}, error) { 2296 return json.Number(string(c.text)), nil 2297 2298 } 2299 2300 func (p *parser) callonInteger1() (interface{}, error) { 2301 stack := p.vstack[len(p.vstack)-1] 2302 _ = stack 2303 return p.cur.onInteger1() 2304 } 2305 2306 func (c *current) onFloat1() (interface{}, error) { 2307 return json.Number(string(c.text)), nil 2308 2309 } 2310 2311 func (p *parser) callonFloat1() (interface{}, error) { 2312 stack := p.vstack[len(p.vstack)-1] 2313 _ = stack 2314 return p.cur.onFloat1() 2315 } 2316 2317 func (c *current) onUnquotedFieldPiece1() (interface{}, error) { 2318 return string(c.text), nil 2319 } 2320 2321 func (p *parser) callonUnquotedFieldPiece1() (interface{}, error) { 2322 stack := p.vstack[len(p.vstack)-1] 2323 _ = stack 2324 return p.cur.onUnquotedFieldPiece1() 2325 } 2326 2327 func (c *current) onStar1() (interface{}, error) { 2328 return "*", nil 2329 } 2330 2331 func (p *parser) callonStar1() (interface{}, error) { 2332 stack := p.vstack[len(p.vstack)-1] 2333 _ = stack 2334 return p.cur.onStar1() 2335 } 2336 2337 func (c *current) onQuotedValue1() (interface{}, error) { 2338 c.text = bytes.Replace(c.text, []byte(`\/`), []byte(`/`), -1) 2339 return strconv.Unquote(string(c.text)) 2340 } 2341 2342 func (p *parser) callonQuotedValue1() (interface{}, error) { 2343 stack := p.vstack[len(p.vstack)-1] 2344 _ = stack 2345 return p.cur.onQuotedValue1() 2346 } 2347 2348 var ( 2349 // errNoRule is returned when the grammar to parse has no rule. 2350 errNoRule = errors.New("grammar has no rule") 2351 2352 // errInvalidEntrypoint is returned when the specified entrypoint rule 2353 // does not exit. 2354 errInvalidEntrypoint = errors.New("invalid entrypoint") 2355 2356 // errInvalidEncoding is returned when the source is not properly 2357 // utf8-encoded. 2358 errInvalidEncoding = errors.New("invalid encoding") 2359 2360 // errMaxExprCnt is used to signal that the maximum number of 2361 // expressions have been parsed. 2362 errMaxExprCnt = errors.New("max number of expresssions parsed") 2363 ) 2364 2365 // Option is a function that can set an option on the parser. It returns 2366 // the previous setting as an Option. 2367 type Option func(*parser) Option 2368 2369 // MaxExpressions creates an Option to stop parsing after the provided 2370 // number of expressions have been parsed, if the value is 0 then the parser will 2371 // parse for as many steps as needed (possibly an infinite number). 2372 // 2373 // The default for maxExprCnt is 0. 2374 func MaxExpressions(maxExprCnt uint64) Option { 2375 return func(p *parser) Option { 2376 oldMaxExprCnt := p.maxExprCnt 2377 p.maxExprCnt = maxExprCnt 2378 return MaxExpressions(oldMaxExprCnt) 2379 } 2380 } 2381 2382 // Entrypoint creates an Option to set the rule name to use as entrypoint. 2383 // The rule name must have been specified in the -alternate-entrypoints 2384 // if generating the parser with the -optimize-grammar flag, otherwise 2385 // it may have been optimized out. Passing an empty string sets the 2386 // entrypoint to the first rule in the grammar. 2387 // 2388 // The default is to start parsing at the first rule in the grammar. 2389 func Entrypoint(ruleName string) Option { 2390 return func(p *parser) Option { 2391 oldEntrypoint := p.entrypoint 2392 p.entrypoint = ruleName 2393 if ruleName == "" { 2394 p.entrypoint = g.rules[0].name 2395 } 2396 return Entrypoint(oldEntrypoint) 2397 } 2398 } 2399 2400 // Statistics adds a user provided Stats struct to the parser to allow 2401 // the user to process the results after the parsing has finished. 2402 // Also the key for the "no match" counter is set. 2403 // 2404 // Example usage: 2405 // 2406 // input := "input" 2407 // stats := Stats{} 2408 // _, err := Parse("input-file", []byte(input), Statistics(&stats, "no match")) 2409 // if err != nil { 2410 // log.Panicln(err) 2411 // } 2412 // b, err := json.MarshalIndent(stats.ChoiceAltCnt, "", " ") 2413 // if err != nil { 2414 // log.Panicln(err) 2415 // } 2416 // fmt.Println(string(b)) 2417 func Statistics(stats *Stats, choiceNoMatch string) Option { 2418 return func(p *parser) Option { 2419 oldStats := p.Stats 2420 p.Stats = stats 2421 oldChoiceNoMatch := p.choiceNoMatch 2422 p.choiceNoMatch = choiceNoMatch 2423 if p.Stats.ChoiceAltCnt == nil { 2424 p.Stats.ChoiceAltCnt = make(map[string]map[string]int) 2425 } 2426 return Statistics(oldStats, oldChoiceNoMatch) 2427 } 2428 } 2429 2430 // Debug creates an Option to set the debug flag to b. When set to true, 2431 // debugging information is printed to stdout while parsing. 2432 // 2433 // The default is false. 2434 func Debug(b bool) Option { 2435 return func(p *parser) Option { 2436 old := p.debug 2437 p.debug = b 2438 return Debug(old) 2439 } 2440 } 2441 2442 // Memoize creates an Option to set the memoize flag to b. When set to true, 2443 // the parser will cache all results so each expression is evaluated only 2444 // once. This guarantees linear parsing time even for pathological cases, 2445 // at the expense of more memory and slower times for typical cases. 2446 // 2447 // The default is false. 2448 func Memoize(b bool) Option { 2449 return func(p *parser) Option { 2450 old := p.memoize 2451 p.memoize = b 2452 return Memoize(old) 2453 } 2454 } 2455 2456 // AllowInvalidUTF8 creates an Option to allow invalid UTF-8 bytes. 2457 // Every invalid UTF-8 byte is treated as a utf8.RuneError (U+FFFD) 2458 // by character class matchers and is matched by the any matcher. 2459 // The returned matched value, c.text and c.offset are NOT affected. 2460 // 2461 // The default is false. 2462 func AllowInvalidUTF8(b bool) Option { 2463 return func(p *parser) Option { 2464 old := p.allowInvalidUTF8 2465 p.allowInvalidUTF8 = b 2466 return AllowInvalidUTF8(old) 2467 } 2468 } 2469 2470 // Recover creates an Option to set the recover flag to b. When set to 2471 // true, this causes the parser to recover from panics and convert it 2472 // to an error. Setting it to false can be useful while debugging to 2473 // access the full stack trace. 2474 // 2475 // The default is true. 2476 func Recover(b bool) Option { 2477 return func(p *parser) Option { 2478 old := p.recover 2479 p.recover = b 2480 return Recover(old) 2481 } 2482 } 2483 2484 // GlobalStore creates an Option to set a key to a certain value in 2485 // the globalStore. 2486 func GlobalStore(key string, value interface{}) Option { 2487 return func(p *parser) Option { 2488 old := p.cur.globalStore[key] 2489 p.cur.globalStore[key] = value 2490 return GlobalStore(key, old) 2491 } 2492 } 2493 2494 // InitState creates an Option to set a key to a certain value in 2495 // the global "state" store. 2496 func InitState(key string, value interface{}) Option { 2497 return func(p *parser) Option { 2498 old := p.cur.state[key] 2499 p.cur.state[key] = value 2500 return InitState(key, old) 2501 } 2502 } 2503 2504 // ParseFile parses the file identified by filename. 2505 func ParseFile(filename string, opts ...Option) (i interface{}, err error) { 2506 f, err := os.Open(filename) 2507 if err != nil { 2508 return nil, err 2509 } 2510 defer func() { 2511 if closeErr := f.Close(); closeErr != nil { 2512 err = closeErr 2513 } 2514 }() 2515 return ParseReader(filename, f, opts...) 2516 } 2517 2518 // ParseReader parses the data from r using filename as information in the 2519 // error messages. 2520 func ParseReader(filename string, r io.Reader, opts ...Option) (interface{}, error) { 2521 b, err := io.ReadAll(r) 2522 if err != nil { 2523 return nil, err 2524 } 2525 2526 return Parse(filename, b, opts...) 2527 } 2528 2529 // Parse parses the data from b using filename as information in the 2530 // error messages. 2531 func Parse(filename string, b []byte, opts ...Option) (interface{}, error) { 2532 return newParser(filename, b, opts...).parse(g) 2533 } 2534 2535 // position records a position in the text. 2536 type position struct { 2537 line, col, offset int 2538 } 2539 2540 func (p position) String() string { 2541 return strconv.Itoa(p.line) + ":" + strconv.Itoa(p.col) + " [" + strconv.Itoa(p.offset) + "]" 2542 } 2543 2544 // savepoint stores all state required to go back to this point in the 2545 // parser. 2546 type savepoint struct { 2547 position 2548 rn rune 2549 w int 2550 } 2551 2552 type current struct { 2553 pos position // start position of the match 2554 text []byte // raw text of the match 2555 2556 // state is a store for arbitrary key,value pairs that the user wants to be 2557 // tied to the backtracking of the parser. 2558 // This is always rolled back if a parsing rule fails. 2559 state storeDict 2560 2561 // globalStore is a general store for the user to store arbitrary key-value 2562 // pairs that they need to manage and that they do not want tied to the 2563 // backtracking of the parser. This is only modified by the user and never 2564 // rolled back by the parser. It is always up to the user to keep this in a 2565 // consistent state. 2566 globalStore storeDict 2567 } 2568 2569 type storeDict map[string]interface{} 2570 2571 // the AST types... 2572 2573 type grammar struct { 2574 pos position 2575 rules []*rule 2576 } 2577 2578 type rule struct { 2579 pos position 2580 name string 2581 displayName string 2582 expr interface{} 2583 } 2584 2585 type choiceExpr struct { 2586 pos position 2587 alternatives []interface{} 2588 } 2589 2590 type actionExpr struct { 2591 pos position 2592 expr interface{} 2593 run func(*parser) (interface{}, error) 2594 } 2595 2596 type recoveryExpr struct { 2597 pos position 2598 expr interface{} 2599 recoverExpr interface{} 2600 failureLabel []string 2601 } 2602 2603 type seqExpr struct { 2604 pos position 2605 exprs []interface{} 2606 } 2607 2608 type throwExpr struct { 2609 pos position 2610 label string 2611 } 2612 2613 type labeledExpr struct { 2614 pos position 2615 label string 2616 expr interface{} 2617 } 2618 2619 type expr struct { 2620 pos position 2621 expr interface{} 2622 } 2623 2624 type andExpr expr 2625 type notExpr expr 2626 type zeroOrOneExpr expr 2627 type zeroOrMoreExpr expr 2628 type oneOrMoreExpr expr 2629 2630 type ruleRefExpr struct { 2631 pos position 2632 name string 2633 } 2634 2635 type stateCodeExpr struct { 2636 pos position 2637 run func(*parser) error 2638 } 2639 2640 type andCodeExpr struct { 2641 pos position 2642 run func(*parser) (bool, error) 2643 } 2644 2645 type notCodeExpr struct { 2646 pos position 2647 run func(*parser) (bool, error) 2648 } 2649 2650 type litMatcher struct { 2651 pos position 2652 val string 2653 ignoreCase bool 2654 want string 2655 } 2656 2657 type charClassMatcher struct { 2658 pos position 2659 val string 2660 basicLatinChars [128]bool 2661 chars []rune 2662 ranges []rune 2663 classes []*unicode.RangeTable 2664 ignoreCase bool 2665 inverted bool 2666 } 2667 2668 type anyMatcher position 2669 2670 // errList cumulates the errors found by the parser. 2671 type errList []error 2672 2673 func (e *errList) add(err error) { 2674 *e = append(*e, err) 2675 } 2676 2677 func (e errList) err() error { 2678 if len(e) == 0 { 2679 return nil 2680 } 2681 e.dedupe() 2682 return e 2683 } 2684 2685 func (e *errList) dedupe() { 2686 var cleaned []error 2687 set := make(map[string]bool) 2688 for _, err := range *e { 2689 if msg := err.Error(); !set[msg] { 2690 set[msg] = true 2691 cleaned = append(cleaned, err) 2692 } 2693 } 2694 *e = cleaned 2695 } 2696 2697 func (e errList) Error() string { 2698 switch len(e) { 2699 case 0: 2700 return "" 2701 case 1: 2702 return e[0].Error() 2703 default: 2704 var buf bytes.Buffer 2705 2706 for i, err := range e { 2707 if i > 0 { 2708 buf.WriteRune('\n') 2709 } 2710 buf.WriteString(err.Error()) 2711 } 2712 return buf.String() 2713 } 2714 } 2715 2716 // parserError wraps an error with a prefix indicating the rule in which 2717 // the error occurred. The original error is stored in the Inner field. 2718 type parserError struct { 2719 Inner error 2720 pos position 2721 prefix string 2722 expected []string 2723 } 2724 2725 // Error returns the error message. 2726 func (p *parserError) Error() string { 2727 return p.prefix + ": " + p.Inner.Error() 2728 } 2729 2730 // newParser creates a parser with the specified input source and options. 2731 func newParser(filename string, b []byte, opts ...Option) *parser { 2732 stats := Stats{ 2733 ChoiceAltCnt: make(map[string]map[string]int), 2734 } 2735 2736 p := &parser{ 2737 filename: filename, 2738 errs: new(errList), 2739 data: b, 2740 pt: savepoint{position: position{line: 1}}, 2741 recover: true, 2742 cur: current{ 2743 state: make(storeDict), 2744 globalStore: make(storeDict), 2745 }, 2746 maxFailPos: position{col: 1, line: 1}, 2747 maxFailExpected: make([]string, 0, 20), 2748 Stats: &stats, 2749 // start rule is rule [0] unless an alternate entrypoint is specified 2750 entrypoint: g.rules[0].name, 2751 } 2752 p.setOptions(opts) 2753 2754 if p.maxExprCnt == 0 { 2755 p.maxExprCnt = math.MaxUint64 2756 } 2757 2758 return p 2759 } 2760 2761 // setOptions applies the options to the parser. 2762 func (p *parser) setOptions(opts []Option) { 2763 for _, opt := range opts { 2764 opt(p) 2765 } 2766 } 2767 2768 type resultTuple struct { 2769 v interface{} 2770 b bool 2771 end savepoint 2772 } 2773 2774 const choiceNoMatch = -1 2775 2776 // Stats stores some statistics, gathered during parsing 2777 type Stats struct { 2778 // ExprCnt counts the number of expressions processed during parsing 2779 // This value is compared to the maximum number of expressions allowed 2780 // (set by the MaxExpressions option). 2781 ExprCnt uint64 2782 2783 // ChoiceAltCnt is used to count for each ordered choice expression, 2784 // which alternative is used how may times. 2785 // These numbers allow to optimize the order of the ordered choice expression 2786 // to increase the performance of the parser 2787 // 2788 // The outer key of ChoiceAltCnt is composed of the name of the rule as well 2789 // as the line and the column of the ordered choice. 2790 // The inner key of ChoiceAltCnt is the number (one-based) of the matching alternative. 2791 // For each alternative the number of matches are counted. If an ordered choice does not 2792 // match, a special counter is incremented. The name of this counter is set with 2793 // the parser option Statistics. 2794 // For an alternative to be included in ChoiceAltCnt, it has to match at least once. 2795 ChoiceAltCnt map[string]map[string]int 2796 } 2797 2798 type parser struct { 2799 filename string 2800 pt savepoint 2801 cur current 2802 2803 data []byte 2804 errs *errList 2805 2806 depth int 2807 recover bool 2808 debug bool 2809 2810 memoize bool 2811 // memoization table for the packrat algorithm: 2812 // map[offset in source] map[expression or rule] {value, match} 2813 memo map[int]map[interface{}]resultTuple 2814 2815 // rules table, maps the rule identifier to the rule node 2816 rules map[string]*rule 2817 // variables stack, map of label to value 2818 vstack []map[string]interface{} 2819 // rule stack, allows identification of the current rule in errors 2820 rstack []*rule 2821 2822 // parse fail 2823 maxFailPos position 2824 maxFailExpected []string 2825 maxFailInvertExpected bool 2826 2827 // max number of expressions to be parsed 2828 maxExprCnt uint64 2829 // entrypoint for the parser 2830 entrypoint string 2831 2832 allowInvalidUTF8 bool 2833 2834 *Stats 2835 2836 choiceNoMatch string 2837 // recovery expression stack, keeps track of the currently available recovery expression, these are traversed in reverse 2838 recoveryStack []map[string]interface{} 2839 } 2840 2841 // push a variable set on the vstack. 2842 func (p *parser) pushV() { 2843 if cap(p.vstack) == len(p.vstack) { 2844 // create new empty slot in the stack 2845 p.vstack = append(p.vstack, nil) 2846 } else { 2847 // slice to 1 more 2848 p.vstack = p.vstack[:len(p.vstack)+1] 2849 } 2850 2851 // get the last args set 2852 m := p.vstack[len(p.vstack)-1] 2853 if m != nil && len(m) == 0 { 2854 // empty map, all good 2855 return 2856 } 2857 2858 m = make(map[string]interface{}) 2859 p.vstack[len(p.vstack)-1] = m 2860 } 2861 2862 // pop a variable set from the vstack. 2863 func (p *parser) popV() { 2864 // if the map is not empty, clear it 2865 m := p.vstack[len(p.vstack)-1] 2866 if len(m) > 0 { 2867 // GC that map 2868 p.vstack[len(p.vstack)-1] = nil 2869 } 2870 p.vstack = p.vstack[:len(p.vstack)-1] 2871 } 2872 2873 // push a recovery expression with its labels to the recoveryStack 2874 func (p *parser) pushRecovery(labels []string, expr interface{}) { 2875 if cap(p.recoveryStack) == len(p.recoveryStack) { 2876 // create new empty slot in the stack 2877 p.recoveryStack = append(p.recoveryStack, nil) 2878 } else { 2879 // slice to 1 more 2880 p.recoveryStack = p.recoveryStack[:len(p.recoveryStack)+1] 2881 } 2882 2883 m := make(map[string]interface{}, len(labels)) 2884 for _, fl := range labels { 2885 m[fl] = expr 2886 } 2887 p.recoveryStack[len(p.recoveryStack)-1] = m 2888 } 2889 2890 // pop a recovery expression from the recoveryStack 2891 func (p *parser) popRecovery() { 2892 // GC that map 2893 p.recoveryStack[len(p.recoveryStack)-1] = nil 2894 2895 p.recoveryStack = p.recoveryStack[:len(p.recoveryStack)-1] 2896 } 2897 2898 func (p *parser) print(prefix, s string) string { 2899 if !p.debug { 2900 return s 2901 } 2902 2903 fmt.Printf("%s %d:%d:%d: %s [%#U]\n", 2904 prefix, p.pt.line, p.pt.col, p.pt.offset, s, p.pt.rn) 2905 return s 2906 } 2907 2908 func (p *parser) in(s string) string { 2909 p.depth++ 2910 return p.print(strings.Repeat(" ", p.depth)+">", s) 2911 } 2912 2913 func (p *parser) out(s string) string { 2914 p.depth-- 2915 return p.print(strings.Repeat(" ", p.depth)+"<", s) 2916 } 2917 2918 func (p *parser) addErr(err error) { 2919 p.addErrAt(err, p.pt.position, []string{}) 2920 } 2921 2922 func (p *parser) addErrAt(err error, pos position, expected []string) { 2923 var buf bytes.Buffer 2924 if p.filename != "" { 2925 buf.WriteString(p.filename) 2926 } 2927 if buf.Len() > 0 { 2928 buf.WriteString(":") 2929 } 2930 buf.WriteString(fmt.Sprintf("%d:%d (%d)", pos.line, pos.col, pos.offset)) 2931 if len(p.rstack) > 0 { 2932 if buf.Len() > 0 { 2933 buf.WriteString(": ") 2934 } 2935 rule := p.rstack[len(p.rstack)-1] 2936 if rule.displayName != "" { 2937 buf.WriteString("rule " + rule.displayName) 2938 } else { 2939 buf.WriteString("rule " + rule.name) 2940 } 2941 } 2942 pe := &parserError{Inner: err, pos: pos, prefix: buf.String(), expected: expected} 2943 p.errs.add(pe) 2944 } 2945 2946 func (p *parser) failAt(fail bool, pos position, want string) { 2947 // process fail if parsing fails and not inverted or parsing succeeds and invert is set 2948 if fail == p.maxFailInvertExpected { 2949 if pos.offset < p.maxFailPos.offset { 2950 return 2951 } 2952 2953 if pos.offset > p.maxFailPos.offset { 2954 p.maxFailPos = pos 2955 p.maxFailExpected = p.maxFailExpected[:0] 2956 } 2957 2958 if p.maxFailInvertExpected { 2959 want = "!" + want 2960 } 2961 p.maxFailExpected = append(p.maxFailExpected, want) 2962 } 2963 } 2964 2965 // read advances the parser to the next rune. 2966 func (p *parser) read() { 2967 p.pt.offset += p.pt.w 2968 rn, n := utf8.DecodeRune(p.data[p.pt.offset:]) 2969 p.pt.rn = rn 2970 p.pt.w = n 2971 p.pt.col++ 2972 if rn == '\n' { 2973 p.pt.line++ 2974 p.pt.col = 0 2975 } 2976 2977 if rn == utf8.RuneError && n == 1 { // see utf8.DecodeRune 2978 if !p.allowInvalidUTF8 { 2979 p.addErr(errInvalidEncoding) 2980 } 2981 } 2982 } 2983 2984 // restore parser position to the savepoint pt. 2985 func (p *parser) restore(pt savepoint) { 2986 if p.debug { 2987 defer p.out(p.in("restore")) 2988 } 2989 if pt.offset == p.pt.offset { 2990 return 2991 } 2992 p.pt = pt 2993 } 2994 2995 // Cloner is implemented by any value that has a Clone method, which returns a 2996 // copy of the value. This is mainly used for types which are not passed by 2997 // value (e.g map, slice, chan) or structs that contain such types. 2998 // 2999 // This is used in conjunction with the global state feature to create proper 3000 // copies of the state to allow the parser to properly restore the state in 3001 // the case of backtracking. 3002 type Cloner interface { 3003 Clone() interface{} 3004 } 3005 3006 var statePool = &sync.Pool{ 3007 New: func() interface{} { return make(storeDict) }, 3008 } 3009 3010 func (sd storeDict) Discard() { 3011 for k := range sd { 3012 delete(sd, k) 3013 } 3014 statePool.Put(sd) 3015 } 3016 3017 // clone and return parser current state. 3018 func (p *parser) cloneState() storeDict { 3019 if p.debug { 3020 defer p.out(p.in("cloneState")) 3021 } 3022 3023 state := statePool.Get().(storeDict) 3024 for k, v := range p.cur.state { 3025 if c, ok := v.(Cloner); ok { 3026 state[k] = c.Clone() 3027 } else { 3028 state[k] = v 3029 } 3030 } 3031 return state 3032 } 3033 3034 // restore parser current state to the state storeDict. 3035 // every restoreState should applied only one time for every cloned state 3036 func (p *parser) restoreState(state storeDict) { 3037 if p.debug { 3038 defer p.out(p.in("restoreState")) 3039 } 3040 p.cur.state.Discard() 3041 p.cur.state = state 3042 } 3043 3044 // get the slice of bytes from the savepoint start to the current position. 3045 func (p *parser) sliceFrom(start savepoint) []byte { 3046 return p.data[start.position.offset:p.pt.position.offset] 3047 } 3048 3049 func (p *parser) getMemoized(node interface{}) (resultTuple, bool) { 3050 if len(p.memo) == 0 { 3051 return resultTuple{}, false 3052 } 3053 m := p.memo[p.pt.offset] 3054 if len(m) == 0 { 3055 return resultTuple{}, false 3056 } 3057 res, ok := m[node] 3058 return res, ok 3059 } 3060 3061 func (p *parser) setMemoized(pt savepoint, node interface{}, tuple resultTuple) { 3062 if p.memo == nil { 3063 p.memo = make(map[int]map[interface{}]resultTuple) 3064 } 3065 m := p.memo[pt.offset] 3066 if m == nil { 3067 m = make(map[interface{}]resultTuple) 3068 p.memo[pt.offset] = m 3069 } 3070 m[node] = tuple 3071 } 3072 3073 func (p *parser) buildRulesTable(g *grammar) { 3074 p.rules = make(map[string]*rule, len(g.rules)) 3075 for _, r := range g.rules { 3076 p.rules[r.name] = r 3077 } 3078 } 3079 3080 func (p *parser) parse(g *grammar) (val interface{}, err error) { 3081 if len(g.rules) == 0 { 3082 p.addErr(errNoRule) 3083 return nil, p.errs.err() 3084 } 3085 3086 // TODO : not super critical but this could be generated 3087 p.buildRulesTable(g) 3088 3089 if p.recover { 3090 // panic can be used in action code to stop parsing immediately 3091 // and return the panic as an error. 3092 defer func() { 3093 if e := recover(); e != nil { 3094 if p.debug { 3095 defer p.out(p.in("panic handler")) 3096 } 3097 val = nil 3098 switch e := e.(type) { 3099 case error: 3100 p.addErr(e) 3101 default: 3102 p.addErr(fmt.Errorf("%v", e)) 3103 } 3104 err = p.errs.err() 3105 } 3106 }() 3107 } 3108 3109 startRule, ok := p.rules[p.entrypoint] 3110 if !ok { 3111 p.addErr(errInvalidEntrypoint) 3112 return nil, p.errs.err() 3113 } 3114 3115 p.read() // advance to first rune 3116 val, ok = p.parseRule(startRule) 3117 if !ok { 3118 if len(*p.errs) == 0 { 3119 // If parsing fails, but no errors have been recorded, the expected values 3120 // for the farthest parser position are returned as error. 3121 maxFailExpectedMap := make(map[string]struct{}, len(p.maxFailExpected)) 3122 for _, v := range p.maxFailExpected { 3123 maxFailExpectedMap[v] = struct{}{} 3124 } 3125 expected := make([]string, 0, len(maxFailExpectedMap)) 3126 eof := false 3127 if _, ok := maxFailExpectedMap["!."]; ok { 3128 delete(maxFailExpectedMap, "!.") 3129 eof = true 3130 } 3131 for k := range maxFailExpectedMap { 3132 expected = append(expected, k) 3133 } 3134 sort.Strings(expected) 3135 if eof { 3136 expected = append(expected, "EOF") 3137 } 3138 p.addErrAt(errors.New("no match found, expected: "+listJoin(expected, ", ", "or")), p.maxFailPos, expected) 3139 } 3140 3141 return nil, p.errs.err() 3142 } 3143 return val, p.errs.err() 3144 } 3145 3146 func listJoin(list []string, sep string, lastSep string) string { 3147 switch len(list) { 3148 case 0: 3149 return "" 3150 case 1: 3151 return list[0] 3152 default: 3153 return strings.Join(list[:len(list)-1], sep) + " " + lastSep + " " + list[len(list)-1] 3154 } 3155 } 3156 3157 func (p *parser) parseRule(rule *rule) (interface{}, bool) { 3158 if p.debug { 3159 defer p.out(p.in("parseRule " + rule.name)) 3160 } 3161 3162 if p.memoize { 3163 res, ok := p.getMemoized(rule) 3164 if ok { 3165 p.restore(res.end) 3166 return res.v, res.b 3167 } 3168 } 3169 3170 start := p.pt 3171 p.rstack = append(p.rstack, rule) 3172 p.pushV() 3173 val, ok := p.parseExpr(rule.expr) 3174 p.popV() 3175 p.rstack = p.rstack[:len(p.rstack)-1] 3176 if ok && p.debug { 3177 p.print(strings.Repeat(" ", p.depth)+"MATCH", string(p.sliceFrom(start))) 3178 } 3179 3180 if p.memoize { 3181 p.setMemoized(start, rule, resultTuple{val, ok, p.pt}) 3182 } 3183 return val, ok 3184 } 3185 3186 func (p *parser) parseExpr(expr interface{}) (interface{}, bool) { 3187 var pt savepoint 3188 3189 if p.memoize { 3190 res, ok := p.getMemoized(expr) 3191 if ok { 3192 p.restore(res.end) 3193 return res.v, res.b 3194 } 3195 pt = p.pt 3196 } 3197 3198 p.ExprCnt++ 3199 if p.ExprCnt > p.maxExprCnt { 3200 panic(errMaxExprCnt) 3201 } 3202 3203 var val interface{} 3204 var ok bool 3205 switch expr := expr.(type) { 3206 case *actionExpr: 3207 val, ok = p.parseActionExpr(expr) 3208 case *andCodeExpr: 3209 val, ok = p.parseAndCodeExpr(expr) 3210 case *andExpr: 3211 val, ok = p.parseAndExpr(expr) 3212 case *anyMatcher: 3213 val, ok = p.parseAnyMatcher(expr) 3214 case *charClassMatcher: 3215 val, ok = p.parseCharClassMatcher(expr) 3216 case *choiceExpr: 3217 val, ok = p.parseChoiceExpr(expr) 3218 case *labeledExpr: 3219 val, ok = p.parseLabeledExpr(expr) 3220 case *litMatcher: 3221 val, ok = p.parseLitMatcher(expr) 3222 case *notCodeExpr: 3223 val, ok = p.parseNotCodeExpr(expr) 3224 case *notExpr: 3225 val, ok = p.parseNotExpr(expr) 3226 case *oneOrMoreExpr: 3227 val, ok = p.parseOneOrMoreExpr(expr) 3228 case *recoveryExpr: 3229 val, ok = p.parseRecoveryExpr(expr) 3230 case *ruleRefExpr: 3231 val, ok = p.parseRuleRefExpr(expr) 3232 case *seqExpr: 3233 val, ok = p.parseSeqExpr(expr) 3234 case *stateCodeExpr: 3235 val, ok = p.parseStateCodeExpr(expr) 3236 case *throwExpr: 3237 val, ok = p.parseThrowExpr(expr) 3238 case *zeroOrMoreExpr: 3239 val, ok = p.parseZeroOrMoreExpr(expr) 3240 case *zeroOrOneExpr: 3241 val, ok = p.parseZeroOrOneExpr(expr) 3242 default: 3243 panic(fmt.Sprintf("unknown expression type %T", expr)) 3244 } 3245 if p.memoize { 3246 p.setMemoized(pt, expr, resultTuple{val, ok, p.pt}) 3247 } 3248 return val, ok 3249 } 3250 3251 func (p *parser) parseActionExpr(act *actionExpr) (interface{}, bool) { 3252 if p.debug { 3253 defer p.out(p.in("parseActionExpr")) 3254 } 3255 3256 start := p.pt 3257 val, ok := p.parseExpr(act.expr) 3258 if ok { 3259 p.cur.pos = start.position 3260 p.cur.text = p.sliceFrom(start) 3261 state := p.cloneState() 3262 actVal, err := act.run(p) 3263 if err != nil { 3264 p.addErrAt(err, start.position, []string{}) 3265 } 3266 p.restoreState(state) 3267 3268 val = actVal 3269 } 3270 if ok && p.debug { 3271 p.print(strings.Repeat(" ", p.depth)+"MATCH", string(p.sliceFrom(start))) 3272 } 3273 return val, ok 3274 } 3275 3276 func (p *parser) parseAndCodeExpr(and *andCodeExpr) (interface{}, bool) { 3277 if p.debug { 3278 defer p.out(p.in("parseAndCodeExpr")) 3279 } 3280 3281 state := p.cloneState() 3282 3283 ok, err := and.run(p) 3284 if err != nil { 3285 p.addErr(err) 3286 } 3287 p.restoreState(state) 3288 3289 return nil, ok 3290 } 3291 3292 func (p *parser) parseAndExpr(and *andExpr) (interface{}, bool) { 3293 if p.debug { 3294 defer p.out(p.in("parseAndExpr")) 3295 } 3296 3297 pt := p.pt 3298 state := p.cloneState() 3299 p.pushV() 3300 _, ok := p.parseExpr(and.expr) 3301 p.popV() 3302 p.restoreState(state) 3303 p.restore(pt) 3304 3305 return nil, ok 3306 } 3307 3308 func (p *parser) parseAnyMatcher(any *anyMatcher) (interface{}, bool) { 3309 if p.debug { 3310 defer p.out(p.in("parseAnyMatcher")) 3311 } 3312 3313 if p.pt.rn == utf8.RuneError && p.pt.w == 0 { 3314 // EOF - see utf8.DecodeRune 3315 p.failAt(false, p.pt.position, ".") 3316 return nil, false 3317 } 3318 start := p.pt 3319 p.read() 3320 p.failAt(true, start.position, ".") 3321 return p.sliceFrom(start), true 3322 } 3323 3324 func (p *parser) parseCharClassMatcher(chr *charClassMatcher) (interface{}, bool) { 3325 if p.debug { 3326 defer p.out(p.in("parseCharClassMatcher")) 3327 } 3328 3329 cur := p.pt.rn 3330 start := p.pt 3331 3332 // can't match EOF 3333 if cur == utf8.RuneError && p.pt.w == 0 { // see utf8.DecodeRune 3334 p.failAt(false, start.position, chr.val) 3335 return nil, false 3336 } 3337 3338 if chr.ignoreCase { 3339 cur = unicode.ToLower(cur) 3340 } 3341 3342 // try to match in the list of available chars 3343 for _, rn := range chr.chars { 3344 if rn == cur { 3345 if chr.inverted { 3346 p.failAt(false, start.position, chr.val) 3347 return nil, false 3348 } 3349 p.read() 3350 p.failAt(true, start.position, chr.val) 3351 return p.sliceFrom(start), true 3352 } 3353 } 3354 3355 // try to match in the list of ranges 3356 for i := 0; i < len(chr.ranges); i += 2 { 3357 if cur >= chr.ranges[i] && cur <= chr.ranges[i+1] { 3358 if chr.inverted { 3359 p.failAt(false, start.position, chr.val) 3360 return nil, false 3361 } 3362 p.read() 3363 p.failAt(true, start.position, chr.val) 3364 return p.sliceFrom(start), true 3365 } 3366 } 3367 3368 // try to match in the list of Unicode classes 3369 for _, cl := range chr.classes { 3370 if unicode.Is(cl, cur) { 3371 if chr.inverted { 3372 p.failAt(false, start.position, chr.val) 3373 return nil, false 3374 } 3375 p.read() 3376 p.failAt(true, start.position, chr.val) 3377 return p.sliceFrom(start), true 3378 } 3379 } 3380 3381 if chr.inverted { 3382 p.read() 3383 p.failAt(true, start.position, chr.val) 3384 return p.sliceFrom(start), true 3385 } 3386 p.failAt(false, start.position, chr.val) 3387 return nil, false 3388 } 3389 3390 func (p *parser) incChoiceAltCnt(ch *choiceExpr, altI int) { 3391 choiceIdent := fmt.Sprintf("%s %d:%d", p.rstack[len(p.rstack)-1].name, ch.pos.line, ch.pos.col) 3392 m := p.ChoiceAltCnt[choiceIdent] 3393 if m == nil { 3394 m = make(map[string]int) 3395 p.ChoiceAltCnt[choiceIdent] = m 3396 } 3397 // We increment altI by 1, so the keys do not start at 0 3398 alt := strconv.Itoa(altI + 1) 3399 if altI == choiceNoMatch { 3400 alt = p.choiceNoMatch 3401 } 3402 m[alt]++ 3403 } 3404 3405 func (p *parser) parseChoiceExpr(ch *choiceExpr) (interface{}, bool) { 3406 if p.debug { 3407 defer p.out(p.in("parseChoiceExpr")) 3408 } 3409 3410 for altI, alt := range ch.alternatives { 3411 // dummy assignment to prevent compile error if optimized 3412 _ = altI 3413 3414 state := p.cloneState() 3415 3416 p.pushV() 3417 val, ok := p.parseExpr(alt) 3418 p.popV() 3419 if ok { 3420 p.incChoiceAltCnt(ch, altI) 3421 return val, ok 3422 } 3423 p.restoreState(state) 3424 } 3425 p.incChoiceAltCnt(ch, choiceNoMatch) 3426 return nil, false 3427 } 3428 3429 func (p *parser) parseLabeledExpr(lab *labeledExpr) (interface{}, bool) { 3430 if p.debug { 3431 defer p.out(p.in("parseLabeledExpr")) 3432 } 3433 3434 p.pushV() 3435 val, ok := p.parseExpr(lab.expr) 3436 p.popV() 3437 if ok && lab.label != "" { 3438 m := p.vstack[len(p.vstack)-1] 3439 m[lab.label] = val 3440 } 3441 return val, ok 3442 } 3443 3444 func (p *parser) parseLitMatcher(lit *litMatcher) (interface{}, bool) { 3445 if p.debug { 3446 defer p.out(p.in("parseLitMatcher")) 3447 } 3448 3449 start := p.pt 3450 for _, want := range lit.val { 3451 cur := p.pt.rn 3452 if lit.ignoreCase { 3453 cur = unicode.ToLower(cur) 3454 } 3455 if cur != want { 3456 p.failAt(false, start.position, lit.want) 3457 p.restore(start) 3458 return nil, false 3459 } 3460 p.read() 3461 } 3462 p.failAt(true, start.position, lit.want) 3463 return p.sliceFrom(start), true 3464 } 3465 3466 func (p *parser) parseNotCodeExpr(not *notCodeExpr) (interface{}, bool) { 3467 if p.debug { 3468 defer p.out(p.in("parseNotCodeExpr")) 3469 } 3470 3471 state := p.cloneState() 3472 3473 ok, err := not.run(p) 3474 if err != nil { 3475 p.addErr(err) 3476 } 3477 p.restoreState(state) 3478 3479 return nil, !ok 3480 } 3481 3482 func (p *parser) parseNotExpr(not *notExpr) (interface{}, bool) { 3483 if p.debug { 3484 defer p.out(p.in("parseNotExpr")) 3485 } 3486 3487 pt := p.pt 3488 state := p.cloneState() 3489 p.pushV() 3490 p.maxFailInvertExpected = !p.maxFailInvertExpected 3491 _, ok := p.parseExpr(not.expr) 3492 p.maxFailInvertExpected = !p.maxFailInvertExpected 3493 p.popV() 3494 p.restoreState(state) 3495 p.restore(pt) 3496 3497 return nil, !ok 3498 } 3499 3500 func (p *parser) parseOneOrMoreExpr(expr *oneOrMoreExpr) (interface{}, bool) { 3501 if p.debug { 3502 defer p.out(p.in("parseOneOrMoreExpr")) 3503 } 3504 3505 var vals []interface{} 3506 3507 for { 3508 p.pushV() 3509 val, ok := p.parseExpr(expr.expr) 3510 p.popV() 3511 if !ok { 3512 if len(vals) == 0 { 3513 // did not match once, no match 3514 return nil, false 3515 } 3516 return vals, true 3517 } 3518 vals = append(vals, val) 3519 } 3520 } 3521 3522 func (p *parser) parseRecoveryExpr(recover *recoveryExpr) (interface{}, bool) { 3523 if p.debug { 3524 defer p.out(p.in("parseRecoveryExpr (" + strings.Join(recover.failureLabel, ",") + ")")) 3525 } 3526 3527 p.pushRecovery(recover.failureLabel, recover.recoverExpr) 3528 val, ok := p.parseExpr(recover.expr) 3529 p.popRecovery() 3530 3531 return val, ok 3532 } 3533 3534 func (p *parser) parseRuleRefExpr(ref *ruleRefExpr) (interface{}, bool) { 3535 if p.debug { 3536 defer p.out(p.in("parseRuleRefExpr " + ref.name)) 3537 } 3538 3539 if ref.name == "" { 3540 panic(fmt.Sprintf("%s: invalid rule: missing name", ref.pos)) 3541 } 3542 3543 rule := p.rules[ref.name] 3544 if rule == nil { 3545 p.addErr(fmt.Errorf("undefined rule: %s", ref.name)) 3546 return nil, false 3547 } 3548 return p.parseRule(rule) 3549 } 3550 3551 func (p *parser) parseSeqExpr(seq *seqExpr) (interface{}, bool) { 3552 if p.debug { 3553 defer p.out(p.in("parseSeqExpr")) 3554 } 3555 3556 vals := make([]interface{}, 0, len(seq.exprs)) 3557 3558 pt := p.pt 3559 state := p.cloneState() 3560 for _, expr := range seq.exprs { 3561 val, ok := p.parseExpr(expr) 3562 if !ok { 3563 p.restoreState(state) 3564 p.restore(pt) 3565 return nil, false 3566 } 3567 vals = append(vals, val) 3568 } 3569 return vals, true 3570 } 3571 3572 func (p *parser) parseStateCodeExpr(state *stateCodeExpr) (interface{}, bool) { 3573 if p.debug { 3574 defer p.out(p.in("parseStateCodeExpr")) 3575 } 3576 3577 err := state.run(p) 3578 if err != nil { 3579 p.addErr(err) 3580 } 3581 return nil, true 3582 } 3583 3584 func (p *parser) parseThrowExpr(expr *throwExpr) (interface{}, bool) { 3585 if p.debug { 3586 defer p.out(p.in("parseThrowExpr")) 3587 } 3588 3589 for i := len(p.recoveryStack) - 1; i >= 0; i-- { 3590 if recoverExpr, ok := p.recoveryStack[i][expr.label]; ok { 3591 if val, ok := p.parseExpr(recoverExpr); ok { 3592 return val, ok 3593 } 3594 } 3595 } 3596 3597 return nil, false 3598 } 3599 3600 func (p *parser) parseZeroOrMoreExpr(expr *zeroOrMoreExpr) (interface{}, bool) { 3601 if p.debug { 3602 defer p.out(p.in("parseZeroOrMoreExpr")) 3603 } 3604 3605 var vals []interface{} 3606 3607 for { 3608 p.pushV() 3609 val, ok := p.parseExpr(expr.expr) 3610 p.popV() 3611 if !ok { 3612 return vals, true 3613 } 3614 vals = append(vals, val) 3615 } 3616 } 3617 3618 func (p *parser) parseZeroOrOneExpr(expr *zeroOrOneExpr) (interface{}, bool) { 3619 if p.debug { 3620 defer p.out(p.in("parseZeroOrOneExpr")) 3621 } 3622 3623 p.pushV() 3624 val, _ := p.parseExpr(expr.expr) 3625 p.popV() 3626 // whether it matched or not, consider it a match 3627 return val, true 3628 }