Source

spix / Spix / PEG / Parser.pir

Full commit
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
# Copyright (C) 2010, Parrot Foundation.

.sub 'onload' :load
    load_bytecode 'Spix/Match.pir'
    .local pmc class
    class = newclass ['Spix';'PEG';'Parser']
    addattribute class, 'pos'
    addattribute class, 'length'
    addattribute class, 'input'
    addattribute class, 'memo'
.end

.namespace ['Spix';'PEG';'Parser']

.sub 'parse' :method
    .param string input
    .param pmc options :slurpy :named
    .local int pos, len, i
    .local pmc derivs
    pos = 0
    self.'pos'(pos)
    len = length input
    self.'length'(len)

    self.'input'(input)

    derivs = new ['FixedPMCArray']
    $I0 = len + 1
    derivs = $I0 
    self.'memo'(derivs)

    $P0 = self.'Grammar'(input, pos)
    .return ($P0)
.end

.sub 'new_node' :method 
    .param pmc children :slurpy
    .param pmc options :slurpy :named 

    $P0 = new ['Spix';'Match']
    $P0.'attrs'(options)
    $P0.'children'(children)

    .return($P0)
.end


.sub 'pos' :method
    .param int value :optional
    .param int got_value :opt_flag

    if got_value goto setvalue
    $P0 = getattribute self, 'pos'
    $I0 = $P0
    .return ($I0)
  setvalue:
    $P0 = box value
    setattribute self, 'pos', $P0
    .return (value)
.end

.sub 'length' :method
    .param int value :optional
    .param int got_value :opt_flag

    if got_value goto setvalue
    $P0 = getattribute self, 'length'
    $I0 = $P0
    .return ($I0)
  setvalue:
    $P0 = box value
    setattribute self, 'length', $P0
    .return (value)
.end

.sub 'input' :method
    .param pmc value :optional
    .param int got_value :opt_flag

    if got_value goto setvalue
    $P0 = getattribute self, 'input'
    .return ($P0)
  setvalue:
    setattribute self, 'input', value
    .return (value)
.end

.sub 'memo' :method
    .param pmc value :optional
    .param int got_value :opt_flag

    if got_value goto setvalue
    $P0 = getattribute self, 'memo'
    .return ($P0)
  setvalue:
    setattribute self, 'memo', value
    .return (value)
.end

.sub 'set_memo' :method
    .param int pos
    .param string name
    .param pmc memo
    .local pmc memo_store

    $P0 = getattribute self, 'memo'
    if null $P0 goto no_memo
    $I0 = exists $P0[pos]
    if $I0 goto pos_exists
    memo_store = new ['Hash']
    $P0[pos] = memo_store
    goto set_memo
  pos_exists:
    memo_store = $P0[pos]

  set_memo:
    memo_store[name] = memo
  no_memo:
.end

.sub 'get_memo' :method
    .param int pos
    .param string name
    .local pmc memo

    $P0 = getattribute self, 'memo'
    $I0 = exists $P0[pos]
    unless $I0 goto return
    $P1 = $P0[pos]
    $I0 = $P1[name]
    unless $I0 goto return
    memo = $P1[name]

  return:
    .return (memo)
.end

.sub 'token' :method
    .param string input
    .param int pos
    .param string name
    .param string value
    .local pmc tree
    tree = self.'get_memo'(pos, name)
    unless null tree goto got_tree

    $I0 = self.'length'()
    if pos >= $I0 goto got_tree

    $I0 = ord input, pos
    $I1 = ord value

    if $I0 == $I1 goto matched
    goto got_tree

  matched:
    $I0 = pos + 1
    self.'pos'($I0)
    tree = self.'new_node'('name'=>name, 'src'=>value, 'pos'=>pos)
    self.'set_memo'(pos, name, tree)

  got_tree:
    .return (tree)
.end

