This file is indexed.

/usr/share/perl5/PPIx/Regexp/Tokenizer.pm is in libppix-regexp-perl 0.050-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
package PPIx::Regexp::Tokenizer;

use strict;
use warnings;

use base qw{ PPIx::Regexp::Support };

use Carp qw{ carp croak confess };
use PPIx::Regexp::Constant qw{
    MINIMUM_PERL
    TOKEN_LITERAL
    TOKEN_UNKNOWN
};
use PPIx::Regexp::Token::Assertion		();
use PPIx::Regexp::Token::Backreference		();
use PPIx::Regexp::Token::Backtrack		();
use PPIx::Regexp::Token::CharClass::POSIX	();
use PPIx::Regexp::Token::CharClass::POSIX::Unknown	();
use PPIx::Regexp::Token::CharClass::Simple	();
use PPIx::Regexp::Token::Code			();
use PPIx::Regexp::Token::Comment		();
use PPIx::Regexp::Token::Condition		();
use PPIx::Regexp::Token::Control		();
use PPIx::Regexp::Token::Delimiter		();
use PPIx::Regexp::Token::Greediness		();
use PPIx::Regexp::Token::GroupType::Assertion	();
use PPIx::Regexp::Token::GroupType::BranchReset	();
use PPIx::Regexp::Token::GroupType::Code	();
use PPIx::Regexp::Token::GroupType::Modifier	();
use PPIx::Regexp::Token::GroupType::NamedCapture	();
use PPIx::Regexp::Token::GroupType::Subexpression	();
use PPIx::Regexp::Token::GroupType::Switch	();
use PPIx::Regexp::Token::Interpolation		();
use PPIx::Regexp::Token::Literal		();
use PPIx::Regexp::Token::Modifier		();
use PPIx::Regexp::Token::Operator		();
use PPIx::Regexp::Token::Quantifier		();
use PPIx::Regexp::Token::Recursion		();
use PPIx::Regexp::Token::Structure		();
use PPIx::Regexp::Token::Unknown		();
use PPIx::Regexp::Token::Whitespace		();
use PPIx::Regexp::Util qw{ __choose_tokenizer_class __instance };
use Scalar::Util qw{ looks_like_number };

our $VERSION = '0.050';

our $DEFAULT_POSTDEREF;
defined $DEFAULT_POSTDEREF
    or $DEFAULT_POSTDEREF = 1;

{
    # Names of classes containing tokenization machinery. There are few
    # known ordering requirements, since each class recognizes its own,
    # and I have tried to prevent overlap. Absent such constraints, the
    # order is in perceived frequency of acceptance, to keep the search
    # as short as possible. If I were conscientious I would gather
    # statistics on this.
    my @classes = (	# TODO make readonly when acceptable way appears
	'PPIx::Regexp::Token::Literal',
	'PPIx::Regexp::Token::Interpolation',
	'PPIx::Regexp::Token::Control',			# Note 1
	'PPIx::Regexp::Token::CharClass::Simple',	# Note 2
	'PPIx::Regexp::Token::Quantifier',
	'PPIx::Regexp::Token::Greediness',
	'PPIx::Regexp::Token::CharClass::POSIX',	# Note 3
	'PPIx::Regexp::Token::Structure',
	'PPIx::Regexp::Token::Assertion',
	'PPIx::Regexp::Token::Backreference',
	'PPIx::Regexp::Token::Operator',		# Note 4
    );

    # Note 1: If we are in quote mode ( \Q ... \E ), Control makes a
    #		literal out of anything it sees other than \E. So it
    #		needs to come before almost all other tokenizers. Not
    #		Literal, which already makes literals, and not
    #		Interpolation, which is legal in quote mode, but
    #		everything else.

    # Note 2: CharClass::Simple must come after Literal, because it
    #		relies on Literal to recognize a Unicode named character
    #		( \N{something} ), so any \N that comes through to it
    #		must be the \N simple character class (which represents
    #		anything but a newline, and was introduced in Perl
    #		5.11.0.

    # Note 3: CharClass::POSIX has to come before Structure, since both
    #		look for square brackets, and CharClass::POSIX is the
    #		more particular.

    # Note 4: Operator relies on Literal making the characters literal
    #		if they appear in a context where they can not be
    #		operators, and Control making them literals if quoting,
    #		so it must come after both.

    # Return the declared tokenizer classes.
    sub __tokenizer_classes {
	return @classes;
    }

}

{
    my $errstr;

    sub new {
	my ( $class, $re, %args ) = @_;
	ref $class and $class = ref $class;

	$errstr = undef;

	exists $args{default_modifiers}
	    and 'ARRAY' ne ref $args{default_modifiers}
	    and do {
		$errstr = 'default_modifiers must be an array reference';
		return;
	    };

	my $self = {
	    capture => undef,	# Captures from find_regexp.
	    content => undef,	# The string we are tokenizing.
	    cookie => {},	# Cookies
	    cursor_curr => 0,	# The current position in the string.
	    cursor_limit => undef, # The end of the portion of the
	    			   # string being tokenized.
	    cursor_orig => undef, # Position of cursor when tokenizer
	    			# called. Used by get_token to prevent
				# recursion.
	    cursor_modifiers => undef,	# Position of modifiers.
	    default_modifiers => $args{default_modifiers} || [],
	    delimiter_finish => undef,	# Finishing delimiter of regexp.
	    delimiter_start => undef,	# Starting delimiter of regexp.
	    encoding => $args{encoding}, # Character encoding.
	    expect => undef,	# Extra classes to expect.
	    expect_next => undef, # Extra classes as of next parse cycle
	    failures => 0,	# Number of parse failures.
	    find => undef,	# String for find_regexp
	    known => {},	# Known tokenizers, by mode.
	    match => undef,	# Match from find_regexp.
	    mode => 'init',	# Initialize
	    modifiers => [{}],	# Modifier hash.
	    pending => [],	# Tokens made but not returned.
	    postderef => defined $args{postderef} ?
		$args{postderef} :
		$DEFAULT_POSTDEREF,
	    prior => TOKEN_UNKNOWN,	# Prior significant token.
	    source => $re,	# The object we were initialized with.
	    strict => $args{strict},	# like "use re 'strict';".
	    trace => __PACKAGE__->__defined_or(
		$args{trace}, $ENV{PPIX_REGEXP_TOKENIZER_TRACE}, 0 ),
	};

	if ( __instance( $re, 'PPI::Element' ) ) {
	    defined( my $tokenizer_class = __choose_tokenizer_class(
		    $re, { parse => 'guess' } ) )
		or return __set_errstr( ref $re, 'not supported' );
	    $class eq $tokenizer_class
		or return __set_errstr( ref $re, 'not supported by', $class );
	    # TODO conditionalizstion on PPI class does not really
	    # belong here, but at the moment I have no other idea of
	    # where to put it.
	    $self->{content} = $re->isa( 'PPI::Token::HereDoc' ) ?
		join( '', $re->content(), "\n", $re->heredoc(),
		    $re->terminator(), "\n" ) :
		$re->content();
	} elsif ( ref $re ) {
	    return __set_errstr( ref $re, 'not supported' );
	} else {
	    $self->{content} = $re;
	}

	bless $self, $class;

	$self->{content} = $self->decode( $self->{content} );

	$self->{cursor_limit} = length $self->{content};

	$self->{trace}
	    and warn "\ntokenizing '$self->{content}'\n";

	return $self;
    }

    sub __set_errstr {
	$errstr = join ' ', @_;
	return;
    }

    sub errstr {
	return $errstr;
    }

}

