Skip to content

Instantly share code, notes, and snippets.

@awwaiid
Created December 7, 2015 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awwaiid/7ae087830e31cfb02809 to your computer and use it in GitHub Desktop.
Save awwaiid/7ae087830e31cfb02809 to your computer and use it in GitHub Desktop.
#!/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>);
[hello]
[this is]
[a]
[test to see]
[if this]
[can calculate]
[the offside]
[rule]
[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