.sub 'Grammar' :method
    .param string input
    .param int pos
    .local pmc tree, children, definitions
    .local int new_pos
    new_pos = pos

    tree = self.'get_memo'(pos, 'Grammar')
    unless null tree goto got_tree
    children = new ['ResizablePMCArray']

    # Spacing is optional
    $P0 = self.'Spacing'(input, pos)
    if null $P0 goto no_leading_space
    push children, $P0
  no_leading_space:

    # First definition is required
    new_pos = self.'pos'()
    $P0 = self.'Definition'(input, new_pos)
    if null $P0 goto warn_definition
    definitions = new ['ResizablePMCArray']
    push children, definitions
    push definitions, $P0

    # Following definitions are optional
  definition_loop:
    new_pos = self.'pos'()
    $P0 = self.'Definition'(input, new_pos)
    if null $P0 goto definition_end
    push definitions, $P0
    goto definition_loop
  definition_end:

    # End-of-file is required
    new_pos = self.'pos'()
    $P0 = self.'EndOfFile'(input, new_pos)
    if null $P0 goto warn_endoffile
    push children, $P0


    tree = self.'new_node'(children :flat, 'name'=>'Grammar', 'pos'=>pos)
    self.'set_memo'(pos, 'Grammar', tree)

  got_tree:
    .return (tree)

  warn_definition:
    printerr "warning: Unable to match the top-level definition.\n"
    goto got_tree
  warn_endoffile:
    printerr "warning: Unable to match the end of the file.\n"
    goto got_tree
.end

.sub 'Definition' :method
    .param string input
    .param int pos
    .local pmc tree, children
    .local int new_pos
    new_pos = pos

    tree = self.'get_memo'(pos, 'Definition')
    unless null tree goto got_tree
    children = new ['ResizablePMCArray']

    # Identifier is required
    $P0 = self.'Identifier'(input, new_pos)
    if null $P0 goto warn_identifier
    push children, $P0
    new_pos = self.'pos'()

    # LEFTARROW is required
    $P0 = self.'LEFTARROW'(input, new_pos)
    if null $P0 goto got_tree
    push children, $P0
    new_pos = self.'pos'()

    # Expression is required
    $P0 = self.'Expression'(input, new_pos)
    if null $P0 goto got_tree
    push children, $P0

    tree = self.'new_node'(children :flat, 'name'=>'Definition', 'pos'=>pos)
    self.'set_memo'(pos, 'Definition', tree)

  got_tree:
    .return (tree)

  warn_identifier:
    printerr "warning: Unmatched identifier at position "
    printerr new_pos
    printerr ".\n"
    goto got_tree
.end

.sub 'Expression' :method
    .param string input
    .param int pos
    .local pmc tree, children, sequences

    tree = self.'get_memo'(pos, 'Expression')
    unless null tree goto got_tree
    children = new ['ResizablePMCArray']

    # First sequence is required
    $P0 = self.'Sequence'(input, pos)
    if null $P0 goto got_tree
    push children, $P0

    # Following sequences are optional
  sequence_loop:
    $I0 = self.'pos'()
    $P0 = self.'SLASH'(input, $I0)
    if null $P0 goto sequence_end
    push children, $P0
    $I1 = self.'pos'()
    $P0 = self.'Sequence'(input, $I1)
    if null $P0 goto sequence_backtrack
    push children, $P0
    goto sequence_loop
  sequence_backtrack:
    self.'pos'($I0)
  sequence_end:

    tree = self.'new_node'(children :flat, 'name'=>'Expression', 'pos'=>pos)
    self.'set_memo'(pos, 'Expression', tree)

  got_tree:
    .return (tree)
.end

.sub 'Sequence' :method
    .param string input
    .param int pos
    .local pmc tree, children, definitions

    tree = self.'get_memo'(pos, 'Sequence')
    unless null tree goto got_tree
    children = new ['ResizablePMCArray']

    # All prefixes are optional
  prefix_loop:
    $I0 = self.'pos'()
    $P0 = self.'Prefix'(input, $I0)
    if null $P0 goto prefix_end
    push children, $P0
    goto prefix_loop
  prefix_end:

    tree = self.'new_node'('name'=>'Sequence', 'pos'=>pos, 'Prefix'=>children)
    self.'set_memo'(pos, 'Sequence', tree)

  got_tree:
    .return (tree)
.end

