forked from pmichaud/pmtcl
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:partcl/partcl-nqp
- Loading branch information
Showing
6 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
class ARE::Actions is HLL::Actions; | ||
|
||
method TOP($/) { | ||
my $past := buildsub( $<nibbler>.ast ); | ||
$past.node($/); | ||
make $past; | ||
} | ||
|
||
method nibbler($/) { | ||
my $past; | ||
if +$<termish> > 1 { | ||
$past := PAST::Regex.new( :pasttype('alt'), :node($/) ); | ||
for $<termish> { $past.push($_.ast); } | ||
} | ||
else { | ||
$past := $<termish>[0].ast; | ||
} | ||
make $past; | ||
} | ||
|
||
method termish($/) { | ||
my $past := PAST::Regex.new( :pasttype('concat'), :node($/) ); | ||
my $lastlit := 0; | ||
for $<noun> { | ||
my $ast := $_.ast; | ||
if $ast { | ||
if $lastlit && $ast.pasttype eq 'literal' | ||
&& !PAST::Node.ACCEPTS($ast[0]) { | ||
$lastlit[0] := $lastlit[0] ~ $ast[0]; | ||
} | ||
else { | ||
$past.push($ast); | ||
$lastlit := $ast.pasttype eq 'literal' | ||
&& !PAST::Node.ACCEPTS($ast[0]) | ||
?? $ast !! 0; | ||
} | ||
} | ||
} | ||
make $past; | ||
} | ||
|
||
method quantified_atom($/) { | ||
my $past := $<atom>.ast; | ||
if $<quantifier> { | ||
my $qast := $<quantifier>[0].ast; | ||
$qast.unshift($past); | ||
$past := $qast; | ||
} | ||
make $past; | ||
} | ||
|
||
method atom($/) { | ||
my $past := $<metachar> | ||
?? $<metachar>.ast | ||
!! PAST::Regex.new( ~$/, :pasttype<literal>, :node($/) ); | ||
make $past; | ||
} | ||
|
||
method quantifier:sym<*>($/) { | ||
make PAST::Regex.new( :pasttype<quant>, :node($/) ); | ||
} | ||
method quantifier:sym<+>($/) { | ||
make PAST::Regex.new( :pasttype<quant>, :min(1), :node($/) ); | ||
} | ||
method quantifier:sym<?>($/) { | ||
make PAST::Regex.new( :pasttype<quant>, :min(0), :max(1), :node($/) ); | ||
} | ||
|
||
method metachar:sym<.>($/) { | ||
make PAST::Regex.new( :pasttype<charclass>, :subtype<.>, :node($/) ); | ||
} | ||
|
||
method metachar:sym<back>($/) { make $<backslash>.ast; } | ||
|
||
method backslash:sym<w>($/) { | ||
make PAST::Regex.new( :pasttype<charclass>, :subtype(~$<sym>), :node($/)); | ||
} | ||
|
||
sub buildsub($rpast, $block = PAST::Block.new() ) { | ||
$rpast := PAST::Regex.new( | ||
PAST::Regex.new( :pasttype('scan') ), | ||
$rpast, | ||
PAST::Regex.new( :pasttype('pass') ), | ||
:pasttype('concat'), | ||
); | ||
unless $block.symbol('$¢') { $block.symbol('$¢', :scope<lexical>); } | ||
unless $block.symbol('$/') { $block.symbol('$/', :scope<lexical>); } | ||
$block.push($rpast); | ||
$block.blocktype('method'); | ||
$block; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class ARE::Compiler is HLL::Compiler { | ||
INIT { | ||
ARE::Compiler.parsegrammar(ARE::Grammar); | ||
ARE::Compiler.parseactions(ARE::Actions); | ||
ARE::Compiler.language('ARE'); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
grammar ARE::Grammar is HLL::Grammar; | ||
|
||
token TOP { | ||
<nibbler> | ||
[ $ || <.panic: 'Confused'> ] | ||
} | ||
|
||
token nibbler { | ||
<termish> [ '|' <termish> ]* | ||
} | ||
|
||
token termish { | ||
<noun=.quantified_atom>+ | ||
} | ||
|
||
token quantified_atom { | ||
<atom> <quantifier>? | ||
} | ||
|
||
token atom { | ||
[ | ||
| \w [ \w+! <?before \w> ]? | ||
| <metachar> | ||
] | ||
} | ||
|
||
proto token quantifier { <...> } | ||
token quantifier:sym<*> { <sym> } | ||
token quantifier:sym<+> { <sym> } | ||
token quantifier:sym<?> { <sym> } | ||
|
||
proto token metachar { <...> } | ||
token metachar:sym<.> { <sym> } | ||
token metachar:sym<back> { \\ <backslash> } | ||
|
||
proto token backslash { <...> } | ||
token backslash:sym<w> { $<sym>=[<[dswDSW]>] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters