| 1 | use v5.10; | ||
| 2 | use MooseX::Declare; | ||
| 3 | class Mildew::Frontend::STD::Cached { | ||
| 4 | use CHI; | ||
| 5 | use Digest::MD4 qw(md4); | ||
| 6 | |||
| 7 | has cache => (is=>'ro',default=> sub {CHI->new( driver => 'File', root_dir => 'parser_cache',serializer=>'YAML')}); | ||
| 8 | has parser => (is=>'ro',default=> sub {require STD;require Mildew::Frontend::STD;Mildew::Frontend::STD->new()},lazy=>1); | ||
| 9 | |||
| 10 | method parse($source) { | ||
| 11 | my $checksum = md4($source); | ||
| 12 | if (my $parse = $self->cache->get($checksum)) { | ||
| 13 | use Data::Dumper::Concise; | ||
| 14 | { | ||
| 15 | local $INC{"STD.pm"} = "do-not-load"; | ||
| 16 | do 'viv'; | ||
| 17 | } | ||
| 18 | warn "using cached ast"; | ||
| 19 | |||
| 20 | # generating the missing VAST classes | ||
| 21 | for (@{$parse->{gen_class}}) { | ||
| 22 | STD::Actions::gen_class(@{$_}) | ||
| 23 | } | ||
| 24 | |||
| 25 | # the $::ORIG var contains the orginal string | ||
| 26 | $::ORIG = $parse->{ORIG}; | ||
| 27 | |||
| 28 | return $parse->{VAST}; | ||
| 29 | } | ||
| 30 | |||
| 31 | # we need to duplicate the calls to gen_class before we can use the Mildew::AST | ||
| 32 | require STD::Actions; | ||
| 33 | my $gen_class = \&STD::Actions::gen_class; | ||
| 34 | my @gen_class; | ||
| 35 | { | ||
| 36 | no warnings 'redefine'; | ||
| 37 | *STD::Actions::gen_class = sub { | ||
| 38 | say "generating class $_[0]"; | ||
| 39 | push(@gen_class,[@_]); | ||
| 40 | $gen_class->(@_); | ||
| 41 | }; | ||
| 42 | } | ||
| 43 | |||
| 44 | my $vast = $self->parser->parse($source); | ||
| 45 | $self->cache->set($checksum,{ORIG=>$::ORIG,VAST=>$vast,gen_class=>\@gen_class}); | ||
| 46 | $vast->emit_m0ld; | ||
| 47 | } | ||
| 48 | } |