.sub 'Prefix' :method
    .param string input
    .param int pos
    .local pmc tree, named_children

    tree = self.'get_memo'(pos, 'Prefix')
    unless null tree goto got_tree
    named_children = new ['Hash']

    # AND and NOT are optional, but mutually exclusive
    $P0 = self.'AND'(input, pos)
    if null $P0 goto no_and
    named_children['AND'] = $P0
    goto done_alternation
  no_and:
    $P0 = self.'NOT'(input, pos)
    if null $P0 goto done_alternation
    named_children['NOT'] = $P0
  done_alternation:

    # Suffix is required
    $P0 = self.'Suffix'(input, pos)
    if null $P0 goto got_tree
    named_children['Suffix'] = $P0

    tree = self.'new_node'('name'=>'Prefix', 'pos'=>pos, named_children :flat :named)
    self.'set_memo'(pos, 'Prefix', tree)

  got_tree:
    .return (tree)
.end

.sub 'Suffix' :method
    .param string input
    .param int pos
    .local pmc tree, named_children

    tree = self.'get_memo'(pos, 'Suffix')
    unless null tree goto got_tree
    named_children = new ['Hash']

    # Primary is required
    $P0 = self.'Primary'(input, pos)
    if null $P0 goto got_tree
    named_children['Primary'] = $P0

    # QUESTION, STAR, and PLUS are optional, but mutually exclusive
    $P0 = self.'QUESTION'(input, pos)
    if null $P0 goto no_question
    named_children['QUESTION'] = $P0
    goto done_alternation
  no_question:
    $P0 = self.'STAR'(input, pos)
    if null $P0 goto no_star
    named_children['STAR'] = $P0
  no_star:
    $P0 = self.'PLUS'(input, pos)
    if null $P0 goto done_alternation
    named_children['PLUS'] = $P0
  done_alternation:

    tree = self.'new_node'('name'=>'Suffix', 'pos'=>pos, named_children :flat :named)
    self.'set_memo'(pos, 'Suffix', tree)

  got_tree:
    .return (tree)
.end

.sub 'Primary' :method
    .param string input
    .param int pos
    .local pmc tree, named_children
    .local int matched

    tree = self.'get_memo'(pos, 'Primary')
    unless null tree goto got_tree
    matched = 0
    named_children = new ['Hash']

    # One of Identifier, Expression, Literal, Class, or DOT is
    # required, all are mutually exclusive
    $P0 = self.'Identifier'(input, pos)
    if null $P0 goto try_expression
    $I0 = self.'pos'()
    $P1 = self.'LEFTARROW'(input, $I0)
    unless null $P1 goto backtrack_identifier
    named_children['Identifier'] = $P0
    matched = 1
    goto done_alternation
  backtrack_identifier:
    self.'pos'(pos)

  try_expression:
    $P0 = self.'OPEN'(input, pos)
    if null $P0 goto try_literal
    $P1 = self.'Expression'(input, pos)
    if null $P1 goto backtrack_expression
    $P2 = self.'CLOSE'(input, pos)
    if null $P2 goto backtrack_expression

    named_children['OPEN'] = $P0
    named_children['Expression'] = $P1
    named_children['CLOSE'] = $P2

    goto done_alternation
  backtrack_expression:
    self.'pos'(pos)

  try_literal:
    $P0 = self.'Literal'(input, pos)
    if null $P0 goto try_class
    named_children['Literal'] = $P0

  try_class:
    $P0 = self.'Class'(input, pos)
    if null $P0 goto try_dot
    named_children['Class'] = $P0

  try_dot:
    $P0 = self.'DOT'(input, pos)
    if null $P0 goto done_alternation
    named_children['DOT'] = $P0
  done_alternation:

    unless matched goto got_tree
    tree = self.'new_node'('name'=>'Primary', 'pos'=>pos, named_children :flat :named)
    self.'set_memo'(pos, 'Primary', tree)

  got_tree:
    .return (tree)
.end

