This file is indexed.

/usr/share/perl5/PPI/Element.pm is in libppi-perl 1.215-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
package PPI::Element;

=pod

=head1 NAME

PPI::Element - The abstract Element class, a base for all source objects

=head1 INHERITANCE

  PPI::Element is the root of the PDOM tree

=head1 DESCRIPTION

The abstract C<PPI::Element> serves as a base class for all source-related
objects, from a single whitespace token to an entire document. It provides
a basic set of methods to provide a common interface and basic
implementations.

=head1 METHODS

=cut

use strict;
use Clone           ();
use Scalar::Util    qw{refaddr};
use Params::Util    qw{_INSTANCE _ARRAY};
use List::MoreUtils ();
use PPI::Util       ();
use PPI::Node       ();

use vars qw{$VERSION $errstr %_PARENT};
BEGIN {
	$VERSION = '1.215';
	$errstr  = '';

	# Master Child -> Parent index
	%_PARENT = ();
}

use overload 'bool' => \&PPI::Util::TRUE;
use overload '""'   => 'content';
use overload '=='   => '__equals';
use overload '!='   => '__nequals';
use overload 'eq'   => '__eq';
use overload 'ne'   => '__ne';





#####################################################################
# General Properties

=pod

=head2 significant

Because we treat whitespace and other non-code items as Tokens (in order to
be able to "round trip" the L<PPI::Document> back to a file) the
C<significant> method allows us to distinguish between tokens that form a
part of the code, and tokens that aren't significant, such as whitespace,
POD, or the portion of a file after (and including) the C<__END__> token.

Returns true if the Element is significant, or false it not.

=cut

### XS -> PPI/XS.xs:_PPI_Element__significant 0.845+
sub significant { 1 }

=pod

=head2 class

The C<class> method is provided as a convenience, and really does nothing
more than returning C<ref($self)>. However, some people have found that
they appreciate the laziness of C<$Foo-E<gt>class eq 'whatever'>, so I
have caved to popular demand and included it.

Returns the class of the Element as a string

=cut

sub class { ref($_[0]) }

=pod

=head2 tokens

The C<tokens> method returns a list of L<PPI::Token> objects for the
Element, essentially getting back that part of the document as if it had
not been lexed.

This also means there are no Statements and no Structures in the list,
just the Token classes.

=cut

sub tokens { $_[0] }

=pod

=head2 content

For B<any> C<PPI::Element>, the C<content> method will reconstitute the
base code for it as a single string. This method is also the method used
for overloading stringification. When an Element is used in a double-quoted
string for example, this is the method that is called.

B<WARNING:>

You should be aware that because of the way that here-docs are handled, any
here-doc content is not included in C<content>, and as such you should
B<not> eval or execute the result if it contains any L<PPI::Token::HereDoc>.

The L<PPI::Document> method C<serialize> should be used to stringify a PDOM
document into something that can be executed as expected.

Returns the basic code as a string (excluding here-doc content).

=cut

### XS -> PPI/XS.xs:_PPI_Element__content 0.900+
sub content { '' }





#####################################################################
# Naigation Methods

=pod

=head2 parent

Elements themselves are not intended to contain other Elements, that is
left to the L<PPI::Node> abstract class, a subclass of C<PPI::Element>.
However, all Elements can be contained B<within> a parent Node.

If an Element is within a parent Node, the C<parent> method returns the
Node.

=cut

sub parent { $_PARENT{refaddr $_[0]} }

=pod

=head2 descendant_of $element

Answers whether a C<PPI::Element> is contained within another one.

C<PPI::Element>s are considered to be descendants of themselves.

=begin testing descendant_of 9

my $Document = PPI::Document->new( \'( [ thingy ] ); $blarg = 1' );
isa_ok( $Document, 'PPI::Document' );
ok(
	$Document->descendant_of($Document),
	'Document is a descendant of itself.',
);

my $words = $Document->find('Token::Word');
is(scalar @{$words}, 1, 'Document contains 1 Word.');
my $word = $words->[0];
ok(
	$word->descendant_of($word),
	'Word is a descendant of itself.',
);
ok(
	$word->descendant_of($Document),
	'Word is a descendant of the Document.',
);
ok(
	! $Document->descendant_of($word),
	'Document is not a descendant of the Word.',
);

