| 1 | # Do not edit this file - Generated by Perlito5 9.027 | ||
| 2 | |||
| 3 | { | ||
| 4 | package main; | ||
| 5 | package Perlito5::Grammar::Use; | ||
| 6 | use Perlito5::Grammar::Precedence ; | ||
| 7 | use Perlito5::Grammar ; | ||
| 8 | use strict ; | ||
| 9 | our @Perlito_internal_lib_directory = ("Perlito5X", ''); | ||
| 10 | our %Perlito_internal_module; | ||
| 11 | sub Perlito5::Grammar::Use::use_decl { | ||
| 12 | my $str = $_[0]; | ||
| 13 | my $pos = $_[1]; | ||
| 14 | my $MATCH = {"str" => $str, "from" => $pos, "to" => $pos, }; | ||
| 15 | my $tmp = ((do { | ||
| 16 | my $tmp102 = $MATCH->{"to"}; | ||
| 17 | ((("u" eq $str->[($MATCH->{"to"}) + 0]) && ("s" eq $str->[($MATCH->{"to"}) + 1]) && ("e" eq $str->[($MATCH->{"to"}) + 2]) && ($MATCH->{"to"} += 3))) || ($MATCH->{"to"} = $tmp102, (("n" eq $str->[($MATCH->{"to"}) + 0]) && ("o" eq $str->[($MATCH->{"to"}) + 1]) && ($MATCH->{"to"} += 2))) | ||
| 18 | })); | ||
| 19 | $tmp ? $MATCH : undef | ||
| 20 | } | ||
| 21 | sub Perlito5::Grammar::Use::version_string { | ||
| 22 | my $str = $_[0]; | ||
| 23 | my $pos = $_[1]; | ||
| 24 | my $MATCH = {"str" => $str, "from" => $pos, "to" => $pos, }; | ||
| 25 | my $tmp = ((do { | ||
| 26 | my $tmp108 = $MATCH->{"to"}; | ||
| 27 | (((do { | ||
| 28 | my $m2 = Perlito5::Grammar::Number::val_version($str, $MATCH->{"to"}); | ||
| 29 | if ($m2) { | ||
| 30 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 31 | $MATCH->{"Perlito5::Grammar::Number::val_version"} = $m2; | ||
| 32 | 1 | ||
| 33 | } | ||
| 34 | else {; | ||
| 35 | 0 | ||
| 36 | } | ||
| 37 | }) && (do { | ||
| 38 | $MATCH->{"capture"} = $MATCH->{"Perlito5::Grammar::Number::val_version"}->{"capture"}; | ||
| 39 | 1 | ||
| 40 | }))) || ($MATCH->{"to"} = $tmp108, ((do { | ||
| 41 | my $m2 = Perlito5::Grammar::Number::term_digit($str, $MATCH->{"to"}); | ||
| 42 | if ($m2) { | ||
| 43 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 44 | $MATCH->{"Perlito5::Grammar::Number::term_digit"} = $m2; | ||
| 45 | 1 | ||
| 46 | } | ||
| 47 | else {; | ||
| 48 | 0 | ||
| 49 | } | ||
| 50 | }) && (do { | ||
| 51 | my $version = $MATCH->{"Perlito5::Grammar::Number::term_digit"}->{"capture"}->[1]->{"buf"} || $MATCH->{"Perlito5::Grammar::Number::term_digit"}->{"capture"}->[1]->{"int"} || $MATCH->{"Perlito5::Grammar::Number::term_digit"}->{"capture"}->[1]->{"num"}; | ||
| 52 | $MATCH->{"capture"} = Perlito5::AST::Buf::->new("buf", $version); | ||
| 53 | 1 | ||
| 54 | }))) | ||
| 55 | })); | ||
| 56 | $tmp ? $MATCH : undef | ||
| 57 | } | ||
| 58 | sub Perlito5::Grammar::Use::term_require { | ||
| 59 | my $str = $_[0]; | ||
| 60 | my $pos = $_[1]; | ||
| 61 | my $MATCH = {"str" => $str, "from" => $pos, "to" => $pos, }; | ||
| 62 | my $tmp = (((("r" eq $str->[($MATCH->{"to"}) + 0]) && ("e" eq $str->[($MATCH->{"to"}) + 1]) && ("q" eq $str->[($MATCH->{"to"}) + 2]) && ("u" eq $str->[($MATCH->{"to"}) + 3]) && ("i" eq $str->[($MATCH->{"to"}) + 4]) && ("r" eq $str->[($MATCH->{"to"}) + 5]) && ("e" eq $str->[($MATCH->{"to"}) + 6]) && ($MATCH->{"to"} += 7)) && (do { | ||
| 63 | my $m2 = Perlito5::Grammar::Space::ws($str, $MATCH->{"to"}); | ||
| 64 | if ($m2) { | ||
| 65 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 66 | 1 | ||
| 67 | } | ||
| 68 | else {; | ||
| 69 | 0 | ||
| 70 | } | ||
| 71 | }) && (do { | ||
| 72 | my $tmp117 = $MATCH->{"to"}; | ||
| 73 | (((do { | ||
| 74 | my $m2 = Perlito5::Grammar::Use::version_string($str, $MATCH->{"to"}); | ||
| 75 | if ($m2) { | ||
| 76 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 77 | $MATCH->{"version_string"} = $m2; | ||
| 78 | 1 | ||
| 79 | } | ||
| 80 | else {; | ||
| 81 | 0 | ||
| 82 | } | ||
| 83 | }) && (do { | ||
| 84 | my $version = $MATCH->{"version_string"}->{"capture"}; | ||
| 85 | $version->{"is_version_string"} = 1; | ||
| 86 | $MATCH->{"capture"} = ["term", Perlito5::AST::Apply::->new("code", "require", "namespace", '', "arguments", [$version])]; | ||
| 87 | 1 | ||
| 88 | }))) || ($MATCH->{"to"} = $tmp117, ((do { | ||
| 89 | my $m2 = Perlito5::Grammar::full_ident($str, $MATCH->{"to"}); | ||
| 90 | if ($m2) { | ||
| 91 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 92 | $MATCH->{"Perlito5::Grammar::full_ident"} = $m2; | ||
| 93 | 1 | ||
| 94 | } | ||
| 95 | else {; | ||
| 96 | 0 | ||
| 97 | } | ||
| 98 | }) && (do { | ||
| 99 | my $module_name = Perlito5::Match::flat($MATCH->{"Perlito5::Grammar::full_ident"}); | ||
| 100 | $Perlito5::PACKAGES->{$module_name} = 1; | ||
| 101 | my $filename = Perlito5::Grammar::Use::modulename_to_filename($module_name); | ||
| 102 | $MATCH->{"capture"} = ["term", Perlito5::AST::Apply::->new("code", "require", "namespace", '', "arguments", [Perlito5::AST::Buf::->new("buf", $filename)])]; | ||
| 103 | 1 | ||
| 104 | }))) | ||
| 105 | }))); | ||
| 106 | $tmp ? $MATCH : undef | ||
| 107 | } | ||
| 108 | sub Perlito5::Grammar::Use::stmt_use { | ||
| 109 | my $str = $_[0]; | ||
| 110 | my $pos = $_[1]; | ||
| 111 | my $MATCH = {"str" => $str, "from" => $pos, "to" => $pos, }; | ||
| 112 | my $tmp = (((do { | ||
| 113 | my $m2 = Perlito5::Grammar::Use::use_decl($str, $MATCH->{"to"}); | ||
| 114 | if ($m2) { | ||
| 115 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 116 | $MATCH->{"use_decl"} = $m2; | ||
| 117 | 1 | ||
| 118 | } | ||
| 119 | else {; | ||
| 120 | 0 | ||
| 121 | } | ||
| 122 | }) && (do { | ||
| 123 | my $m2 = Perlito5::Grammar::Space::ws($str, $MATCH->{"to"}); | ||
| 124 | if ($m2) { | ||
| 125 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 126 | 1 | ||
| 127 | } | ||
| 128 | else {; | ||
| 129 | 0 | ||
| 130 | } | ||
| 131 | }) && (do { | ||
| 132 | my $tmp129 = $MATCH->{"to"}; | ||
| 133 | (((do { | ||
| 134 | my $m2 = Perlito5::Grammar::Use::version_string($str, $MATCH->{"to"}); | ||
| 135 | if ($m2) { | ||
| 136 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 137 | $MATCH->{"version_string"} = $m2; | ||
| 138 | 1 | ||
| 139 | } | ||
| 140 | else {; | ||
| 141 | 0 | ||
| 142 | } | ||
| 143 | }) && (do { | ||
| 144 | my $version = $MATCH->{"version_string"}->{"capture"}->{"buf"}; | ||
| 145 | Perlito5::test_perl_version($version); | ||
| 146 | $MATCH->{"capture"} = Perlito5::Grammar::Block::ast_nop(); | ||
| 147 | 1 | ||
| 148 | }))) || ($MATCH->{"to"} = $tmp129, ((do { | ||
| 149 | my $m2 = Perlito5::Grammar::full_ident($str, $MATCH->{"to"}); | ||
| 150 | if ($m2) { | ||
| 151 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 152 | $MATCH->{"Perlito5::Grammar::full_ident"} = $m2; | ||
| 153 | 1 | ||
| 154 | } | ||
| 155 | else {; | ||
| 156 | 0 | ||
| 157 | } | ||
| 158 | }) && (do { | ||
| 159 | my $m = $MATCH; | ||
| 160 | if (!((("-" eq $str->[($MATCH->{"to"}) + 0]) && ($MATCH->{"to"} += 1)) && (do { | ||
| 161 | my $m2 = Perlito5::Grammar::ident($str, $MATCH->{"to"}); | ||
| 162 | if ($m2) { | ||
| 163 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 164 | push(@{$MATCH->{"Perlito5::Grammar::ident"}}, $m2); | ||
| 165 | 1 | ||
| 166 | } | ||
| 167 | else {; | ||
| 168 | 0 | ||
| 169 | } | ||
| 170 | }))) {; | ||
| 171 | $MATCH = $m | ||
| 172 | } | ||
| 173 | 1 | ||
| 174 | }) && (do { | ||
| 175 | my $m = $MATCH; | ||
| 176 | if (!((do { | ||
| 177 | my $m2 = Perlito5::Grammar::Space::ws($str, $MATCH->{"to"}); | ||
| 178 | if ($m2) { | ||
| 179 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 180 | 1 | ||
| 181 | } | ||
| 182 | else {; | ||
| 183 | 0 | ||
| 184 | } | ||
| 185 | }) && (do { | ||
| 186 | my $m2 = Perlito5::Grammar::Use::version_string($str, $MATCH->{"to"}); | ||
| 187 | if ($m2) { | ||
| 188 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 189 | push(@{$MATCH->{"version_string"}}, $m2); | ||
| 190 | 1 | ||
| 191 | } | ||
| 192 | else {; | ||
| 193 | 0 | ||
| 194 | } | ||
| 195 | }) && (do { | ||
| 196 | my $m2 = Perlito5::Grammar::Space::opt_ws($str, $MATCH->{"to"}); | ||
| 197 | if ($m2) { | ||
| 198 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 199 | 1 | ||
| 200 | } | ||
| 201 | else {; | ||
| 202 | 0 | ||
| 203 | } | ||
| 204 | }) && (do { | ||
| 205 | my $tmp = $MATCH; | ||
| 206 | $MATCH = {"from" => $tmp->{"to"}, "to" => $tmp->{"to"}, }; | ||
| 207 | my $res = (("," eq $str->[($MATCH->{"to"}) + 0]) && ($MATCH->{"to"} += 1)); | ||
| 208 | $MATCH = $tmp; | ||
| 209 | $res ? 0 : 1 | ||
| 210 | }))) {; | ||
| 211 | $MATCH = $m | ||
| 212 | } | ||
| 213 | 1 | ||
| 214 | }) && (do { | ||
| 215 | my $tmp130 = $MATCH->{"to"}; | ||
| 216 | ((do { | ||
| 217 | my $m2 = Perlito5::Grammar::Expression::exp_parse($str, $MATCH->{"to"}); | ||
| 218 | if ($m2) { | ||
| 219 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 220 | $MATCH->{"Perlito5::Grammar::Expression::exp_parse"} = $m2; | ||
| 221 | 1 | ||
| 222 | } | ||
| 223 | else {; | ||
| 224 | 0 | ||
| 225 | } | ||
| 226 | })) || ($MATCH->{"to"} = $tmp130, (do { | ||
| 227 | my $m2 = Perlito5::Grammar::Space::opt_ws($str, $MATCH->{"to"}); | ||
| 228 | if ($m2) { | ||
| 229 | $MATCH->{"to"} = $m2->{"to"}; | ||
| 230 | 1 | ||
| 231 | } | ||
| 232 | else {; | ||
| 233 | 0 | ||
| 234 | } | ||
| 235 | })) | ||
| 236 | }) && (do { | ||
| 237 | my $version = $MATCH->{"version_string"}->[0]->{"capture"}->{"buf"}; | ||
| 238 | my $list = Perlito5::Match::flat($MATCH->{"Perlito5::Grammar::Expression::exp_parse"}); | ||
| 239 | if (ref($list) eq "Perlito5::AST::Buf") {; | ||
| 240 | $list = [$list->{"buf"}] | ||
| 241 | } | ||
| 242 | elsif ($list) { | ||
| 243 | Perlito5::Grammar::Scope::check_variable_declarations(); | ||
| 244 | my $ast = Perlito5::AST::Block::->new("stmts", [Perlito5::AST::Apply::->new("code", "circumfix:<[ ]>", "arguments", [$list])]); | ||
| 245 | $list = Perlito5::Grammar::Block::eval_begin_block($ast) | ||
| 246 | } | ||
| 247 | else {; | ||
| 248 | $list = undef | ||
| 249 | } | ||
| 250 | my $full_ident = Perlito5::Match::flat($MATCH->{"Perlito5::Grammar::full_ident"}); | ||
| 251 | $Perlito5::PACKAGES->{$full_ident} = 1; | ||
| 252 | my $use_decl = Perlito5::Match::flat($MATCH->{"use_decl"}); | ||
| 253 | if ($use_decl eq "use" && $full_ident eq "vars" && $list) { | ||
| 254 | for my $name (@{$list}) {; | ||
| 255 | $Perlito5::VARS{substr($name, 0, 1) . $Perlito5::PKG_NAME . "::" . substr($name, 1)} = 1 | ||
| 256 | } | ||
| 257 | $MATCH->{"capture"} = Perlito5::Grammar::Block::ast_nop() | ||
| 258 | } | ||
| 259 | elsif ($use_decl eq "use" && $full_ident eq "subs" && $list) { | ||
| 260 | for my $name (@{$list}) { | ||
| 261 | my $glob = $Perlito5::PKG_NAME . "::" . $name; | ||
| 262 | if (!exists($Perlito5::PROTO->{$glob})) {; | ||
| 263 | $Perlito5::PROTO->{$glob} = undef | ||
| 264 | } | ||
| 265 | } | ||
| 266 | $MATCH->{"capture"} = Perlito5::Grammar::Block::ast_nop() | ||
| 267 | } | ||
| 268 | elsif ($use_decl eq "use" && $full_ident eq "constant") { | ||
| 269 | my @ast; | ||
| 270 | if ($list) { | ||
| 271 | my $name = shift(@{$list}); | ||
| 272 | if (ref($name) eq "HASH") {; | ||
| 273 | for my $key (sort {; | ||
| 274 | $a cmp $b | ||
| 275 | } keys(%{$name})) { | ||
| 276 | my $code = "sub " . $key . " () { " . Perlito5::Dumper::_dumper($name->{$key}) . " }"; | ||
| 277 | my $m = Perlito5::Grammar::Statement::statement_parse([split('', $code)], 0); | ||
| 278 | !$m && Perlito5::Compiler::error("not a valid constant: " . join(${"\""}, @{$list})); | ||
| 279 | push(@ast, $m->{"capture"}) | ||
| 280 | } | ||
| 281 | } | ||
| 282 | else { | ||
| 283 | my $code = "sub " . $name . " () { (" . join(", ", map {; | ||
| 284 | Perlito5::Dumper::_dumper($_) | ||
| 285 | } @{$list}) . ") }"; | ||
| 286 | my $m = Perlito5::Grammar::Statement::statement_parse([split('', $code)], 0); | ||
| 287 | !$m && Perlito5::Compiler::error("not a valid constant: " . join(${"\""}, @{$list})); | ||
| 288 | push(@ast, $m->{"capture"}) | ||
| 289 | } | ||
| 290 | } | ||
| 291 | $MATCH->{"capture"} = Perlito5::AST::Block::->new("stmts", \@ast) | ||
| 292 | } | ||
| 293 | elsif ($Perlito5::EMIT_USE) {; | ||
| 294 | $MATCH->{"capture"} = Perlito5::AST::Apply::->new("code", "use", "special_arg", Perlito5::AST::Apply::->new("code", $full_ident, "bareword", 1, "arguments", []), "arguments", $list) | ||
| 295 | } | ||
| 296 | else {; | ||
| 297 | $MATCH->{"capture"} = Perlito5::Grammar::Use::parse_time_eval({"mod" => $full_ident, "code" => $use_decl, "arguments" => $list, }) | ||
| 298 | } | ||
| 299 | 1 | ||
| 300 | }))) || ($MATCH->{"to"} = $tmp129, (do { | ||
| 301 | Perlito5::Compiler::error("Syntax error"); | ||
| 302 | 1 | ||
| 303 | })) | ||
| 304 | }))); | ||
| 305 | $tmp ? $MATCH : undef | ||
| 306 | } | ||
| 307 | sub Perlito5::Grammar::Use::parse_time_eval { | ||
| 308 | my $ast = shift; | ||
| 309 | my $module_name = $ast->{"mod"}; | ||
| 310 | my $use_or_not = $ast->{"code"}; | ||
| 311 | my $arguments = $ast->{"arguments"}; | ||
| 312 | my $skip_import = defined($arguments) && @{$arguments} == 0; | ||
| 313 | defined($arguments) || ($arguments = []); | ||
| 314 | if ($Perlito5::EXPAND_USE) { | ||
| 315 | my $caller = [$Perlito5::PKG_NAME, $Perlito5::FILE_NAME, $Perlito5::LINE_NUMBER], my $filename = Perlito5::Grammar::Use::modulename_to_filename($module_name); | ||
| 316 | unshift(@Perlito5::CALLER, [$module_name]); | ||
| 317 | Perlito5::Grammar::Use::require($filename); | ||
| 318 | shift(@Perlito5::CALLER); | ||
| 319 | if (!$skip_import) { | ||
| 320 | if ($use_or_not eq "use") { | ||
| 321 | my $code = $module_name->can("import"); | ||
| 322 | if (defined($code)) { | ||
| 323 | unshift(@Perlito5::CALLER, $caller); | ||
| 324 | eval { | ||
| 325 | $module_name->import(@{$arguments}); | ||
| 326 | 1 | ||
| 327 | } or Perlito5::Compiler::error(${"\@"}); | ||
| 328 | shift(@Perlito5::CALLER) | ||
| 329 | } | ||
| 330 | } | ||
| 331 | elsif ($use_or_not eq "no") { | ||
| 332 | my $code = $module_name->can("unimport"); | ||
| 333 | if (defined($code)) { | ||
| 334 | unshift(@Perlito5::CALLER, $caller); | ||
| 335 | eval { | ||
| 336 | $module_name->unimport(@{$arguments}); | ||
| 337 | 1 | ||
| 338 | } or Perlito5::Compiler::error(${"\@"}); | ||
| 339 | shift(@Perlito5::CALLER) | ||
| 340 | } | ||
| 341 | } | ||
| 342 | } | ||
| 343 | } | ||
| 344 | else { | ||
| 345 | Perlito5::Grammar::Use::bootstrapping_use($ast); | ||
| 346 | Perlito5::Grammar::Use::emit_time_eval($ast) | ||
| 347 | } | ||
| 348 | return Perlito5::Grammar::Block::ast_nop() | ||
| 349 | } | ||
| 350 | sub Perlito5::Grammar::Use::emit_time_eval { | ||
| 351 | my $ast = shift; | ||
| 352 | if ($ast->{"mod"} eq "strict") { | ||
| 353 | if ($ast->{"code"} eq "use") {; | ||
| 354 | strict::->import() | ||
| 355 | } | ||
| 356 | elsif ($ast->{"code"} eq "no") {; | ||
| 357 | strict::->unimport() | ||
| 358 | } | ||
| 359 | } | ||
| 360 | } | ||
| 361 | sub Perlito5::Grammar::Use::modulename_to_filename { | ||
| 362 | my $s = shift; | ||
| 363 | exists($Perlito_internal_module{$s}) && ($s = $Perlito_internal_module{$s}); | ||
| 364 | $s =~ s!::!/!g; | ||
| 365 | return $s . ".pm" | ||
| 366 | } | ||
| 367 | sub Perlito5::Grammar::Use::filename_lookup { | ||
| 368 | my $filename = shift; | ||
| 369 | if (exists($INC{$filename})) { | ||
| 370 | $INC{$filename} && return "done"; | ||
| 371 | Perlito5::Compiler::error("Compilation failed in require") | ||
| 372 | } | ||
| 373 | if (substr($filename, 0, 2) eq "./") {; | ||
| 374 | if (-f $filename) { | ||
| 375 | $INC{$filename} = $filename; | ||
| 376 | return "todo" | ||
| 377 | } | ||
| 378 | } | ||
| 379 | for my $prefix (@INC) {; | ||
| 380 | for my $internal (@Perlito_internal_lib_directory) { | ||
| 381 | my $realfilename = join("/", $prefix, ($internal ? $internal : ()), $filename); | ||
| 382 | if (-f $realfilename) { | ||
| 383 | $INC{$filename} = $realfilename; | ||
| 384 | return "todo" | ||
| 385 | } | ||
| 386 | } | ||
| 387 | } | ||
| 388 | Perlito5::Compiler::error("Can't locate " . $filename . " in \@INC " . "(\@INC contains " . join(" ", @INC) . ").") | ||
| 389 | } | ||
| 390 | sub Perlito5::Grammar::Use::bootstrapping_use { | ||
| 391 | my $stmt = shift; | ||
| 392 | my $module_name = $stmt->{"mod"}; | ||
| 393 | my $filename = Perlito5::Grammar::Use::modulename_to_filename($module_name); | ||
| 394 | Perlito5::Grammar::Use::filename_lookup($filename) eq "done" && return; | ||
| 395 | local $Perlito5::FILE_NAME = $filename; | ||
| 396 | local $Perlito5::LINE_NUMBER = 1; | ||
| 397 | my $realfilename = $INC{$filename}; | ||
| 398 | open(FILE, "<", $realfilename) or Perlito5::Compiler::error("Cannot read " . $realfilename . ": " . ${"!"} . " | ||
| 399 | "); | ||
| 400 | local $/ = undef; | ||
| 401 | my $source = <FILE>; | ||
| 402 | close(FILE); | ||
| 403 | local @Perlito5::BASE_SCOPE = (Perlito5::Grammar::Scope::->new_base_scope()); | ||
| 404 | local $Perlito5::CLOSURE_SCOPE = 0; | ||
| 405 | local @Perlito5::SCOPE_STMT = (); | ||
| 406 | local ${^H} = 0; | ||
| 407 | local %{^H} = (); | ||
| 408 | my $m = Perlito5::Grammar::exp_stmts($source, 0); | ||
| 409 | $m->{"to"} != length($source) && Perlito5::Compiler::error("Syntax Error near ", $m->{"to"}); | ||
| 410 | if ($m->{"to"} != length($source)) { | ||
| 411 | my $pos = ($m->{"to"}) - 10; | ||
| 412 | $pos < 0 && ($pos = 0); | ||
| 413 | print("* near: ", substr($source, $pos, 20), " | ||
| 414 | "); | ||
| 415 | print("* filename: " . $realfilename . " | ||
| 416 | "); | ||
| 417 | Perlito5::Compiler::error("Syntax Error") | ||
| 418 | } | ||
| 419 | push(@Perlito5::COMP_UNIT, Perlito5::AST::CompUnit::->new("name", "main", "body", Perlito5::Match::flat($m))); | ||
| 420 | return | ||
| 421 | } | ||
| 422 | sub Perlito5::Grammar::Use::require { | ||
| 423 | my $filename = shift; | ||
| 424 | my $m2 = Perlito5::Grammar::Use::version_string([split('', $filename)], 0); | ||
| 425 | if ($m2) { | ||
| 426 | my $version = $m2->{"version_string"}->{"capture"}->{"buf"}; | ||
| 427 | Perlito5::test_perl_version($version); | ||
| 428 | return 1 | ||
| 429 | } | ||
| 430 | if (length($filename) > 3 && substr($filename, -3) eq ".pm") { | ||
| 431 | my $module_name = substr($filename, 0, -3); | ||
| 432 | $module_name =~ s!/!::!g; | ||
| 433 | if (exists($Perlito_internal_module{$module_name})) { | ||
| 434 | my $s = $Perlito_internal_module{$module_name}; | ||
| 435 | $s =~ s!::!/!g; | ||
| 436 | $filename = $s . ".pm" | ||
| 437 | } | ||
| 438 | } | ||
| 439 | Perlito5::Grammar::Use::filename_lookup($filename) eq "done" && return; | ||
| 440 | my $source = Perlito5::Grammar::Use::slurp_file($filename); | ||
| 441 | local $Perlito5::FILE_NAME = $filename; | ||
| 442 | Perlito5::Grammar::Scope::check_variable_declarations(); | ||
| 443 | local @Perlito5::BASE_SCOPE = (Perlito5::Grammar::Scope::->new_base_scope()); | ||
| 444 | local $Perlito5::CLOSURE_SCOPE = 0; | ||
| 445 | local @Perlito5::SCOPE_STMT = (); | ||
| 446 | local ${^H} = 0; | ||
| 447 | local %{^H} = (); | ||
| 448 | my $m = Perlito5::Grammar::exp_stmts($source, 0); | ||
| 449 | my $ast = Perlito5::AST::Block::->new("stmts", Perlito5::Match::flat($m)); | ||
| 450 | my $result = Perlito5::Grammar::Block::eval_begin_block($ast); | ||
| 451 | if (${"\@"}) { | ||
| 452 | $INC{$filename} = undef; | ||
| 453 | Perlito5::Compiler::error(${"\@"}) | ||
| 454 | } | ||
| 455 | elsif (!$result) { | ||
| 456 | delete($INC{$filename}); | ||
| 457 | ${"\@"} && warn(${"\@"}); | ||
| 458 | Perlito5::Compiler::error($filename . " did not return true value") | ||
| 459 | } | ||
| 460 | else {; | ||
| 461 | return $result | ||
| 462 | } | ||
| 463 | } | ||
| 464 | sub Perlito5::Grammar::Use::slurp_file { | ||
| 465 | my $filename = shift; | ||
| 466 | eval { | ||
| 467 | Perlito5::Grammar::Use::filename_lookup($filename); | ||
| 468 | 1 | ||
| 469 | } or do { | ||
| 470 | $INC{$filename} = undef; | ||
| 471 | ${"\@"} = ''; | ||
| 472 | ${"!"} = "No such file or directory"; | ||
| 473 | return "undef" | ||
| 474 | }; | ||
| 475 | my $realfilename = $INC{$filename}; | ||
| 476 | Perlito5::Grammar::Use::slurp_source_file($realfilename) | ||
| 477 | } | ||
| 478 | sub Perlito5::Grammar::Use::slurp_source_file { | ||
| 479 | my $realfilename = shift; | ||
| 480 | open(FILE, "<", $realfilename) or Perlito5::Compiler::error("Cannot read " . $realfilename . ": " . ${"!"} . " | ||
| 481 | "); | ||
| 482 | binmode(FILE); | ||
| 483 | local $/ = undef; | ||
| 484 | my $source = <FILE>; | ||
| 485 | close(FILE); | ||
| 486 | my $charset = ''; | ||
| 487 | eval { | ||
| 488 | my $c0 = substr($source, 0, 1); | ||
| 489 | my $c1 = substr($source, 1, 1); | ||
| 490 | my $c2 = substr($source, 2, 1); | ||
| 491 | my $c3 = substr($source, 3, 1); | ||
| 492 | if ($c0 eq "\x{ef}" && $c1 eq "\x{bb}" && $c2 eq "\x{bf}") { | ||
| 493 | $charset = "UTF-8"; | ||
| 494 | $source = substr($source, 3) | ||
| 495 | } | ||
| 496 | elsif ($c0 eq "\x{fe}" && $c1 eq "\x{ff}") { | ||
| 497 | $charset = "UTF-16BE"; | ||
| 498 | $source = substr($source, 2) | ||
| 499 | } | ||
| 500 | elsif ($c0 eq "\x{ff}" && $c1 eq "\x{fe}") { | ||
| 501 | $charset = "UTF-16LE"; | ||
| 502 | $source = substr($source, 2) | ||
| 503 | } | ||
| 504 | elsif ($c0 eq "\x{0}" && $c1 ne "\x{0}" && $c2 eq "\x{0}" && $c3 ne "\x{0}") {; | ||
| 505 | $charset = "UTF-16BE" | ||
| 506 | } | ||
| 507 | elsif ($c0 ne "\x{0}" && $c1 eq "\x{0}" && $c2 ne "\x{0}" && $c3 eq "\x{0}") {; | ||
| 508 | $charset = "UTF-16LE" | ||
| 509 | } | ||
| 510 | $charset && ($source = Encode::decode($charset, $source)); | ||
| 511 | 1 | ||
| 512 | } or warn("Source code charset '" . $charset . "' decoding failed: " . ${"\@"}); | ||
| 513 | return $source | ||
| 514 | } | ||
| 515 | Perlito5::Grammar::Statement::add_statement("no", \&stmt_use); | ||
| 516 | Perlito5::Grammar::Statement::add_statement("use", \&stmt_use); | ||
| 517 | Perlito5::Grammar::Precedence::add_term("require", \&term_require); | ||
| 518 | 1 | ||
| 519 | } | ||
| 520 | ;1 |