.sub 'Identifier' :method
    .param string input
    .param int pos
    .local pmc tree, named_children, conts
    .local string ident_string
    .local int new_pos
    ident_string = ''
    new_pos = pos

    tree = self.'get_memo'(pos, 'Identifier')
    unless null tree goto got_tree
    named_children = new ['Hash']
    conts = new ['ResizablePMCArray']

    # IdentStart is required
    $P0 = self.'IdentStart'(input, pos)
    if null $P0 goto got_tree
    named_children['IdentStart'] = $P0
    $S0 = $P0.'src'()
    ident_string .= $S0

    # IdentCont is optional, and multiple
  cont_loop:
    new_pos = self.'pos'()
    $P0 = self.'IdentCont'(input, new_pos)
    if null $P0 goto cont_end
    push conts, $P0
    $S0 = $P0.'src'()
    ident_string .= $S0
    goto cont_loop
  cont_end:

    $I0 = elements conts
    unless $I0 > 0 goto no_conts
    named_children['IdentCont'] = conts
  no_conts:

    # Spacing is optional
    new_pos = self.'pos'()
    $P0 = self.'Spacing'(input, new_pos)
    if null $P0 goto no_leading_space
    named_children['Spacing'] = $P0
  no_leading_space:

    tree = self.'new_node'('name'=>'Identifier', 'pos'=>pos, 'src'=>ident_string, named_children :flat :named)
    self.'set_memo'(pos, 'Identifier', tree)

  got_tree:
    .return (tree)
.end

.sub 'IdentStart' :method
    .param string input
    .param int pos
    .local pmc tree
    .local string ident_string
    tree = self.'get_memo'(pos, 'IdentStart')
    unless null tree goto got_tree
    ident_string = ''

    $I0 = self.'length'()
    if pos == $I0 goto got_tree

    .local int char
    char = ord input, pos
    ident_string = chr char

    # Check lower class
    .local int a, z
    a = ord 'a'
    z = ord 'z'
    $I0 = isge char, a
    $I1 = isle char, z
    $I2 = and $I0, $I1
    if $I2 goto matched

    # Check upper class
    a = ord 'A'
    z = ord 'Z'
    $I0 = isge char, a
    $I1 = isle char, z
    $I2 = and $I0, $I1
    if $I2 goto matched

    # Check underscore
    if ident_string == '_' goto matched

    goto got_tree

  matched:
    $I0 = pos + 1
    self.'pos'($I0)
    tree = self.'new_node'('name'=>'IdentStart', 'src'=>ident_string, 'pos'=>pos)
    self.'set_memo'(pos, 'IdentStart', tree)

  got_tree:
    .return (tree)
.end

.sub 'IdentCont' :method
    .param string input
    .param int pos
    .local pmc tree
    .local string ident_string
    tree = self.'get_memo'(pos, 'IdentCont')
    unless null tree goto got_tree

    $I0 = self.'length'()
    if pos == $I0 goto got_tree

    # One of IdentStart or digit is required, mutually exclusive
    $P0 = self.'IdentStart'(input, pos)
    if null $P0 goto try_digit
    ident_string = $P0.'src'()
    tree = self.'new_node'('name'=>'IdentCont', 'src'=>ident_string, 'pos'=>pos, 'IdentStart'=>$P0)
    $I0 = pos + 1
    self.'pos'($I0)
    goto memo_tree

  try_digit:
    .local int char
    char = ord input, pos
    ident_string = chr char

    # Check digits
    .local int zero, nine
    zero = ord '0'
    nine = ord '9'
    $I0 = isge char, zero
    $I1 = isle char, nine
    $I2 = and $I0, $I1
    if $I2 goto matched

    goto got_tree

  matched:
    $I0 = pos + 1
    self.'pos'($I0)
    tree = self.'new_node'('name'=>'IdentCont', 'src'=>ident_string, 'pos'=>pos)

  memo_tree:
    self.'set_memo'(pos, 'IdentCont', tree)

  got_tree:
    .return (tree)
.end

