| 1 | package Pugs::Grammar::BaseCategory; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | #use Pugs::Compiler::Rule; | ||
| 5 | use Pugs::Compiler::Regex; | ||
| 6 | use base qw(Pugs::Grammar::Base); | ||
| 7 | |||
| 8 | sub ws { | ||
| 9 | my $grammar = shift; | ||
| 10 | $_[0] = "" unless defined $_[0]; | ||
| 11 | my $bool = $_[0] =~ / | ||
| 12 | ^ | ||
| 13 | ( | ||
| 14 | (?: | ||
| 15 | # pod start | ||
| 16 | (?: \n | ^ ) \= \w | ||
| 17 | # pod body | ||
| 18 | .*? | ||
| 19 | # pod end | ||
| 20 | \n \= (?: end | cut ) (?-s:.)* | ||
| 21 | |||
| 22 | | | ||
| 23 | # normal space | ||
| 24 | \s | ||
| 25 | | | ||
| 26 | # a comment until end-of-line | ||
| 27 | \# (?-s:.)* | ||
| 28 | )+ | ||
| 29 | ) | ||
| 30 | (.*)$ # "tail" | ||
| 31 | /sx; | ||
| 32 | #print "ws: $grammar [$_[0]] -- [$2] \n"; | ||
| 33 | return { | ||
| 34 | bool => $bool, | ||
| 35 | match => $1, | ||
| 36 | tail => $2, | ||
| 37 | } | ||
| 38 | }; | ||
| 39 | |||
| 40 | sub add_rule { | ||
| 41 | my ( $class, $key, $rule ) = @_; | ||
| 42 | no strict qw( refs ); | ||
| 43 | ${"${class}::hash"}{$key} = Pugs::Compiler::Regex->compile( | ||
| 44 | $rule, | ||
| 45 | { grammar => $class }, | ||
| 46 | ); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub capture { | ||
| 50 | # print Dumper ${$_[0]}->{match}[0]{match}[1]{capture}; | ||
| 51 | return ${$_[0]}->{match}[0]{match}[1]{capture}; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub recompile { | ||
| 55 | my ( $class ) = @_; | ||
| 56 | no strict qw( refs ); | ||
| 57 | #print "creating ${class}::parse()\n"; | ||
| 58 | *{"${class}::parse"} = Pugs::Compiler::Regex->compile( ' | ||
| 59 | %' . $class . '::hash | ||
| 60 | { return Pugs::Grammar::BaseCategory::capture( $/ ) } | ||
| 61 | ' )->code; | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; |