Skip to content

Instantly share code, notes, and snippets.

@awwaiid
Created December 6, 2015 16:49
Show Gist options
  • Save awwaiid/d3ca4a4d4d895d696f51 to your computer and use it in GitHub Desktop.
Save awwaiid/d3ca4a4d4d895d696f51 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl6
# use Grammar::Tracer;
# use Grammar::Debugger;
grammar Offsides {
token TOP {
<tree>*
}
token tree {
$<indent>=(\s*) <text>
<subtree(~$<indent>)>*
}
token subtree($current-indent) {
$current-indent $<more-indent>=(\s+) <text>
# If I comment this line out, $<more-indent> doesn't
# get passed into the subtree!
{ say "more-indent: [{ $<more-indent> }]" }
# 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);
# say $match;
# use LREP;
# LREP::here;
sub print-trees($trees, $current-indent = 0) {
for $trees.flat -> $tree {
say (" " x $current-indent) ~ "[{ $tree<text> }]";
# say "indent: $current-indent";
# say "text: $tree<text>";
print-trees($tree<subtree>, $current-indent + 1);
}
}
print-trees($match<tree>);
Use of Nil in string context in regex tree at ./parse.p6:11
more-indent: [
]
more-indent: [ ]
Use of Nil in string context in regex tree at ./parse.p6:11
more-indent: [
]
Use of Nil in string context in regex tree at ./parse.p6:11
more-indent: [
]
more-indent: [ ]
more-indent: [ ]
Use of Nil in string context in regex tree at ./parse.p6:11
more-indent: [
]
Use of Nil in string context in regex tree at ./parse.p6:11
[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