File Coverage

blib/lib/CQL/Token.pm
Criterion Covered Total %
statement 144 144 100.0
branch 4 4 100.0
condition 1 3 33.3
subroutine 47 47 100.0
pod 3 3 100.0
total 199 201 99.0


line stmt bran cond sub pod time code
1             package CQL::Token;
2              
3 9     9   71930 use strict;
  9         20  
  9         440  
4 9     9   46 use warnings;
  9         17  
  9         251  
5 9     9   47 use base qw( Exporter );
  9         37  
  9         1072  
6              
7             =head1 NAME
8              
9             CQL::Token - class for token objects returned by CQL::Lexer
10              
11             =head1 SYNOPSIS
12              
13             my $token = $lexer->nextToken();
14            
15             if ( $token->type() == CQL_WORD ) {
16             print "the token is a word with value=", $token->string(), "\n";
17             }
18              
19             =head1 DESCRIPTION
20              
21             Ordinarily you won't really care about the tokens returned by the
22             CQL::Lexer since the lexer is used behind the scenes by CQL::Parser.
23              
24             =head1 METHODS
25              
26             =head2 new()
27              
28             my $token = CQL::Token->new( '=' );
29              
30             =cut
31              
32             ## CQL keyword types
33 9     9   48 use constant CQL_LT => 100; ## The "<" relation
  9         25  
  9         12274  
34 9     9   63 use constant CQL_GT => 101; ## The ">" relation
  9         22  
  9         1288  
35 9     9   666 use constant CQL_EQ => 102; ## The "=" relation
  9         15  
  9         1869  
36 9     9   51 use constant CQL_LE => 103; ## The "<=" relation
  9         19  
  9         431  
37 9     9   42 use constant CQL_GE => 104; ## The ">=" relation
  9         16  
  9         987  
38 9     9   41 use constant CQL_NE => 105; ## The "<>" relation
  9         14  
  9         356  
39 9     9   67 use constant CQL_AND => 106; ## The "and" boolean
  9         12  
  9         355  
40 9     9   288 use constant CQL_OR => 107; ## The "or" boolean
  9         27  
  9         371  
41 9     9   45 use constant CQL_NOT => 108; ## The "not" boolean
  9         16  
  9         390  
42 9     9   44 use constant CQL_PROX => 109; ## The "prox" boolean
  9         25  
  9         3440  
43 9     9   56 use constant CQL_ANY => 110; ## The "any" relation
  9         13  
  9         480  
44 9     9   40 use constant CQL_ALL => 111; ## The "all" relation
  9         16  
  9         571  
45 9     9   43 use constant CQL_EXACT => 112; ## The "exact" relation
  9         13  
  9         420  
46 9     9   42 use constant CQL_WITHIN => 113; ## The "within" relation
  9         16  
  9         387  
47 9     9   42 use constant CQL_ENCLOSES => 114; ## The "encloses" relation
  9         14  
  9         379  
48 9     9   57 use constant CQL_PARTIAL => 115; ## The "partial" relation
  9         16  
  9         426  
49 9     9   808 use constant CQL_PWORD => 116; ## The "word" proximity unit and the "word" relation modifier
  9         18  
  9         397  
50 9     9   51 use constant CQL_SENTENCE => 117; ## The "sentence" proximity unit
  9         13  
  9         398  
51 9     9   41 use constant CQL_PARAGRAPH => 118; ## The "paragraph" proximity unit
  9         18  
  9         425  
52 9     9   46 use constant CQL_ELEMENT => 119; ## The "element" proximity unit
  9         18  
  9         442  
53 9     9   42 use constant CQL_ORDERED => 120; ## The "ordered" proximity ordering
  9         19  
  9         548  
54 9     9   43 use constant CQL_UNORDERED => 121; ## The "unordered" proximity ordering
  9         19  
  9         440  
55 9     9   42 use constant CQL_RELEVANT => 122; ## The "relevant" relation modifier
  9         19  
  9         380  
56 9     9   42 use constant CQL_FUZZY => 123; ## The "fuzzy" relation modifier
  9         19  
  9         348  
57 9     9   40 use constant CQL_STEM => 124; ## The "stem" relation modifier
  9         14  
  9         388  
58 9     9   41 use constant CQL_SCR => 125; ## The server choice relation
  9         13  
  9         447  
59 9     9   41 use constant CQL_PHONETIC => 126; ## The "phonetic" relation modifier
  9         15  
  9         354  
60 9     9   199 use constant CQL_WORD => 127; ## A general word (not an operator)
  9         47  
  9         708  
61 9     9   47 use constant CQL_LPAREN => 128; ## A left paren
  9         15  
  9         382  
62 9     9   38 use constant CQL_RPAREN => 129; ## A right paren
  9         17  
  9         355  
63 9     9   43 use constant CQL_EOF => 130; ## End of query
  9         16  
  9         509  
64 9     9   38 use constant CQL_MODIFIER => 131; ## Start of modifier '/'
  9         13  
  9         442  
65 9     9   47 use constant CQL_STRING => 132; ## The "string" relation modifier
  9         16  
  9         376  