my $symbols = $Document->find('Token::Symbol');
is(scalar @{$symbols}, 1, 'Document contains 1 Symbol.');
my $symbol = $symbols->[0];
ok(
	! $word->descendant_of($symbol),
	'Word is not a descendant the Symbol.',
);
ok(
	! $symbol->descendant_of($word),
	'Symbol is not a descendant the Word.',
);

=end testing

=cut

sub descendant_of {
	my $cursor = shift;
	my $parent = shift or return undef;
	while ( refaddr $cursor != refaddr $parent ) {
		$cursor = $_PARENT{refaddr $cursor} or return '';
	}
	return 1;
}

=pod

=head2 ancestor_of $element

Answers whether a C<PPI::Element> is contains another one.

C<PPI::Element>s are considered to be ancestors of themselves.

=begin testing ancestor_of 9

my $Document = PPI::Document->new( \'( [ thingy ] ); $blarg = 1' );
isa_ok( $Document, 'PPI::Document' );
ok(
	$Document->ancestor_of($Document),
	'Document is an ancestor of itself.',
);

my $words = $Document->find('Token::Word');
is(scalar @{$words}, 1, 'Document contains 1 Word.');
my $word = $words->[0];
ok(
	$word->ancestor_of($word),
	'Word is an ancestor of itself.',
);
ok(
	! $word->ancestor_of($Document),
	'Word is not an ancestor of the Document.',
);
ok(
	$Document->ancestor_of($word),
	'Document is an ancestor of the Word.',
);

my $symbols = $Document->find('Token::Symbol');
is(scalar @{$symbols}, 1, 'Document contains 1 Symbol.');
my $symbol = $symbols->[0];
ok(
	! $word->ancestor_of($symbol),
	'Word is not an ancestor the Symbol.',
);
ok(
	! $symbol->ancestor_of($word),
	'Symbol is not an ancestor the Word.',
);

=end testing

=cut

sub ancestor_of {
	my $self   = shift;
	my $cursor = shift or return undef;
	while ( refaddr $cursor != refaddr $self ) {
		$cursor = $_PARENT{refaddr $cursor} or return '';
	}
	return 1;
}

=pod

=head2 statement

For a C<PPI::Element> that is contained (at some depth) within a
L<PPI::Statment>, the C<statement> method will return the first parent
Statement object lexically 'above' the Element.

Returns a L<PPI::Statement> object, which may be the same Element if the
Element is itself a L<PPI::Statement> object.

Returns false if the Element is not within a Statement and is not itself
a Statement.

=cut

sub statement {
	my $cursor = shift;
	while ( ! _INSTANCE($cursor, 'PPI::Statement') ) {
		$cursor = $_PARENT{refaddr $cursor} or return '';
	}
	$cursor;
}

=pod

=head2 top

For a C<PPI::Element> that is contained within a PDOM tree, the C<top> method
will return the top-level Node in the tree. Most of the time this should be
a L<PPI::Document> object, however this will not always be so. For example,
if a subroutine has been removed from its Document, to be moved to another
Document.

Returns the top-most PDOM object, which may be the same Element, if it is
not within any parent PDOM object.

=cut

sub top {
	my $cursor = shift;
	while ( my $parent = $_PARENT{refaddr $cursor} ) {
		$cursor = $parent;
	}
	$cursor;
}

=pod

=head2 document

For an Element that is contained within a L<PPI::Document> object,
the C<document> method will return the top-level Document for the Element.

Returns the L<PPI::Document> for this Element, or false if the Element is not
contained within a Document.

=cut

sub document {
	my $top = shift->top;
	_INSTANCE($top, 'PPI::Document') and $top;
}

=pod

=head2 next_sibling

All L<PPI::Node> objects (specifically, our parent Node) contain a number of
C<PPI::Element> objects. The C<next_sibling> method returns the C<PPI::Element>
immediately after the current one, or false if there is no next sibling.

=cut