.sub 'Literal' :method
    .param string input
    .param int pos
    .local pmc tree, children
    .local string literal_string
    .local int new_pos
    tree = self.'get_memo'(pos, 'Literal')
    unless null tree goto got_tree
    children = new ['ResizablePMCArray']
    new_pos = pos

    $I0 = ord input, pos
    $S0 = chr $I0

    if $S0 != "'" goto try_double
    literal_string = $S0
  single_loop:
    inc new_pos
    $P0 = self.'Char'(input, new_pos)
    if null $P0 goto got_tree
    $S0 = $P0.'src'()
    literal_string .= $S0
    if $S0 == "'" goto single_end
    push children, $P0
    goto single_loop
  single_end:
    new_pos = self.'pos'()
    goto matched
  backtrack_single:
    self.'pos'(pos)
    new_pos = pos
    $I0 = ord input, pos
    $S0 = chr $I0

  try_double:
    if $S0 != '"' goto got_tree
    inc new_pos
  double_loop:
    $P0 = self.'Char'(input, new_pos)
    if null $P0 goto got_tree
    $S0 = $P0.'src'()
    if $S0 == '"' goto double_end
    literal_string .= $S0
    push children, $P0
    new_pos = self.'pos'()
    goto double_loop
  double_end:
    inc new_pos
    goto matched
  backtrack_double:
    self.'pos'(pos)
    $I0 = ord input, pos
    $S0 = chr $I0
    goto got_tree

  matched:
    # Consume optional space
    new_pos = self.'pos'()
    $P0 = self.'Spacing'(input, new_pos)

    tree = self.'new_node'(children :flat, 'name'=>'Literal', 'src'=>literal_string, 'pos'=>pos)
    self.'set_memo'(pos, 'Literal', tree)

  got_tree:
    .return (tree)
.end

.sub 'Class' :method
    .param string input
    .param int pos
    .local pmc tree, children
    .local int new_pos
    tree = self.'get_memo'(pos, 'Class')
    unless null tree goto got_tree
    children = new ['ResizablePMCArray']

    $I0 = ord input, pos
    $S0 = chr $I0

    # Opening bracket is required
    if $S0 != '[' goto got_tree

    # Range is optional, but must not start with a bracket
    new_pos = pos + 1
  class_loop:
    $I0 = ord input, new_pos
    $S0 = chr $I0
    if $S0 == ']' goto class_end # bracket ends the match

    $P0 = self.'Range'(input, new_pos)
    if null $P0 goto backtrack_class # not range or bracket means failed
    push children, $P0
    new_pos = self.'pos'()
    goto class_loop
  class_end:
    inc new_pos
    goto matched

  backtrack_class:
    self.'pos'(pos)
    $I0 = ord input, pos
    $S0 = chr $I0
    goto got_tree

  matched:
    self.'pos'(new_pos)
    tree = self.'new_node'(children :flat, 'name'=>'Class', 'pos'=>pos)
    self.'set_memo'(pos, 'Class', tree)

  got_tree:
    .return (tree)
.end

.sub 'Range' :method
    .param string input
    .param int pos
    .local pmc tree, children
    .local int new_pos
    tree = self.'get_memo'(pos, 'Range')
    unless null tree goto got_tree
    children = new ['ResizablePMCArray']
    new_pos = pos

  range_loop:
    $P0 = self.'Char'(input, new_pos)
    if null $P0 goto got_tree
    push children, $P0
    new_pos = self.'pos'()

    $I0 = ord input, new_pos
    $S0 = chr $I0
    unless $S0 == '-' goto range_end
    inc new_pos
    goto range_loop
  range_end:

  matched:
    self.'pos'(new_pos)
    tree = self.'new_node'(children :flat, 'name'=>'Range', 'pos'=>pos)
    self.'set_memo'(pos, 'Range', tree)

  got_tree:
    .return(tree)
.end

