Skip to content

Instantly share code, notes, and snippets.

@msporleder
Last active January 14, 2016 21:19
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 msporleder/29644fce49e662aa7ae4 to your computer and use it in GitHub Desktop.
Save msporleder/29644fce49e662aa7ae4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl6
use v6;
grammar Fmt {
token TOP { ^[<para>+||<literal>+||<wordspace>+]+ <theend> }
#token wordspace { [<:Cc>*||\w+][\h+||\n] }
token wordspace { \S+[\h+||\n] }
token para { ^^\h*\n }
token literal { ^^\h+\N+\n }
token theend { $ }
}
class FmtAction {
has Str $.buf = "";
has Int $!max = 80;
method wordspace($/) {
my $x = $/.make(~$/);
$x.=subst(/\n/, ' ');
if $x.chars + $!buf.chars < $!max {
$!buf ~= $x;
} else {
say "$!buf";
$!buf = $x;
}
}
method literal($/) {
say "$!buf" if $!buf.chars > 0;
$!buf = "";
print $/.make(~$/);
}
method para($/) {
say "$!buf" if $!buf.chars > 0;
$!buf = "";
print $/.make(~$/);
}
method theend($/) {
say "$!buf";
$!buf = "";
}
}
my $a = FmtAction.new;
my $doc = $*IN.slurp-rest;
my $n = Fmt.parse($doc, :actions($a));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment