| 1 | use v5; | ||
| 2 | |||
| 3 | package Perlito5::Grammar::Sigil; | ||
| 4 | |||
| 5 | use Perlito5::Grammar::Precedence; | ||
| 6 | |||
| 7 | |||
| 8 | # the special variables list | ||
| 9 | # obtained with: | ||
| 10 | # $ perldoc -u perlvar | perl -ne ' /^\s*$/ && next; if (/^=item\s+([^\n]+)/) { push @item, $1; print "@item - $_" } else { if (@item) { push @xx, [@item]; print "push\n"; @item = () } }; END {use Data::Dumper; print Dumper \@xx} ' | ||
| 11 | |||
| 12 | # $ perldoc -u perlvar | perl -ne ' /^\s*$/ && next; if (/^=item\s+([^\n]+)/) { push @item, $1; print "@item - $_" } else { if (@item) { push @xx, grep { /^[\@\$\%][^a-zA-Z0-9]$/ } @item; print "push\n"; @item = () } }; END {use Data::Dumper; print "$_ => 1,\n" for @xx} ' | ||
| 13 | |||
| 14 | my %special_var = ( | ||
| 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 | # '$*' => '', # "$* is no longer supported" | ||
| 52 | # '$#' => 1, # "$# is no longer supported" | ||
| 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 | sub term_special_var { | ||
| 92 | my $str = $_[0]; | ||
| 93 | my $pos = $_[1]; | ||
| 94 | my $len = 0; | ||
| 95 | |||
| 96 | # TODO: | ||
| 97 | # | ||
| 98 | # this is ok: | ||
| 99 | # ' $ {!} ', ' @ {+} ', ' $#{+} ' | ||
| 100 | # ' @{ x ->[10] } ' | ||
| 101 | # ' ${v {int} -> {t}} ' | ||
| 102 | # | ||
| 103 | # syntax errors: | ||
| 104 | # ' $# {+} ', ' $ #{+} ', ' @ { + } ' | ||
| 105 | # ' @x->[10] ' | ||
| 106 | # | ||
| 107 | # this is never a function call: | ||
| 108 | # ' ${main::x} ' | ||
| 109 | # | ||
| 110 | |||
| 111 | my $s = $str->[$pos] . $str->[$pos + 1] . $str->[$pos + 2]; | ||
| 112 | if ( $s eq '$#[' ) { | ||
| 113 | # special case: $# is not valid, but @# is ok | ||
| 114 | $len = 2; | ||
| 115 | } | ||
| 116 | elsif ( exists $special_var{$s} ) { | ||
| 117 | $len = length($s); | ||
| 118 | } | ||
| 119 | else { | ||
| 120 | $s = $str->[$pos] . $str->[$pos + 1]; | ||
| 121 | if ( exists $special_var{$s} ) { | ||
| 122 | $len = 2; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | if ( $len ) { | ||
| 126 | my $c0 = $str->[ $pos + $len - 1 ]; | ||
| 127 | my $c1 = $str->[ $pos + $len ]; | ||
| 128 | if ( | ||
| 129 | ( $Perlito5::Grammar::is_var_sigil{$c0} ) | ||
| 130 | && | ||
| 131 | ( $Perlito5::Grammar::is_var_sigil{$c1} | ||
| 132 | || $Perlito5::Grammar::is_ident_middle{$c1} | ||
| 133 | ) | ||
| 134 | ) | ||
| 135 | { | ||
| 136 | # TODO - this needs more testing | ||
| 137 | # looks like a prefix operator, not a special var | ||
| 138 | } | ||
| 139 | else { | ||
| 140 | return { | ||
| 141 | str => $str, | ||
| 142 | from => $pos, | ||
| 143 | to => $pos + $len, | ||
| 144 | capture => [ 'term', | ||
| 145 | Perlito5::AST::Var->new( | ||
| 146 | sigil => substr($s, 0, $len - 1), | ||
| 147 | namespace => $special_var{$s}, | ||
| 148 | name => substr($s, $len - 1, 1), | ||
| 149 | _namespace => 'main', | ||
| 150 | ) | ||
| 151 | ] | ||
| 152 | }; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | return 0; | ||
| 156 | } | ||
| 157 | |||
| 158 | my %sigil = ( | ||
| 159 | '$' => 1, | ||
| 160 | '%' => 1, | ||
| 161 | '@' => 1, | ||
| 162 | '&' => 1, | ||
| 163 | '*' => 1, | ||
| 164 | ); | ||
| 165 | |||
| 166 | sub term_sigil { | ||
| 167 | my $str = $_[0]; | ||
| 168 | my $pos = $_[1]; | ||
| 169 | |||
| 170 | my $c1 = $str->[$pos]; | ||
| 171 | return unless exists $sigil{$c1}; | ||
| 172 | |||
| 173 | my $p = $pos + 1; | ||
| 174 | my $sigil = $c1; | ||
| 175 | if ( $c1 eq '$' && $str->[$pos+1] eq '#') { | ||
| 176 | $sigil = '$#'; | ||
| 177 | $p++; | ||
| 178 | } | ||
| 179 | |||
| 180 | my $m = Perlito5::Grammar::Space::opt_ws($str, $p); | ||
| 181 | $p = $m->{to}; | ||
| 182 | |||
| 183 | my $p0 = $p; | ||
| 184 | $c1 = $str->[$p]; | ||
| 185 | my $q = $p + 1; | ||
| 186 | if ( $c1 eq '{' ) { | ||
| 187 | # ${ ... | ||
| 188 | my $p = $q; | ||
| 189 | $m = Perlito5::Grammar::Space::opt_ws($str, $p); | ||
| 190 | $p = $m->{to}; | ||
| 191 | |||
| 192 | $m = Perlito5::Grammar::optional_namespace_before_ident( $str, $p ); | ||
| 193 | if ($m) { | ||
| 194 | my $namespace = Perlito5::Match::flat($m); | ||
| 195 | my $pos = $m->{to}; | ||
| 196 | # ${name ... | ||
| 197 | my $n = Perlito5::Grammar::var_name( $str, $m->{to} ); | ||
| 198 | my $name; | ||
| 199 | if ($n) { | ||
| 200 | $name = Perlito5::Match::flat($n); | ||
| 201 | $pos = $n->{to}; | ||
| 202 | } | ||
| 203 | if ( | ||
| 204 | ( $namespace || $name ) | ||
| 205 | && !( | ||
| 206 | ( $namespace eq 'CORE' || $namespace eq '' ) | ||
| 207 | && ( $name eq 'sub' ) | ||
| 208 | ) | ||
| 209 | ) | ||
| 210 | { | ||
| 211 | my $spc = Perlito5::Grammar::Space::opt_ws($str, $pos); | ||
| 212 | my $pos = $spc->{to}; | ||
| 213 | if ($str->[$pos] eq '{' || $str->[$pos] eq '[' || $str->[$pos] eq '}') { | ||
| 214 | # we are not parsing: ${subr()} | ||
| 215 | # we are parsing: ${var} ${var{index}} ${var[index]} | ||
| 216 | # create the 'Var' object | ||
| 217 | $m->{capture} = Perlito5::AST::Var->new( | ||
| 218 | sigil => $sigil, | ||
| 219 | namespace => $namespace, | ||
| 220 | name => $name, | ||
| 221 | ); | ||
| 222 | $m->{to} = $spc->{to}; | ||
| 223 | # hijack some string interpolation code to parse the subscript | ||
| 224 | $m = Perlito5::Grammar::String::double_quoted_var_with_subscript($m); | ||
| 225 | $m->{capture} = [ 'term', $m->{capture} ]; | ||
| 226 | $spc = Perlito5::Grammar::Space::opt_ws($str, $m->{to}); | ||
| 227 | my $p = $spc->{to}; | ||
| 228 | if ( $str->[$p] eq '}' ) { | ||
| 229 | $m->{to} = $p + 1; | ||
| 230 | return $m; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | elsif ($str->[$pos] eq '-' && $str->[$pos + 1] eq '>') { | ||
| 234 | my $spc = Perlito5::Grammar::Space::opt_ws($str, $pos + 2); | ||
| 235 | my $pos = $spc->{to}; | ||
| 236 | if ($str->[$pos] eq '{' || $str->[$pos] eq '[') { | ||
| 237 | # we are parsing: ${var->{index}} ${var->[index]} | ||
| 238 | # create the 'Var' object | ||
| 239 | $m->{capture} = Perlito5::AST::Var->new( | ||
| 240 | sigil => $sigil, | ||
| 241 | namespace => ( $namespace || $Perlito5::PKG_NAME ), # deref variable is always global | ||
| 242 | name => $name, | ||
| 243 | ); | ||
| 244 | $m->{to} = $spc->{to}; | ||
| 245 | # hijack some string interpolation code to parse the subscript | ||
| 246 | $m = Perlito5::Grammar::String::double_quoted_var_with_subscript($m); | ||
| 247 | $m->{capture} = [ 'term', $m->{capture} ]; | ||
| 248 | $spc = Perlito5::Grammar::Space::opt_ws($str, $m->{to}); | ||
| 249 | my $p = $spc->{to}; | ||
| 250 | if ( $str->[$p] eq '}' ) { | ||
| 251 | $m->{to} = $p + 1; | ||
| 252 | return $m; | ||
| 253 | } | ||
| 254 | } | ||
| 255 | } | ||
| 256 | } | ||
| 257 | } | ||
| 258 | my $caret = Perlito5::Grammar::caret_char( $str, $p ); | ||
| 259 | if ( $caret ) { | ||
| 260 | # ${^ ... | ||
| 261 | my $p = $caret->{to}; | ||
| 262 | my $name = Perlito5::Match::flat($caret); | ||
| 263 | $m = Perlito5::Grammar::var_name($str, $p); | ||
| 264 | if ($m) { | ||
| 265 | $name = $name . Perlito5::Match::flat($m); | ||
| 266 | $p = $m->{to}; | ||
| 267 | } | ||
| 268 | if ( $str->[$p] eq '}' ) { | ||
| 269 | $caret->{capture} = [ 'term', | ||
| 270 | Perlito5::AST::Var->new( | ||
| 271 | name => $name, | ||
| 272 | namespace => '', | ||
| 273 | sigil => $sigil, | ||
| 274 | ), | ||
| 275 | ]; | ||
| 276 | $caret->{to} = $p + 1; | ||
| 277 | return $caret; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | my $special = $sigil . $str->[$p]; | ||
| 281 | if ( exists $special_var{$special} ) { | ||
| 282 | # ${@} $#{+} - special variable | ||
| 283 | my $m = Perlito5::Grammar::Space::opt_ws($str, $p + 1); | ||
| 284 | my $p2 = $m->{to}; | ||
| 285 | my $c2 = $str->[$p2]; | ||
| 286 | if ($c2 eq '}') { | ||
| 287 | $m->{to} = $p2 + 1; | ||
| 288 | $m->{capture} = [ 'term', | ||
| 289 | Perlito5::AST::Var->new( | ||
| 290 | sigil => $sigil, | ||
| 291 | namespace => '', | ||
| 292 | name => $str->[$p], | ||
| 293 | ( $sigil eq '$#' ? ( _real_sigil => '@' ) : () ), | ||
| 294 | _namespace => 'main', | ||
| 295 | ) | ||
| 296 | ]; | ||
| 297 | return $m; | ||
| 298 | } | ||
| 299 | } | ||
| 300 | if ($str->[$p] eq '}') { | ||
| 301 | # ${} | ||
| 302 | Perlito5::Compiler::error( "syntax error" ); | ||
| 303 | } | ||
| 304 | $m = Perlito5::Grammar::block( $str, $p0 ); | ||
| 305 | if ($m) { | ||
| 306 | # ${ ... } | ||
| 307 | my $ast = Perlito5::Match::flat($m); | ||
| 308 | |||
| 309 | if (@{$ast->{stmts}} == 1 | ||
| 310 | && ( ref($ast->{stmts}[0]) eq 'Perlito5::AST::Apply' | ||
| 311 | || ref($ast->{stmts}[0]) eq 'Perlito5::AST::Call' | ||
| 312 | || ref($ast->{stmts}[0]) eq 'Perlito5::AST::Var' | ||
| 313 | || ref($ast->{stmts}[0]) eq 'Perlito5::AST::Buf' | ||
| 314 | || ref($ast->{stmts}[0]) eq 'Perlito5::AST::Index' | ||
| 315 | || ref($ast->{stmts}[0]) eq 'Perlito5::AST::Lookup' ) | ||
| 316 | ) { | ||
| 317 | $m->{capture} = [ | ||
| 318 | 'term', | ||
| 319 | Perlito5::AST::Apply->new( | ||
| 320 | code => 'prefix:<' . $sigil . '>', | ||
| 321 | namespace => '', | ||
| 322 | arguments => [ $ast->{stmts}[0] ], | ||
| 323 | _strict_refs => ( $^H & $Perlito5::STRICT_REFS ), | ||
| 324 | ), | ||
| 325 | ]; | ||
| 326 | return $m; | ||
| 327 | } | ||
| 328 | |||
| 329 | $m->{capture} = [ | ||
| 330 | 'term', | ||
| 331 | Perlito5::AST::Apply->new( | ||
| 332 | code => 'prefix:<' . $sigil . '>', | ||
| 333 | arguments => [ | ||
| 334 | Perlito5::AST::Apply->new( | ||
| 335 | code => 'do', | ||
| 336 | namespace => '', | ||
| 337 | arguments => [ $ast ], | ||
| 338 | ) | ||
| 339 | ], | ||
| 340 | _strict_refs => ( $^H & $Perlito5::STRICT_REFS ), | ||
| 341 | ) | ||
| 342 | ]; | ||
| 343 | return $m; | ||
| 344 | } | ||
| 345 | } | ||
| 346 | my $caret = Perlito5::Grammar::caret_char( $str, $p ); | ||
| 347 | if ( $caret ) { | ||
| 348 | # $^ ... | ||
| 349 | my $name = Perlito5::Match::flat($caret); | ||
| 350 | $caret->{capture} = [ 'term', | ||
| 351 | Perlito5::AST::Var->new( | ||
| 352 | name => $name, | ||
| 353 | namespace => '', | ||
| 354 | sigil => $sigil, | ||
| 355 | ), | ||
| 356 | ]; | ||
| 357 | return $caret; | ||
| 358 | } | ||
| 359 | if ( $c1 eq '$' ) { | ||
| 360 | # $$ ... | ||
| 361 | my $m2 = Perlito5::Grammar::Space::ws($str, $p + 1); | ||
| 362 | if (!$m2) { | ||
| 363 | my $p2 = $p + 1; | ||
| 364 | my $c2 = $str->[$p2]; | ||
| 365 | if ( $c2 eq '_' || $c2 eq '$' || !exists $special_var{ '$' . $c2 } ) { | ||
| 366 | # not '$$;' not '$$,' not '$$+1' | ||
| 367 | $m = term_sigil( $str, $p ); | ||
| 368 | if ($m) { | ||
| 369 | $m->{capture} = [ 'term', | ||
| 370 | Perlito5::AST::Apply->new( | ||
| 371 | arguments => [ $m->{capture}[1] ], | ||
| 372 | code => 'prefix:<' . $sigil . '>', | ||
| 373 | namespace => '', | ||
| 374 | _strict_refs => ( $^H & $Perlito5::STRICT_REFS ), | ||
| 375 | ) | ||
| 376 | ]; | ||
| 377 | return $m; | ||
| 378 | } | ||
| 379 | } | ||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
| 383 | $m = Perlito5::Grammar::optional_namespace_before_ident( $str, $p ); | ||
| 384 | if ($m) { | ||
| 385 | my $namespace = Perlito5::Match::flat($m); | ||
| 386 | # $name ... | ||
| 387 | my $n = Perlito5::Grammar::var_name( $str, $m->{to} ); | ||
| 388 | if ($n) { | ||
| 389 | $n->{capture} = [ 'term', | ||
| 390 | Perlito5::AST::Var->new( | ||
| 391 | sigil => $sigil, | ||
| 392 | namespace => $namespace, | ||
| 393 | name => Perlito5::Match::flat($n), | ||
| 394 | ( $sigil eq '$#' ? ( _real_sigil => '@' ) : () ), | ||
| 395 | ) | ||
| 396 | ]; | ||
| 397 | return $n; | ||
| 398 | } | ||
| 399 | if ($namespace) { | ||
| 400 | $m->{capture} = [ 'term', | ||
| 401 | Perlito5::AST::Var->new( | ||
| 402 | sigil => $sigil, | ||
| 403 | namespace => $namespace, | ||
| 404 | name => undef, | ||
| 405 | ( $sigil eq '$#' ? ( _real_sigil => '@' ) : () ), | ||
| 406 | ) | ||
| 407 | ]; | ||
| 408 | return $m; | ||
| 409 | } | ||
| 410 | |||
| 411 | } | ||
| 412 | |||
| 413 | # $! ... | ||
| 414 | return term_special_var( $str, $pos ); | ||
| 415 | }; | ||
| 416 | |||
| 417 | |||
| 418 | Perlito5::Grammar::Precedence::add_term( '$' => \&term_sigil ); | ||
| 419 | Perlito5::Grammar::Precedence::add_term( '@' => \&term_sigil ); | ||
| 420 | Perlito5::Grammar::Precedence::add_term( '%' => \&term_sigil ); | ||
| 421 | Perlito5::Grammar::Precedence::add_term( '&' => \&term_sigil ); | ||
| 422 | Perlito5::Grammar::Precedence::add_term( '*' => \&term_sigil ); | ||
| 423 | |||
| 424 | |||
| 425 | 1; | ||
| 426 | |||
| 427 | =begin | ||
| 428 | |||
| 429 | =head1 NAME | ||
| 430 | |||
| 431 | Perlito5::Grammar::Sigil - Parser module for Perlito | ||
| 432 | |||
| 433 | =head1 SYNOPSIS | ||
| 434 | |||
| 435 | =head1 DESCRIPTION | ||
| 436 | |||
| 437 | This module parses source code for Perl 5 statements and generates Perlito5 AST. | ||
| 438 | |||
| 439 | =head1 AUTHORS | ||
| 440 | |||
| 441 | Flavio Soibelmann Glock <fglock@gmail.com>. | ||
| 442 | The Pugs Team. | ||
| 443 | |||
| 444 | =head1 COPYRIGHT | ||
| 445 | |||
| 446 | Copyright 2010, 2011, 2012 by Flavio Soibelmann Glock and others. | ||
| 447 | |||
| 448 | This program is free software; you can redistribute it and/or modify it | ||
| 449 | under the same terms as Perl itself. | ||
| 450 | |||
| 451 | See L<http://www.perl.com/perl/misc/Artistic.html> | ||
| 452 | |||
| 453 | =end | ||
| 454 |