.sub 'Char' :method
    .param string input
    .param int pos
    .local pmc tree
    .local string char_string
    .local int new_pos

    tree = self.'get_memo'(pos, 'Char')
    unless null tree goto got_tree
    char_string = ''

    # A character is one of: a literal escape sequence, a numbered
    # escape sequence, or any other character
    $I0 = ord input, pos
    $S0 = chr $I0
    char_string = $S0
    unless $S0 == '\' goto try_anychar
    .local int zero, seven
    zero = ord '0'
    seven = ord '7'

  # \n, \r, \t, \', \", \[, \], \\
  try_literalescape:
    new_pos = pos + 1
    $I0 = ord input, new_pos
    $S0 = chr $I0
    if $S0 == "n" goto got_literalescape
    if $S0 == "r" goto got_literalescape
    if $S0 == "t" goto got_literalescape
    if $S0 == "'" goto got_literalescape
    if $S0 == '"' goto got_literalescape
    if $S0 == '[' goto got_literalescape
    if $S0 == ']' goto got_literalescape
    if $S0 == '\' goto got_literalescape
    goto try_longnumberedescape
  got_literalescape:
    char_string .= $S0
    goto matched
    
  try_longnumberedescape:
    new_pos = pos + 1
    $I0 = ord input, new_pos
    $S0 = chr $I0
    if $S0 == "0" goto try_second_longnumberescape
    if $S0 == "1" goto try_second_longnumberescape
    if $S0 == "2" goto try_second_longnumberescape
    goto try_shortnumberescape
  try_second_longnumberescape:
    inc new_pos
    $I1 = ord input, new_pos
    $S1 = chr $I1
    $I2 = isge $I1, zero
    $I3 = isle $I1, seven
    $I4 = and $I2, $I3
    unless $I4 goto try_shortnumberescape
    inc new_pos
    $I1 = ord input, new_pos
    $S2 = chr $I1
    $I2 = isge $I1, zero
    $I3 = isle $I1, seven
    $I4 = and $I2, $I3
    unless $I4 goto try_shortnumberescape

  got_longnumberescape:
    char_string .= $S0
    char_string .= $S1
    char_string .= $S2
    goto matched

  try_shortnumberescape:
    new_pos = pos + 1
    $I1 = ord input, new_pos
    $S1 = chr $I1
    $I2 = isge $I1, zero
    $I3 = isle $I1, seven
    $I4 = and $I2, $I3
    unless $I4 goto try_anychar
    char_string .= $S1

    $I5 = new_pos + 1
    $I1 = ord input, $I5
    $S1 = chr $I1
    $I2 = isge $I1, zero
    $I3 = isle $I1, seven
    $I4 = and $I2, $I3
    unless $I4 goto matched
    inc new_pos
    char_string .= $S1
    goto matched

  try_anychar:
    new_pos = pos

  matched:
    inc new_pos
    self.'pos'(new_pos)
    tree = self.'new_node'('name'=>'Char', 'src'=>char_string, 'pos'=>pos)
    self.'set_memo'(pos, 'Char', tree)

  got_tree:
    .return (tree)
.end

.sub 'LEFTARROW' :method
    .param string input
    .param int pos
    .local pmc tree
    .local int new_pos

    tree = self.'get_memo'(pos, 'LEFTARROW')
    unless null tree goto got_tree
    new_pos = pos

    $I0 = ord input, new_pos
    $S0 = chr $I0
    if $S0 == "<" goto matched_first
    goto got_tree

  matched_first:
    inc new_pos
    $I0 = ord input, new_pos
    $S1 = chr $I0
    if $S1 == "-" goto matched_second
    goto got_tree

  matched_second:
    # Spacing is optional
    inc new_pos
    $P0 = self.'Spacing'(input, new_pos)
    if null $P0 goto no_leading_space
    new_pos = self.'pos'()
  no_leading_space:

    self.'pos'(new_pos)
    $S2 = $S0 . $S1
    tree = self.'new_node'('name'=>'LEFTARROW', 'src'=>$S2, 'pos'=>pos)
    self.'set_memo'(pos, 'LEFTARROW', tree)

  got_tree:
    .return (tree)
.end

.sub 'SLASH' :method
    .param pmc args :slurpy
    .tailcall self.'token'(args :flat, 'SLASH', '/')
.end

.sub 'AND' :method
    .param pmc args :slurpy
    .tailcall self.'token'(args :flat, 'AND', '&')
.end

.sub 'NOT' :method
    .param pmc args :slurpy
    .tailcall self.'token'(args :flat, 'NOT', '!')
.end

.sub 'QUESTION' :method
    .param pmc args :slurpy
    .tailcall self.'token'(args :flat, 'QUESTION', '?')
