1#!/usr/bin/env python3.8 2# @generated by pegen from metagrammar.gram 3 4import ast 5from typing import Optional, Any 6 7from pegen.parser import memoize, memoize_left_rec, logger, Parser 8from ast import literal_eval 9 10from pegen.grammar import ( 11 Alt, 12 Cut, 13 Forced, 14 Gather, 15 Group, 16 Item, 17 Lookahead, 18 LookaheadOrCut, 19 MetaTuple, 20 MetaList, 21 NameLeaf, 22 NamedItem, 23 NamedItemList, 24 NegativeLookahead, 25 Opt, 26 Plain, 27 PositiveLookahead, 28 Repeat0, 29 Repeat1, 30 Rhs, 31 Rule, 32 RuleList, 33 RuleName, 34 Grammar, 35 StringLeaf, 36) 37 38class GeneratedParser(Parser): 39 40 @memoize 41 def start(self) -> Optional[Grammar]: 42 # start: grammar $ 43 mark = self.mark() 44 cut = False 45 if ( 46 (grammar := self.grammar()) 47 and 48 (endmarker := self.expect('ENDMARKER')) 49 ): 50 return grammar 51 self.reset(mark) 52 if cut: return None 53 return None 54 55 @memoize 56 def grammar(self) -> Optional[Grammar]: 57 # grammar: metas rules | rules 58 mark = self.mark() 59 cut = False 60 if ( 61 (metas := self.metas()) 62 and 63 (rules := self.rules()) 64 ): 65 return Grammar ( rules , metas ) 66 self.reset(mark) 67 if cut: return None 68 cut = False 69 if ( 70 (rules := self.rules()) 71 ): 72 return Grammar ( rules , [ ] ) 73 self.reset(mark) 74 if cut: return None 75 return None 76 77 @memoize 78 def metas(self) -> Optional[MetaList]: 79 # metas: meta metas | meta 80 mark = self.mark() 81 cut = False 82 if ( 83 (meta := self.meta()) 84 and 85 (metas := self.metas()) 86 ): 87 return [ meta ] + metas 88 self.reset(mark) 89 if cut: return None 90 cut = False 91 if ( 92 (meta := self.meta()) 93 ): 94 return [ meta ] 95 self.reset(mark) 96 if cut: return None 97 return None 98 99 @memoize 100 def meta(self) -> Optional[MetaTuple]: 101 # meta: "@" NAME NEWLINE | "@" NAME NAME NEWLINE | "@" NAME STRING NEWLINE 102 mark = self.mark() 103 cut = False 104 if ( 105 (literal := self.expect("@")) 106 and 107 (name := self.name()) 108 and 109 (newline := self.expect('NEWLINE')) 110 ): 111 return ( name . string , None ) 112 self.reset(mark) 113 if cut: return None 114 cut = False 115 if ( 116 (literal := self.expect("@")) 117 and 118 (a := self.name()) 119 and 120 (b := self.name()) 121 and 122 (newline := self.expect('NEWLINE')) 123 ): 124 return ( a . string , b . string ) 125 self.reset(mark) 126 if cut: return None 127 cut = False 128 if ( 129 (literal := self.expect("@")) 130 and 131 (name := self.name()) 132 and 133 (string := self.string()) 134 and 135 (newline := self.expect('NEWLINE')) 136 ): 137 return ( name . string , literal_eval ( string . string ) ) 138 self.reset(mark) 139 if cut: return None 140 return None 141 142 @memoize 143 def rules(self) -> Optional[RuleList]: 144 # rules: rule rules | rule 145 mark = self.mark() 146 cut = False 147 if ( 148 (rule := self.rule()) 149 and 150 (rules := self.rules()) 151 ): 152 return [ rule ] + rules 153 self.reset(mark) 154 if cut: return None 155 cut = False 156 if ( 157 (rule := self.rule()) 158 ): 159 return [ rule ] 160 self.reset(mark) 161 if cut: return None 162 return None 163 164 @memoize 165 def rule(self) -> Optional[Rule]: 166 # rule: rulename memoflag? ":" alts NEWLINE INDENT more_alts DEDENT | rulename memoflag? ":" NEWLINE INDENT more_alts DEDENT | rulename memoflag? ":" alts NEWLINE 167 mark = self.mark() 168 cut = False 169 if ( 170 (rulename := self.rulename()) 171 and 172 (opt := self.memoflag(),) 173 and 174 (literal := self.expect(":")) 175 and 176 (alts := self.alts()) 177 and 178 (newline := self.expect('NEWLINE')) 179 and 180 (indent := self.expect('INDENT')) 181 and 182 (more_alts := self.more_alts()) 183 and 184 (dedent := self.expect('DEDENT')) 185 ): 186 return Rule ( rulename [ 0 ] , rulename [ 1 ] , Rhs ( alts . alts + more_alts . alts ) , memo = opt ) 187 self.reset(mark) 188 if cut: return None 189 cut = False 190 if ( 191 (rulename := self.rulename()) 192 and 193 (opt := self.memoflag(),) 194 and 195 (literal := self.expect(":")) 196 and 197 (newline := self.expect('NEWLINE')) 198 and 199 (indent := self.expect('INDENT')) 200 and 201 (more_alts := self.more_alts()) 202 and 203 (dedent := self.expect('DEDENT')) 204 ): 205 return Rule ( rulename [ 0 ] , rulename [ 1 ] , more_alts , memo = opt ) 206 self.reset(mark) 207 if cut: return None 208 cut = False 209 if ( 210 (rulename := self.rulename()) 211 and 212 (opt := self.memoflag(),) 213 and 214 (literal := self.expect(":")) 215 and 216 (alts := self.alts()) 217 and 218 (newline := self.expect('NEWLINE')) 219 ): 220 return Rule ( rulename [ 0 ] , rulename [ 1 ] , alts , memo = opt ) 221 self.reset(mark) 222 if cut: return None 223 return None 224 225 @memoize 226 def rulename(self) -> Optional[RuleName]: 227 # rulename: NAME '[' NAME '*' ']' | NAME '[' NAME ']' | NAME 228 mark = self.mark() 229 cut = False 230 if ( 231 (name := self.name()) 232 and 233 (literal := self.expect('[')) 234 and 235 (type := self.name()) 236 and 237 (literal_1 := self.expect('*')) 238 and 239 (literal_2 := self.expect(']')) 240 ): 241 return ( name . string , type . string + "*" ) 242 self.reset(mark) 243 if cut: return None 244 cut = False 245 if ( 246 (name := self.name()) 247 and 248 (literal := self.expect('[')) 249 and 250 (type := self.name()) 251 and 252 (literal_1 := self.expect(']')) 253 ): 254 return ( name . string , type . string ) 255 self.reset(mark) 256 if cut: return None 257 cut = False 258 if ( 259 (name := self.name()) 260 ): 261 return ( name . string , None ) 262 self.reset(mark) 263 if cut: return None 264 return None 265 266 @memoize 267 def memoflag(self) -> Optional[str]: 268 # memoflag: '(' 'memo' ')' 269 mark = self.mark() 270 cut = False 271 if ( 272 (literal := self.expect('(')) 273 and 274 (literal_1 := self.expect('memo')) 275 and 276 (literal_2 := self.expect(')')) 277 ): 278 return "memo" 279 self.reset(mark) 280 if cut: return None 281 return None 282 283 @memoize 284 def alts(self) -> Optional[Rhs]: 285 # alts: alt "|" alts | alt 286 mark = self.mark() 287 cut = False 288 if ( 289 (alt := self.alt()) 290 and 291 (literal := self.expect("|")) 292 and 293 (alts := self.alts()) 294 ): 295 return Rhs ( [ alt ] + alts . alts ) 296 self.reset(mark) 297 if cut: return None 298 cut = False 299 if ( 300 (alt := self.alt()) 301 ): 302 return Rhs ( [ alt ] ) 303 self.reset(mark) 304 if cut: return None 305 return None 306 307 @memoize 308 def more_alts(self) -> Optional[Rhs]: 309 # more_alts: "|" alts NEWLINE more_alts | "|" alts NEWLINE 310 mark = self.mark() 311 cut = False 312 if ( 313 (literal := self.expect("|")) 314 and 315 (alts := self.alts()) 316 and 317 (newline := self.expect('NEWLINE')) 318 and 319 (more_alts := self.more_alts()) 320 ): 321 return Rhs ( alts . alts + more_alts . alts ) 322 self.reset(mark) 323 if cut: return None 324 cut = False 325 if ( 326 (literal := self.expect("|")) 327 and 328 (alts := self.alts()) 329 and 330 (newline := self.expect('NEWLINE')) 331 ): 332 return Rhs ( alts . alts ) 333 self.reset(mark) 334 if cut: return None 335 return None 336 337 @memoize 338 def alt(self) -> Optional[Alt]: 339 # alt: items '$' action | items '$' | items action | items 340 mark = self.mark() 341 cut = False 342 if ( 343 (items := self.items()) 344 and 345 (literal := self.expect('$')) 346 and 347 (action := self.action()) 348 ): 349 return Alt ( items + [ NamedItem ( None , NameLeaf ( 'ENDMARKER' ) ) ] , action = action ) 350 self.reset(mark) 351 if cut: return None 352 cut = False 353 if ( 354 (items := self.items()) 355 and 356 (literal := self.expect('$')) 357 ): 358 return Alt ( items + [ NamedItem ( None , NameLeaf ( 'ENDMARKER' ) ) ] , action = None ) 359 self.reset(mark) 360 if cut: return None 361 cut = False 362 if ( 363 (items := self.items()) 364 and 365 (action := self.action()) 366 ): 367 return Alt ( items , action = action ) 368 self.reset(mark) 369 if cut: return None 370 cut = False 371 if ( 372 (items := self.items()) 373 ): 374 return Alt ( items , action = None ) 375 self.reset(mark) 376 if cut: return None 377 return None 378 379 @memoize 380 def items(self) -> Optional[NamedItemList]: 381 # items: named_item items | named_item 382 mark = self.mark() 383 cut = False 384 if ( 385 (named_item := self.named_item()) 386 and 387 (items := self.items()) 388 ): 389 return [ named_item ] + items 390 self.reset(mark) 391 if cut: return None 392 cut = False 393 if ( 394 (named_item := self.named_item()) 395 ): 396 return [ named_item ] 397 self.reset(mark) 398 if cut: return None 399 return None 400 401 @memoize 402 def named_item(self) -> Optional[NamedItem]: 403 # named_item: NAME '[' NAME '*' ']' '=' ~ item | NAME '[' NAME ']' '=' ~ item | NAME '=' ~ item | item | forced_atom | lookahead 404 mark = self.mark() 405 cut = False 406 if ( 407 (name := self.name()) 408 and 409 (literal := self.expect('[')) 410 and 411 (type := self.name()) 412 and 413 (literal_1 := self.expect('*')) 414 and 415 (literal_2 := self.expect(']')) 416 and 417 (literal_3 := self.expect('=')) 418 and 419 (cut := True) 420 and 421 (item := self.item()) 422 ): 423 return NamedItem ( name . string , item , f"{type.string}*" ) 424 self.reset(mark) 425 if cut: return None 426 cut = False 427 if ( 428 (name := self.name()) 429 and 430 (literal := self.expect('[')) 431 and 432 (type := self.name()) 433 and 434 (literal_1 := self.expect(']')) 435 and 436 (literal_2 := self.expect('=')) 437 and 438 (cut := True) 439 and 440 (item := self.item()) 441 ): 442 return NamedItem ( name . string , item , type . string ) 443 self.reset(mark) 444 if cut: return None 445 cut = False 446 if ( 447 (name := self.name()) 448 and 449 (literal := self.expect('=')) 450 and 451 (cut := True) 452 and 453 (item := self.item()) 454 ): 455 return NamedItem ( name . string , item ) 456 self.reset(mark) 457 if cut: return None 458 cut = False 459 if ( 460 (item := self.item()) 461 ): 462 return NamedItem ( None , item ) 463 self.reset(mark) 464 if cut: return None 465 cut = False 466 if ( 467 (it := self.forced_atom()) 468 ): 469 return NamedItem ( None , it ) 470 self.reset(mark) 471 if cut: return None 472 cut = False 473 if ( 474 (it := self.lookahead()) 475 ): 476 return NamedItem ( None , it ) 477 self.reset(mark) 478 if cut: return None 479 return None 480 481 @memoize 482 def forced_atom(self) -> Optional[NamedItem]: 483 # forced_atom: '&' '&' ~ atom 484 mark = self.mark() 485 cut = False 486 if ( 487 (literal := self.expect('&')) 488 and 489 (literal_1 := self.expect('&')) 490 and 491 (cut := True) 492 and 493 (atom := self.atom()) 494 ): 495 return Forced ( atom ) 496 self.reset(mark) 497 if cut: return None 498 return None 499 500 @memoize 501 def lookahead(self) -> Optional[LookaheadOrCut]: 502 # lookahead: '&' ~ atom | '!' ~ atom | '~' 503 mark = self.mark() 504 cut = False 505 if ( 506 (literal := self.expect('&')) 507 and 508 (cut := True) 509 and 510 (atom := self.atom()) 511 ): 512 return PositiveLookahead ( atom ) 513 self.reset(mark) 514 if cut: return None 515 cut = False 516 if ( 517 (literal := self.expect('!')) 518 and 519 (cut := True) 520 and 521 (atom := self.atom()) 522 ): 523 return NegativeLookahead ( atom ) 524 self.reset(mark) 525 if cut: return None 526 cut = False 527 if ( 528 (literal := self.expect('~')) 529 ): 530 return Cut ( ) 531 self.reset(mark) 532 if cut: return None 533 return None 534 535 @memoize 536 def item(self) -> Optional[Item]: 537 # item: '[' ~ alts ']' | atom '?' | atom '*' | atom '+' | atom '.' atom '+' | atom 538 mark = self.mark() 539 cut = False 540 if ( 541 (literal := self.expect('[')) 542 and 543 (cut := True) 544 and 545 (alts := self.alts()) 546 and 547 (literal_1 := self.expect(']')) 548 ): 549 return Opt ( alts ) 550 self.reset(mark) 551 if cut: return None 552 cut = False 553 if ( 554 (atom := self.atom()) 555 and 556 (literal := self.expect('?')) 557 ): 558 return Opt ( atom ) 559 self.reset(mark) 560 if cut: return None 561 cut = False 562 if ( 563 (atom := self.atom()) 564 and 565 (literal := self.expect('*')) 566 ): 567 return Repeat0 ( atom ) 568 self.reset(mark) 569 if cut: return None 570 cut = False 571 if ( 572 (atom := self.atom()) 573 and 574 (literal := self.expect('+')) 575 ): 576 return Repeat1 ( atom ) 577 self.reset(mark) 578 if cut: return None 579 cut = False 580 if ( 581 (sep := self.atom()) 582 and 583 (literal := self.expect('.')) 584 and 585 (node := self.atom()) 586 and 587 (literal_1 := self.expect('+')) 588 ): 589 return Gather ( sep , node ) 590 self.reset(mark) 591 if cut: return None 592 cut = False 593 if ( 594 (atom := self.atom()) 595 ): 596 return atom 597 self.reset(mark) 598 if cut: return None 599 return None 600 601 @memoize 602 def atom(self) -> Optional[Plain]: 603 # atom: '(' ~ alts ')' | NAME | STRING 604 mark = self.mark() 605 cut = False 606 if ( 607 (literal := self.expect('(')) 608 and 609 (cut := True) 610 and 611 (alts := self.alts()) 612 and 613 (literal_1 := self.expect(')')) 614 ): 615 return Group ( alts ) 616 self.reset(mark) 617 if cut: return None 618 cut = False 619 if ( 620 (name := self.name()) 621 ): 622 return NameLeaf ( name . string ) 623 self.reset(mark) 624 if cut: return None 625 cut = False 626 if ( 627 (string := self.string()) 628 ): 629 return StringLeaf ( string . string ) 630 self.reset(mark) 631 if cut: return None 632 return None 633 634 @memoize 635 def action(self) -> Optional[str]: 636 # action: "{" ~ target_atoms "}" 637 mark = self.mark() 638 cut = False 639 if ( 640 (literal := self.expect("{")) 641 and 642 (cut := True) 643 and 644 (target_atoms := self.target_atoms()) 645 and 646 (literal_1 := self.expect("}")) 647 ): 648 return target_atoms 649 self.reset(mark) 650 if cut: return None 651 return None 652 653 @memoize 654 def target_atoms(self) -> Optional[str]: 655 # target_atoms: target_atom target_atoms | target_atom 656 mark = self.mark() 657 cut = False 658 if ( 659 (target_atom := self.target_atom()) 660 and 661 (target_atoms := self.target_atoms()) 662 ): 663 return target_atom + " " + target_atoms 664 self.reset(mark) 665 if cut: return None 666 cut = False 667 if ( 668 (target_atom := self.target_atom()) 669 ): 670 return target_atom 671 self.reset(mark) 672 if cut: return None 673 return None 674 675 @memoize 676 def target_atom(self) -> Optional[str]: 677 # target_atom: "{" ~ target_atoms "}" | NAME | NUMBER | STRING | "?" | ":" | !"}" OP 678 mark = self.mark() 679 cut = False 680 if ( 681 (literal := self.expect("{")) 682 and 683 (cut := True) 684 and 685 (target_atoms := self.target_atoms()) 686 and 687 (literal_1 := self.expect("}")) 688 ): 689 return "{" + target_atoms + "}" 690 self.reset(mark) 691 if cut: return None 692 cut = False 693 if ( 694 (name := self.name()) 695 ): 696 return name . string 697 self.reset(mark) 698 if cut: return None 699 cut = False 700 if ( 701 (number := self.number()) 702 ): 703 return number . string 704 self.reset(mark) 705 if cut: return None 706 cut = False 707 if ( 708 (string := self.string()) 709 ): 710 return string . string 711 self.reset(mark) 712 if cut: return None 713 cut = False 714 if ( 715 (literal := self.expect("?")) 716 ): 717 return "?" 718 self.reset(mark) 719 if cut: return None 720 cut = False 721 if ( 722 (literal := self.expect(":")) 723 ): 724 return ":" 725 self.reset(mark) 726 if cut: return None 727 cut = False 728 if ( 729 self.negative_lookahead(self.expect, "}") 730 and 731 (op := self.op()) 732 ): 733 return op . string 734 self.reset(mark) 735 if cut: return None 736 return None 737 738 739if __name__ == '__main__': 740 from pegen.parser import simple_parser_main 741 simple_parser_main(GeneratedParser) 742