/usr/share/perl5/JE/Parser.pm is in libje-perl 0.056-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 52 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 | package JE::Parser;
our $VERSION = '0.056';
use strict; # :-(
use warnings;# :-(
no warnings 'utf8';
use Scalar::Util 'blessed';
require JE::Code ;
require JE::Number; # ~~~ Don't want to do this
import JE::Code 'add_line_number';
sub add_line_number;
our ($_parser, $global, @_decls, @_stms, $_vars);
#----------METHODS---------#
sub new {
my %self = (
stm_names => [qw[
-function block empty if while with for switch try
labelled var do continue break return throw expr
]],
stm => {
-function => \&function, block => \&block,
empty => \&empty, if => \&if,
while => \&while, with => \&with,
for => \&for, switch => \&switch,
try => \&try, labelled => \&labelled,
var => \&var, do => \&do,
continue => \&continue, break => \&break,
return => \&return, throw => \&throw,
expr => \&expr_statement,
},
global => pop,
);
return bless \%self, shift;
}
sub add_statement {
my($self,$name,$parser) = shift;
my $in_list;
# no warnings 'exiting';
grep $_ eq $name && ++$in_list && goto END_GREP,
@{$$self{stm_names}};
END_GREP:
$in_list or unshift @{$$self{stm_names}} ,$name;
$$self{stm}{$name} = $parser;
return; # Don't return anything for now, because if we return some-
# thing, even if it's not documented, someone might start
# relying on it.
}
sub delete_statement {
my $self = shift;
for my $name (@_) {
delete $$self{stm}{$name};
@{$$self{stm_names}} =
grep $_ ne $name, @{$$self{stm_names}};
}
return $self;
}
sub statement_list {
$_[0]{stm_names};
}
sub parse {
local $_parser = shift;
local(@_decls, @_stms); # Doing this here and localising it saves
for(@{$_parser->{stm_names}}) { # us from having to do it multiple
push @{/^-/ ? \@_decls : \@_stms}, # times.
$_parser->{stm}{$_};
}
JE::Code::parse($_parser->{global}, @_);
}
sub eval {
shift->parse(@_)->execute
}
#----------PARSER---------#
use Exporter 5.57 'import';
our @EXPORT_OK = qw/ $h $n $optional_sc $ss $s $S $id_cont
str num skip ident expr expr_noin statement
statements expected optional_sc/;
our @EXPORT_TAGS = (
vars => [qw/ $h $n $optional_sc $ss $s $S $id_cont/],
functions => [qw/ str num skip ident expr expr_noin statement
statements expected optional_sc /],
);
use re 'taint';
#use subs qw'statement statements assign assign_noin expr new';
use constant JECE => 'JE::Code::Expression';
use constant JECS => 'JE::Code::Statement';
require JE::String;
import JE::String 'desurrogify';
import JE::String 'surrogify';
sub desurrogify($);
sub surrogify($);
# die is called with a scalar ref when the string contains what is
# expected. This will be converted to a longer message afterwards, which
# will read something like "Expected %s but found %s" (probably the most
# common error message, which is why there is a shorthand). Using an array
# ref is the easiest way to stop the 'at ..., line ...' from being appended
# when there is no line break at the end already. die is called with a
# double reference to a string if the string is the complete error
# message.
# ~~~ We may need a function for this second usage, in case we change the
# \\ yet again.
# @ret != push @ret, ... is a funny way of pushing and then checking to
# see whether anything was pushed.
sub expected($) { # public
die \shift
}
# public vars:
# optional horizontal comments and whitespace
our $h = qr(
(?> [ \t\x0b\f\xa0\p{Zs}]* )
(?> (?>/\*[^\cm\cj\x{2028}\x{2029}]*?\*/) [ \t\x0b\f\xa0\p{Zs}]* )?
)x;
# line terminators
our $n = qr((?>[\cm\cj\x{2028}\x{2029}]));
# single space char
our $ss = qr((?>[\p{Zs}\s\ck\x{2028}\x{2029}]));
# optional comments and whitespace
our $s = qr((?>
(?> $ss* )
(?> (?> //[^\cm\cj\x{2028}\x{2029}]* (?>$n|\z) | /\*.*?\*/ )
(?> $ss* )
) *
))sx;
# mandatory comments/whitespace
our $S = qr(
(?>
$ss
|
//[^\cm\cj\x{2028}\x{2029}]*
|
/\*.*?\*/
)
$s
)xs;
our $id_cont = qr(
(?>
\\u([0-9A-Fa-f]{4})
|
[\p{ID_Continue}\$_]
)
)x;
# end public vars
sub str() { # public
# For very long strings (>~45000), this pattern hits a perl bug (Complex regular subexpression recursion limit (32766) exceeded)
#/\G (?: '((?>(?:[^'\\] | \\.)*))'
# |
# "((?>(?:[^"\\] | \\.)*))" )/xcgs or return;
# There are two solutions:
# 1) Use the unrolling technique from the Owl Book.
# 2) Use shorter patterns but more code (contributed by Kevin
# Cameron)
# Number 1 should be faster, but it crashes under perl 5.8.8 on
# Windows, and perhaps on other platforms, too. So we use #2 for
# 5.8.x regardless of platform to be on the safe side.
use constant old_perl => $] < 5.01; # Use a constant so the
my $yarn; # if-block disappears
if(old_perl) { # at compile-time.
# Use a simpler pattern (but more code) to break strings up
# into extents bounded by the quote or escape
my $qt = substr($_,pos($_),1);
$qt =~ /['"]/ or return; # not a string literal if first
pos($_)++; # char not a quote
my $done = 0;
while (defined(substr($_,pos($_),1))) {
my ($part) = /\G([^\\$qt]*)/xcgs;
defined($part) or $part = "";
$yarn .= $part;
my $next = substr($_,pos($_)++,1);
if ($next eq "\\") {
#pass on any escaped char
$next = substr($_,pos($_)++,1);
$yarn .= "\\$next";
} else {
# handle end quote
$done = 1;
last;
}
}
# error if EOF before end of string
return if !$done;
}
else {
/\G (?: '([^'\\]*(?:\\.[^'\\]*)*)'
|
"([^"\\]*(?:\\.[^"\\]*)*)" )/xcgs or return;
$yarn = $+;
}
# Get rid of that constant, as it’s no longer needed.
BEGIN { no strict; delete ${__PACKAGE__.'::'}{old_perl}; }
# transform special chars
no re 'taint'; # I need eval "qq-..." to work
$yarn =~ s/\\(?:
u([0-9a-fA-F]{4})
|
x([0-9a-fA-F]{2})
|
(\r\n?|[\n\x{2028}\x{2029}])
|
([bfnrt])
|
(v)
|
([0-3][0-7]{0,2}|[4-7][0-7]?) # actually slightly looser
| # than what ECMAScript v3 has in its
(.) # addendum (it forbids \0 when followed by 8)
)/
$1 ? chr(hex $1) :
$2 ? chr(hex $2) :
$3 ? "" : # escaped line feed disappears
$4 ? eval "qq-\\$4-" :
$5 ? "\cK" :
defined $6 ? chr oct $6 :
$7
/sgex;
"s$yarn";
}
sub num() { # public
/\G (?:
0[Xx] ([A-Fa-f0-9]+)
|
0 ([01234567]+)
|
(?=[0-9]|\.[0-9])
(
(?:0|[1-9][0-9]*)?
(?:\.[0-9]*)?
(?:[Ee][+-]?[0-9]+)?
)
) /xcg
or return;
return defined $1 ? hex $1 : defined $2 ? oct $2 : $3;
}
our $ident = qr(
(?! (?: case | default ) (?!$id_cont) )
(?:
\\u[0-9A-Fa-f]{4}
|
[\p{ID_Start}\$_]
)
(?> $id_cont* )
)x;
sub unescape_ident($) {
my $ident = shift;
$ident =~ s/\\u([0-9a-fA-F]{4})/chr hex $1/ge;
$ident = desurrogify $ident;
$ident =~ /^[\p{ID_Start}\$_]
[\p{ID_Continue}\$_]*
\z/x
or die \\"'$ident' is not a valid identifier";
$ident;
}
# public
sub skip() { /\G$s/g } # skip whitespace
sub ident() { # public
return unless my($ident) = /\G($ident)/cgox;
unescape_ident $ident;
}
sub params() { # Only called when we know we need it, which is why it dies
# on the second line
my @ret;
/\G\(/gc or expected "'('";
&skip;
if (@ret != push @ret, &ident) { # first identifier (not prec.
# by comma)
while (/\G$s,$s/gc) {
# if there's a comma we need another ident
@ret != push @ret, &ident or expected 'identifier';
}
&skip;
}
/\G\)/gc or expected "')'";
\@ret;
}
sub term() {
my $pos = pos;
my $tmp;
if(/\Gfunction(?!$id_cont)$s/cg) {
my @ret = (func => ident);
@ret == 2 and &skip;
push @ret, ¶ms;
&skip;
/\G \{ /gcx or expected "'{'";
{
local $_vars = [];
push @ret, &statements, $_vars;
}
/\G \} /gocx or expected "'}'";
return bless [[$pos, pos], @ret], JECE;
}
# We don’t call the ident subroutine here,
# because we need to sift out null/true/false/this.
elsif(($tmp)=/\G($ident)/cgox) {
$tmp=~/^(?:(?:tru|fals)e|null)\z/ &&return $global->$tmp;
$tmp eq 'this' and return $tmp;
return "i" . unescape_ident $tmp;
}
elsif(defined($tmp = &str) or
defined($tmp = &num)) {
return $tmp;
}
elsif(m-\G
/
( (?:[^/*\\] | \\.) (?>(?:[^/\\] | \\.)*) )
/
($id_cont*)
-cogx ) {
# I have to use local *_ because
# 'require JE::Object::RegExp' causes
# Scalar::Util->import() to be called (import is inherited
# from Exporter), and &Exporter::import does 'local $_',
# which, in p5.8.8 (though not 5.9.5) causes pos()
# to be reset.
{ local *_; require JE::Object::RegExp; }
# ~~~ This needs to unescape the flags.
return JE::Object::RegExp->new( $global, $1, $2);
}
elsif(/\G\[$s/cg) {
my $anon;
my @ret;
my $length;
while () {
@ret != ($length = push @ret, &assign) and &skip;
push @ret, bless \$anon, 'comma' while /\G,$s/cg;
$length == @ret and last;
}
/\G]/cg or expected "']'";
return bless [[$pos, pos], array => @ret], JECE;
}
elsif(/\G\{$s/cg) {
my @ret;
if($tmp = &ident or defined($tmp = &str)&&$tmp=~s/^s// or
defined($tmp = &num)) {
# first elem, not preceded by comma
push @ret, $tmp;
&skip;
/\G:$s/cggg or expected 'colon';
@ret != push @ret, &assign
or expected \'expression';
&skip;
while (/\G,$s/cg) {
$tmp = ident
or defined($tmp = &str)&&$tmp=~s/^s// or
defined($tmp = &num)
or expected
'identifier, or string or number literal';
push @ret, $tmp;
&skip;
/\G:$s/cggg or expected 'colon';
@ret != push @ret, &assign
or expected 'expression';
&skip;
}
}
/\G}/cg or expected "'}'";
return bless [[$pos, pos], hash => @ret], JECE;
}
elsif (/\G\($s/cg) {
my $ret = &expr or expected 'expression';
&skip;
/\G\)/cg or expected "')'";
return $ret;
}
return
}
sub subscript() { # skips leading whitespace
my $pos = pos;
my $subscript;
if (/\G$s\[$s/cg) {
$subscript = &expr or expected 'expression';
&skip;
/\G]/cog or expected "']'";
}
elsif (/\G$s\.$s/cg) {
$subscript = &ident or expected 'identifier';
}
else { return }
return bless [[$pos, pos], $subscript], 'JE::Code::Subscript';
}
sub args() { # skips leading whitespace
my $pos = pos;
my @ret;
/\G$s\($s/gc or return;
if (@ret != push @ret, &assign) { # first expression (not prec.
# by comma)
while (/\G$s,$s/gc) {
# if there's a comma we need another expression
@ret != push @ret, &assign
or expected 'expression';
}
&skip;
}
/\G\)/gc or expected "')'";
return bless [[$pos, pos], @ret], 'JE::Code::Arguments';
}
sub new_expr() {
/\G new(?!$id_cont) $s /cgx or return;
my $ret = bless [[pos], 'new'], JECE;
my $pos = pos;
my @member_expr = &new_expr || &term
|| expected "identifier, literal, 'new' or '('";
0 while @member_expr != push @member_expr, &subscript;
push @$ret, @member_expr == 1 ? @member_expr :
bless [[$pos, pos], 'member/call', @member_expr],
JECE;
push @$ret, args;
$ret;
}
sub left_expr() {
my($pos,@ret) = pos;
@ret != push @ret, &new_expr || &term or return;
0 while @ret != push @ret, &subscript, &args;
@ret ? @ret == 1 ? @ret :
bless([[$pos, pos], 'member/call', @ret],
JECE)
: return;
}
sub postfix() {
my($pos,@ret) = pos;
@ret != push @ret, &left_expr or return;
push @ret, $1 while /\G $h ( \+\+ | -- ) /cogx;
@ret == 1 ? @ret : bless [[$pos, pos], 'postfix', @ret],
JECE;
}
sub unary() {
my($pos,@ret) = pos;
push @ret, $1 while /\G $s (
(?: delete | void | typeof )(?!$id_cont)
|
\+\+? | --? | ~ | !
) $s /cgx;
@ret != push @ret, &postfix or (
@ret
? expected "expression"
: return
);
@ret == 1 ? @ret : bless [[$pos, pos], 'prefix', @ret],
JECE;
}
sub multi() {
my($pos,@ret) = pos;
@ret != push @ret, &unary or return;
while(m-\G $s ( [*%](?!=) | / (?![*/=]) ) $s -cgx) {
push @ret, $1;
@ret == push @ret, &unary and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub add() {
my($pos,@ret) = pos;
@ret != push @ret, &multi or return;
while(/\G $s ( \+(?![+=]) | -(?![-=]) ) $s /cgx) {
push @ret, $1;
@ret == push @ret, &multi and expected 'expression'
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub bitshift() {
my($pos,@ret) = pos;
@ret == push @ret, &add and return;
while(/\G $s (>>> | >>(?!>) | <<)(?!=) $s /cgx) {
push @ret, $1;
@ret == push @ret, &add and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub rel() {
my($pos,@ret) = pos;
@ret == push @ret, &bitshift and return;
while(/\G $s ( ([<>])(?!\2|=) | [<>]= |
in(?:stanceof)?(?!$id_cont) ) $s /cgx) {
push @ret, $1;
@ret== push @ret, &bitshift and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub rel_noin() {
my($pos,@ret) = pos;
@ret == push @ret, &bitshift and return;
while(/\G $s ( ([<>])(?!\2|=) | [<>]= | instanceof(?!$id_cont) )
$s /cgx) {
push @ret, $1;
@ret == push @ret, &bitshift and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub equal() {
my($pos,@ret) = pos;
@ret == push @ret, &rel and return;
while(/\G $s ([!=]==?) $s /cgx) {
push @ret, $1;
@ret == push @ret, &rel and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub equal_noin() {
my($pos,@ret) = pos;
@ret == push @ret, &rel_noin and return;
while(/\G $s ([!=]==?) $s /cgx) {
push @ret, $1;
@ret == push @ret, &rel_noin and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub bit_and() {
my($pos,@ret) = pos;
@ret == push @ret, &equal and return;
while(/\G $s &(?![&=]) $s /cgx) {
@ret == push @ret, '&', &equal and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub bit_and_noin() {
my($pos,@ret) = pos;
@ret == push @ret, &equal_noin and return;
while(/\G $s &(?![&=]) $s /cgx) {
@ret == push @ret, '&', &equal_noin
and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub bit_or() {
my($pos,@ret) = pos;
@ret == push @ret, &bit_and and return;
while(/\G $s \|(?![|=]) $s /cgx) {
@ret == push @ret, '|', &bit_and and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub bit_or_noin() {
my($pos,@ret) = pos;
@ret == push @ret, &bit_and_noin and return;
while(/\G $s \|(?![|=]) $s /cgx) {
@ret == push @ret, '|', &bit_and_noin
and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub bit_xor() {
my($pos,@ret) = pos;
@ret == push @ret, &bit_or and return;
while(/\G $s \^(?!=) $s /cgx) {
@ret == push @ret, '^', &bit_or and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub bit_xor_noin() {
my($pos,@ret) = pos;
@ret == push @ret, &bit_or_noin and return;
while(/\G $s \^(?!=) $s /cgx) {
@ret == push @ret, '^', &bit_or_noin
and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub and_expr() { # If I just call it 'and', then I have to write
# CORE::and for the operator! (Far too cumbersome.)
my($pos,@ret) = pos;
@ret == push @ret, &bit_xor and return;
while(/\G $s && $s /cgx) {
@ret == push @ret, '&&', &bit_xor
and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub and_noin() {
my($pos,@ret) = pos;
@ret == push @ret, &bit_xor_noin and return;
while(/\G $s && $s /cgx) {
@ret == push @ret, '&&', &bit_xor_noin
and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub or_expr() {
my($pos,@ret) = pos;
@ret == push @ret, &and_expr and return;
while(/\G $s \|\| $s /cgx) {
@ret == push @ret, '||', &and_expr
and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub or_noin() {
my($pos,@ret) = pos;
@ret == push @ret, &and_noin and return;
while(/\G $s \|\| $s /cgx) {
@ret == push @ret, '||', &and_noin
and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'lassoc', @ret],
JECE;
}
sub assign() {
my($pos,@ret) = pos;
@ret == push @ret, &or_expr and return;
while(m@\G $s ((?>(?: [-*/%+&^|] | << | >>>? )?)=) $s @cgx) {
push @ret, $1;
@ret == push @ret, &or_expr and expected 'expression';
}
if(/\G$s\?$s/cg) {
@ret == push @ret, &assign and expected 'expression';
&skip;
/\G:$s/cg or expected "colon";
@ret == push @ret, &assign and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'assign', @ret],
JECE;
}
sub assign_noin() {
my($pos,@ret) = pos;
@ret == push @ret, &or_noin and return;
while(m~\G $s ((?>(?: [-*/%+&^|] | << | >>>? )?)=) $s ~cgx) {
push @ret, $1;
@ret == push @ret, &or_noin and expected 'expression';
}
if(/\G$s\?$s/cg) {
@ret == push @ret, &assign and expected 'expression';
&skip;
/\G:$s/cg or expected "colon";
@ret == push @ret, &assign_noin and expected 'expression';
}
@ret == 1 ? @ret : bless [[$pos, pos], 'assign', @ret],
JECE;
}
sub expr() { # public
my $ret = bless [[pos], 'expr'], JECE;
@$ret == push @$ret, &assign and return;
while(/\G$s,$s/cg) {
@$ret == push @$ret,& assign and expected 'expression';
}
push @{$$ret[0]},pos;
$ret;
}
sub expr_noin() { # public
my $ret = bless [[pos], 'expr'], JECE;
@$ret == push @$ret, &assign_noin and return;
while(/\G$s,$s/cg) {
@$ret == push @$ret, &assign_noin
and expected 'expression';
}
push @{$$ret[0]},pos;
$ret;
}
sub vardecl() { # vardecl is only called when we *know* we need it, so it
# will die when it can't get the first identifier, instead
# of returning undef
my @ret;
@ret == push @ret, &ident and expected 'identifier';
/\G$s=$s/cg and
(@ret != push @ret, &assign or expected 'expression');
push @$_vars, $ret[0];
\@ret;
}
sub vardecl_noin() {
my @ret;
@ret == push @ret, &ident and expected 'identifier';
/\G$s=$s/cg and
(@ret != push @ret, &assign_noin or expected 'expression');
push @$_vars, $ret[0];
\@ret;
}
sub finish_for_sc_sc() { # returns the last two expressions of a for (;;)
# loop header
my @ret;
my $msg;
if(@ret != push @ret, expr) {
$msg = '';
&skip
} else {
push @ret, 'empty';
$msg = 'expression or '
}
/\G;$s/cg or expected "${msg}semicolon";
if(@ret != push @ret, expr) {
$msg = '';
&skip
} else {
push @ret, 'empty';
$msg = 'expression or '
}
/\G\)$s/cg or expected "${msg}')'";
@ret;
}
# ----------- Statement types ------------ #
# (used by custom parsers)
our $optional_sc = # public
qr-\G (?:
$s (?: \z | ; $s | (?=\}) )
|
# optional horizontal whitespace
# then a line terminator or a comment containing one
# then optional trailing whitespace
$h
(?: $n | //[^\cm\cj\x{2028}\x{2029}]* $n |
/\* [^*\cm\cj\x{2028}\x{2029}]*
(?: \*(?!/) [^*\cm\cj\x{2028}\x{2029}] )*
$n
(?s:.)*?
\*/
)
$s
)-x;
sub optional_sc() {
/$optional_sc/gc or expected "semicolon, '}' or end of line";
}
sub block() {
/\G\{/gc or return;
my $ret = [[pos()-1], 'statements'];
&skip;
while() { # 'last' does not work when 'while' is a
# statement modifier
@$ret == push @$ret, &statement and last;
}
expected "'}'" unless /\G\}$s/gc;
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub empty() {
my $pos = pos;
/\G;$s/cg or return;
bless [[$pos,pos], 'empty'], JECS;
}
sub function() {
my $pos = pos;
/\Gfunction$S/cg or return;
my $ret = [[$pos], 'function'];
@$ret == push @$ret, &ident
and expected "identifier";
&skip;
push @$ret, ¶ms;
&skip;
/\G \{ /gcx or expected "'{'";
{
local $_vars = [];
push @$ret, &statements, $_vars;
}
/\G \}$s /gcx or expected "'}'";
push @{$$ret[0]},pos;
push @$_vars, $ret;
bless $ret, JECS;
}
sub if() {
my $pos = pos;
/\Gif$s\($s/cg or return;
my $ret = [[$pos], 'if'];
@$ret == push @$ret, &expr
and expected 'expression';
&skip;
/\G\)$s/gc or expected "')'";
@$ret != push @$ret, &statement
or expected 'statement';
if (/\Gelse(?!$id_cont)$s/cg) {
@$ret == push @$ret, &statement
and expected 'statement';
}
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub while() {
my $pos = pos;
/\Gwhile$s\($s/cg or return;
my $ret = [[$pos], 'while'];
@$ret == push @$ret, &expr
and expected 'expression';
&skip;
/\G\)$s/gc or expected "')'";
@$ret != push @$ret, &statement
or expected 'statement';
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub for() {
my $pos = pos;
/\Gfor$s\($s/cg or return;
my $ret = [[$pos], 'for'];
if (/\G var$S/cgx) {
push @$ret, my $var = bless
[[pos() - length $1], 'var'],
'JE::Code::Statement';
push @$var, &vardecl_noin;
&skip;
if (/\G([;,])$s/gc) {
# if there's a comma or sc then
# this is a for(;;) loop
if ($1 eq ',') {
# finish getting the var
# decl list
do{
@$var ==
push @$var, &vardecl
and expected
'identifier'
} while (/\G$s,$s/gc);
&skip;
/\G;$s/cg
or expected 'semicolon';
}
push @$ret, &finish_for_sc_sc;
}
else {
/\Gin$s/cg or expected
"'in', comma or semicolon";
push @$ret, 'in';
@$ret == push @$ret, &expr
and expected 'expresssion';
&skip;
/\G\)$s/cg or expected "')'";
}
}
elsif(@$ret != push @$ret, &expr_noin) {
&skip;
if (/\G;$s/gc) {
# if there's a semicolon then
# this is a for(;;) loop
push @$ret, &finish_for_sc_sc;
}
else {
/\Gin$s/cg or expected
"'in' or semicolon";
push @$ret, 'in';
@$ret == push @$ret, &expr
and expected 'expresssion';
&skip;
/\G\)$s/cg or expected "')'";
}
}
else {
push @$ret, 'empty';
/\G;$s/cg
or expected 'expression or semicolon';
push @$ret, &finish_for_sc_sc;
}
# body of the for loop
@$ret != push @$ret, &statement
or expected 'statement';
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub with() { # almost identical to while
my $pos = pos;
/\Gwith$s\($s/cg or return;
my $ret = [[$pos], 'with'];
@$ret == push @$ret, &expr
and expected 'expression';
&skip;
/\G\)$s/gc or expected "')'";
@$ret != push @$ret, &statement
or expected 'statement';
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub switch() {
my $pos = pos;
/\Gswitch$s\($s/cg or return;
my $ret = [[$pos], 'switch'];
@$ret == push @$ret, &expr
and expected 'expression';
&skip;
/\G\)$s/gc or expected "')'";
/\G\{$s/gc or expected "'{'";
while (/\G case(?!$id_cont) $s/cgx) {
@$ret == push @$ret, &expr
and expected 'expression';
&skip;
/\G:$s/cg or expected 'colon';
push @$ret, &statements;
}
my $default=0;
if (/\G default(?!$id_cont) $s/cgx) {
/\G : $s /cgx or expected 'colon';
push @$ret, default => &statements;
++$default;
}
while (/\G case(?!$id_cont) $s/cgx) {
@$ret == push @$ret, &expr
and expected 'expression';
&skip;
/\G:$s/cg or expected 'colon';
push @$ret, &statements;
}
/\G \} $s /cgx or expected (
$default
? "'}' or 'case'"
: "'}', 'case' or 'default'"
);
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub try() {
my $pos = pos;
/\Gtry$s\{$s/cg or return;
my $ret = [[$pos], 'try', &statements];
/\G \} $s /cgx or expected "'}'";
$pos = pos;
if(/\Gcatch$s/cg) {
/\G \( $s /cgx or expected "'('";
@$ret == push @$ret, &ident
and expected 'identifier';
&skip;
/\G \) $s /cgx or expected "')'";
/\G \{ $s /cgx or expected "'{'";
push @$ret, &statements;
/\G \} $s /cgx or expected "'}'";
}
if(/\Gfinally$s/cg) {
/\G \{ $s /cgx or expected "'{'";
push @$ret, &statements;
/\G \} $s /cgx or expected "'}'";
}
pos eq $pos and expected "'catch' or 'finally'";
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub labelled() {
my $pos = pos;
/\G ($ident) $s : $s/cgx or return;
my $ret = [[$pos], 'labelled', unescape_ident $1];
while (/\G($ident)$s:$s/cg) {
push @$ret, unescape_ident $1;
}
@$ret != push @$ret, &statement
or expected 'statement';
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub var() {
my $pos = pos;
/\G var $S/cgx or return;
my $ret = [[$pos], 'var'];
do{
push @$ret, &vardecl;
} while(/\G$s,$s/gc);
optional_sc;
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub do() {
my $pos = pos;
/\G do(?!$id_cont)$s/cgx or return;
my $ret = [[$pos], 'do'];
@$ret != push @$ret, &statement
or expected 'statement';
/\Gwhile$s/cg or expected "'while'";
/\G\($s/cg or expected "'('";
@$ret != push @$ret, &expr
or expected 'expression';
&skip;
/\G\)/cog or expected "')'";
optional_sc;
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub continue() {
my $pos = pos;
/\G continue(?!$id_cont)/cogx or return;
my $ret = [[$pos], 'continue'];
/\G$h($ident)/cog
and push @$ret, unescape_ident $1;
optional_sc;
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub break() { # almost identical to continue
my $pos = pos;
/\G break(?!$id_cont)/cogx or return;
my $ret = [[$pos], 'break'];
/\G$h($ident)/cog
and push @$ret, unescape_ident $1;
optional_sc;
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub return() {
my $pos = pos;
/\G return(?!$id_cont)/cogx or return;
my $ret = [[$pos], 'return'];
$pos = pos;
/\G$h/g; # skip horz ws
@$ret == push @$ret, &expr and pos = $pos;
# reverse to before the white space if
# there is no expr
optional_sc;
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub throw() {
my $pos = pos;
/\G throw(?!$id_cont)/cogx
or return;
my $ret = [[$pos], 'throw'];
/\G$h/g; # skip horz ws
@$ret == push @$ret, &expr and expected 'expression';
optional_sc;
push @{$$ret[0]},pos;
bless $ret, JECS;
}
sub expr_statement() {
my $ret = &expr or return;
optional_sc; # the only difference in behaviour between
# this and &expr
$ret;
}
# -------- end of statement types----------#
# This takes care of trailing white space.
sub statement_default() {
my $ret = [[pos]];
# Statements that do not have an optional semicolon
if (/\G (?:
( \{ | ; )
|
(function)$S
|
( if | w(?:hile|ith) | for | switch ) $s \( $s
|
( try $s \{ $s )
|
($ident) $s : $s
) /xgc) {
no warnings 'uninitialized';
if($1 eq '{') {
push @$ret, 'statements';
&skip;
while() { # 'last' does not work when 'while' is a
# statement modifier
@$ret == push @$ret,
&statement_default and last;
}
expected "'}'" unless /\G\}$s/gc;
}
elsif($1 eq ';') {
push @$ret, 'empty';
&skip;
}
elsif($2) {
push @$ret, 'function';
@$ret == push @$ret, &ident
and expected "identifier";
&skip;
push @$ret, ¶ms;
&skip;
/\G \{ /gcx or expected "'{'";
{
local $_vars = [];
push @$ret, &statements, $_vars;
}
/\G \}$s /gcx or expected "'}'";
push @$_vars, $ret;
}
elsif($3 eq 'if') {
push @$ret, 'if';
@$ret == push @$ret, &expr
and expected 'expression';
&skip;
/\G\)$s/gc or expected "')'";
@$ret != push @$ret, &statement_default
or expected 'statement';
if (/\Gelse(?!$id_cont)$s/cg) {
@$ret == push @$ret,
&statement_default
and expected 'statement';
}
}
elsif($3 eq 'while') {
push @$ret, 'while';
@$ret == push @$ret, &expr
and expected 'expression';
&skip;
/\G\)$s/gc or expected "')'";
@$ret != push @$ret, &statement_default
or expected 'statement';
}
elsif($3 eq 'for') {
push @$ret, 'for';
if (/\G var$S/cgx) {
push @$ret, my $var = bless
[[pos() - length $1], 'var'],
'JE::Code::Statement';
push @$var, &vardecl_noin;
&skip;
if (/\G([;,])$s/gc) {
# if there's a comma or sc then
# this is a for(;;) loop
if ($1 eq ',') {
# finish getting the var
# decl list
do{
@$var ==
push @$var, &vardecl
and expected
'identifier'
} while (/\G$s,$s/gc);
&skip;
/\G;$s/cg
or expected 'semicolon';
}
push @$ret, &finish_for_sc_sc;
}
else {
/\Gin$s/cg or expected
"'in', comma or semicolon";
push @$ret, 'in';
@$ret == push @$ret, &expr
and expected 'expresssion';
&skip;
/\G\)$s/cg or expected "')'";
}
}
elsif(@$ret != push @$ret, &expr_noin) {
&skip;
if (/\G;$s/gc) {
# if there's a semicolon then
# this is a for(;;) loop
push @$ret, &finish_for_sc_sc;
}
else {
/\Gin$s/cg or expected
"'in' or semicolon";
push @$ret, 'in';
@$ret == push @$ret, &expr
and expected 'expresssion';
&skip;
/\G\)$s/cg or expected "')'";
}
}
else {
push @$ret, 'empty';
/\G;$s/cg
or expected 'expression or semicolon';
push @$ret, &finish_for_sc_sc;
}
# body of the for loop
@$ret != push @$ret, &statement_default
or expected 'statement';
}
elsif($3 eq 'with') {
push @$ret, 'with';
@$ret == push @$ret, &expr
and expected 'expression';
&skip;
/\G\)$s/gc or expected "')'";
@$ret != push @$ret, &statement_default
or expected 'statement';
}
elsif($3 eq 'switch') {
push @$ret, 'switch';
@$ret == push @$ret, &expr
and expected 'expression';
&skip;
/\G\)$s/gc or expected "')'";
/\G\{$s/gc or expected "'{'";
while (/\G case(?!$id_cont) $s/cgx) {
@$ret == push @$ret, &expr
and expected 'expression';
&skip;
/\G:$s/cg or expected 'colon';
push @$ret, &statements;
}
my $default=0;
if (/\G default(?!$id_cont) $s/cgx) {
/\G : $s /cgx or expected 'colon';
push @$ret, default => &statements;
++$default;
}
while (/\G case(?!$id_cont) $s/cgx) {
@$ret == push @$ret, &expr
and expected 'expression';
&skip;
/\G:$s/cg or expected 'colon';
push @$ret, &statements;
}
/\G \} $s /cgx or expected (
$default
? "'}' or 'case'"
: "'}', 'case' or 'default'"
);
}
elsif($4) { # try
push @$ret, 'try', &statements;
/\G \} $s /cgx or expected "'}'";
my $pos = pos;
if(/\Gcatch$s/cg) {
/\G \( $s /cgx or expected "'('";
@$ret == push @$ret, &ident
and expected 'identifier';
&skip;
/\G \) $s /cgx or expected "')'";
/\G \{ $s /cgx or expected "'{'";
push @$ret, &statements;
/\G \} $s /cgx or expected "'}'";
}
if(/\Gfinally$s/cg) {
/\G \{ $s /cgx or expected "'{'";
push @$ret, &statements;
/\G \} $s /cgx or expected "'}'";
}
pos eq $pos and expected "'catch' or 'finally'";
}
else { # labelled statement
push @$ret, 'labelled', unescape_ident $5;
while (/\G($ident)$s:$s/cg) {
push @$ret, unescape_ident $1;
}
@$ret != push @$ret, &statement_default
or expected 'statement';
}
}
# Statements that do have an optional semicolon
else {
if (/\G var$S/xcg) {
push @$ret, 'var';
do{
push @$ret, &vardecl;
} while(/\G$s,$s/gc);
}
elsif(/\Gdo(?!$id_cont)$s/cg) {
push @$ret, 'do';
@$ret != push @$ret, &statement_default
or expected 'statement';
/\Gwhile$s/cg or expected "'while'";
/\G\($s/cg or expected "'('";
@$ret != push @$ret, &expr
or expected 'expression';
&skip;
/\G\)/cog or expected "')'";
}
elsif(/\G(continue|break)(?!$id_cont)/cog) {
push @$ret, $1;
/\G$h($ident)/cog
and push @$ret, unescape_ident $1;
}
elsif(/\Greturn(?!$id_cont)/cog) {
push @$ret, 'return';
my $pos = pos;
/\G$h/g; # skip horz ws
@$ret == push @$ret, &expr and pos = $pos;
# reverse to before the white space if
# there is no expr
}
elsif(/\Gthrow(?!$id_cont)/cog) {
push @$ret, 'throw';
/\G$h/g; # skip horz ws
@$ret == push @$ret, &expr
and expected 'expression';
}
else { # expression statement
$ret = &expr or return;
}
# Check for optional semicolon
m-$optional_sc-cgx
or expected "semicolon, '}' or end of line";
}
push @{$$ret[0]},pos unless @{$$ret[0]} == 2; # an expr will
# already have this
ref $ret eq 'ARRAY' and bless $ret, 'JE::Code::Statement';
return $ret;
}
sub statement() { # public
my $ret;
for my $sub(@_stms) {
defined($ret = &$sub)
and last;
}
defined $ret ? $ret : ()
}
# This takes care of leading white space.
sub statements() {
my $ret = bless [[pos], 'statements'], 'JE::Code::Statement';
/\G$s/g; # skip initial whitespace
while () { # 'last' does not work when 'while' is a
# statement modifier
@$ret != push @$ret,
$_parser ? &statement : &statement_default
or last;
}
push @{$$ret[0]},pos;
return $ret;
}
sub program() { # like statements(), but it allows function declarations
# as well
my $ret = bless [[pos], 'statements'], 'JE::Code::Statement';
/\G$s/g; # skip initial whitespace
if($_parser) {
while () {
DECL: {
for my $sub(@_decls) {
@$ret != push @$ret, &$sub
and redo DECL;
}
}
@$ret != push @$ret, &statement or last;
}
}
else {
while () {
while() {
@$ret == push @$ret, &function and last;
}
@$ret != push @$ret, &statement_default or last;
}
}
push @{$$ret[0]},pos;
return $ret;
}
# ~~~ The second arg to add_line_number is a bit ridiculous. I may change
# add_line_number's parameter list, perhaps so it accepts either a
# code object, or (src,file,line) if $_[1] isn'ta JE::Code. I don't
# know....
sub _parse($$$;$$) { # Returns just the parse tree, not a JE::Code object.
# Actually, it returns the source followed by the
# parse tree in list context, or just the parse tree
# in scalar context.
my ($rule, $src, $my_global, $file, $line) = @_;
local our($_source, $_file, $_line) =($src,$file,$line);
# Note: We *hafta* stringify the $src, because it could be an
# object with overloading (e.g., JE::String) and we
# need to rely on its pos(), which simply cannot be
# done with an object. Furthermore, perl5.8.5 is
# a bit buggy and sometimes mangles the contents
# of $1 when one does $obj =~ /(...)/.
$src = defined blessed $src && $src->isa("JE::String")
? $src->value16
: surrogify("$src");
# remove unicode format chrs
$src =~ s/\p{Cf}//g;
# In HTML mode, modify the whitespace regexps to remove HTML com-
# ment delimiters and following junk up to the end of the line.
$my_global->html_mode and
local $s = qr((?>
(?> [ \t\x0b\f\xa0\p{Zs}]* )
(?> (?>
$n
(?>(?:
(?>[ \t\x0b\f\xa0\p{Zs}]*) -->
(?>[^\cm\cj\x{2028}\x{2029}]*)(?>$n|\z)
)?)
|
^
(?>[ \t\x0b\f\xa0\p{Zs}]*) -->
(?>[^\cm\cj\x{2028}\x{2029}]*)(?>$n|\z)
|
(?>//|<!--)(?>[^\cm\cj\x{2028}\x{2029}]*)(?>$n|\z)
|
/\*.*?\*/
)
(?> [ \t\x0b\f\xa0\p{Zs}]* )
) *
))sx,
local $S = qr(
(?>
$ss
|
(?>//|<!--)[^\cm\cj\x{2028}\x{2029}]*
|
/\*.*?\*/
)
$s
)xs,
local $optional_sc = qr _\G (?:
$s (?: \z | ; $s | (?=\}) )
|
# optional horizontal whitespace
# then a line terminator or a comment containing one
# then optional trailing whitespace
$h
(?:
$n
|
(?>//|<!--)[^\cm\cj\x{2028}\x{2029}]* $n
|
/\* [^*\cm\cj\x{2028}\x{2029}]*
(?: \*(?!/) [^*\cm\cj\x{2028}\x{2029}] )*
$n
(?s:.)*?
\*/
)
$s
)_x;
my $tree;
local $_vars = [];
$rule eq 'program' and !$_parser
and ($ENV{'YES_I_WANT_JE_TO_OPTIMISE'}||'') eq 2
and do { require 'JE/parsetoperl.pl', $rule = \&ptp_program };
for($src) {
pos = 0;
eval {
local $global = $my_global;
$tree = (\&$rule)->();
!defined pos or pos == length
or expected 'statement or function declaration';
};
if(ref $@ ne '') {
defined blessed $@ and
$@->isa('JE::Object::Error')
? last : die;
ref($@) =~ /^(?:SCALAR|REF)\z/ or die;
$@
= ref ${$@} eq 'SCALAR'
? JE::Object::Error::SyntaxError->new(
$my_global,
add_line_number(
$${$@},
{file=>$file,line=>$line,source=>\$src},
pos)
)
: JE::Object::Error::SyntaxError->new(
$my_global,
# ~~~ This should perhaps show more context
add_line_number
"Expected ${$@} but found '".
substr($_, pos, 10) . "'",
{file=>$file,line=>$line,source=>\$src},
pos
);
return;
}
elsif($@) { die }
}
#use Data::Dumper;
#print Dumper $tree;
wantarray ? ($src, $tree, $_vars) : $tree;
}
#----------DOCS---------#
!!!0;
=head1 NAME
JE::Parser - Framework for customising JE's parser
=cut
# Actually, this *is* JE's parser. But since JE::Parser's methods are never
# used directly with the default parser, I think it's actually less confus-
# ing to call it this.
=head1 SYNOPSIS
use JE;
use JE::Parser;
$je = new JE;
$p = new JE::Parser $je; # or: $p = $je->new_parser
$p->delete_statement('for', 'while', 'do'); # disable loops
$p->add_statement(try => \&parser); # replace existing 'try' statement
=head1 DESCRIPTION
This allows one to change the list of statement types that the parser
looks for. For instance, one could disable loops for a mini-JavaScript, or
add extensions to the language, such as the 'catch-if' clause of a C<try>
statement.
As yet, C<delete_statement> works, but I've not finished
designing the API for C<add_statement>.
I might provide an API for extending expressions, if I can resolve the
complications caused by the 'new' operator. If anyone else wants to have a
go at it, be my guest. :-)
=head1 METHODS
=over 4
=item $p = new JE::Parser
Creates a new parser object.
=item $p->add_statement($name, \&parser);
This adds a new statement (source element, to be precise) type
to the
list of statements types the parser supports. If a statement type called
C<$name> already exists, it will be replaced.
Otherwise, the new statement type will be added to the top of the list.
(C<$name> ought to be optional; it should only be necessary if one wants to
delete
it
afterwards or rearrange the list.)
If the name of a statement type begins with a hyphen, it is only allowed at
the 'program' level, not within compound statements. Function declarations
use this. Maybe this
convention is too unintuitive.... (Does anyone think I should change it?
What should I change it too?)
C<&parser> will need to parse code contained in C<$_> starting at C<pos()>, then either
return an object, list or coderef (see below)
and set C<pos()> to the position of the next token[1], or, if it
could not
parse anything, return undef and reset C<pos()> to its initial value if it
changed.
[1] I.e., it is expected to move C<pos> past any trailing whitespace.
The return value of C<&parser> can be one of the following:
=over 4
=item 1)
An object with an C<eval> method, that will execute the statement, and/or
an C<init> method, which will be called
before the code runs.
=item 2)
B<(Not yet
supported!)> A coderef, which will be called when the code is executed.
=item 3)
B<(Not yet
supported.)> A hash-style list, the two keys being C<eval> and C<init>
(corresponding to
the methods under item 1) and the values being coderefs; i.e.:
( init => \&init_sub, eval => \&eval_sub )
=back
Maybe we need support for a JavaScript function to be called to handnle the
statement.
=item $p->delete_statement(@names);
Deletes the given statement types and returns C<$p>.
=item $p->statement_list
B<(Not yet implemented.)>
Returns an array ref of the names of the various statement types. You can
rearrange this
list, but it is up to you to make sure you do not add to it any statement
types that have not been added via C<add_statement> (or were not there by
default). The statement types in the list will be tried in order, except
that items beginning with a hyphen always come before other items.
The default list is C<qw/-function block empty if while with for switch try
labelled var do continue break return throw expr/>
=item $p->parse($code)
Parses the C<$code> and returns a parse tree (JE::Code object).
=item $p->eval($code)
Shorthand for $p->parse($code)->execute;
=back
=head1 EXPORTS
None by default. You may choose to export the following:
=head2 Exported Variables
... blah blah blah ...
=head2 Exported Functions
These all have C<()> for their prototype, except for C<expected> which has
C<($)>.
... blah blah blah ...
=head1 SYNTAX ERRORS
(To be written)
expected 'aaaa'; # will be changed to 'Expected aaaa but found....'
die \\"You can't put a doodad after a frombiggle!"; # complete message
die 'aoenstuhoeanthu'; # big no-no (the error is propagated)
=head1 EXAMPLES
=head2 Mini JavaScript
This is an example of a mini JavaScript that does not allow loops or the
creation of functions.
use JE;
$j = new JE;
$p = $j->new_parser;
$p->delete_statement('for','while','do','-function');
Since function expressions could still create functions, we need to remove
the Function prototype object. Someone might then try to put it back with
C<Function = parseInt.constructor>, so we'll overwrite Function with an
undeletable read-only undefined property.
$j->prop({ name => 'Function',
value => undef,
readonly => 1,
dontdel => 1 });
Then, after this, we call C<< $p->eval('...') >> to run JS code.
=head2 Perl-style for(LIST) loop
Well, after writing this example, it seems to me this API is not
sufficient....
This example doesn't actually work yet.
use JE;
use JE::Parser qw'$s ident expr statement expected';
$j = new JE;
$p = $j->new_parser;
$p->add_statement('for-list',
sub {
/\Gfor$s/cog or return;
my $loopvar = ident or return;
/\G$s\($s/cog or return;
my @expressions;
do {
# This line doesn't actually work properly because
# 'expr' will gobble up all the commas
@expressions == push @expressions, expr
and return; # If nothing gets pushed on to the
# list, we need to give the default
# 'for' handler a chance, instead of
# throwing an error.
} while /\G$s,$s/cog;
my $statement = statement or expected 'statement';
return bless {
var => $loopvar,
expressions => \@expressions,
statement => $statement
}, 'Local::JEx::ForList';
}
);
package Local::JEx::ForList;
sub eval {
my $self = shift;
local $JE::Code::scope =
bless [@$JE::Code::scope], 'JE::Scope';
# I've got to come up with a better interface than this.
my $obj = $JE::Code::global->eval('new Object');
push @$JE::Code::scope, $obj;
for (@{$self->{expressions}}) {
$obj->{ $self->{loopvar} } = $_->eval;
$self->{statement}->execute;
}
}
=head1 SEE ALSO
L<JE> and L<JE::Code>.
=cut
|