.end

.sub 'STAR' :method
    .param pmc args :slurpy
    .tailcall self.'token'(args :flat, 'STAR', '*')
.end

.sub 'PLUS' :method
    .param pmc args :slurpy
    .tailcall self.'token'(args :flat, 'PLUS', '+')
.end

.sub 'OPEN' :method
    .param pmc args :slurpy
    .tailcall self.'token'(args :flat, 'OPEN', '(')
.end

.sub 'CLOSE' :method
    .param pmc args :slurpy
    .tailcall self.'token'(args :flat, 'CLOSE', ')')
.end


.sub 'DOT' :method
    .param pmc args :slurpy
    .tailcall self.'token'(args :flat, 'DOT', '.')
.end

.sub 'Spacing' :method
    .param string input
    .param int pos
    .local pmc tree
    .local string src
    .local pmc children
    .local int new_pos
    new_pos = pos
    src = ''

    tree = self.'get_memo'(pos, 'Spacing')
    unless null tree goto got_tree
    children = new ['ResizablePMCArray']

  children_loop:
    $P0 = self.'Space'(input, new_pos)
    if null $P0 goto children_done
    push children, $P0
    $S0 = $P0.'src'()
    src .= $S0
    inc new_pos
    goto children_loop
  children_done:

    $I0 = children
    unless $I0 goto got_tree

    tree = self.'new_node'(children :flat, 'name'=>'Spacing', 'src'=>src, 'pos'=>pos)
    self.'set_memo'(pos, 'Spacing', tree)

  got_tree:
    .return (tree)
.end

.sub 'Comment' :method
    .param string input
    .param int pos
    .local pmc tree
    .local int new_pos
    .local string comment_string
    tree = self.'get_memo'(pos, 'Comment')
    unless null tree goto got_tree
    new_pos = pos

    $I0 = ord input, new_pos
    $S0 = chr $I0

    # Opening octothorpe is required for comment
    if $S0 != '#' goto got_tree

    # Rest of comment is optional, terminates at end-of-line
  comment_loop:
    inc new_pos
    $P0 = self.'EndOfLine'(input, new_pos)
    if null $P0 goto comment_loop
  comment_end:

  matched:
    inc new_pos
    self.'pos'(new_pos)
    tree = self.'new_node'('name'=>'Comment', 'pos'=>pos)
    self.'set_memo'(pos, 'Comment', tree)

  got_tree:
    .return (tree)
.end

.sub 'Space' :method
    .param string input
    .param int pos
    .local pmc tree
    tree = self.'get_memo'(pos, 'Space')
    unless null tree goto got_tree

    $I0 = self.'length'()
    if pos >= $I0 goto got_tree

    $I0 = ord input, pos
    $S0 = chr $I0

    if $S0 == "\t" goto matched
    if $S0 == ' ' goto matched

    $P0 = self.'EndOfLine'(input, pos)
    if null $P0 goto got_tree
    tree = self.'new_node'($P0, 'name'=>'Space', 'src'=>$S0, 'pos'=>pos)
    .return(tree)

  matched:
    $I0 = pos + 1
    self.'pos'($I0)
    tree = self.'new_node'('name'=>'Space', 'src'=>$S0, 'pos'=>pos)
    self.'set_memo'(pos, 'Space', tree)

  got_tree:
    .return (tree)
.end

.sub 'EndOfLine' :method
    .param pmc args :slurpy
    .tailcall self.'token'(args :flat, 'EndOfLine', "\n")
.end

.sub 'EndOfFile' :method
    .param string input
    .param int pos
    .local pmc tree

    tree = self.'get_memo'(pos, 'EndOfFile')
    unless null tree goto got_tree

    $I0 = self.'length'()
    if pos == $I0 goto matched
    goto got_tree

  matched:
    self.'pos'(pos)
    tree = self.'new_node'('name'=>'EndOfFile', 'pos'=>pos)
    self.'set_memo'(pos, 'EndOfFile', tree)

  got_tree:
    .return (tree)
.end

# Local Variables:
#   mode: pir
#   fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: