| 1 | package Thrift::IDL; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Thrift::IDL - Parser and OO representation of a Thrift interface definintion language (IDL) | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | my $idl = Thrift::IDL->parse_thrift_file('tutorial.thrift'); | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | foreach my $service ($idl->services) { | ||||||||||||||||||||||||||||
| 12 | printf "Offers service '%s':\n", $service->name; | ||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | The Thrift interface definition language (IDL) file is a structured file describing all the data types, services, methods, etc. of a Thrift interface. This is necessary if you need an in-memory representation of the Thrift schema. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 4 | ▶ | use Parse::RecDescent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 4 | ▶ | use Cwd qw(abs_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | use Thrift::IDL::Comment; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 4 | ▶ | use Thrift::IDL::Constant; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 4 | ▶ | use Thrift::IDL::CppInclude; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | use Thrift::IDL::Definition; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 4 | ▶ | use Thrift::IDL::DocumentHeader; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 4 | ▶ | use Thrift::IDL::Document; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 4 | ▶ | use Thrift::IDL::Enum; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | use Thrift::IDL::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 4 | ▶ | use Thrift::IDL::Field; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 4 | ▶ | use Thrift::IDL::Header; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 4 | ▶ | use Thrift::IDL::Include; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 4 | ▶ | use Thrift::IDL::Method; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 4 | ▶ | use Thrift::IDL::Namespace; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 4 | ▶ | use Thrift::IDL::Senum; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | use Thrift::IDL::Service; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 4 | ▶ | use Thrift::IDL::Struct; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 4 | ▶ | use Thrift::IDL::Type; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 4 | ▶ | use Thrift::IDL::Type::Base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 4 | ▶ | use Thrift::IDL::Type::Custom; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 4 | ▶ | use Thrift::IDL::Type::List; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 4 | ▶ | use Thrift::IDL::Type::Map; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 4 | ▶ | use Thrift::IDL::Type::Set; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 4 | ▶ | use Thrift::IDL::TypeDef; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 4 | our ($wrap, $g_current_header); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 4 | our %reserved_keywords = map { $_ => 1 } qw( | |||||||||||||||||||||||||||
| 53 | abstract and args as assert break case class continue declare def default | ||||||||||||||||||||||||||||
| 54 | del delete do elif else elseif except exec false finally float for | ||||||||||||||||||||||||||||
| 55 | foreach function global goto if implements import in inline instanceof interface is | ||||||||||||||||||||||||||||
| 56 | lambda native new not or pass public print private protected raise return sizeof | ||||||||||||||||||||||||||||
| 57 | static switch synchronized this throw transient true try unsigned var virtual volatile | ||||||||||||||||||||||||||||
| 58 | while with union yield register | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head1 METHODS | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head2 parse_thrift_file ($input_filename, $debug) | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | my $document = Thrift::IDL->parse_thrift_file('tutorial.thrift'); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Given a filename of a Thrift IDL file and an optional boolean for debug output, parse the input file into a L<Thrift::IDL::Document> object and return it. The debug flag will cause verbose output on STDERR. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | ▶ | sub parse_thrift_file { | ||||||||||||||||||||||||||
|
Subroutine
parse_thrift_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 1 | my ($class, $input_fn, $debug) = @_; | |||||||||||||||||||||||||||
| 73 | 1 | return $class->_new_from_parsed({ file => $input_fn }, $debug); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head2 parse_thrift ($data, $debug) | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | my $document = Thrift::IDL->parse_thrift(...); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Given a scalar of a Thrift IDL file and an optional boolean for debug output, parse the input into a L<Thrift::IDL::Document> object and return it. The debug flag will cause verbose output on STDERR. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | NOTE: If the thrift references other documents via an B<include> statement, you'll need to use absolute paths | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | ▶ | sub parse_thrift { | ||||||||||||||||||||||||||
|
Subroutine
parse_thrift: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 1 | my ($class, $data, $debug) = @_; | |||||||||||||||||||||||||||
| 88 | 1 | return $class->_new_from_parsed({ buffer => $data }, $debug); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 3 | ▶ | sub parser { | ||||||||||||||||||||||||||
|
Subroutine
parser: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 3 | my ($class, $debug) = @_; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 3 | $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error | |||||||||||||||||||||||||||
| 95 | 3 | $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c. | |||||||||||||||||||||||||||
| 96 | 3 | $::RD_HINT = 1; # Give out hints to help fix problems. | |||||||||||||||||||||||||||
| 97 | #$::RD_AUTOACTION = q { [@item[0..$#item]] }; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 117 | ▶ | $wrap = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 100 | 117 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 117 | my $class = delete $self->{class}; | |||||||||||||||||||||||||||
| 102 | 117 | ▶ | die "No class!" unless $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 117 | $class = 'Thrift::IDL::' . $class; | |||||||||||||||||||||||||||
| 104 | 117 | my $return = bless $self, $class; | |||||||||||||||||||||||||||
| 105 | 117 | ▶ | if ($return->can('setup')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 10 | $return->setup; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # Store a reference to the current global DocumentHeader | ||||||||||||||||||||||||||||
| 110 | 117 | $self->{header} = $g_current_header; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 117 | ▶ | print "Created $class $return\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 117 | return $return; | |||||||||||||||||||||||||||
| 114 | 3 | }; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 3 | ▶ | my $parser = Parse::RecDescent->new(<<'EOF') or die "Invalid grammer!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | document: (comment | header | definition)(s) | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | comment: ( | ||||||||||||||||||||||||||||
| 120 | /\/[*] .*? [*]\//sx | ||||||||||||||||||||||||||||
| 121 | | /^\/\/ .*/x | ||||||||||||||||||||||||||||
| 122 | | /^[#] .*/x | ||||||||||||||||||||||||||||
| 123 | ) | ||||||||||||||||||||||||||||
| 124 | { $Thrift::IDL::wrap->({ class => 'Comment', value => $item[1] }) } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | header: ( include | cpp_include | namespace ) | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | definition: ( | ||||||||||||||||||||||||||||
| 129 | const | ||||||||||||||||||||||||||||
| 130 | | typedef | ||||||||||||||||||||||||||||
| 131 | | enum | ||||||||||||||||||||||||||||
| 132 | | senum | ||||||||||||||||||||||||||||
| 133 | | struct | ||||||||||||||||||||||||||||
| 134 | | service | ||||||||||||||||||||||||||||
| 135 | ) | ||||||||||||||||||||||||||||
| 136 | { $item[1] } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | include: | ||||||||||||||||||||||||||||
| 139 | 'include' quoted_string | ||||||||||||||||||||||||||||
| 140 | { $Thrift::IDL::wrap->({ class => 'Include', value => $item[2] }) } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | cpp_include: | ||||||||||||||||||||||||||||
| 143 | 'cpp_include' quoted_string | ||||||||||||||||||||||||||||
| 144 | { $Thrift::IDL::wrap->({ class => 'CppInclude', value => $item[2] }) } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | namespace: | ||||||||||||||||||||||||||||
| 147 | 'namespace' (word | '*') word | ||||||||||||||||||||||||||||
| 148 | { $Thrift::IDL::wrap->({ class => 'Namespace', scope => $item[2], value => $item[3] }) } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | const: | ||||||||||||||||||||||||||||
| 151 | 'const' type name '=' value | ||||||||||||||||||||||||||||
| 152 | { $Thrift::IDL::wrap->({ class => 'Constant', type => $item[2], name => $item[3], value => $item[5] }) } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | typedef: | ||||||||||||||||||||||||||||
| 155 | 'typedef' type name | ||||||||||||||||||||||||||||
| 156 | { $Thrift::IDL::wrap->({ class => 'TypeDef', type => $item[2], name => $item[3] }) } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | enum: | ||||||||||||||||||||||||||||
| 159 | 'enum' name '{' enum_pair(s /,/) '}' | ||||||||||||||||||||||||||||
| 160 | { $Thrift::IDL::wrap->({ class => 'Enum', name => $item[2], values => $item[4] }) } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | senum: | ||||||||||||||||||||||||||||
| 163 | 'senum' name '{' quoted_string(s /,/) '}' | ||||||||||||||||||||||||||||
| 164 | { $Thrift::IDL::wrap->({ class => 'Senum', name => $item[2], values => $item[4] }) } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | enum_pair: | ||||||||||||||||||||||||||||
| 167 | name ('=' number)(?) | ||||||||||||||||||||||||||||
| 168 | { [ $item[1] => $item[2][0] ] } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | struct: | ||||||||||||||||||||||||||||
| 171 | ('struct' | 'exception') name '{' field_statement(s /,?/) (',' | '') '}' | ||||||||||||||||||||||||||||
| 172 | { $Thrift::IDL::wrap->({ class => $item[1] eq 'struct' ? 'Struct' : 'Exception', name => $item[2], children => $item[4] }) } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | field_statement: | ||||||||||||||||||||||||||||
| 175 | comment | field | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | field: | ||||||||||||||||||||||||||||
| 178 | number ':' ('optional')(?) type name ('=' value)(?) | ||||||||||||||||||||||||||||
| 179 | { $Thrift::IDL::wrap->({ class => 'Field', id => $item[1], optional => $item[3][0] ? 1 : undef, type => $item[4], name => $item[5], default_value => $item[6][0] }) } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | service: | ||||||||||||||||||||||||||||
| 182 | 'service' name ('extends' word)(?) '{' service_statement(s) '}' | ||||||||||||||||||||||||||||
| 183 | { $Thrift::IDL::wrap->({ class => 'Service', name => $item[2], extends => $item[3][0], children => $item[5] }) } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | service_statement: | ||||||||||||||||||||||||||||
| 186 | comment | method_statement | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | method_statement: | ||||||||||||||||||||||||||||
| 189 | ('oneway')(?) type name '(' field_statement(s? /,?/) (',')(?) ')' (throws)(?) (',')(?) | ||||||||||||||||||||||||||||
| 190 | #{ print "$item[0]: ".join(', ', map { "'$_'" } @item[1 .. $#item]) . "\n" } | ||||||||||||||||||||||||||||
| 191 | { $Thrift::IDL::wrap->({ | ||||||||||||||||||||||||||||
| 192 | class => 'Method', | ||||||||||||||||||||||||||||
| 193 | oneway => $item[1][0] ? 1 : undef, | ||||||||||||||||||||||||||||
| 194 | returns => $item[2], | ||||||||||||||||||||||||||||
| 195 | name => $item[3], | ||||||||||||||||||||||||||||
| 196 | arguments => $item[5], | ||||||||||||||||||||||||||||
| 197 | throws => $item[8][0] ? $item[8][0] : [], | ||||||||||||||||||||||||||||
| 198 | }) } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | throws: | ||||||||||||||||||||||||||||
| 201 | 'throws' '(' field_statement(s? /,/) ')' | ||||||||||||||||||||||||||||
| 202 | #{ print "$item[0]: ".join(', ', map { "'$_'" } @item[1 .. $#item]) . "\n" } | ||||||||||||||||||||||||||||
| 203 | { $item[3] } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | type: | ||||||||||||||||||||||||||||
| 206 | base_type | container_type | custom_type | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | base_type: | ||||||||||||||||||||||||||||
| 209 | ('bool' | 'byte' | 'i16' | 'i32' | 'i64' | 'double' | 'string' | 'binary' | 'slist' | 'void') | ||||||||||||||||||||||||||||
| 210 | { $Thrift::IDL::wrap->({ class => 'Type::Base', name => $item[1] }) } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | custom_type: | ||||||||||||||||||||||||||||
| 213 | name | ||||||||||||||||||||||||||||
| 214 | { $Thrift::IDL::wrap->({ class => 'Type::Custom', name => $item[1] }) } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | container_type: | ||||||||||||||||||||||||||||
| 217 | map_type | set_type | list_type | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | map_type: | ||||||||||||||||||||||||||||
| 220 | 'map' cpp_type(?) '<' type ',' type '>' | ||||||||||||||||||||||||||||
| 221 | { $Thrift::IDL::wrap->({ class => 'Type::Map', key_type => $item[4], val_type => $item[6], cpp_type => $item[2][0] }) } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | set_type: | ||||||||||||||||||||||||||||
| 224 | 'set' cpp_type(?) '<' type '>' | ||||||||||||||||||||||||||||
| 225 | { $Thrift::IDL::wrap->({ class => 'Type::Set', val_type => $item[4], cpp_type => $item[2][0] }) } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | list_type: | ||||||||||||||||||||||||||||
| 228 | 'list' '<' type '>' cpp_type(?) | ||||||||||||||||||||||||||||
| 229 | { $Thrift::IDL::wrap->({ class => 'Type::List', val_type => $item[3], cpp_type => $item[5][0] }) } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | cpp_type: | ||||||||||||||||||||||||||||
| 232 | 'cpp_type' quoted_string | ||||||||||||||||||||||||||||
| 233 | { $item[2] } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | name: | ||||||||||||||||||||||||||||
| 236 | /[A-Za-z_][A-Za-z0-9_.]*/ | ||||||||||||||||||||||||||||
| 237 | { if ($Thrift::IDL::reserved_keywords{$item[1]}) { warn "Cannot use reserved language keyword: $item[1]\n"; return undef } $item[1] } | ||||||||||||||||||||||||||||
| 238 | #{ print "$item[0]: ".join(', ', map { "'$_'" } @item[1 .. $#item]) . "\n"; $return = $item[1]; } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | word: | ||||||||||||||||||||||||||||
| 241 | /(\w|[.])+/ | ||||||||||||||||||||||||||||
| 242 | #{ print "$item[0]: ".join(', ', map { "'$_'" } @item[1 .. $#item]) . "\n"; $return = $item[1]; } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | value: | ||||||||||||||||||||||||||||
| 245 | number | value_map | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | value_map: | ||||||||||||||||||||||||||||
| 248 | '{' value_map_pair(s /,/) '}' | ||||||||||||||||||||||||||||
| 249 | { { map { @$_ } @{ $item[2] } } } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | value_map_pair: | ||||||||||||||||||||||||||||
| 252 | quoted_string ':' quoted_string | ||||||||||||||||||||||||||||
| 253 | { [ $item[1] => $item[3] ] } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | number: | ||||||||||||||||||||||||||||
| 256 | /[0-9]+/ | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | quoted_string: | ||||||||||||||||||||||||||||
| 259 | single_quoted_string | double_quoted_string | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | double_quoted_string: | ||||||||||||||||||||||||||||
| 262 | '"' <skip:''> /[^"]+/ '"' | ||||||||||||||||||||||||||||
| 263 | { $item[3] } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | single_quoted_string: | ||||||||||||||||||||||||||||
| 266 | "'" <skip:''> /[^']+/ "'" | ||||||||||||||||||||||||||||
| 267 | { $item[3] } | ||||||||||||||||||||||||||||
| 268 | EOF | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 3 | return $parser; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 2 | ▶ | sub _new_from_parsed { | ||||||||||||||||||||||||||
|
Subroutine
_new_from_parsed: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 274 | 2 | my ($class, $source, $debug) = @_; | |||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 2 | my ($children, $headers) = $class->_parse_thrift($source, $debug, {}); | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 2 | my $document = Thrift::IDL::Document->new({ children => $children, headers => $headers }); | |||||||||||||||||||||||||||
| 279 | 2 | _set_associations($document); | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 2 | return $document; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 3 | ▶ | sub _parse_thrift { | ||||||||||||||||||||||||||
|
Subroutine
_parse_thrift: called CC=23 SCAR=35.4
| |||||||||||||||||||||||||||||
| 285 | 3 | my ($class, $source, $debug, $state) = @_; | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 3 | ▶ | my $input_fn = $source->{file} || undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
$source->{'file'} || undef
MC/DC100%1/1
$source->{'file'} || undef
$source->{'file'}
Truth table100%2/2
A
A= $source->{'file'}
| |||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 3 | ▶ | if (defined $input_fn && $state->{_parse_input_fn_processed}{$input_fn}++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $input_fn and $state->{"_parse_input_fn_processed"}{$input_fn}++
MC/DC0%0/2
defined $input_fn and $state->{"_parse_input_fn_processed"}{$input_fn}++
defined $input_fn
$state->{"_parse_input_fn_processed"}{$input_fn}++
Truth table66%2/3
A and B
A= defined $input_fnB= $state->{"_parse_input_fn_processed"}{$input_fn}++
| |||||||||||||||||||||||||||||
| 290 | 0 | print "Skipping repeat processing of $input_fn\n"; | |||||||||||||||||||||||||||
| 291 | 0 | return ([], []); | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 3 | ▶ | my $data = $source->{buffer} || undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
$source->{'buffer'} || undef
MC/DC100%1/1
$source->{'buffer'} || undef
$source->{'buffer'}
Truth table100%2/2
A
A= $source->{'buffer'}
| |||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 3 | ▶ | if ($input_fn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | # Slurp the file in | ||||||||||||||||||||||||||||
| 298 | 2 | local $/ = undef; | |||||||||||||||||||||||||||
| 299 | 2 | ▶ | open my $in, '<', $input_fn or die "Can't read from $input_fn: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 2 | $data = <$in>; | |||||||||||||||||||||||||||
| 301 | 2 | close $in; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | # Create a DocumentHeader object | ||||||||||||||||||||||||||||
| 305 | 3 | my $header = Thrift::IDL::DocumentHeader->new({ includes => [], namespaces => [] }); | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 3 | ▶ | if ($input_fn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 2 | my ($basename) = $input_fn =~ m{([^/]+)\.thrift$}; | |||||||||||||||||||||||||||
| 309 | 2 | ▶ | $header->basename($basename) if defined $basename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | # Set the global $g_current_header to this document header so all blessed objects | ||||||||||||||||||||||||||||
| 313 | # created upon parsing will have a reference to $header | ||||||||||||||||||||||||||||
| 314 | 3 | $g_current_header = $header; | |||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 3 | ▶ | print STDERR "Parsing '$input_fn'\n" if $input_fn && $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$input_fn and $debug
MC/DC0%0/2
$input_fn and $debug
$input_fn
$debug
Truth table66%2/3
A and B
A= $input_fnB= $debug
| |||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 3 | ▶ | my $parsed = $class->parser($debug)->document(\$data) or die "Bad input"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 3 | ▶ | if ($data !~ m{^\s*$}s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | 0 | my $error = "Parsing failed to consume all of the input; stopped at:\n"; | |||||||||||||||||||||||||||
| 322 | 0 | my @lines = grep { $_ !~ /^\s*$/ } split /\n/, $data; | |||||||||||||||||||||||||||
| 323 | 0 | ▶ | my $cropped = int @lines > 20 ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 0 | foreach my $line (@lines[0 .. 19]) { | |||||||||||||||||||||||||||
| 325 | #next unless $line; | ||||||||||||||||||||||||||||
| 326 | 0 | chomp $line; | |||||||||||||||||||||||||||
| 327 | 0 | $error .= "> $line\n"; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | 0 | ▶ | if ($cropped) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 0 | $error .= " (more cropped)\n"; | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | 0 | die $error; | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | # Setup return of header list | ||||||||||||||||||||||||||||
| 336 | 3 | my @headers; | |||||||||||||||||||||||||||
| 337 | 3 | push @headers, $header; | |||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 3 | my $include_base = '/'; # Somewhat insane default value | |||||||||||||||||||||||||||
| 340 | 3 | ▶ | if ($input_fn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | # Check for any Include headers and include those elements as well | ||||||||||||||||||||||||||||
| 342 | 2 | $input_fn = abs_path($input_fn); | |||||||||||||||||||||||||||
| 343 | 2 | ($include_base) = $input_fn =~ m{^(.+)/[^/]+$}; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | # Extract header objects and associate each non-header child object with | ||||||||||||||||||||||||||||
| 347 | # the document header (for namespacing mainly) | ||||||||||||||||||||||||||||
| 348 | 3 | my (@parsed, @comments); | |||||||||||||||||||||||||||
| 349 | 3 | foreach my $child (@$parsed) { | |||||||||||||||||||||||||||
| 350 | # Collect comments that come before Include and Namespace objects; dispose of them when we dispose of the object | ||||||||||||||||||||||||||||
| 351 | # Otherwise, we'll have combined comments that don't make sense (comment for now gone Namespace with comment | ||||||||||||||||||||||||||||
| 352 | # for a typedef) | ||||||||||||||||||||||||||||
| 353 | 44 | ▶ | if ($child->isa('Thrift::IDL::Comment')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 354 | 24 | push @comments, $child; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | elsif (! $child->isa('Thrift::IDL::Header')) { | ||||||||||||||||||||||||||||
| 357 | #$child->{header} = $header; | ||||||||||||||||||||||||||||
| 358 | 11 | push @parsed, @comments, $child; | |||||||||||||||||||||||||||
| 359 | 11 | @comments = (); | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | else { | ||||||||||||||||||||||||||||
| 362 | 9 | ▶ | if ($child->isa('Thrift::IDL::Include')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 363 | 1 | push @{ $header->{includes} }, $child; | |||||||||||||||||||||||||||
| 364 | # Insert the included document at the same point as the 'include' declaration | ||||||||||||||||||||||||||||
| 365 | 1 | my $include_fn = abs_path($include_base . '/' . $child->value); | |||||||||||||||||||||||||||
| 366 | 1 | ▶ | if (! -f $include_fn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | 0 | die "include file '".$child->value."' not found in $include_base"; | |||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | 1 | my ($include_parsed, $include_headers) = $class->_parse_thrift({ file => $include_fn }, $debug, $state); | |||||||||||||||||||||||||||
| 370 | 1 | push @parsed, @$include_parsed; | |||||||||||||||||||||||||||
| 371 | 1 | push @headers, @$include_headers; | |||||||||||||||||||||||||||
| 372 | 1 | @comments = (); | |||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | elsif ($child->isa('Thrift::IDL::Namespace')) { | ||||||||||||||||||||||||||||
| 375 | 8 | push @{ $header->{namespaces} }, $child; | |||||||||||||||||||||||||||
| 376 | 8 | @comments = (); | |||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | 3 | return (\@parsed, \@headers); | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 8 | ▶ | sub _set_associations { | ||||||||||||||||||||||||||
|
Subroutine
_set_associations: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 384 | 8 | my $element = shift; | |||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | 8 | my @comments; | |||||||||||||||||||||||||||
| 387 | 8 | ▶ | for (my $i = 0; $i <= $#{ $element->{children} }; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 388 | 34 | my $child = $element->{children}[$i]; | |||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | # Reference myself in the child object | ||||||||||||||||||||||||||||
| 391 | #$child->{parent} = $element; | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 34 | ▶ | $child->{service} = $element if $child->isa('Thrift::IDL::Method'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | # Associate comments with the child elements that follow the comments | ||||||||||||||||||||||||||||
| 396 | 34 | ▶ | if ($child->isa('Thrift::IDL::Comment')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 397 | 8 | push @comments, $child; | |||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | elsif (@comments) { | ||||||||||||||||||||||||||||
| 400 | 8 | $child->{comments} = [ @comments ]; | |||||||||||||||||||||||||||
| 401 | 8 | @comments = (); | |||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 34 | ▶ | $child->{comments} ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$child->{"comments"} ||= []
MC/DC100%1/1
$child->{"comments"} ||= []
$child->{"comments"}
Truth table100%2/2
A
A= $child->{"comments"}
| |||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 34 | ▶ | if ($child->{children}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 407 | 6 | _set_associations($child); | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | 4 | 1; | |||||||||||||||||||||||||||