sub next_sibling {
	my $self     = shift;
	my $parent   = $_PARENT{refaddr $self} or return '';
	my $key      = refaddr $self;
	my $elements = $parent->{children};
	my $position = List::MoreUtils::firstidx {
		refaddr $_ == $key
		} @$elements;
	$elements->[$position + 1] || '';
}

=pod

=head2 snext_sibling

As per the other 's' methods, the C<snext_sibling> method returns the next
B<significant> sibling of the C<PPI::Element> object.

Returns a C<PPI::Element> object, or false if there is no 'next' significant
sibling.

=cut

sub snext_sibling {
	my $self     = shift;
	my $parent   = $_PARENT{refaddr $self} or return '';
	my $key      = refaddr $self;
	my $elements = $parent->{children};
	my $position = List::MoreUtils::firstidx {
		refaddr $_ == $key
		} @$elements;
	while ( defined(my $it = $elements->[++$position]) ) {
		return $it if $it->significant;
	}
	'';
}

=pod

=head2 previous_sibling

All L<PPI::Node> objects (specifically, our parent Node) contain a number of
C<PPI::Element> objects. The C<previous_sibling> method returns the Element
immediately before the current one, or false if there is no 'previous'
C<PPI::Element> object.

=cut

sub previous_sibling {
	my $self     = shift;
	my $parent   = $_PARENT{refaddr $self} or return '';
	my $key      = refaddr $self;
	my $elements = $parent->{children};
	my $position = List::MoreUtils::firstidx {
		refaddr $_ == $key
		} @$elements;
	$position and $elements->[$position - 1] or '';
}

=pod

=head2 sprevious_sibling

As per the other 's' methods, the C<sprevious_sibling> method returns
the previous B<significant> sibling of the C<PPI::Element> object.

Returns a C<PPI::Element> object, or false if there is no 'previous' significant
sibling.

=cut

sub sprevious_sibling {
	my $self     = shift;
	my $parent   = $_PARENT{refaddr $self} or return '';
	my $key      = refaddr $self;
	my $elements = $parent->{children};
	my $position = List::MoreUtils::firstidx {
		refaddr $_ == $key
		} @$elements;
	while ( $position-- and defined(my $it = $elements->[$position]) ) {
		return $it if $it->significant;
	}
	'';
}

=pod

=head2 first_token

As a support method for higher-order algorithms that deal specifically with
tokens and actual Perl content, the C<first_token> method finds the first
PPI::Token object within or equal to this one.

That is, if called on a L<PPI::Node> subclass, it will descend until it
finds a L<PPI::Token>. If called on a L<PPI::Token> object, it will return
the same object.

