-
-
Save msporleder/29644fce49e662aa7ae4 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 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