| 1 | # this file was extracted from the P6 version in Pugs-Compiler-Rule | ||
| 2 | |||
| 3 | package Pugs::Grammar::P6Rule; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Pugs::Compiler::Rule; | ||
| 8 | use Pugs::Compiler::Token; | ||
| 9 | use Pugs::Compiler::Regex; | ||
| 10 | use base 'Pugs::Grammar::Base'; | ||
| 11 | use Pugs::Runtime::Match::Ratchet; # overload doesn't work without this ??? | ||
| 12 | |||
| 13 | our @rule_terms; | ||
| 14 | |||
| 15 | # reuse some subs | ||
| 16 | use Pugs::Grammar::Rule; | ||
| 17 | # XXX - this doesn't work: | ||
| 18 | # "Can't call method "no_match" on an undefined value" | ||
| 19 | #*code = &Pugs::Grammar::Rule::code; | ||
| 20 | #*literal = &Pugs::Grammar::Rule::literal; | ||
| 21 | #*metasyntax = &Pugs::Grammar::Rule::metasyntax; | ||
| 22 | sub code { Pugs::Grammar::Rule::code(@_) } | ||
| 23 | sub literal { Pugs::Grammar::Rule::literal(@_) } | ||
| 24 | sub metasyntax { Pugs::Grammar::Rule::metasyntax(@_) } | ||
| 25 | push @rule_terms, 'metasyntax'; | ||
| 26 | |||
| 27 | *ws = Pugs::Compiler::RegexPerl5->compile(q(^((?:\\s|\\#(?-s:.)*)+)), { P5 => 0 })->code; | ||
| 28 | *variable = Pugs::Compiler::RegexPerl5->compile(q(^([\\$\\%\\@](?:(?:\\:\\:)?[_[:alnum:]]+)+)), { P5 => 0 })->code; | ||
| 29 | *positional_variable = Pugs::Compiler::RegexPerl5->compile(q(^([\\$\\%\\@]\\^(?:[_[:alnum:]]+))), { P5 => 0 })->code; | ||
| 30 | *ident = Pugs::Compiler::RegexPerl5->compile(q(^((?:(?:\\:\\:)?[_[:alnum:]]+)+)), { P5 => 0 })->code; | ||
| 31 | *num_variable = Pugs::Compiler::RegexPerl5->compile(q(^(?:\\$[[:digit:]]+)), { P5 => 0 })->code; | ||
| 32 | *dot = Pugs::Compiler::Regex->compile(q( | ||
| 33 | \\. | ||
| 34 | |||
| 35 | { return { 'dot' => 1 ,} } | ||
| 36 | ))->code; | ||
| 37 | *plain_text = Pugs::Compiler::Regex->compile(q( | ||
| 38 | <alnum> | \\, | \\; | \\_ | \\/ | \\~ | \\" | \\' | \\= | ||
| 39 | |||
| 40 | { return { 'constant' => $() ,} } | ||
| 41 | ))->code; | ||
| 42 | *special_char = Pugs::Compiler::Regex->compile(q( | ||
| 43 | \\\\ . | ||
| 44 | |||
| 45 | { return { special_char => $(), } } | ||
| 46 | ))->code; | ||
| 47 | *non_capturing_group = Pugs::Compiler::Regex->compile(q( | ||
| 48 | \\[ <rule> \\] | ||
| 49 | |||
| 50 | { return $_[0]{rule}() } | ||
| 51 | ))->code; | ||
| 52 | *closure_rule = Pugs::Compiler::Regex->compile(q( | ||
| 53 | <code> | ||
| 54 | |||
| 55 | { return { closure => $_[0]{code}() ,} } | ||
| 56 | ))->code; | ||
| 57 | *variable_rule = Pugs::Compiler::Regex->compile(q( | ||
| 58 | <variable> | <positional_variable> | ||
| 59 | |||
| 60 | { return { variable => $() ,} } | ||
| 61 | ))->code; | ||
| 62 | *match_variable = Pugs::Compiler::Regex->compile(q( | ||
| 63 | <num_variable> | ||
| 64 | |||
| 65 | { return { match_variable => $_[0]{num_variable}() ,} } | ||
| 66 | ))->code; | ||
| 67 | *named_capture_body = Pugs::Compiler::Regex->compile(q( | ||
| 68 | [ \\( <rule> \\) { return { rule => $_[0]{rule}(), } } ] | ||
| 69 | | [ \\[ <rule> \\] { return { rule => $_[0]{rule}(), } } ] | ||
| 70 | | [ <metasyntax> { return { rule => $_[0]{metasyntax}(), } } ] | ||
| 71 | ))->code; | ||
| 72 | *named_capture = Pugs::Compiler::Regex->compile(q( | ||
| 73 | \\$ \\< <ident> \\> <?ws>? \\:\\= <?ws>? <named_capture_body> | ||
| 74 | |||
| 75 | { my $body = $_[0]{named_capture_body}(); | ||
| 76 | $body->{ident} = $_[0]{ident}(); | ||
| 77 | return { named_capture => $body, } | ||
| 78 | } | ||
| 79 | ))->code; | ||
| 80 | *before = Pugs::Compiler::Regex->compile(q( | ||
| 81 | \\< before <?ws> <rule> \\> | ||
| 82 | |||
| 83 | { return { before => { | ||
| 84 | rule => $_[0]{rule}(), | ||
| 85 | }, } | ||
| 86 | } | ||
| 87 | ))->code; | ||
| 88 | *after = Pugs::Compiler::Regex->compile(q( | ||
| 89 | \\< after <?ws> <rule> \\> | ||
| 90 | |||
| 91 | { return { after => { | ||
| 92 | rule => $_[0]{rule}(), | ||
| 93 | }, } | ||
| 94 | } | ||
| 95 | ))->code; | ||
| 96 | *capturing_group = Pugs::Compiler::Regex->compile(q( | ||
| 97 | \\( <rule> \\) | ||
| 98 | |||
| 99 | { return { capturing_group => $_[0]{rule}() ,} } | ||
| 100 | ))->code; | ||
| 101 | *colon = Pugs::Compiler::Regex->compile(q( | ||
| 102 | ( | ||
| 103 | [ \\:\\:\\: ] | | ||
| 104 | [ \\:\\? ] | | ||
| 105 | [ \\:\\+ ] | | ||
| 106 | [ \\:\\: ] | \\: | | ||
| 107 | [ \\$\\$ ] | \\$ | | ||
| 108 | [ \\^\\^ ] | \\^ | ||
| 109 | ) | ||
| 110 | |||
| 111 | { return { colon => $_[0]->() ,} } | ||
| 112 | ))->code; | ||
| 113 | *quantifier = Pugs::Compiler::Regex->compile(q( | ||
| 114 | $<ws1> := (<?ws>?) | ||
| 115 | $<term> := (<@Pugs::Grammar::P6Rule::rule_terms>) | ||
| 116 | $<ws2> := (<?ws>?) | ||
| 117 | $<quant> := ( | ||
| 118 | [ | ||
| 119 | [ \\?\\? ] | | ||
| 120 | [ \\*\\? ] | | ||
| 121 | [ \\+\\? ] | | ||
| 122 | \\? | | ||
| 123 | \\* | | ||
| 124 | \\+ | ||
| 125 | ]? | ||
| 126 | ) | ||
| 127 | $<ws3> := (<?ws>?) | ||
| 128 | |||
| 129 | { return { | ||
| 130 | term => $_[0]{term}(), | ||
| 131 | quant => $_[0]{quant}(), | ||
| 132 | ws1 => $_[0]{ws1}(), | ||
| 133 | ws2 => $_[0]{ws2}(), | ||
| 134 | ws3 => $_[0]{ws3}(), | ||
| 135 | } | ||
| 136 | } | ||
| 137 | ))->code; | ||
| 138 | *concat = Pugs::Compiler::Regex->compile(q( | ||
| 139 | $<q1> := (<quantifier>) | ||
| 140 | [ | ||
| 141 | $<q2> := (<concat>) | ||
| 142 | |||
| 143 | { return { concat => [ | ||
| 144 | { quant => $_[0]{q1}() ,}, | ||
| 145 | $_[0]{q2}(), | ||
| 146 | ] ,} | ||
| 147 | } | ||
| 148 | | | ||
| 149 | { return { quant => $_[0]{q1}() ,} } | ||
| 150 | ] | ||
| 151 | ))->code; | ||
| 152 | *rule = Pugs::Compiler::Regex->compile(q( | ||
| 153 | $<q1> := (<concat>) | ||
| 154 | [ | ||
| 155 | $<q2> := (<rule>) | ||
| 156 | |||
| 157 | { return { alt => [ | ||
| 158 | $_[0]{q1}(), | ||
| 159 | $_[0]{q2}(), | ||
| 160 | ] ,} | ||
| 161 | } | ||
| 162 | | | ||
| 163 | { return $_[0]{q1}() } | ||
| 164 | ] | ||
| 165 | ))->code; | ||
| 166 | |||
| 167 | unshift @rule_terms, 'dot'; | ||
| 168 | unshift @rule_terms, 'plain_text'; | ||
| 169 | unshift @rule_terms, 'special_char'; | ||
| 170 | push @rule_terms, 'non_capturing_group'; | ||
| 171 | unshift @rule_terms, 'closure_rule'; | ||
| 172 | unshift @rule_terms, 'variable_rule'; | ||
| 173 | unshift @rule_terms, 'match_variable'; | ||
| 174 | unshift @rule_terms, 'named_capture'; | ||
| 175 | unshift @rule_terms, 'before'; | ||
| 176 | unshift @rule_terms, 'after'; | ||
| 177 | unshift @rule_terms, 'capturing_group'; | ||
| 178 | push @rule_terms, 'colon'; | ||
| 179 | |||
| 180 | # XXX - currying should be made automatically by <@xxx> runtime | ||
| 181 | # curry @rule_terms with Grammar | ||
| 182 | @rule_terms = map { | ||
| 183 | my $method = $_; | ||
| 184 | sub{ | ||
| 185 | # warn "Trying $method\n"; | ||
| 186 | my $match = Pugs::Grammar::Rule->$method(@_); | ||
| 187 | #warn "Match $method ".Dumper($match) if $match->{bool}; | ||
| 188 | return $match; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | @rule_terms; | ||
| 192 | |||
| 193 | 1; |