Permalink
Browse files

Perlito5 - parser: add __END__, __DATA__

  • Loading branch information...
1 parent b4f4a35 commit ebce10fb496b92c538fde4f165b05198a6149185 @fglock committed May 3, 2012
Showing with 94 additions and 16 deletions.
  1. +8 −0 README-perlito5
  2. +2 −2 TODO-perlito5
  3. +42 −4 html/perlito5.js
  4. +18 −2 perlito5.pl
  5. +24 −8 src5/lib/Perlito5/Grammar/Space.pm
View
@@ -35,6 +35,14 @@ Running the tests using "perl":
prove -r -e 'perl perlito5.pl -I./src5/lib ' t5
+Bootstrap with perl:
+
+ perl perlito5.pl -Isrc5/lib -Cperl5 src5/util/perlito5.pl > perlito5-new.pl && diff perlito5-new.pl perlito5.pl ; cp perlito5-new.pl perlito5.pl
+
+Bootstrap with node.js:
+
+ time node perlito5.js -Isrc5/lib -Cjs src5/util/perlito5.pl > perlito5-new.js && diff perlito5-new.js perlito5.js ; cp perlito5-new.js perlito5.js
+
TODO:
View
@@ -35,7 +35,7 @@ TODO list for Perlito5
* Parser
--- no __END__
+-- create __DATA__
-- "sub _" should be in package "main"
@@ -203,7 +203,7 @@ TODO list for Perlito5
-- use the same error messages and warnings as 'perl'
-- no warnings 'redefine';
--- __LINE__, __DATA__, __FILE__
+-- __LINE__, __FILE__
-- INIT{}, END{}
look at the implementation in perlito6-in-Go
View
@@ -9669,21 +9669,41 @@ return r;
return (List__[p5idx(List__,1)]);
}]));
p5make_sub("Perlito5::Grammar::Space", "term_space", function (List__, p5want) {
+ try {
var v_str = null;
(v_str = (List__[p5idx(List__,0)]));
+ var v_pos = null;
+ (v_pos = (List__[p5idx(List__,1)]));
var v_p = null;
- (v_p = (List__[p5idx(List__,1)]));
+ (v_p = (v_pos));
p5while(function () {
(v_p = ((Hash_space[p5pkg["Perlito5::Grammar::Space"].substr([v_str, v_p, 1], p5want)])([v_str, (p5num(v_p) + 1)])));
}, function () { return (Hash_space).hasOwnProperty(p5pkg["Perlito5::Grammar::Space"].substr([v_str, v_p, 1], p5want)) }, false, "");
- return ((new p5HashRef(p5a_to_h(p5list_to_a('str', v_str, 'from', p5pkg["Perlito5::Grammar::Space"]["v_pos"], 'to', v_p, 'capture', (new p5ArrayRef(['space', ' '])))))));
+ if ( ((p5pkg["Perlito5::Grammar::Space"].substr([v_str, v_p, 7], 0) == '__END__') || (p5pkg["Perlito5::Grammar::Space"].substr([v_str, v_p, 8], 0) == '__DATA__')) ) {
+ throw((new p5HashRef({'str' : v_str, 'from' : v_pos, 'to' : p5pkg["Perlito5::Grammar::Space"].length([v_str], p5want)})));
+ };
+ return ((new p5HashRef(p5a_to_h(p5list_to_a('str', v_str, 'from', v_pos, 'to', v_p, 'capture', (new p5ArrayRef(['space', ' '])))))));
+ }
+ catch(err) {
+ if ( err instanceof Error ) {
+ throw(err);
+ }
+ else {
+ return(err);
+ }
+ }
+ });
+ p5make_sub("Perlito5::Grammar::Space", "term_end", function (List__, p5want) {
+ return ((new p5HashRef(p5a_to_h(p5list_to_a('str', List__[p5idx(List__,0)], 'from', List__[p5idx(List__,1)], 'to', p5pkg["Perlito5::Grammar::Space"].length([List__[p5idx(List__,0)]], 1), 'capture', (new p5ArrayRef(['space', ' '])))))));
});
p5pkg["Perlito5::Precedence"].add_term(['#', p5pkg["Perlito5::Grammar::Space"].term_space], null);
p5pkg["Perlito5::Precedence"].add_term([p5pkg["Perlito5::Grammar::Space"].chr([9], 1), p5pkg["Perlito5::Grammar::Space"].term_space], null);
p5pkg["Perlito5::Precedence"].add_term([p5pkg["Perlito5::Grammar::Space"].chr([10], 1), p5pkg["Perlito5::Grammar::Space"].term_space], null);
p5pkg["Perlito5::Precedence"].add_term([p5pkg["Perlito5::Grammar::Space"].chr([12], 1), p5pkg["Perlito5::Grammar::Space"].term_space], null);
p5pkg["Perlito5::Precedence"].add_term([p5pkg["Perlito5::Grammar::Space"].chr([13], 1), p5pkg["Perlito5::Grammar::Space"].term_space], null);
p5pkg["Perlito5::Precedence"].add_term([p5pkg["Perlito5::Grammar::Space"].chr([32], 1), p5pkg["Perlito5::Grammar::Space"].term_space], null);
+ p5pkg["Perlito5::Precedence"].add_term(['__END__', p5pkg["Perlito5::Grammar::Space"].term_end], null);
+ p5pkg["Perlito5::Precedence"].add_term(['__DATA__', p5pkg["Perlito5::Grammar::Space"].term_end], null);
p5make_sub("Perlito5::Grammar::Space", "to_eol", function (List__, p5want) {
var v_grammar = null;
(v_grammar = (List__[p5idx(List__,0)]));
@@ -9986,6 +10006,9 @@ return r;
p5while(function () {
(v_p = ((Hash_space[p5pkg["Perlito5::Grammar::Space"].substr([v_str, v_p, 1], p5want)])([v_str, (p5num(v_p) + 1)])));
}, function () { return (Hash_space).hasOwnProperty(p5pkg["Perlito5::Grammar::Space"].substr([v_str, v_p, 1], p5want)) }, false, "");
+ if ( ((p5pkg["Perlito5::Grammar::Space"].substr([v_str, v_p, 7], 0) == '__END__') || (p5pkg["Perlito5::Grammar::Space"].substr([v_str, v_p, 8], 0) == '__DATA__')) ) {
+ throw((new p5HashRef({'str' : v_str, 'from' : v_pos, 'to' : p5pkg["Perlito5::Grammar::Space"].length([v_str], p5want)})));
+ };
if ( (p5num(v_p) == p5num(v_pos)) ) {
throw(p5context([0], p5want));
};
@@ -10001,16 +10024,31 @@ return r;
}
});
p5make_sub("Perlito5::Grammar::Space", "opt_ws", function (List__, p5want) {
+ try {
var v_self = null;
(v_self = (p5pkg["Perlito5::Grammar::Space"].shift([List__])));
var v_str = null;
(v_str = (p5pkg["Perlito5::Grammar::Space"].shift([List__])));
+ var v_pos = null;
+ (v_pos = (p5pkg["Perlito5::Grammar::Space"].shift([List__])));
var v_p = null;
- (v_p = (p5pkg["Perlito5::Grammar::Space"].shift([List__])));
+ (v_p = (v_pos));
p5while(function () {
(v_p = ((Hash_space[p5pkg["Perlito5::Grammar::Space"].substr([v_str, v_p, 1], p5want)])([v_str, (p5num(v_p) + 1)])));
}, function () { return (Hash_space).hasOwnProperty(p5pkg["Perlito5::Grammar::Space"].substr([v_str, v_p, 1], p5want)) }, false, "");
- return ((new p5HashRef({'str' : v_str, 'from' : p5pkg["Perlito5::Grammar::Space"]["v_pos"], 'to' : v_p})));
+ if ( ((p5pkg["Perlito5::Grammar::Space"].substr([v_str, v_p, 7], 0) == '__END__') || (p5pkg["Perlito5::Grammar::Space"].substr([v_str, v_p, 8], 0) == '__DATA__')) ) {
+ throw((new p5HashRef({'str' : v_str, 'from' : v_pos, 'to' : p5pkg["Perlito5::Grammar::Space"].length([v_str], p5want)})));
+ };
+ return ((new p5HashRef({'str' : v_str, 'from' : v_pos, 'to' : v_p})));
+ }
+ catch(err) {
+ if ( err instanceof Error ) {
+ throw(err);
+ }
+ else {
+ return(err);
+ }
+ }
});
1;
})()
View
@@ -5897,20 +5897,29 @@ package Perlito5::Grammar::Space;
}));
sub Perlito5::Grammar::Space::term_space {
((my $str) = $_[0]);
- ((my $p) = $_[1]);
+ ((my $pos) = $_[1]);
+ ((my $p) = $pos);
for ( ; exists($space{substr($str, $p, 1)}); do {{
}} ) {
($p = $space{substr($str, $p, 1)}->($str, ($p + 1)))
};
+ if (((substr($str, $p, 7) eq '__END__') || (substr($str, $p, 8) eq '__DATA__'))) {
+ return ({'str', $str, 'from', $pos, 'to', length($str)})
+ };
return ({'str', $str, 'from', $pos, 'to', $p, 'capture', ['space', ' ']})
};
+sub Perlito5::Grammar::Space::term_end {
+ return ({'str', $_[0], 'from', $_[1], 'to', length($_[0]), 'capture', ['space', ' ']})
+};
Perlito5::Precedence::add_term('#', \&term_space);
Perlito5::Precedence::add_term(chr(9), \&term_space);
Perlito5::Precedence::add_term(chr(10), \&term_space);
Perlito5::Precedence::add_term(chr(12), \&term_space);
Perlito5::Precedence::add_term(chr(13), \&term_space);
Perlito5::Precedence::add_term(chr(32), \&term_space);
+Perlito5::Precedence::add_term('__END__', \&term_end);
+Perlito5::Precedence::add_term('__DATA__', \&term_end);
sub Perlito5::Grammar::Space::to_eol {
((my $grammar) = $_[0]);
((my $str) = $_[1]);
@@ -6154,6 +6163,9 @@ sub Perlito5::Grammar::Space::ws {
}} ) {
($p = $space{substr($str, $p, 1)}->($str, ($p + 1)))
};
+ if (((substr($str, $p, 7) eq '__END__') || (substr($str, $p, 8) eq '__DATA__'))) {
+ return ({'str', $str, 'from', $pos, 'to', length($str)})
+ };
if (($p == $pos)) {
return (0)
};
@@ -6162,12 +6174,16 @@ sub Perlito5::Grammar::Space::ws {
sub Perlito5::Grammar::Space::opt_ws {
((my $self) = shift());
((my $str) = shift());
- ((my $p) = shift());
+ ((my $pos) = shift());
+ ((my $p) = $pos);
for ( ; exists($space{substr($str, $p, 1)}); do {{
}} ) {
($p = $space{substr($str, $p, 1)}->($str, ($p + 1)))
};
+ if (((substr($str, $p, 7) eq '__END__') || (substr($str, $p, 8) eq '__DATA__'))) {
+ return ({'str', $str, 'from', $pos, 'to', length($str)})
+ };
return ({'str', $str, 'from', $pos, 'to', $p})
};
1;
@@ -30,20 +30,29 @@ my %space = (
sub term_space {
my $str = $_[0];
- my $p = $_[1];
+ my $pos = $_[1];
+ my $p = $pos;
while (exists $space{substr($str, $p, 1)}) {
$p = $space{substr($str, $p, 1)}->($str, $p+1)
}
+ return { str => $str, from => $pos, to => length($str) }
+ if substr($str, $p, 7) eq '__END__'
+ || substr($str, $p, 8) eq '__DATA__';
return { str => $str, from => $pos, to => $p, capture => [ 'space', ' ' ] }
}
+sub term_end {
+ return { str => $_[0], from => $_[1], to => length($_[0]), capture => [ 'space', ' ' ] }
+}
-Perlito5::Precedence::add_term( '#' => \&term_space );
-Perlito5::Precedence::add_term( chr(9) => \&term_space );
-Perlito5::Precedence::add_term( chr(10) => \&term_space );
-Perlito5::Precedence::add_term( chr(12) => \&term_space );
-Perlito5::Precedence::add_term( chr(13) => \&term_space );
-Perlito5::Precedence::add_term( chr(32) => \&term_space );
+Perlito5::Precedence::add_term( '#' => \&term_space );
+Perlito5::Precedence::add_term( chr(9) => \&term_space );
+Perlito5::Precedence::add_term( chr(10) => \&term_space );
+Perlito5::Precedence::add_term( chr(12) => \&term_space );
+Perlito5::Precedence::add_term( chr(13) => \&term_space );
+Perlito5::Precedence::add_term( chr(32) => \&term_space );
+Perlito5::Precedence::add_term( '__END__' => \&term_end );
+Perlito5::Precedence::add_term( '__DATA__' => \&term_end );
token to_eol {
@@ -80,6 +89,9 @@ sub ws {
while (exists $space{substr($str, $p, 1)}) {
$p = $space{substr($str, $p, 1)}->($str, $p+1)
}
+ return { str => $str, from => $pos, to => length($str) }
+ if substr($str, $p, 7) eq '__END__'
+ || substr($str, $p, 8) eq '__DATA__';
if ($p == $pos) {
return 0;
}
@@ -89,10 +101,14 @@ sub ws {
sub opt_ws {
my $self = shift;
my $str = shift;
- my $p = shift;
+ my $pos = shift;
+ my $p = $pos;
while (exists $space{substr($str, $p, 1)}) {
$p = $space{substr($str, $p, 1)}->($str, $p+1)
}
+ return { str => $str, from => $pos, to => length($str) }
+ if substr($str, $p, 7) eq '__END__'
+ || substr($str, $p, 8) eq '__DATA__';
return { str => $str, from => $pos, to => $p }
}

0 comments on commit ebce10f

Please sign in to comment.