| 1 | ## -*- Mode: CPerl -*- | ||||||||||||||||||||
| 2 | ## File: Lingua::TT::Token.pm | ||||||||||||||||||||
| 3 | ## Author: Bryan Jurish <TT/IO.pm> | ||||||||||||||||||||
| 4 | ## Descript: TT I/O: tokens (and comments) | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | package Lingua::TT::Token; | ||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | ##============================================================================== | ||||||||||||||||||||
| 11 | ## Globals & Constants | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | ##============================================================================== | ||||||||||||||||||||
| 14 | ## Constructors etc. | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | ## $tok = CLASS_OR_OBJECT->new(@vals) | ||||||||||||||||||||
| 17 | ## + $tok: ARRAY-ref: | ||||||||||||||||||||
| 18 | ## [$val1, $val2, ..., $valN] | ||||||||||||||||||||
| 19 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 20 | 0 | my $that = shift; | |||||||||||||||||||
| 21 | 0 | ▶ | return bless([@_], ref($that)||$that); | ||||||||||||||||||
|
Condition0%0/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table0%0/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | ## $tok = CLASS_OR_OBJECT->newFromString($str) | ||||||||||||||||||||
| 25 | ## + should be equivalent to CLASS_OR_OBJECT->new()->fromString($str) | ||||||||||||||||||||
| 26 | 0 | ▶ | sub newFromString { | ||||||||||||||||||
|
Subroutine
newFromString: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 27 | 0 | ▶ | return bless([split(/[\n\r]*[\t\n\r][\n\r]*/,$_[1])], ref($_[0])||$_[0]); | ||||||||||||||||||
|
Condition0%0/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | ## $tok2 = $tok->copy($deep) | ||||||||||||||||||||
| 31 | ## + creates a (shallow) copy of $tok | ||||||||||||||||||||
| 32 | ## + $deep is ignored | ||||||||||||||||||||
| 33 | 0 | ▶ | sub copy { | ||||||||||||||||||
|
Subroutine
copy: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 34 | 0 | return bless([@{$_[0]}], ref($_[0])); | |||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | ##============================================================================== | ||||||||||||||||||||
| 38 | ## Methods: Access | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | ## $bool = $tok->isEmpty() | ||||||||||||||||||||
| 41 | ## + true iff $tok has no non-empty fields | ||||||||||||||||||||
| 42 | 0 | ▶ | sub isEmpty { | ||||||||||||||||||
|
Subroutine
isEmpty: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 43 | 0 | return !grep {$_ ne ''} @{$_[0]}; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | ## $bool = $tok->isComment() | ||||||||||||||||||||
| 47 | ## + true iff $tok is a comment pseudo-token | ||||||||||||||||||||
| 48 | 0 | ▶ | sub isComment { | ||||||||||||||||||
|
Subroutine
isComment: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 49 | 0 | ▶ | return defined($_[0][0]) && $_[0][0] =~ /^\s*\%\%/; | ||||||||||||||||||
|
Condition0%0/3
defined $_[0][0] && $_[0][0] =~ /^\s*\%\%/
MC/DC0%0/2
defined $_[0][0] && $_[0][0] =~ /^\s*\%\%/
defined $_[0][0]
$_[0][0] =~ /^\s*\%\%/
Truth table0%0/3
A && B
A= defined $_[0][0]B= $_[0][0] =~ /^\s*\%\%/
| |||||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | ## $bool = $tok->isVanilla() | ||||||||||||||||||||
| 53 | ## + true if $tok is a "vanilla" (non-empty and non-comment) token | ||||||||||||||||||||
| 54 | 0 | ▶ | sub isVanilla { | ||||||||||||||||||
|
Subroutine
isVanilla: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 55 | 0 | ▶ | return !($_[0]->isEmpty || $_[0]->isComment); | ||||||||||||||||||
|
Condition0%0/3
$_[0]->isEmpty || $_[0]->isComment
MC/DC0%0/2
$_[0]->isEmpty || $_[0]->isComment
$_[0]->isEmpty
$_[0]->isComment
Truth table0%0/3
A || B
A= $_[0]->isEmptyB= $_[0]->isComment
| |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | ## $tok = $tok->rmEmptyFields() | ||||||||||||||||||||
| 59 | ## + removes empty & undefined fields from @$tok | ||||||||||||||||||||
| 60 | 0 | ▶ | sub rmEmptyFields { | ||||||||||||||||||
|
Subroutine
rmEmptyFields: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 61 | 0 | ▶ | @{$_[0]} = grep {defined($_) && $_ ne ''} @{$_[0]}; | ||||||||||||||||||
|
Condition0%0/3
defined $_ && $_ ne ''
MC/DC0%0/2
defined $_ && $_ ne ''
defined $_
$_ ne ''
Truth table0%0/3
A && B
A= defined $_B= $_ ne ''
| |||||||||||||||||||||
| 62 | 0 | return $_[0]; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | ##============================================================================== | ||||||||||||||||||||
| 67 | ## Methods: I/O | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | ## $str = $tok->toString() | ||||||||||||||||||||
| 70 | ## + returns string representing $tok, but without terminating newline | ||||||||||||||||||||
| 71 | 0 | ▶ | sub toString { | ||||||||||||||||||
|
Subroutine
toString: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 72 | 0 | return join("\t", @{$_[0]}); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | ## $tok = $tok->fromString($str) | ||||||||||||||||||||
| 76 | ## + parses token fields from $str, which may (or may not) contain embedded newlines | ||||||||||||||||||||
| 77 | 0 | ▶ | sub fromString { | ||||||||||||||||||
|
Subroutine
fromString: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 78 | #return $_[0]->newFromString($_[1]) if (!ref($_[0])); | ||||||||||||||||||||
| 79 | #my ($tok,$str) = @_; | ||||||||||||||||||||
| 80 | 0 | @{$_[0]} = split(/[\n\r]*[\t\n\r][\n\r]*/,$_[1]); | |||||||||||||||||||
| 81 | 0 | return $_[0]; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | ##============================================================================== | ||||||||||||||||||||
| 85 | ## Footer | ||||||||||||||||||||
| 86 | 1 | 1; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||