sub capture {
    my ( $self ) = @_;
    $self->{capture} or return;
    defined wantarray or return;
    return wantarray ? @{ $self->{capture} } : $self->{capture};
}

sub content {
    my ( $self ) = @_;
    return $self->{content};
}

sub cookie {
    my ( $self, $name, @args ) = @_;
    defined $name
	or confess "Programming error - undefined cookie name";
    @args or return $self->{cookie}{$name};
    my $cookie = shift @args;
    if ( ref $cookie eq 'CODE' ) {
	return ( $self->{cookie}{$name} = $cookie );
    } elsif ( defined $cookie ) {
	confess "Programming error - cookie must be CODE ref or undef";
    } else {
	return delete $self->{cookie}{$name};
    }
}

sub default_modifiers {
    my ( $self ) = @_;
    return [ @{ $self->{default_modifiers} } ];
}

sub __effective_modifiers {
    my ( $self ) = @_;
    'HASH' eq ref $self->{effective_modifiers}
	or return {};
    return { %{ $self->{effective_modifiers} } };
}

sub encoding {
    my ( $self ) = @_;
    return $self->{encoding};
}

sub expect {
    my ( $self, @args ) = @_;

    @args
	or return;

    $self->{expect_next} = [
	map { m/ \A PPIx::Regexp:: /smx ? $_ : 'PPIx::Regexp::' . $_ }
	@args
    ];
    $self->{expect} = undef;
    return;
}

sub failures {
    my ( $self ) = @_;
    return $self->{failures};
}

sub find_matching_delimiter {
    my ( $self ) = @_;
    $self->{cursor_curr} ||= 0;
    my $start = substr
	$self->{content},
	$self->{cursor_curr},
	1;

    my $inx = $self->{cursor_curr};
    my $finish = (
	my $bracketed = $self->close_bracket( $start ) ) || $start;

=begin comment

    $self->{trace}
	and warn "Find matching delimiter: Start with '$start' at $self->{cursor_curr}, end with '$finish' at or before $self->{cursor_limit}\n";

=end comment

=cut

    my $nest = 0;

    while ( ++$inx < $self->{cursor_limit} ) {
	my $char = substr $self->{content}, $inx, 1;

=begin comment

	$self->{trace}
	    and warn "    looking at '$char' at $inx, nest level $nest\n";

=end comment

=cut

	if ( $char eq '\\' && $finish ne '\\' ) {
	    ++$inx;
	} elsif ( $bracketed && $char eq $start ) {
	    ++$nest;
	} elsif ( $char eq $finish ) {
	    --$nest < 0
		and return $inx - $self->{cursor_curr};
	}
    }

    return;
}

sub find_regexp {
    my ( $self, $regexp ) = @_;

    ref $regexp eq 'Regexp'
	or confess
	'Argument is a ', ( ref $regexp || 'scalar' ), ' not a Regexp';

    defined $self->{find} or $self->_remainder();

    $self->{find} =~ $regexp
	or return;

    my @capture;
    foreach my $inx ( 0 .. $#+ ) {
	if ( defined $-[$inx] && defined $+[$inx] ) {
	push @capture, $self->{capture} = substr
		    $self->{find},
		    $-[$inx],
		    $+[$inx] - $-[$inx];
	} else {
	    push @capture, undef;
	}
    }
    $self->{match} = shift @capture;
    $self->{capture} = \@capture;

    # The following circumlocution seems to be needed under Perl 5.13.0
    # for reasons I do not fathom -- at least in the case where
    # wantarray is false. RT 56864 details the symptoms, which I was
    # never able to reproduce outside Perl::Critic. But returning $+[0]
    # directly, the value could transmogrify between here and the
    # calling module.
##  my @data = ( $-[0], $+[0] );
##  return wantarray ? @data : $data[1];
    return wantarray ? ( $-[0] + 0, $+[0] + 0 ) : $+[0] + 0;
}

sub get_mode {
    my ( $self ) = @_;
    return $self->{mode};
}

sub get_start_delimiter {
    my ( $self ) = @_;
    return $self->{delimiter_start};
}

sub get_token {
    my ( $self ) = @_;

    caller eq __PACKAGE__ or $self->{cursor_curr} > $self->{cursor_orig}
	or confess 'Programming error - get_token() called without ',
	    'first calling make_token()';

    my $handler = '__PPIX_TOKENIZER__' . $self->{mode};

    my $code = $self->can( $handler )
	or confess 'Programming error - ',
	    "Getting token in mode '$self->{mode}'. ",
	    "cursor_curr = $self->{cursor_curr}; ",
	    "cursor_limit = $self->{cursor_limit}; ",
	    "length( content ) = ", length $self->{content},
	    "; content = '$self->{content}'";

    my $character = substr(
	$self->{content},
	$self->{cursor_curr},
	1
    );

    $self->{trace}
	and warn "get_token() got '$character' from $self->{cursor_curr}\n";

    return ( $code->( $self, $character ) );
}

