| 1 | package Pugs::Grammar::Term; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base qw(Pugs::Grammar::BaseCategory); | ||
| 5 | use Pugs::Runtime::Match; | ||
| 6 | use Pugs::Compiler::Token; | ||
| 7 | |||
| 8 | # TODO - implement the "magic hash" dispatcher | ||
| 9 | # TODO - term:<...> - yada-yada-yada | ||
| 10 | # moose=>1 | ||
| 11 | # moose:<elk> | ||
| 12 | # moose:{antler()} | ||
| 13 | |||
| 14 | our %hash; | ||
| 15 | |||
| 16 | sub pair { | ||
| 17 | my $class = shift; | ||
| 18 | return $class->no_match unless $_[0]; | ||
| 19 | #print "match pair $_[0]\n"; | ||
| 20 | return Pugs::Runtime::Match->new( { | ||
| 21 | bool => 1, | ||
| 22 | match => $1, | ||
| 23 | tail => $3, | ||
| 24 | capture => { | ||
| 25 | pair => { key => { single_quoted => $1 }, value => defined $2 ? { single_quoted => $2 } : { int => 1 } } | ||
| 26 | }, | ||
| 27 | } ) | ||
| 28 | if $_[0] =~ /^:([_\w]+)(?:<(.*?)>)?(.*)$/s; | ||
| 29 | # :$foo | ||
| 30 | return Pugs::Runtime::Match->new( { | ||
| 31 | bool => 1, | ||
| 32 | match => $1, | ||
| 33 | tail => $2, | ||
| 34 | capture => { | ||
| 35 | pair => { key => { single_quoted => $1 }, value => { scalar => '$'.$1 } } | ||
| 36 | }, | ||
| 37 | } ) | ||
| 38 | if $_[0] =~ /^:\$([_\w]+)(.*)$/s; | ||
| 39 | return $class->no_match; | ||
| 40 | }; | ||
| 41 | |||
| 42 | sub cpan_bareword { | ||
| 43 | my $class = shift; | ||
| 44 | return $class->no_match unless $_[0]; | ||
| 45 | return Pugs::Runtime::Match->new( { | ||
| 46 | bool => 1, | ||
| 47 | match => $1, | ||
| 48 | tail => $2, | ||
| 49 | capture => { cpan_bareword => $1 }, | ||
| 50 | } ) | ||
| 51 | if $_[0] =~ /^ ([_\w\d]+ \- [_\w\d\-\.*]+) ( (?: \(|\;|\s|$ ) .*)$/sx; | ||
| 52 | return $class->no_match; | ||
| 53 | }; | ||
| 54 | |||
| 55 | sub substitution { | ||
| 56 | my $grammar = shift; | ||
| 57 | return $grammar->no_match unless $_[0]; | ||
| 58 | my $options; | ||
| 59 | while ($_[0] =~ s/^:(\w+)//) { | ||
| 60 | $options->{lc($1)} = 1; | ||
| 61 | } | ||
| 62 | return $grammar->no_match unless substr($_[0], 0 , 1) eq '/'; | ||
| 63 | substr($_[0], 0, 1, ''); | ||
| 64 | my ($extracted,$remainder) = Text::Balanced::extract_delimited( "/" . $_[0], "/" ); | ||
| 65 | $extracted = substr( $extracted, 1, -1 ) if length($extracted) > 1; | ||
| 66 | my $extracted2; | ||
| 67 | ($extracted2,$remainder) = Text::Balanced::extract_delimited( "/" . $remainder, "/" ); | ||
| 68 | $extracted2 = substr( $extracted2, 1, -1 ) if length($extracted2) > 1; | ||
| 69 | return Pugs::Runtime::Match->new( { | ||
| 70 | bool => 1, # ( $extracted ne '' ), | ||
| 71 | match => $extracted, | ||
| 72 | tail => $remainder, | ||
| 73 | capture => { options => $options, substitution => [$extracted, $extracted2] }, | ||
| 74 | } ); | ||
| 75 | }; | ||
| 76 | |||
| 77 | sub single_quoted { | ||
| 78 | my $grammar = shift; | ||
| 79 | return $grammar->no_match unless $_[0]; | ||
| 80 | my ($extracted,$remainder) = Text::Balanced::extract_delimited( "'" . $_[0], "'" ); | ||
| 81 | $extracted = substr( $extracted, 1, -1 ) if length($extracted) > 1; | ||
| 82 | return Pugs::Runtime::Match->new( { | ||
| 83 | bool => 1, # ( $extracted ne '' ), | ||
| 84 | match => $extracted, | ||
| 85 | tail => $remainder, | ||
| 86 | capture => $extracted, | ||
| 87 | } ); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub double_quoted { | ||
| 91 | my $grammar = shift; | ||
| 92 | return $grammar->no_match unless $_[0]; | ||
| 93 | my ($extracted,$remainder) = Text::Balanced::extract_delimited( '"' . $_[0], '"' ); | ||
| 94 | $extracted = substr( $extracted, 1, -1 ) if length($extracted) > 1; | ||
| 95 | return Pugs::Runtime::Match->new( { | ||
| 96 | bool => 1, # ( $extracted ne '' ), | ||
| 97 | match => $extracted, | ||
| 98 | tail => $remainder, | ||
| 99 | capture => $extracted, | ||
| 100 | } ); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub angle_quoted { | ||
| 104 | my $grammar = shift; | ||
| 105 | return $grammar->no_match unless $_[0]; | ||
| 106 | my ($extracted,$remainder) = Text::Balanced::extract_bracketed( '<' . $_[0], '<..>' ); | ||
| 107 | $extracted = substr( $extracted, 1, -1 ) if length($extracted) > 1; | ||
| 108 | return Pugs::Runtime::Match->new( { | ||
| 109 | bool => 1, # ( $extracted ne '' ), | ||
| 110 | match => $extracted, | ||
| 111 | tail => $remainder, | ||
| 112 | capture => $extracted, | ||
| 113 | } ); | ||
| 114 | } | ||
| 115 | |||
| 116 | *ident = Pugs::Compiler::Regex->compile( q( | ||
| 117 | \! # $! | ||
| 118 | | \?? # $?CALLER | ||
| 119 | \*? # $*x | ||
| 120 | # \.? # $.x - XXX causes problems with 1..5 for some reason | ||
| 121 | \:? # $:x | ||
| 122 | [ | ||
| 123 | [ \:\: ]? | ||
| 124 | [ _ | <?alpha> ] | ||
| 125 | [ _ | <?alnum> ]* | ||
| 126 | ]+ | ||
| 127 | | <before \< | \[ | \{ > # $<thing> == $/<thing>; $[thing] = $/[thing] | ||
| 128 | | \/ # $/ | ||
| 129 | ) )->code; | ||
| 130 | |||
| 131 | *parenthesis = Pugs::Compiler::Regex->compile( q( | ||
| 132 | <?ws>? <Pugs::Grammar::Perl6.perl6_expression> <?ws>? | ||
| 133 | <'\)'> | ||
| 134 | { return { | ||
| 135 | op1 => { op => "(" }, | ||
| 136 | op2 => { op => ")" }, | ||
| 137 | fixity => "circumfix", | ||
| 138 | exp1 => $_[0]{'Pugs::Grammar::Perl6.perl6_expression'}->() | ||
| 139 | } } | ||
| 140 | | | ||
| 141 | <?ws>? <Pugs::Grammar::Perl6.block> <?ws>? | ||
| 142 | <'\)'> | ||
| 143 | { return { | ||
| 144 | op1 => { op => "(" }, | ||
| 145 | op2 => { op => ")" }, | ||
| 146 | fixity => "circumfix", | ||
| 147 | exp1 => $_[0]{'Pugs::Grammar::Perl6.block'}->() | ||
| 148 | } } | ||
| 149 | | | ||
| 150 | <?ws>? | ||
| 151 | <'\)'> | ||
| 152 | { return { | ||
| 153 | op1 => { op => "(" }, | ||
| 154 | op2 => { op => ")" }, | ||
| 155 | fixity => "circumfix", | ||
| 156 | } } | ||
| 157 | ) )->code; | ||
| 158 | |||
| 159 | sub recompile { | ||
| 160 | my $class = shift; | ||
| 161 | %hash = ( | ||
| 162 | '$' => Pugs::Compiler::Regex->compile( q( | ||
| 163 | <?Pugs::Grammar::Term.ident> | ||
| 164 | { return { scalar => '$' . $_[0]->() ,} } | ||
| 165 | ) ), | ||
| 166 | '$.' => Pugs::Compiler::Regex->compile( q( | ||
| 167 | <?Pugs::Grammar::Term.ident> | ||
| 168 | { return { scalar => '$.' . $_[0]->() ,} } | ||
| 169 | ) ), | ||
| 170 | '@' => Pugs::Compiler::Regex->compile( q( | ||
| 171 | <?Pugs::Grammar::Term.ident> | ||
| 172 | { return { array => "\@" . $_[0]->() ,} } | ||
| 173 | ) ), | ||
| 174 | '%' => Pugs::Compiler::Regex->compile( q( | ||
| 175 | <?Pugs::Grammar::Term.ident> | ||
| 176 | { return { hash => "\%" . $_[0]->() ,} } | ||
| 177 | ) ), | ||
| 178 | '&' => Pugs::Compiler::Regex->compile( q( | ||
| 179 | <?Pugs::Grammar::Term.ident> | ||
| 180 | { return { code => "\&" . $_[0]->() ,} } | ||
| 181 | ) ), | ||
| 182 | |||
| 183 | '(' => Pugs::Compiler::Regex->compile( q( | ||
| 184 | <Pugs::Grammar::Term.parenthesis> | ||
| 185 | { return $_[0]{'Pugs::Grammar::Term.parenthesis'}->() } | ||
| 186 | ) ), | ||
| 187 | '{' => Pugs::Compiler::Regex->compile( q( | ||
| 188 | <?ws>? <Pugs::Grammar::Perl6.statements_or_null> <?ws>? <'}'> | ||
| 189 | { | ||
| 190 | return { | ||
| 191 | bare_block => $_[0]{'Pugs::Grammar::Perl6.statements_or_null'}->(), | ||
| 192 | } } | ||
| 193 | ) ), | ||
| 194 | |||
| 195 | |||
| 196 | '->' => Pugs::Compiler::Regex->compile( q( | ||
| 197 | [ | ||
| 198 | <?ws>? <Pugs::Grammar::Perl6.perl6_expression('no_blocks',0)> <?ws>? | ||
| 199 | \{ <?ws>? <Pugs::Grammar::Perl6.statements_or_null> <?ws>? \} | ||
| 200 | { return { | ||
| 201 | pointy_block => $_[0]{'Pugs::Grammar::Perl6.statements_or_null'}->(), | ||
| 202 | signature => $_[0]{'Pugs::Grammar::Perl6.perl6_expression'}->(), | ||
| 203 | } } | ||
| 204 | | | ||
| 205 | <?ws>? | ||
| 206 | \{ <?ws>? <Pugs::Grammar::Perl6.statements_or_null> <?ws>? \} | ||
| 207 | { return { | ||
| 208 | pointy_block => $_[0]{'Pugs::Grammar::Perl6.statements_or_null'}->(), | ||
| 209 | signature => undef, | ||
| 210 | } } | ||
| 211 | ] | ||
| 212 | ) ), | ||
| 213 | |||
| 214 | '.' => Pugs::Compiler::Regex->compile( q( | ||
| 215 | # .method op | ||
| 216 | <?Pugs::Grammar::Term.ident> | ||
| 217 | { return { dot_bareword => $_[0]->() ,} } | ||
| 218 | ) ), | ||
| 219 | '...' => Pugs::Compiler::Regex->compile( q( | ||
| 220 | { | ||
| 221 | return { die => "not implemented" } | ||
| 222 | } | ||
| 223 | ) ), | ||
| 224 | Inf => Pugs::Compiler::Regex->compile( q( | ||
| 225 | { return { num => 'Inf' ,} } | ||
| 226 | ) ), | ||
| 227 | NaN => Pugs::Compiler::Regex->compile( q( | ||
| 228 | { return { num => 'NaN' ,} } | ||
| 229 | ) ), | ||
| 230 | 'bool::true' => Pugs::Compiler::Regex->compile( q( | ||
| 231 | { return { bool => 1 ,} } | ||
| 232 | ) ), | ||
| 233 | 'bool::false' => Pugs::Compiler::Regex->compile( q( | ||
| 234 | { return { bool => 0 ,} } | ||
| 235 | ) ), | ||
| 236 | q(') => # ' | ||
| 237 | Pugs::Compiler::Regex->compile( q( | ||
| 238 | <Pugs::Grammar::Term.single_quoted> | ||
| 239 | { return { single_quoted => $/{'Pugs::Grammar::Term.single_quoted'}->() ,} } | ||
| 240 | ) ), | ||
| 241 | q(") => Pugs::Compiler::Regex->compile( q( | ||
| 242 | <Pugs::Grammar::Term.double_quoted> | ||
| 243 | { return { double_quoted => $/{'Pugs::Grammar::Term.double_quoted'}->() ,} } | ||
| 244 | ) ), | ||
| 245 | q(s) => Pugs::Compiler::Regex->compile( q( | ||
| 246 | <Pugs::Grammar::Term.substitution> | ||
| 247 | { return { | ||
| 248 | substitution => $/{'Pugs::Grammar::Term.substitution'}->(), | ||
| 249 | } | ||
| 250 | } | ||
| 251 | ) ), | ||
| 252 | # angle is handled by the lexer | ||
| 253 | #q(<) => Pugs::Compiler::Regex->compile( q( | ||
| 254 | # <Pugs::Grammar::Term.angle_quoted> | ||
| 255 | # { return { angle_quoted => $/{'Pugs::Grammar::Term.angle_quoted'}->() ,} } | ||
| 256 | #) ), | ||
| 257 | #~ q(.) => Pugs::Compiler::Regex->compile( q( | ||
| 258 | #~ <Pugs::Grammar::Term.bareword> | ||
| 259 | #~ { return { method => $/{'Pugs::Grammar::Term.bareword'}->() ,} } | ||
| 260 | #~ ) ), | ||
| 261 | q() => Pugs::Compiler::Regex->compile( q! | ||
| 262 | ### floating point | ||
| 263 | \d+\.\d+ { return { num => $() ,} } | ||
| 264 | | | ||
| 265 | ### number | ||
| 266 | \d+ { return { int => $() ,} } | ||
| 267 | | | ||
| 268 | ### long:<name> | ||
| 269 | <Pugs::Grammar::Term.pair> | ||
| 270 | { return $/{'Pugs::Grammar::Term.pair'}->() } | ||
| 271 | #~ | | ||
| 272 | #~ ### func(... func.(... | ||
| 273 | #~ <Pugs::Grammar::Term.sub_call> | ||
| 274 | #~ { return $/{'Pugs::Grammar::Term.sub_call'}->() } | ||
| 275 | | | ||
| 276 | ### Test-0.0.6 | ||
| 277 | <Pugs::Grammar::Term.cpan_bareword> | ||
| 278 | { return $/{'Pugs::Grammar::Term.cpan_bareword'}->() } | ||
| 279 | | | ||
| 280 | ### Test::More | ||
| 281 | <Pugs::Grammar::Term.ident> | ||
| 282 | { return { bareword => $/{'Pugs::Grammar::Term.ident'}->() } } | ||
| 283 | #| | ||
| 284 | # ### v6 | ||
| 285 | # <Pugs::Grammar::Term.bareword> | ||
| 286 | # { return $/{'Pugs::Grammar::Term.bareword'}->() } | ||
| 287 | ! ), | ||
| 288 | ); | ||
| 289 | $class->SUPER::recompile; | ||
| 290 | } | ||
| 291 | |||
| 292 | BEGIN { | ||
| 293 | __PACKAGE__->recompile; | ||
| 294 | } | ||
| 295 | |||
| 296 | 1; |