66 9     9   43 use constant CQL_ISODATE => 133; ## The "isoDate" relation modifier
  9         15  
  9         363  
67 9     9   41 use constant CQL_NUMBER => 134; ## The "number" relation modifier
  9         19  
  9         335  
68 9     9   47 use constant CQL_URI => 135; ## The "uri" relation modifier
  9         12  
  9         366  
69 9     9   40 use constant CQL_MASKED => 137; ## The "masked" relation modifier
  9         14  
  9         375  
70 9     9   40 use constant CQL_UNMASKED => 138; ## The "unmasked" relation modifier
  9         13  
  9         550  
71 9     9   48 use constant CQL_NWSE => 139; ## The "nwse" relation modifier
  9         12  
  9         339  
72 9     9   41 use constant CQL_DISTANCE => 140; ## The "distance" proximity modifier
  9         14  
  9         372  
73 9     9   47 use constant CQL_UNIT => 141; ## The "unit" proximity modifier
  9         13  
  9         6627  
74              
75             ## lookup table for easily determining token type
76             our %lookupTable = (
77             '<' => CQL_LT,
78             '>' => CQL_GT,
79             '=' => CQL_EQ,
80             '<=' => CQL_LE,
81             '>=' => CQL_GE,
82             '<>' => CQL_NE,
83             'and' => CQL_AND,
84             'or' => CQL_OR,
85             'not' => CQL_NOT,
86             'prox' => CQL_PROX,
87             'any' => CQL_ANY,
88             'within' => CQL_WITHIN,
89             'encloses' => CQL_ENCLOSES,
90             'partial' => CQL_PARTIAL,
91             'all' => CQL_ALL,
92             'exact' => CQL_EXACT,
93             'word' => CQL_PWORD,
94             'sentence' => CQL_SENTENCE,
95             'paragraph' => CQL_PARAGRAPH,
96             'element' => CQL_ELEMENT,
97             'ordered' => CQL_ORDERED,
98             'unordered' => CQL_UNORDERED,
99             'relevant' => CQL_RELEVANT,
100             'fuzzy' => CQL_FUZZY,
101             'stem' => CQL_STEM,
102             'phonetic' => CQL_PHONETIC,
103             '(' => CQL_LPAREN,
104             ')' => CQL_RPAREN,
105             '/' => CQL_MODIFIER,
106             '' => CQL_EOF,
107             'string' => CQL_STRING,
108             'isodate' => CQL_ISODATE,
109             'number' => CQL_NUMBER,
110             'uri' => CQL_URI,
111             'masked' => CQL_MASKED,
112             'unmasked' => CQL_UNMASKED,
113             'nwse' => CQL_NWSE,
114             'distance' => CQL_DISTANCE,
115             'unit' => CQL_UNIT,
116             );
117              
118             ## constants available for folks to use when looking at
119             ## token types
120              
121             our @EXPORT = qw(
122             CQL_LT CQL_GT CQL_EQ CQL_LE CQL_GE CQL_NE CQL_AND CQL_OR CQL_NOT
123             CQL_PROX CQL_ANY CQL_ALL CQL_EXACT CQL_PWORD CQL_SENTENCE CQL_PARAGRAPH
124             CQL_ELEMENT CQL_ORDERED CQL_UNORDERED CQL_RELEVANT CQL_FUZZY
125             CQL_STEM CQL_SCR CQL_PHONETIC CQL_RPAREN CQL_LPAREN
126             CQL_WORD CQL_PHRASE CQL_EOF CQL_MODIFIER CQL_STRING CQL_ISODATE
127             CQL_NUMBER CQL_URI CQL_MASKED CQL_UNMASKED CQL_WITHIN CQL_PARTIAL
128             CQL_ENCLOSES CQL_NWSE
129             CQL_DISTANCE CQL_UNIT
130             );
131              
132             =head2 new()
133              
134             =cut
135              
136             sub new {
137 415     415 1 638 my ($class,$string) = @_;
138 415         446 my $type;
139              
140             # see if it's a reserved word, which are case insensitive
141 415         686 my $normalString = lc($string);
142 415 100       3573 if ( exists($lookupTable{$normalString}) ) {
143 244         507 $type = $lookupTable{$normalString};
144             }
145             else {
146 171         297 $type = CQL_WORD;
147             # remove outer quotes if present
148 171 100       896 if ($string =~ m/^"(.*)"$/g) {
149 25         70 $string = $1;
150             # replace escaped double quote with double quote.
151             # Is save this way cause the string is assumed to be syntactically correct
152 25         120 $string =~ s/\\"/"/g;
153             }
154             }
155 415   33     4521 return bless { string=>$string, type=>$type }, ref($class) || $class;
156             }
157              
158             =head2 getType()
159              
160             Returns the token type which will be available as one of the constants
161             that CQL::Token exports. See internals for a list of available constants.
162              
163             =cut
164              
165 1921     1921 1 6561 sub getType { return shift->{type}; }
166              
167             =head2 getString()
168              
169             Retruns the string equivalent of the token. Particularly useful when
170             you only know it's a CQL_WORD.
171              
172             =cut
173              
174 1434     1434 1 8087 sub getString { return shift->{string}; }
175              
176             1;