Returns a L<PPI::Token> object, or dies on error (which should be extremely
rare and only occur if an illegal empty L<PPI::Statement> exists below the
current Element somewhere.

=cut

sub first_token {
	my $cursor = shift;
	while ( $cursor->isa('PPI::Node') ) {
		$cursor = $cursor->first_element
		or die "Found empty PPI::Node while getting first token";
	}
	$cursor;
}


=pod

=head2 last_token

As a support method for higher-order algorithms that deal specifically with
tokens and actual Perl content, the C<last_token> method finds the last
PPI::Token object within or equal to this one.

That is, if called on a L<PPI::Node> subclass, it will descend until it
finds a L<PPI::Token>. If called on a L<PPI::Token> object, it will return
the itself.

Returns a L<PPI::Token> object, or dies on error (which should be extremely
rare and only occur if an illegal empty L<PPI::Statement> exists below the
current Element somewhere.

=cut

sub last_token {
	my $cursor = shift;
	while ( $cursor->isa('PPI::Node') ) {
		$cursor = $cursor->last_element
		or die "Found empty PPI::Node while getting first token";
	}
	$cursor;
}

=pod

=head2 next_token

As a support method for higher-order algorithms that deal specifically with
tokens and actual Perl content, the C<next_token> method finds the
L<PPI::Token> object that is immediately after the current Element, even if
it is not within the same parent L<PPI::Node> as the one for which the
method is being called.

Note that this is B<not> defined as a L<PPI::Token>-specific method,
because it can be useful to find the next token that is after, say, a
L<PPI::Statement>, although obviously it would be useless to want the
next token after a L<PPI::Document>.

Returns a L<PPI::Token> object, or false if there are no more tokens after
the Element.

=cut

sub next_token {
	my $cursor = shift;

	# Find the next element, going upwards as needed
	while ( 1 ) {
		my $element = $cursor->next_sibling;
		if ( $element ) {
			return $element if $element->isa('PPI::Token');
			return $element->first_token;
		}
		$cursor = $cursor->parent or return '';
		if ( $cursor->isa('PPI::Structure') and $cursor->finish ) {
			return $cursor->finish;
		}
	}
}

=pod

=head2 previous_token

As a support method for higher-order algorithms that deal specifically with
tokens and actual Perl content, the C<previous_token> method finds the
L<PPI::Token> object that is immediately before the current Element, even
if it is not within the same parent L<PPI::Node> as this one.

Note that this is not defined as a L<PPI::Token>-only method, because it can
be useful to find the token is before, say, a L<PPI::Statement>, although
obviously it would be useless to want the next token before a
L<PPI::Document>.

Returns a L<PPI::Token> object, or false if there are no more tokens before
the C<Element>.

=cut

sub previous_token {
	my $cursor = shift;

	# Find the previous element, going upwards as needed
	while ( 1 ) {
		my $element = $cursor->previous_sibling;
		if ( $element ) {
			return $element if $element->isa('PPI::Token');
			return $element->last_token;
		}
		$cursor = $cursor->parent or return '';
		if ( $cursor->isa('PPI::Structure') and $cursor->start ) {
			return $cursor->start;
		}
	}
}





#####################################################################
# Manipulation

=pod

=head2 clone

As per the L<Clone> module, the C<clone> method makes a perfect copy of
an Element object. In the generic case, the implementation is done using
the L<Clone> module's mechanism itself. In higher-order cases, such as for
Nodes, there is more work involved to keep the parent-child links intact.

=cut

sub clone {
	Clone::clone(shift);
}

=pod

=head2 insert_before @Elements

The C<insert_before> method allows you to insert lexical perl content, in
the form of C<PPI::Element> objects, before the calling C<Element>. You
need to be very careful when modifying perl code, as it's easy to break
things.

In its initial incarnation, this method allows you to insert a single
Element, and will perform some basic checking to prevent you inserting
something that would be structurally wrong (in PDOM terms).

In future, this method may be enhanced to allow the insertion of multiple
Elements, inline-parsed code strings or L<PPI::Document::Fragment> objects.

Returns true if the Element was inserted, false if it can not be inserted,
or C<undef> if you do not provide a L<PPI::Element> object as a parameter.

=begin testing __insert_before 6

my $Document = PPI::Document->new( \"print 'Hello World';" );
isa_ok( $Document, 'PPI::Document' );
my $semi = $Document->find_first('Token::Structure');
isa_ok( $semi, 'PPI::Token::Structure' );
is( $semi->content, ';', 'Got expected token' );
my $foo = PPI::Token::Word->new('foo');
isa_ok( $foo, 'PPI::Token::Word' );
is( $foo->content, 'foo', 'Created Word token' );
$semi->__insert_before( $foo );
is( $Document->serialize, "print 'Hello World'foo;",
	'__insert_before actually inserts' );

=end testing

=begin testing insert_before after __insert_before 6

my $Document = PPI::Document->new( \"print 'Hello World';" );
isa_ok( $Document, 'PPI::Document' );
my $semi = $Document->find_first('Token::Structure');
isa_ok( $semi, 'PPI::Token::Structure' );
is( $semi->content, ';', 'Got expected token' );
my $foo = PPI::Token::Word->new('foo');
isa_ok( $foo, 'PPI::Token::Word' );
is( $foo->content, 'foo', 'Created Word token' );
$semi->insert_before( $foo );
is( $Document->serialize, "print 'Hello World'foo;",
	'insert_before actually inserts' );

=end testing

=cut

sub __insert_before {
	my $self = shift;
	$self->parent->__insert_before_child( $self, @_ );
}

=pod

=head2 insert_after @Elements

The C<insert_after> method allows you to insert lexical perl content, in
the form of C<PPI::Element> objects, after the calling C<Element>. You need
to be very careful when modifying perl code, as it's easy to break things.

In its initial incarnation, this method allows you to insert a single
Element, and will perform some basic checking to prevent you inserting
something that would be structurally wrong (in PDOM terms).

In future, this method may be enhanced to allow the insertion of multiple
Elements, inline-parsed code strings or L<PPI::Document::Fragment> objects.

Returns true if the Element was inserted, false if it can not be inserted,
or C<undef> if you do not provide a L<PPI::Element> object as a parameter.

=begin testing __insert_after 6

my $Document = PPI::Document->new( \"print 'Hello World';" );
isa_ok( $Document, 'PPI::Document' );
my $string = $Document->find_first('Token::Quote');
isa_ok( $string, 'PPI::Token::Quote' );
is( $string->content, "'Hello World'", 'Got expected token' );
my $foo = PPI::Token::Word->new('foo');
isa_ok( $foo, 'PPI::Token::Word' );
is( $foo->content, 'foo', 'Created Word token' );
$string->__insert_after( $foo );
is( $Document->serialize, "print 'Hello World'foo;",
	'__insert_after actually inserts' );

=end testing

=begin testing insert_after after __insert_after 6

my $Document = PPI::Document->new( \"print 'Hello World';" );
isa_ok( $Document, 'PPI::Document' );
my $string = $Document->find_first('Token::Quote');
isa_ok( $string, 'PPI::Token::Quote' );
is( $string->content, "'Hello World'", 'Got expected token' );
my $foo = PPI::Token::Word->new('foo');
isa_ok( $foo, 'PPI::Token::Word' );
is( $foo->content, 'foo', 'Created Word token' );
$string->insert_after( $foo );
is( $Document->serialize, "print 'Hello World'foo;",
	'insert_after actually inserts' );

=end testing

=cut

sub __insert_after {
	my $self = shift;
	$self->parent->__insert_after_child( $self, @_ );
}

=pod

=head2 remove

For a given C<PPI::Element>, the C<remove> method will remove it from its
parent B<intact>, along with all of its children.

Returns the C<Element> itself as a convenience, or C<undef> if an error
occurs while trying to remove the C<Element>.

=cut

sub remove {
	my $self   = shift;
	my $parent = $self->parent or return $self;
	$parent->remove_child( $self );
}

=pod

=head2 delete

For a given C<PPI::Element>, the C<delete> method will remove it from its
parent, immediately deleting the C<Element> and all of its children (if it
has any).

Returns true if the C<Element> was successfully deleted, or C<undef> if
an error occurs while trying to remove the C<Element>.

=cut

sub delete {
	$_[0]->remove or return undef;
	$_[0]->DESTROY;
	1;
}

=pod

=head2 replace $Element

Although some higher level class support more exotic forms of replace,
at the basic level the C<replace> method takes a single C<Element> as
an argument and replaces the current C<Element> with it.

To prevent accidental damage to code, in this initial implementation the
replacement element B<must> be of the same class (or a subclass) as the
one being replaced.

=cut

sub replace {
	my $self    = ref $_[0] ? shift : return undef;
	my $Element = _INSTANCE(shift, ref $self) or return undef;
	die "The ->replace method has not yet been implemented";
}

=pod

=head2 location

If the Element exists within a L<PPI::Document> that has
indexed the Element locations using C<PPI::Document::index_locations>, the
C<location> method will return the location of the first character of the
Element within the Document.

Returns the location as a reference to a five-element array in the form C<[
$line, $rowchar, $col, $logical_line, $logical_file_name ]>. The values are in
a human format, with the first character of the file located at C<[ 1, 1, 1, ?,
'something' ]>.

The second and third numbers are similar, except that the second is the
literal horizontal character, and the third is the visual column, taking
into account tabbing (see L<PPI::Document/"tab_width [ $width ]">).

The fourth number is the line number, taking into account any C<#line>
directives.  The fifth element is the name of the file that the element was
found in, if available, taking into account any C<#line> directives.

Returns C<undef> on error, or if the L<PPI::Document> object has not been
indexed.

=cut

sub location {
	my $self = shift;

	$self->_ensure_location_present or return undef;

	# Return a copy, not the original
	return [ @{$self->{_location}} ];
}

=pod

=head2 line_number

If the Element exists within a L<PPI::Document> that has indexed the Element
locations using C<PPI::Document::index_locations>, the C<line_number> method
will return the line number of the first character of the Element within the
Document.

Returns C<undef> on error, or if the L<PPI::Document> object has not been
indexed.

=begin testing line_number 3

my $document = PPI::Document->new(\<<'END_PERL');


   foo
END_PERL

isa_ok( $document, 'PPI::Document' );
my $words = $document->find('PPI::Token::Word');
is( scalar @{$words}, 1, 'Found expected word token.' );
is( $words->[0]->line_number, 3, 'Got correct line number.' );

=end testing

=cut

sub line_number {
	my $self = shift;

	my $location = $self->location() or return undef;
	return $location->[0];
}

=pod

=head2 column_number

If the Element exists within a L<PPI::Document> that has indexed the Element
locations using C<PPI::Document::index_locations>, the C<column_number> method
will return the column number of the first character of the Element within the
Document.

Returns C<undef> on error, or if the L<PPI::Document> object has not been
indexed.

=begin testing column_number 3

my $document = PPI::Document->new(\<<'END_PERL');


   foo
END_PERL

isa_ok( $document, 'PPI::Document' );
my $words = $document->find('PPI::Token::Word');
is( scalar @{$words}, 1, 'Found expected word token.' );
is( $words->[0]->column_number, 4, 'Got correct column number.' );

=end testing

=cut

sub column_number {
	my $self = shift;

	my $location = $self->location() or return undef;
	return $location->[1];
}

=pod

=head2 visual_column_number

If the Element exists within a L<PPI::Document> that has indexed the Element
locations using C<PPI::Document::index_locations>, the C<visual_column_number>
method will return the visual column number of the first character of the
Element within the Document, according to the value of
L<PPI::Document/"tab_width [ $width ]">.

Returns C<undef> on error, or if the L<PPI::Document> object has not been
indexed.

=begin testing visual_column_number 3

my $document = PPI::Document->new(\<<"END_PERL");


\t foo
END_PERL

isa_ok( $document, 'PPI::Document' );
my $tab_width = 5;
$document->tab_width($tab_width);  # don't use a "usual" value.
my $words = $document->find('PPI::Token::Word');
is( scalar @{$words}, 1, 'Found expected word token.' );
is(
	$words->[0]->visual_column_number,
	$tab_width + 2,
	'Got correct visual column number.',
);

=end testing

=cut

sub visual_column_number {
	my $self = shift;

	my $location = $self->location() or return undef;
	return $location->[2];
}

=pod

=head2 logical_line_number

If the Element exists within a L<PPI::Document> that has indexed the Element
locations using C<PPI::Document::index_locations>, the C<logical_line_number>
method will return the line number of the first character of the Element within
the Document, taking into account any C<#line> directives.

Returns C<undef> on error, or if the L<PPI::Document> object has not been
indexed.

=begin testing logical_line_number 3

# Double quoted so that we don't really have a "#line" at the beginning and
# errors in this file itself aren't affected by this.
my $document = PPI::Document->new(\<<"END_PERL");


\#line 1 test-file
   foo
END_PERL

isa_ok( $document, 'PPI::Document' );
my $words = $document->find('PPI::Token::Word');
is( scalar @{$words}, 1, 'Found expected word token.' );
is( $words->[0]->logical_line_number, 1, 'Got correct logical line number.' );

=end testing

=cut

sub logical_line_number {
	my $self = shift;

	return $self->location()->[3];
}

=pod

=head2 logical_filename

If the Element exists within a L<PPI::Document> that has indexed the Element
locations using C<PPI::Document::index_locations>, the C<logical_filename>
method will return the logical file name containing the first character of the
Element within the Document, taking into account any C<#line> directives.

Returns C<undef> on error, or if the L<PPI::Document> object has not been
indexed.

=begin testing logical_filename 3

# Double quoted so that we don't really have a "#line" at the beginning and
# errors in this file itself aren't affected by this.
my $document = PPI::Document->new(\<<"END_PERL");


\#line 1 test-file
   foo
END_PERL

isa_ok( $document, 'PPI::Document' );
my $words = $document->find('PPI::Token::Word');
is( scalar @{$words}, 1, 'Found expected word token.' );
is(
	$words->[0]->logical_filename,
	'test-file',
	'Got correct logical line number.',
);

=end testing

=cut

sub logical_filename {
	my $self = shift;

	my $location = $self->location() or return undef;
	return $location->[4];
}

sub _ensure_location_present {
	my $self = shift;

	unless ( exists $self->{_location} ) {
		# Are we inside a normal document?
		my $Document = $self->document or return undef;
		if ( $Document->isa('PPI::Document::Fragment') ) {
			# Because they can't be serialized, document fragments
			# do not support the concept of location.
			return undef;
		}

		# Generate the locations. If they need one location, then
		# the chances are they'll want more, and it's better that
		# everything is already pre-generated.
		$Document->index_locations or return undef;
		unless ( exists $self->{_location} ) {
			# erm... something went very wrong here
			return undef;
		}
	}

	return 1;
}

# Although flush_locations is only publically a Document-level method,
# we are able to implement it at an Element level, allowing us to
# selectively flush only the part of the document that occurs after the
# element for which the flush is called.
sub _flush_locations {
	my $self  = shift;
	unless ( $self == $self->top ) {
		return $self->top->_flush_locations( $self );
	}

	# Get the full list of all Tokens
	my @Tokens = $self->tokens;

	# Optionally allow starting from an arbitrary element (or rather,
	# the first Token equal-to-or-within an arbitrary element)
	if ( _INSTANCE($_[0], 'PPI::Element') ) {
		my $start = shift->first_token;
		while ( my $Token = shift @Tokens ) {
			return 1 unless $Token->{_location};
			next unless refaddr($Token) == refaddr($start);

			# Found the start. Flush it's location
			delete $$Token->{_location};
			last;
		}
	}

	# Iterate over any remaining Tokens and flush their location
	foreach my $Token ( @Tokens ) {
		delete $Token->{_location};
	}

	1;
}





#####################################################################
# XML Compatibility Methods

sub _xml_name {
	my $class = ref $_[0] || $_[0];
	my $name  = lc join( '_', split /::/, $class );
	substr($name, 4);
}

sub _xml_attr {
	return {};
}

sub _xml_content {
	defined $_[0]->{content} ? $_[0]->{content} : '';
}





#####################################################################
# Internals

# Set the error string
sub _error {
	$errstr = $_[1];
	undef;
}

# Clear the error string
sub _clear {
	$errstr = '';
	$_[0];
}

# Being DESTROYed in this manner, rather than by an explicit
# ->delete means our reference count has probably fallen to zero.
# Therefore we don't need to remove ourselves from our parent,
# just the index ( just in case ).
### XS -> PPI/XS.xs:_PPI_Element__DESTROY 0.900+
sub DESTROY { delete $_PARENT{refaddr $_[0]} }

# Operator overloads
sub __equals  { ref $_[1] and refaddr($_[0]) == refaddr($_[1]) }
sub __nequals { !__equals(@_) }
sub __eq {
	my $self  = _INSTANCE($_[0], 'PPI::Element') ? $_[0]->content : $_[0];
	my $other = _INSTANCE($_[1], 'PPI::Element') ? $_[1]->content : $_[1];
	$self eq $other;
}
sub __ne { !__eq(@_) }

1;

=pod

=head1 TO DO

It would be nice if C<location> could be used in an ad-hoc manner. That is,
if called on an Element within a Document that has not been indexed, it will
do a one-off calculation to find the location. It might be very painful if
someone started using it a lot, without remembering to index the document,
but it would be handy for things that are only likely to use it once, such
as error handlers.

=head1 SUPPORT

See the L<support section|PPI/SUPPORT> in the main module.

=head1 AUTHOR

Adam Kennedy E<lt>adamk@cpan.orgE<gt>

=head1 COPYRIGHT

Copyright 2001 - 2011 Adam Kennedy.

This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the
LICENSE file included with this module.

=cut