Created
December 7, 2015 00:29
-
-
Save awwaiid/7ae087830e31cfb02809 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env perl6 | |
# use Grammar::Tracer; | |
grammar Offsides { | |
token TOP { | |
<tree>* | |
} | |
token tree { | |
$<indent>=\s* <text> \n? | |
<subtree($<indent> || "")>* | |
} | |
token subtree($current-indent) { | |
$current-indent $<more-indent>=\s+ <text> \n? | |
# If I comment this line out, $<more-indent> doesn't | |
# get passed into the subtree! | |
{ } | |
# Might have some sub-trees with more indent | |
<subtree($current-indent ~ ($<more-indent> || ""))>* | |
} | |
token text { | |
\S .*? $$ | |
} | |
} | |
my $test-input = "hello | |
this is | |
a | |
test to see | |
if this | |
can calculate | |
the offside | |
rule"; | |
my $match = Offsides.parse($test-input); | |
# use LREP; | |
# LREP::here; | |
sub print-trees($trees, $current-indent = 0) { | |
for $trees.flat -> $tree { | |
say (" " x $current-indent) ~ "[{ $tree<text> }]"; | |
print-trees($tree<subtree>, $current-indent + 1); | |
} | |
} | |
print-trees($match<tree>); | |
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
[hello] | |
[this is] | |
[a] | |
[test to see] | |
[if this] | |
[can calculate] | |
[the offside] | |
[rule] |
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
[hello] | |
[this is] | |
[a] | |
[test to see] | |
[if this] | |
[can calculate] | |
[the offside] | |
[rule] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment