JE
view release on metacpan or search on metacpan
lib/JE/Parser.pm view on Meta::CPAN
# 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'
lib/JE/Parser.pm view on Meta::CPAN
/\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
( run in 2.297 seconds using v1.01-cache-2.11-cpan-a49fcb8fa48 )