sub interpolates {
    my ( $self ) = @_;
    return $self->{delimiter_start} ne q{'};
}

sub make_token {
    my ( $self, $length, $class, $arg ) = @_;
    defined $class or $class = caller;

    if ( $length + $self->{cursor_curr} > $self->{cursor_limit} ) {
	$length = $self->{cursor_limit} - $self->{cursor_curr}
	    or return;
    }

    $class =~ m/ \A PPIx::Regexp:: /smx
	or $class = 'PPIx::Regexp::' . $class;
    my $content = substr
	    $self->{content},
	    $self->{cursor_curr},
	    $length;

    $self->{trace}
	and warn "make_token( $length, '$class' ) => '$content'\n";
    $self->{trace} > 1
	and warn "    make_token: cursor_curr = $self->{cursor_curr}; ",
	    "cursor_limit = $self->{cursor_limit}\n";
    my $token = $class->__new( $content,
	tokenizer	=> $self,
	%{ $arg || {} } )
	or return;

    $token->significant()
	and $self->{expect} = undef;

    $token->isa( TOKEN_UNKNOWN ) and $self->{failures}++;

    $self->{cursor_curr} += $length;
    $self->{find} = undef;
    $self->{match} = undef;
    $self->{capture} = undef;

    foreach my $name ( keys %{ $self->{cookie} } ) {
	my $cookie = $self->{cookie}{$name};
	$cookie->( $self, $token )
	    or delete $self->{cookie}{$name};
    }

    # Record this token as the prior token if it is significant. We must
    # do this after processing cookies, so that the cookies have access
    # to the old token if they want.
    $token->significant()
	and $self->{prior_significant_token} = $token;

    return $token;
}

sub match {
    my ( $self ) = @_;
    return $self->{match};
}

sub modifier {
    my ( $self, $modifier ) = @_;
    return PPIx::Regexp::Token::Modifier::__asserts(
	$self->{modifiers}[-1], $modifier );
}

sub modifier_duplicate {
    my ( $self ) = @_;
    push @{ $self->{modifiers} },
	{ %{ $self->{modifiers}[-1] } };
    return;
}

sub modifier_modify {
    my ( $self, %args ) = @_;

    # Modifier code is centralized in PPIx::Regexp::Token::Modifier
    $self->{modifiers}[-1] =
	PPIx::Regexp::Token::Modifier::__PPIX_TOKENIZER__modifier_modify(
	$self->{modifiers}[-1], \%args );

    return;

}

sub modifier_pop {
    my ( $self ) = @_;
    @{ $self->{modifiers} } > 1
	and pop @{ $self->{modifiers} };
    return;
}

sub next_token {
    my ( $self ) = @_;

    {

	if ( @{ $self->{pending} } ) {
	    return shift @{ $self->{pending} };
	}

	if ( $self->{cursor_curr} >= $self->{cursor_limit} ) {
	    $self->{cursor_limit} >= length $self->{content}
		and return;
	    $self->{mode} eq 'finish' and return;
	    $self->_set_mode( 'finish' );
	    $self->{cursor_limit} += length $self->{delimiter_finish};
	}

	if ( my @tokens = $self->get_token() ) {
	    push @{ $self->{pending} }, @tokens;
	    redo;

	}

    }

    return;

}

sub peek {
    my ( $self, $offset ) = @_;
    defined $offset or $offset = 0;
    $offset < 0 and return;
    $offset += $self->{cursor_curr};
    $offset >= $self->{cursor_limit} and return;
    return substr $self->{content}, $offset, 1;
}

sub ppi_document {
    my ( $self ) = @_;

    defined $self->{find} or $self->_remainder();

    return PPI::Document->new( \"$self->{find}" );
}

sub prior {
    my ( $self, $method, @args ) = @_;
    $self->_deprecation_notice( method => 'prior',
	'prior_significant_token()' );
    defined $method or return $self->{prior_significant_token};
    $self->{prior_significant_token}->can( $method )
	or confess 'Programming error - ',
	    ( ref $self->{prior_significant_token} ||
		$self->{prior_significant_token} ),
	    ' does not support method ', $method;
    return $self->{prior_significant_token}->$method( @args );
}

sub prior_significant_token {
    my ( $self, $method, @args ) = @_;
    defined $method or return $self->{prior_significant_token};
    $self->{prior_significant_token}->can( $method )
	or confess 'Programming error - ',
	    ( ref $self->{prior_significant_token} ||
		$self->{prior_significant_token} ),
	    ' does not support method ', $method;
    return $self->{prior_significant_token}->$method( @args );
}

# my $length = $token->__recognize_postderef( $tokenizer, $iterator ).
#
# This method is private to the PPIx-Regexp package, and may be changed
# or retracted without warning. What it does is to recognize postfix
# dereferences. It returns the length in characters of the first postfix
# dereference found, or a false value if none is found. This returns
# false immediately unless the tokenizer was instantiated with the
# C<postderef> argument true, or if it was not specified and
# C<$DEFAULT_POSTDEREF> was true when the tokenizer was instantiated.
#
# The optional $iterator argument can be one of the following:
#   - A code reference, which will be called to provide PPI::Element
#     objects to be checked to see if they represent a postfix
#     dereference.
#   - A PPI::Element, which is checked to see if it is a postfix
#     dereference.
#   - Undef, or omitted, in which case ppi() is called on the invocant,
#     and everything that follows the '->' operator is checked to see if
#     it is a postfix dereference.
#   - Anything else results in an exception and stack trace.

{
    # %* &* **
    my %magic_var = map { $_ => 1 } qw{ @* $* };
    my %magic_oper = map { $_ => 1 } qw{ & ** % };
    my %sliceable = map { $_ => 1 } qw{ @ % };
    my %post_slice = map { $_ => 1 } qw< { [ >;	# ] }

    sub __recognize_postderef {
	my ( $self, $token, $iterator ) = @_;
	$self->{postderef}
	    or return;
	# Note that if ppi() gets called I have to hold a reference to
	# the returned object until I am done with all its children.
	my $ppi;
	if ( ! defined $iterator ) {
	    $ppi = $token->ppi();
	    my @ops = grep { '->' eq $_->content() } @{
		$ppi->find( 'PPI::Token::Operator' ) || [] };
	    $iterator = sub {
		my $op = shift @ops
		    or return;
		return $op->snext_sibling();
	    };
	} elsif ( $iterator->isa( 'PPI::Element' ) ) {
	    my @eles = ( $iterator );
	    $iterator = sub {
		return shift @eles;
	    };
	} elsif ( 'CODE' ne ref $iterator ) {
	    confess 'Programming error - Iterator not understood';
	}

	my $accept = $token->__postderef_accept_cast();

	while ( my $elem = $iterator->() ) {

	    my $content = $elem->content();
	    $content =~ m/ \A ( . \#? ) /smx
		and $accept->{$1}
		or next;

	    my $length = length $content;

	    # PPI parses '$x->@*' as containing magic variable '@*'.
	    # Similarly for '$*' and '$#*'. I think this is a bug, and
	    # they should be parsed as casts, but ...
	    if ( $elem->isa( 'PPI::Token::Magic' ) ) {
		$magic_var{$content}
		    and return $length;
		if ( '$#' eq $content ) {
		    my $next = $elem->snext_sibling()
			or return $length;
		    '*' eq substr $next->content(), 0, 1
			and return $length + 1;
		}
	    }

	    # PPI parses '%*' as a cast of '%' followed by a splat, but
	    # I think it is likely that if it ever supports postderef
	    # operators that they will be casts. It currently parses
	    # '**' as an operator and '&*' as two operators, but the
	    # logic is pretty much the same as for a cast, so they get
	    # handled here too.
	    if ( $elem->isa( 'PPI::Token::Cast' ) || $elem->isa(
		    'PPI::Token::Operator' ) && $magic_oper{$content} ) {
		# Maybe PPI will eventually parse something like '$*' as
		# a cast, so ...
		$content =~ m/ [*] \z /smx
		    and return $length;
		# Or maybe it will parse the asterisk separately, but I
		# have no idea what its class will be.
		my $next = $elem->snext_sibling()
		    or return;
		my $next_content = $next->content();
		my $next_char = substr $next_content, 0, 1;
		'*' eq $next_char
		    and return $length + 1;
		# We may still have a slice.
		$sliceable{$content}
		    and $post_slice{$next_char}
		    and return $length + length $next_content;
		# TODO maybe PPI will do something completely
		# unanticipated with postderef.
	    }

	    # Otherwise, we're not a postfix dereference; try the next
	    # iteration.
	}

	# No postfix dereference found.
	return;
    }
}

sub significant {
    return 1;
}

sub strict {
    my ( $self ) = @_;
    return $self->{strict};
}

sub _known_tokenizers {
    my ( $self ) = @_;

    my $mode = $self->{mode};

    my @expect;
    if ( $self->{expect_next} ) {
	$self->{expect} = $self->{expect_next};
	$self->{expect_next} = undef;
    }
    if ( $self->{expect} ) {
	@expect = $self->_known_tokenizer_check(
	    @{ $self->{expect} } );
    }

    exists $self->{known}{$mode} and return (
	@expect, @{ $self->{known}{$mode} } );

    my @found = $self->_known_tokenizer_check(
	$self->__tokenizer_classes() );

    $self->{known}{$mode} = \@found;
    return (@expect, @found);
}

sub _known_tokenizer_check {
    my ( $self, @args ) = @_;

    my $handler = '__PPIX_TOKENIZER__' . $self->{mode};
    my @found;

    foreach my $class ( @args ) {

	$class->can( $handler ) or next;
	push @found, $class;

    }

    return @found;
}

sub tokens {
    my ( $self ) = @_;

    my @rslt;
    while ( my $token = $self->next_token() ) {
	push @rslt, $token;
    }

    return @rslt;
}

#	$self->_deprecation_notice( $type, $name );
#
#	This method centralizes deprecation. Type is 'attribute' or
#	'method'. Deprecation is driven of the %deprecate hash. Values
#	are:
#	    false - no warning
#	    1 - warn on first use
#	    2 - warn on each use
#	    3 - die on each use.
#
#	$self->_deprecation_in_progress( $type, $name )
#
#	This method returns true if the deprecation is in progress. In
#	fact it returns the deprecation level.

{

    my %deprecate = (
	attribute => {
	},
	method => {
	    prior	=> 0,
	},
    );

    sub _deprecation_notice {
	my ( undef, $type, $name, $repl ) = @_;		# Invocant unused
	$deprecate{$type} or return;
	$deprecate{$type}{$name} or return;
	my $msg = sprintf 'The %s %s is %s', $name, $type,
	    $deprecate{$type}{$name} > 2 ? 'removed' : 'deprecated';
	defined $repl
	    and $msg .= "; use $repl instead";
	$deprecate{$type}{$name} >= 3
	    and croak $msg;
	warnings::enabled( 'deprecated' )
	    and carp $msg;
	$deprecate{$type}{$name} == 1
	    and $deprecate{$type}{$name} = 0;
	return;
    }

=begin comment

    sub _deprecation_in_progress {
	my ( $self, $type, $name ) = @_;
	$deprecate{$type} or return;
	return $deprecate{$type}{$name};
    }

=end comment

=cut

}

sub _remainder {
    my ( $self ) = @_;

    $self->{cursor_curr} > $self->{cursor_limit}
	and confess "Programming error - Trying to find past end of string";
    $self->{find} = substr(
	$self->{content},
	$self->{cursor_curr},
	$self->{cursor_limit} - $self->{cursor_curr}
    );

    return;
}

sub _make_final_token {
    my ( $self, $len, $class, $arg ) = @_;
    my $token = $self->make_token( $len, $class, $arg );
    $self->_set_mode( 'kaput' );
    return $token;
}

sub _set_mode {
    my ( $self, $mode ) = @_;
    $self->{trace}
	and warn "Tokenizer going from mode $self->{mode} to $mode\n";
    $self->{mode} = $mode;
    if ( 'kaput' eq $mode ) {
	$self->{cursor_curr} = $self->{cursor_limit} =
	    length $self->{content};
    }
    return;
}

sub __init_error {
    my ( $self , $err ) = @_;
    defined $err
	or $err = 'Tokenizer found illegal first characters';
    return $self->_make_final_token(
	length $self->{content}, TOKEN_UNKNOWN, {
	    error	=> $err,
	},
    );
}

sub __PPIX_TOKENIZER__init {
    my ( $self ) = @_;

    $self->find_regexp(
	qr{ \A ( \s* ) ( qr | m | s )? ( \s* ) (?: [^\w\s] ) }smx )
	or return $self->__init_error();

    my ( $leading_white, $type, $next_white ) = $self->capture();

    defined $type
	or $type = '';

    $self->{type} = $type;

    my @tokens;

    '' ne $leading_white
	and push @tokens, $self->make_token( length $leading_white,
	'PPIx::Regexp::Token::Whitespace' );
    push @tokens, $self->make_token( length $type,
	'PPIx::Regexp::Token::Structure' );
    '' ne $next_white
	and push @tokens, $self->make_token( length $next_white,
	'PPIx::Regexp::Token::Whitespace' );

    $self->{delimiter_start} = substr
	$self->{content},
	$self->{cursor_curr},
	1;

    $self->{trace}
	and warn "Tokenizer found regexp start delimiter '$self->{delimiter_start}' at $self->{cursor_curr}\n";

    if ( my $offset = $self->find_matching_delimiter() ) {
	my $cursor_limit = $self->{cursor_curr} + $offset;
	$self->{trace}
	    and warn "Tokenizer found regexp end delimiter at $cursor_limit\n";
	if ( $self->__number_of_extra_parts() ) {
###	    my $found_embedded_comments;
	    if ( $self->close_bracket(
		    $self->{delimiter_start} ) ) {
		pos $self->{content} = $self->{cursor_curr} +
		$offset + 1;
		# If we're bracketed, there may be Perl comments between
		# the regex and the replacement. PPI gets the parse
		# wrong as of 1.220, but if we get the handling of the
		# underlying string right, we will Just Work when PPI
		# gets it right.
		while ( $self->{content} =~
		    m/ \G \s* \n \s* \# [^\n]* /smxgc ) {
##		    $found_embedded_comments = 1;
		}
		$self->{content} =~ m/ \s* /smxgc;
	    } else {
		pos $self->{content} = $self->{cursor_curr} +
		$offset;
	    }
	    # Localizing cursor_curr and delimiter_start would be
	    # cleaner, but I don't want the old values restored if a
	    # parse error occurs.
	    my $cursor_curr = $self->{cursor_curr};
	    my $delimiter_start = $self->{delimiter_start};
	    $self->{cursor_curr} = pos $self->{content};
	    $self->{delimiter_start} = substr
		$self->{content},
		$self->{cursor_curr},
		1;
	    $self->{trace}
		and warn "Tokenizer found replacement start delimiter '$self->{delimiter_start}' at $self->{cursor_curr}\n";
	    if ( my $s_off = $self->find_matching_delimiter() ) {
		$self->{cursor_modifiers} =
		    $self->{cursor_curr} + $s_off + 1;
		$self->{trace}
		    and warn "Tokenizer found replacement end delimiter at @{[
			$self->{cursor_curr} + $s_off ]}\n";
		$self->{cursor_curr} = $cursor_curr;
		$self->{delimiter_start} = $delimiter_start;
	    } else {
		$self->{trace}
		    and warn 'Tokenizer failed to find replacement',
			"end delimiter starting at $self->{cursor_curr}\n";
		$self->{cursor_curr} = 0;
		# TODO If I were smart enough here I could check for
		# PPI mis-parses like s{foo}
		#                     #{bar}
		#                      {baz}
		# here, doing so if $found_embedded_comments (commented
		# out above) is true. The problem is that there seem to
		# as many mis-parses as there are possible delimiters.
		return $self->__init_error(
		    'Tokenizer found mismatched replacement delimiters',
		);
	    }
	} else {
	    $self->{cursor_modifiers} = $cursor_limit + 1;
	}
	$self->{cursor_limit} = $cursor_limit;
    } else {
	$self->{cursor_curr} = 0;
	return $self->_make_final_token(
	    length( $self->{content} ), TOKEN_UNKNOWN, {
		error	=> 'Tokenizer found mismatched regexp delimiters',
	    },
	);
    }

    {
	my @mods = @{ $self->{default_modifiers} };
	pos $self->{content} = $self->{cursor_modifiers};
	local $self->{cursor_curr} = $self->{cursor_modifiers};
	local $self->{cursor_limit} = length $self->{content};
	my @trailing;
	{
	    my $len = $self->find_regexp( qr{ \A [[:lower:]]* }smx );
	    push @trailing, $self->make_token( $len,
		'PPIx::Regexp::Token::Modifier' );
	}
	if ( my $len = $self->find_regexp( qr{ \A \s+ }smx ) ) {
	    push @trailing, $self->make_token( $len,
		'PPIx::Regexp::Token::Whitespace' );
	}
	if ( my $len = $self->find_regexp( qr{ \A .+ }smx ) ) {
	    push @trailing, $self->make_token( $len, TOKEN_UNKNOWN, {
		    error	=> 'Trailing characters after expression',
		} );
	}
	$self->{trailing_tokens} = \@trailing;
	push @mods, $trailing[0]->content();
	$self->{effective_modifiers} =
	    PPIx::Regexp::Token::Modifier::__aggregate_modifiers (
		@mods );
	$self->{modifiers} = [
	    { %{ $self->{effective_modifiers} } },
	];
    }

    $self->{delimiter_finish} = substr
	$self->{content},
	$self->{cursor_limit},
	1;

    push @tokens, $self->make_token( 1,
	'PPIx::Regexp::Token::Delimiter' );

    $self->_set_mode( 'regexp' );

    $self->{find} = undef;

    return @tokens;
}

# Match the initial part of the regexp including any leading white
# space. The initial delimiter is the first thing not consumed, though
# we check it for sanity.
sub __initial_match {
    my ( $self ) = @_;

    $self->find_regexp(
	qr{ \A ( \s* ) ( qr | m | s )? ( \s* ) (?: [^\w\s] ) }smx )
	or return;

    my ( $leading_white, $type, $next_white ) = $self->capture();

    defined $type
	or $type = '';

    $self->{type} = $type;

    my @tokens;

    '' ne $leading_white
	and push @tokens, $self->make_token( length $leading_white,
	'PPIx::Regexp::Token::Whitespace' );
    push @tokens, $self->make_token( length $type,
	'PPIx::Regexp::Token::Structure' );
    '' ne $next_white
	and push @tokens, $self->make_token( length $next_white,
	'PPIx::Regexp::Token::Whitespace' );

    return @tokens;
}

{
    my %extra_parts = (
	s	=> 1,
    );

    # Return the number of extra delimited parts. This will be 0 except
    # for s///, which will be 1.
    sub __number_of_extra_parts {
	my ( $self ) = @_;
	return $extra_parts{$self->{type}} || 0;
    }
}

{
    my @part_class = qw{
	PPIx::Regexp::Structure::Regexp
	PPIx::Regexp::Structure::Replacement
    };

    # Return the classes for the parts of the expression.
    sub __part_classes {
	my ( $self ) = @_;
	my $max = $self->__number_of_extra_parts();
	return @part_class[ 0 .. $max ];
    }
}

sub __PPIX_TOKENIZER__regexp {
    my ( $self, $character ) = @_;

    my $mode = $self->{mode};
    my $handler = '__PPIX_TOKENIZER__' . $mode;

    $self->{cursor_orig} = $self->{cursor_curr};
    foreach my $class ( $self->_known_tokenizers() ) {
	my @tokens = grep { $_ } $class->$handler( $self, $character );
	$self->{trace}
	    and warn $class, "->$handler( \$self, '$character' )",
		" => (@tokens)\n";
	@tokens
	    and return ( map {
		ref $_ ? $_ : $self->make_token( $_,
		    $class ) } @tokens );
    }

    # Find a fallback processor for the character.
    my $fallback = __PACKAGE__->can( '__PPIX_TOKEN_FALLBACK__' . $mode )
	|| __PACKAGE__->can( '__PPIX_TOKEN_FALLBACK__regexp' )
	|| confess "Programming error - unable to find fallback for $mode";
    return $fallback->( $self, $character );
}

*__PPIX_TOKENIZER__repl = \&__PPIX_TOKENIZER__regexp;

sub __PPIX_TOKEN_FALLBACK__regexp {
    my ( $self, $character ) = @_;

    # As a fallback in regexp mode, any escaped character is a literal.
    if ( $character eq '\\'
	&& $self->{cursor_limit} - $self->{cursor_curr} > 1
    ) {
	return $self->make_token( 2, TOKEN_LITERAL );
    }

    # Any normal character is unknown.
    return $self->make_token( 1, TOKEN_UNKNOWN, {
	    error	=> 'Tokenizer found unexpected literal',
	},
    );
}

sub __PPIX_TOKEN_FALLBACK__repl {
    my ( $self, $character ) = @_;

    # As a fallback in replacement mode, any escaped character is a literal.
    if ( $character eq '\\'
	&& defined ( my $next = $self->peek( 1 ) ) ) {

	if ( $self->interpolates() || $next eq q<'> || $next eq '\\' ) {
	    return $self->make_token( 2, TOKEN_LITERAL );
	}
	return $self->make_token( 1, TOKEN_LITERAL );
    }

    # So is any normal character.
    return $self->make_token( 1, TOKEN_LITERAL );
}

sub __PPIX_TOKENIZER__finish {
    my ( $self ) = @_;		# $character unused

    $self->{cursor_limit} > length $self->{content}
	and confess "Programming error - ran off string";

    my @tokens = $self->make_token( length $self->{delimiter_finish},
	'PPIx::Regexp::Token::Delimiter' );

    if ( $self->{cursor_curr} == $self->{cursor_modifiers} ) {

	# We are out of string. Add the trailing tokens (created when we
	# did the initial bracket scan) and close up shop.

	push @tokens, @{ delete $self->{trailing_tokens} };

	$self->_set_mode( 'kaput' );

    } else {

	# Clear the cookies, because we are going around again.
	$self->{cookie} = {};

	# Move the cursor limit to just before the modifiers.
	$self->{cursor_limit} = $self->{cursor_modifiers} - 1;

	# If the preceding regular expression was bracketed, we need to
	# consume possible whitespace and find another delimiter.

	if ( $self->close_bracket( $self->{delimiter_start} ) ) {
	    my $accept;
	    # If we are bracketed, there can be honest-to-God Perl
	    # comments between the regexp and the replacement, not just
	    # regexp comments. As of version 1.220, PPI does not get
	    # this parse right, but if we can handle this is a string,
	    # then we will Just Work when PPI gets itself straight.
	    while ( $self->find_regexp(
		    qr{ \A ( \s* \n \s* ) ( \# [^\n]* \n ) }smx ) ) {
		my ( $white_space, $comment ) = $self->capture();
		push @tokens, $self->make_token(
		    length $white_space,
		    'PPIx::Regexp::Token::Whitespace',
		), $self->make_token(
		    length $comment,
		    'PPIx::Regexp::Token::Comment',
		);
	    }
	    $accept = $self->find_regexp( qr{ \A \s+ }smx )
		and push @tokens, $self->make_token(
		$accept, 'PPIx::Regexp::Token::Whitespace' );
	    my $character = $self->peek();
	    $self->{delimiter_start} = $character;
	    push @tokens, $self->make_token(
		1, 'PPIx::Regexp::Token::Delimiter' );
	    $self->{delimiter_finish} = substr
		$self->{content},
		$self->{cursor_limit} - 1,
		1;
	}

	if ( $self->modifier( 'e*' ) ) {
	    # With /e or /ee, the replacement portion is code. We make
	    # it all into one big PPIx::Regexp::Token::Code, slap on the
	    # trailing delimiter and modifiers, and return it all.
	    push @tokens, $self->make_token(
		$self->{cursor_limit} - $self->{cursor_curr},
		'PPIx::Regexp::Token::Code',
		{ perl_version_introduced => MINIMUM_PERL },
	    );
	    $self->{cursor_limit} = length $self->{content};
	    push @tokens, $self->make_token( 1,
		'PPIx::Regexp::Token::Delimiter' ),
		@{ delete $self->{trailing_tokens} };
	    $self->_set_mode( 'kaput' );
	} else {
	    # Put our mode to replacement.
	    $self->_set_mode( 'repl' );
	}

    }

    return @tokens;

}

1;

__END__

=head1 NAME

PPIx::Regexp::Tokenizer - Tokenize a regular expression

=head1 SYNOPSIS

 use PPIx::Regexp::Dumper;
 PPIx::Regexp::Dumper->new( 'qr{foo}smx' )
     ->print();

=head1 INHERITANCE

C<PPIx::Regexp::Tokenizer> is a
L<PPIx::Regexp::Support|PPIx::Regexp::Support>.

C<PPIx::Regexp::Tokenizer> has no descendants.

=head1 DESCRIPTION

This class provides tokenization of the regular expression.

=head1 METHODS

This class provides the following public methods. Methods not documented
here (or documented below under L</EXTERNAL TOKENIZERS>) are private,
and unsupported in the sense that the author reserves the right to
change or remove them without notice.

=head2 new

 my $tokenizer = PPIx::Regexp::Tokenizer->new( 'xyzzy' );

This static method instantiates the tokenizer. You must pass it the
regular expression to be parsed, either as a string or as a
L<PPI::Element|PPI::Element> of some sort. You can also pass optional
name/value pairs of arguments. The option names are specified B<without>
a leading dash. Supported options are:

=over

=item default_modifiers array_reference

This argument specifies default statement modifiers. It is optional, but
if specified must be an array reference. See the
L<PPIx::Regexp|PPIx::Regexp> L<new()|PPIx::Regexp/new> documentation for
the details.

=item encoding name

This option specifies the encoding of the string to be tokenized. If
specified, an C<Encode::decode> is done on the string (or the C<content>
of the PPI class) before it is tokenized.

=item postderef boolean

This option specifies whether the tokenizer recognizes postfix
dereferencing. See the L<PPIx::Regexp|PPIx::Regexp>
L<new()|PPIx::Regexp/new> documentation for the details.

C<$PPIx::Regexp::Tokenizer::DEFAULT_POSTDEREF> is not exported.

=item strict boolean

This option specifies whether tokenization should assume
C<use re 'strict';> is in effect.

The C<'strict'> pragma was introduced in Perl 5.22, and its
documentation says that it is experimental, and that there is no
commitment to backward compatibility. The same applies to the
tokenization produced when this option is asserted.

=item trace number

Specifying a positive value for this option causes a trace of the
tokenization. This option is unsupported in the sense that the author
reserves the right to alter it without notice.

If this option is unspecified, the value comes from environment variable
C<PPIX_REGEXP_TOKENIZER_TRACE> (see L</ENVIRONMENT VARIABLES>). If this
environment variable does not exist, the default is 0.

=back

Undocumented options are unsupported.

The returned value is the instantiated tokenizer, or C<undef> if
instantiation failed. In the latter case a call to L</errstr> will
return the reason.

=head2 content

 print $tokenizer->content();

This method returns the string being tokenized. This will be the result
of the L<< PPI::Element->content()|PPI::Element/content >> method if the
object was instantiated with a L<PPI::Element|PPI::Element>.

=head2 default_modifiers

 print join ', ', @{ $tokenizer->default_modifiers() };

This method returns a reference to a copy of the array passed to the
C<default_modifiers> argument to L<new()|/new>. If this argument was not
used to instantiate the object, the return is a reference to an empty
array.

=head2 encoding

This method returns the encoding of the data being parsed, if one was
set when the class was instantiated; otherwise it simply returns undef.

=head2 errstr

 my $tokenizer = PPIx::Regexp::Tokenizer->new( 'xyzzy' )
     or die PPIx::Regexp::Tokenizer->errstr();

This static method returns an error description if tokenizer
instantiation failed.

=head2 failures

 print $tokenizer->failures(), " tokenization failures\n";

This method returns the number of tokenization failures encountered. A
tokenization failure is represented in the output token stream by a
L<PPIx::Regexp::Token::Unknown|PPIx::Regexp::Token::Unknown>.

=head2 modifier

 $tokenizer->modifier( 'x' )
     and print "Tokenizing an extended regular expression\n";

This method returns true if the given modifier character was found on
the end of the regular expression, and false otherwise.

Starting with version 0.036_01, if the argument is a
single-character modifier followed by an asterisk (intended as a wild
card character), the return is the number of times that modifier
appears. In this case an exception will be thrown if you specify a
multi-character modifier (e.g.  C<'ee*'>), or if you specify one of the
match semantics modifiers (e.g.  C<'a*'>).

=head2 next_token

 my $token = $tokenizer->next_token();

This method returns the next token in the token stream, or nothing if
there are no more tokens.

=head2 significant

This method exists simply for the convenience of
L<PPIx::Regexp::Dumper|PPIx::Regexp::Dumper>. It always returns true.

=head2 tokens

 my @tokens = $tokenizer->tokens();

This method returns all remaining tokens in the token stream.

=head1 EXTERNAL TOKENIZERS

This class does very little of its own tokenization. Instead the token
classes contain external tokenization routines, whose name is
'__PPIX_TOKENIZER__' concatenated with the current mode of the tokenizer
('regexp' for regular expressions, 'repl' for the replacement string).

These external tokenizers are called as static methods, and passed the
C<PPIx::Regexp::Tokenizer> object and the current character in the
character stream.

If the external tokenizer wants to make one or more tokens, it returns
an array containing either length in characters for tokens of the
tokenizer's own class, or the results of one or more L</make_token>
calls for tokens of an arbitrary class.

If the external tokenizer is not interested in the characters starting
at the current position it simply returns.

The following methods are for the use of external tokenizers, and B<are
not part of the public interface to this class.>

=head2 capture

 if ( $tokenizer->find_regexp( qr{ \A ( foo ) }smx ) ) {
     foreach ( $tokenizer->capture() ) {
         print "$_\n";
     }
 }

This method returns all the contents of any capture buffers from the
previous call to L</find_regexp>. The first element of the array (i.e.
element 0) corresponds to C<$1>, and so on.

The captures are cleared by L</make_token>, as well as by another call
to L</find_regexp>.

=head2 cookie

 $tokenizer->cookie( foo => sub { 1 } );
 my $cookie = $tokenizer->cookie( 'foo' );
 my $old_hint = $tokenizer->cookie( foo => undef );

This method either creates, deletes, or accesses a cookie.

A cookie is a code reference which is called whenever the tokenizer makes
a token. If it returns a false value, it is deleted. Explicitly setting
the cookie to C<undef> also deletes it.

When you call C<< $tokenizer->cookie( 'foo' ) >>, the current cookie is
returned. If you pass a new value of C<undef> to delete the token, the
deleted cookie (if any) is returned.

When the L</make_token> method calls a cookie, it passes it the tokenizer
and the token just made. If a token calls a cookie, it is recommended that
it merely pass the tokenizer, though of course the token can do whatever
it wants.

The cookie mechanism seems to be a bit of a crock, but it appeared to be
more work to fix things up in the lexer after the tokenizer got
something wrong.

The recommended way to write a cookie is to use a closure to store any
necessary data, and have a call to the cookie return the data; otherwise
the ultimate consumer of the cookie has no way to access the data. Of
course, it may be that the presence of the cookie at a certain point in
the parse is all that is required.

=head2 expect

 $tokenizer->expect( 'PPIx::Regexp::Token::Code' );

This method inserts a given class at the head of the token scan, for the
next iteration only. More than one class can be specified. Class names
can be abbreviated by removing the leading 'PPIx::Regexp::'.

If no class is specified, this method does nothing.

The expectation lasts from the next time L</get_token> is called until
the next time L<make_token> makes a significant token, or until the next
C<expect> call if that is done sooner.

=head2 find_regexp

 my $end = $tokenizer->find_regexp( qr{ \A \w+ }smx );
 my ( $begin, $end ) = $tokenizer->find_regexp(
     qr{ \A \w+ }smx );

This method finds the given regular expression in the content, starting
at the current position. If called in scalar context, the offset from
the current position to the end of the matched string is returned. If
called in list context, the offsets to both the beginning and the end of
the matched string are returned.

=head2 find_matching_delimiter

 my $offset = $tokenizer->find_matching_delimiter();

This method is used by tokenizers to find the delimiter matching the
character at the current position in the content string. If the
delimiter is an opening bracket of some sort, bracket nesting will be
taken into account.

When searching for the matching delimiter, the back slash character is
considered to escape the following character, so back-slashed delimiters
will be ignored. No other quoting mechanisms are recognized, though, so
delimiters inside quotes still count. This is actually the way Perl
works, as

 $ perl -e 'qr<(?{ print "}" })>'

demonstrates.

This method returns the offset from the current position in the content
string to the matching delimiter (which will always be positive), or
undef if no match can be found.

=head2 get_mode

This method returns the name of the current mode of the tokenizer.

=head2 get_start_delimiter

 my $start_delimiter = $tokenizer->get_start_delimiter();

This method is used by tokenizers to access the start delimiter for the
regular expression.

=head2 get_token

 my $token = $tokenizer->make_token( 3 );
 my @tokens = $tokenizer->get_token();

This method returns the next token that can be made from the input
stream. It is B<not> part of the external interface, but is intended for
the use of an external tokenizer which calls it after making and
retaining its own token to look at the next token ( if any ) in the
input stream.

If any external tokenizer calls get_token without first calling
make_token, a fatal error occurs; this is better than the infinite
recursion which would occur if the condition were not trapped.

An external tokenizer B<must> return anything returned by get_token;
otherwise tokens get lost.

=head2 interpolates

This method returns true if the top-level structure being tokenized
interpolates; that is, if the delimiter is not a single quote.

=head2 make_token

 return $tokenizer->make_token( 3, 'PPIx::Regexp::Token::Unknown' );

This method is used by this class (and possibly by individual
tokenizers) to manufacture a token. Its arguments are the number of
characters to include in the token, and optionally the class of the
token. If no class name is given, the caller's class is used. Class
names may be shortened by removing the initial 'PPIx::Regexp::', which
will be restored by this method.

The token will be manufactured from the given number of characters
starting at the current cursor position, which will be adjusted.

If the given length would include characters past the end of the string
being tokenized, the length is reduced appropriately. If this means a
token with no characters, nothing is returned.

=head2 match

 if ( $tokenizer->find_regexp( qr{ \A \w+ }smx ) ) {
     print $tokenizer->match(), "\n";
 }

This method returns the string matched by the previous call to
L</find_regexp>.

The match is set to C<undef> by L</make_token>, as well as by another
call to L</find_regexp>.

=head2 modifier_duplicate

 $tokenizer->modifier_duplicate();

This method duplicates the modifiers on the top of the modifier stack,
with the intent of creating a locally-scoped copy of the modifiers. This
should only be called by an external tokenizer that is actually creating
a modifier scope. In other words, only when creating a
L<PPIx::Regexp::Token::Structure|PPIx::Regexp::Token::Structure> token
whose content is '('.

=head2 modifier_modify

 $tokenizer->modifier_modify( name => $value ... );

This method sets new values for the modifiers in the local scope. Only
the modifiers whose names are actually passed have their values changed.

This method is intended to be called after manufacturing a
L<PPIx::Regexp::Token::Modifier|PPIx::Regexp::Token::Modifier> token,
and passed the results of its C<modifiers> method.

=head2 modifier_pop

 $tokenizer->modifier_pop();

This method removes the modifiers on the top of the modifier stack. This
should only be called by an external tokenizer that is ending a modifier
scope. In other words, only when creating a
L<PPIx::Regexp::Token::Structure|PPIx::Regexp::Token::Structure> token
whose content is ')'.

Note that this method will never pop the last modifier item off the
stack, to guard against unmatched right parentheses.

=head2 peek

 my $character = $tokenizer->peek();
 my $next_char = $tokenizer->peek( 1 );

This method returns the character at the given non-negative offset from
the current position. If no offset is given, an offset of 0 is used.

If you ask for a negative offset or an offset off the end of the sting,
C<undef> is returned.

=head2 ppi_document

This method makes a PPI document out of the remainder of the string, and
returns it.

=head2 prior

 $tokenizer->prior( 'can_be_quantified' )
    and print "The prior token can be quantified.\n";

This method is deprecated in favor of
L<prior_significant_tokrn()|/prior_significant_token>. Six months after
the release of version 0.044_01 it will start warning on the
first use. Six months after that it will warn on every use. Six months
after that a fatal exception will be thrown when it is called. I am not
sure I need to put this though a deprecation cycle, given that this
method is documented as not being part of the public interface, but I
choose to err on the side of caution.

This method calls the named method on the most-recently-instantiated
significant token, and returns the result. Any arguments subsequent to
the method name will be passed to the method.

Because this method is designed to be used within the tokenizing system,
it will die horribly if the named method does not exist.

If called with no arguments at all the most-recently-instantiated
significant token is returned.

=head2 prior_significant_token

 $tokenizer->prior_significant_token( 'can_be_quantified' )
    and print "The prior token can be quantified.\n";

This method calls the named method on the most-recently-instantiated
significant token, and returns the result. Any arguments subsequent to
the method name will be passed to the method.

Because this method is designed to be used within the tokenizing system,
it will die horribly if the named method does not exist.

If called with no arguments at all the most-recently-instantiated
significant token is returned.

=head2 strict

 say 'Parse is ', $tokenizer->strict() ? 'strict' : 'lenient';

This method simply returns true or false, depending on whether the
C<'strict'> option to C<new()> was true or false.

=head1 ENVIRONMENT VARIABLES

A tokenizer trace can be requested by setting environment variable
PPIX_REGEXP_TOKENIZER_TRACE to a numeric value other than 0. Use of this
environment variable is unsupported in the same sense that the C<trace>
option of L</new> is unsupported. Explicitly specifying the C<trace>
option to L</new> overrides the environment variable.

The real reason this is documented is to give the user a way to
troubleshoot funny output from the tokenizer.

=head1 SUPPORT

Support is by the author. Please file bug reports at
L<http://rt.cpan.org>, or in electronic mail to the author.

=head1 AUTHOR

Thomas R. Wyant, III F<wyant at cpan dot org>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009-2016 by Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5.10.0. For more details, see the full text
of the licenses in the directory LICENSES.

This program is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.

=cut

# ex: set textwidth=72 :