| 1 | package Pugs::Emitter::Rule::Parsec; | ||
| 2 | |||
| 3 | # p6-rule parsec emitter | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Pugs::Grammar::MiniPerl6; | ||
| 8 | use Data::Dumper; | ||
| 9 | $Data::Dumper::Indent = 1; | ||
| 10 | |||
| 11 | our $sigspace = 0; | ||
| 12 | our $capture_counter = 0; | ||
| 13 | sub get_capture_var { return 'capture_' . $capture_counter++; } | ||
| 14 | |||
| 15 | sub rule_rename($){ | ||
| 16 | my $orig_name = shift; | ||
| 17 | return 'rule' . (uc substr $orig_name, 0, 1) . substr $orig_name, 1; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub to_genparser_string($) { | ||
| 21 | my $char_parser = shift; | ||
| 22 | return "($char_parser >>= \\c -> return [c])"; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub call_constant { | ||
| 26 | my $str = shift; | ||
| 27 | $str =~ s/\\/\\\\/g; | ||
| 28 | $str =~ s/"/\\"/g; | ||
| 29 | return 'string "' . $str . '"'; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub emit { | ||
| 33 | my ($grammar, $ast, $param) = @_; | ||
| 34 | local $sigspace = $param->{sigspace}; # XXX - $sigspace should be lexical | ||
| 35 | local $capture_counter = 0; | ||
| 36 | emit_rule( $ast, '' ) . "\n"; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub emit_rule { | ||
| 40 | my $n = $_[0]; | ||
| 41 | my $tab = $_[1]; | ||
| 42 | die "unknown node: ", Dumper( $n ) | ||
| 43 | unless ref( $n ) eq 'HASH'; | ||
| 44 | #print "NODE ", Dumper($n); | ||
| 45 | my ( $k, $v ) = each %$n; | ||
| 46 | # XXX - use real references | ||
| 47 | no strict 'refs'; | ||
| 48 | my $code = &$k( $v, $tab ); | ||
| 49 | return $code; | ||
| 50 | } | ||
| 51 | |||
| 52 | #rule nodes | ||
| 53 | |||
| 54 | sub non_capturing_group { | ||
| 55 | return emit_rule( $_[0], $_[1] ); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub quant { | ||
| 59 | my $term = $_[0]->{'term'}; | ||
| 60 | my $quantifier = $_[0]->{quant}; | ||
| 61 | my $spacing = | ||
| 62 | ( $sigspace && | ||
| 63 | ($_[0]->{ws1} ne '' && $_[0]->{ws2} ne '') | ||
| 64 | ); | ||
| 65 | my $tab = $spacing ? $_[1] . ' ' : $_[1]; | ||
| 66 | my $rul = emit_rule( $term, $tab ); | ||
| 67 | |||
| 68 | my $ws = metasyntax('?ws', $tab); | ||
| 69 | $rul = "$ws\n$tab$rul" if $sigspace && $_[0]->{ws1} ne ''; | ||
| 70 | $rul = "$rul\n$tab$ws" if $sigspace && $_[0]->{ws2} ne ''; | ||
| 71 | $rul = "do\n$tab$rul" if $spacing; | ||
| 72 | return $rul | ||
| 73 | if $quantifier eq ''; | ||
| 74 | |||
| 75 | # * + ? | ||
| 76 | my $qual = ''; | ||
| 77 | return "option \"\" \$ $rul" if $quantifier eq '?'; | ||
| 78 | |||
| 79 | $qual = 'many' if $quantifier eq '*'; | ||
| 80 | $qual = 'many1' if $quantifier eq '+'; | ||
| 81 | |||
| 82 | die "quantifier not implemented: $quantifier" if $qual eq ''; | ||
| 83 | |||
| 84 | my $final_rul = | ||
| 85 | "(($qual \$ $rul) >>= \\arr -> return \$ foldr (++) \"\" arr)"; | ||
| 86 | |||
| 87 | return "$final_rul >>\n$tab$ws" if $sigspace and $_[0]->{ws3} ne ''; | ||
| 88 | return $final_rul; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub alt { | ||
| 92 | my @s; | ||
| 93 | my @alt = @{$_[0]}; | ||
| 94 | |||
| 95 | # clean up alternative body | ||
| 96 | # XXX maybe can be done earlier (e.g. in parser) | ||
| 97 | foreach(@alt){ | ||
| 98 | if(ref eq 'HASH'){ | ||
| 99 | my($k) = keys %$_; | ||
| 100 | |||
| 101 | if($k eq 'alt'){ | ||
| 102 | my @sub_alt = @{$_->{alt}}; | ||
| 103 | $_ = $sub_alt[0]; | ||
| 104 | push @alt, @sub_alt[1 .. $#sub_alt]; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | my $indent = $_[1] . ' '; | ||
| 110 | foreach(@alt){ | ||
| 111 | my $tmp = emit_rule( $_, $indent ); | ||
| 112 | push @s, $tmp if $tmp; | ||
| 113 | } | ||
| 114 | return "do\n$indent" . join "\n$indent<|>\n$indent", @s; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub concat { | ||
| 118 | my @inner = @{$_[0]}; | ||
| 119 | |||
| 120 | # clean up concatenation body | ||
| 121 | # XXX maybe can be done earlier (e.g. in parser) | ||
| 122 | foreach(@inner){ | ||
| 123 | if(ref eq 'HASH'){ | ||
| 124 | my($k) = keys %$_; | ||
| 125 | |||
| 126 | if($k eq 'concat'){ | ||
| 127 | my @sub_inner = @{$_->{concat}}; | ||
| 128 | $_ = $sub_inner[0]; | ||
| 129 | push @inner, @sub_inner[1 .. $#sub_inner]; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | my $indent = $_[1] . ' '; | ||
| 135 | my $result = 'do'; | ||
| 136 | foreach(@inner){ | ||
| 137 | my $tmp = emit_rule( $_, $indent ); | ||
| 138 | $result .= "\n$indent" . $tmp if $tmp; | ||
| 139 | } | ||
| 140 | return $result; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub dot { | ||
| 144 | return to_genparser_string("anyChar"); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub variable {} | ||
| 148 | |||
| 149 | use vars qw( %special_chars ); | ||
| 150 | BEGIN { | ||
| 151 | %special_chars = ( | ||
| 152 | r => "char '\\r'", | ||
| 153 | n => "char '\\n'", | ||
| 154 | t => "char '\\t'", | ||
| 155 | e => "char '\\033'", | ||
| 156 | f => "char '\\f'", | ||
| 157 | w => "(alphaNum <|> char '_')", | ||
| 158 | d => 'digit', | ||
| 159 | s => 'space', | ||
| 160 | W => "satisfy (\\x -> x /= '_' && not \$ isAlphaNum x)", | ||
| 161 | D => 'noneOf "0123456789"', | ||
| 162 | S => 'noneOf " \\v\\f\\t\\r\\n"', | ||
| 163 | ); | ||
| 164 | |||
| 165 | while(my ($k, $v) = each %special_chars){ | ||
| 166 | next if | ||
| 167 | $k eq uc $k or | ||
| 168 | exists $special_chars{uc $k} or | ||
| 169 | $v !~ /^char/; | ||
| 170 | my $chars = substr $v, 6; | ||
| 171 | chop $chars; | ||
| 172 | $special_chars{uc $k} = "noneOf \"$chars\""; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | sub special_char { | ||
| 176 | my $char = substr($_[0],1); | ||
| 177 | return to_genparser_string($special_chars{$char}) | ||
| 178 | if exists $special_chars{$char}; | ||
| 179 | $char = '\\\\' if $char eq '\\'; | ||
| 180 | return "string \"$char\""; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub match_variable {} | ||
| 184 | |||
| 185 | sub closure { | ||
| 186 | my $miniperl6 = substr $_[0], 1, length($_[0]) - 2; | ||
| 187 | my $haskell = Pugs::Grammar::MiniPerl6->ProductionRule($miniperl6); | ||
| 188 | $haskell =~ s/\n/\n$_[1]/sg; | ||
| 189 | # print ">>> MiniPerl6\n$miniperl6\n===\n$haskell\n<<< Haskell\n"; | ||
| 190 | return "$haskell"; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub capturing_group { | ||
| 194 | my $program = $_[0]; | ||
| 195 | |||
| 196 | $program = emit_rule( $program, $_[1] . ' ' ) | ||
| 197 | if ref( $program ); | ||
| 198 | |||
| 199 | return &get_capture_var . ' <- ' . $program; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub named_capture { | ||
| 203 | my $name = $_[0]{ident}; | ||
| 204 | my $program = $_[0]{rule}; | ||
| 205 | |||
| 206 | return "$name <- " . metasyntax($program->{metasyntax}, $_[1] . ' ', 1) | ||
| 207 | if exists $program->{metasyntax}; | ||
| 208 | |||
| 209 | return "$name <- " . emit_rule($program, $_[1] . ' '); | ||
| 210 | } | ||
| 211 | |||
| 212 | sub negate { | ||
| 213 | my $body = $_[0]; | ||
| 214 | return not_after($body->{after}, $_[1]) if exists $body->{after}; | ||
| 215 | return not_before($body->{before}, $_[1]) if exists $body->{before}; | ||
| 216 | return ''; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub before { | ||
| 220 | my $program = $_[0]{rule}; | ||
| 221 | return 'lookAhead (' . emit_rule($program, $_[1] . ' ') . ')'; | ||
| 222 | } | ||
| 223 | |||
| 224 | sub not_before { | ||
| 225 | my $program = $_[0]{rule}; | ||
| 226 | return 'notFollowedBy $ (' . | ||
| 227 | emit_rule($program, $_[1] . ' ') . | ||
| 228 | ") >> return ' '"; | ||
| 229 | # notFollowedBy :: Show tok => GenParser tok st tok -> GenParser tok st () | ||
| 230 | # tok = Char in the context so that we have to cast it | ||
| 231 | } | ||
| 232 | |||
| 233 | sub after {} | ||
| 234 | sub not_after {} | ||
| 235 | sub colon {} | ||
| 236 | |||
| 237 | sub constant { | ||
| 238 | return "string \"$_[0]\""; | ||
| 239 | } | ||
| 240 | |||
| 241 | use vars qw( %char_class ); | ||
| 242 | BEGIN { | ||
| 243 | %char_class = ( | ||
| 244 | alpha => 'letter', | ||
| 245 | alnum => 'alphaNum', | ||
| 246 | ascii => 'satisfy isAscii', | ||
| 247 | blank => 'oneOf " \\t"', | ||
| 248 | cntrl => 'satisfy isCotrol', | ||
| 249 | digit => 'digit', | ||
| 250 | graph => "satisfy (\\x -> isPrint x && x /= ' ')", | ||
| 251 | lower => 'lower', | ||
| 252 | print => 'satisfy isPrint', | ||
| 253 | punct => "satisfy (\\x -> isPrint x && x /= ' ' && not (isAlphaNum x))", | ||
| 254 | space => 'space', | ||
| 255 | upper => 'upper', | ||
| 256 | word => "(alphaNum <|> char '_')", | ||
| 257 | xdigit => 'hexDigit', | ||
| 258 | ); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub metasyntax { | ||
| 262 | # <cmd> | ||
| 263 | my $cmd = $_[0]; | ||
| 264 | my $prefix = substr( $cmd, 0, 1 ); | ||
| 265 | |||
| 266 | my $named_capturing = !$_[2]; | ||
| 267 | my $negative_lookahead = 0; | ||
| 268 | |||
| 269 | if ( $prefix eq '@' ) { | ||
| 270 | # XXX - wrap @array items - see end of Pugs::Grammar::Rule | ||
| 271 | # TODO - param list | ||
| 272 | return | ||
| 273 | "$_[1] do {\n" . | ||
| 274 | "$_[1] my \$match;\n" . | ||
| 275 | "$_[1] for my \$subrule ( $cmd ) {\n" . | ||
| 276 | "$_[1] \$match = " . | ||
| 277 | call_subrule( '$subrule', '', () ) . ";\n" . | ||
| 278 | "$_[1] last if \$match;\n" . | ||
| 279 | "$_[1] }\n" . | ||
| 280 | "$_[1] my \$bool = (!\$match != 1);\n" . | ||
| 281 | "$_[1] \$pos = \$match->to if \$bool;\n" . | ||
| 282 | "$_[1] \$bool;\n" . | ||
| 283 | "$_[1] }"; | ||
| 284 | } | ||
| 285 | if ( $prefix eq '$' ) { | ||
| 286 | if ( $cmd =~ /::/ ) { | ||
| 287 | # call method in fully qualified $package::var | ||
| 288 | # ...->match( $rule, $str, $grammar, $flags, $state ) | ||
| 289 | # TODO - send $pos to subrule | ||
| 290 | return | ||
| 291 | "$_[1] do {\n" . | ||
| 292 | "$_[1] push \@match,\n" . | ||
| 293 | "$_[1] $cmd->match( \$s, \$grammar, {p => \$pos}, undef );\n" . | ||
| 294 | "$_[1] \$pos = \$match[-1]->to;\n" . | ||
| 295 | "$_[1] !\$match[-1] != 1;\n" . | ||
| 296 | "$_[1] }" | ||
| 297 | } | ||
| 298 | # call method in lexical $var | ||
| 299 | # TODO - send $pos to subrule | ||
| 300 | return | ||
| 301 | "$_[1] do {\n" . | ||
| 302 | "$_[1] my \$r = Pugs::Runtime::Rule::get_variable( '$cmd' );\n" . | ||
| 303 | "$_[1] push \@match,\n" . | ||
| 304 | "$_[1] \$r->match( \$s, \$grammar, {p => \$pos}, undef );\n" . | ||
| 305 | "$_[1] \$pos = \$match[-1]->to;\n" . | ||
| 306 | "$_[1] !\$match[-1] != 1;\n" . | ||
| 307 | "$_[1] }" | ||
| 308 | } | ||
| 309 | if ( $prefix eq q(') ) { # single quoted literal ' | ||
| 310 | $cmd = substr( $cmd, 1, -1 ); | ||
| 311 | return call_constant( $cmd ); | ||
| 312 | } | ||
| 313 | if ( $prefix eq q(") ) { # interpolated literal " | ||
| 314 | $cmd = substr( $cmd, 1, -1 ); | ||
| 315 | warn "<\"...\"> not implemented"; | ||
| 316 | return; | ||
| 317 | } | ||
| 318 | if ( $prefix =~ /[-+[]/ ) { # character class | ||
| 319 | if ( $prefix eq '-' ) { | ||
| 320 | my $str = substr $cmd, 2, length($cmd) - 3; | ||
| 321 | $str =~ s/\\>/>/g; # XXX | ||
| 322 | $str =~ s/\\/\\\\/g; | ||
| 323 | $str =~ s/"/\\"/g; | ||
| 324 | |||
| 325 | return to_genparser_string("noneOf \"$str\""); | ||
| 326 | } | ||
| 327 | elsif ( $prefix eq '+' ) { | ||
| 328 | $cmd = substr($cmd, 2); | ||
| 329 | } | ||
| 330 | |||
| 331 | my $str = substr $cmd, 1, length($cmd) - 2; | ||
| 332 | $str =~ s/\\>/>/g; # XXX | ||
| 333 | $str =~ s/\\/\\\\/g; | ||
| 334 | $str =~ s/"/\\"/g; | ||
| 335 | |||
| 336 | return to_genparser_string("oneOf \"$str\""); | ||
| 337 | } | ||
| 338 | if ( $prefix eq '?' ) { # non_capturing_subrule / code assertion | ||
| 339 | $cmd = substr( $cmd, 1 ); | ||
| 340 | if ( $cmd =~ /^{/ ) { | ||
| 341 | warn "code assertion not implemented"; | ||
| 342 | return; | ||
| 343 | } | ||
| 344 | $prefix = substr( $cmd, 0, 1 ); | ||
| 345 | $named_capturing = 0; | ||
| 346 | } | ||
| 347 | if ( $prefix eq '!' ) { # negated_subrule / code assertion | ||
| 348 | $cmd = substr( $cmd, 1 ); | ||
| 349 | if ( $cmd =~ /^{/ ) { | ||
| 350 | warn "code assertion not implemented"; | ||
| 351 | return; | ||
| 352 | } | ||
| 353 | $prefix = substr( $cmd, 0, 1 ); | ||
| 354 | $negative_lookahead = 1; | ||
| 355 | warn "<$cmd> not implemented"; | ||
| 356 | return; | ||
| 357 | } | ||
| 358 | if ( $cmd eq '.' ) { | ||
| 359 | warn "<$cmd> not implemented"; | ||
| 360 | return; | ||
| 361 | } | ||
| 362 | if ( $prefix =~ /[_[:alnum:]]/ ) { | ||
| 363 | # "before" and "after" are handled in a separate rule | ||
| 364 | if ( $cmd eq 'ws' ){ | ||
| 365 | return 'perl6WhiteSpace'; | ||
| 366 | # assuming function: | ||
| 367 | # perl6WhiteSpace = do cls <- getPrevCharClass | ||
| 368 | # let mod = if cls == WordClass then many1 else many | ||
| 369 | # do mod whiteSpace | ||
| 370 | # <|> | ||
| 371 | # (satisfy (\c -> charClassOf c /= WordClass) >> return "") | ||
| 372 | |||
| 373 | } | ||
| 374 | if ( $cmd eq 'cut' ) { | ||
| 375 | warn "<$cmd> not implemented"; | ||
| 376 | return; | ||
| 377 | } | ||
| 378 | if ( $cmd eq 'commit' ) { | ||
| 379 | warn "<$cmd> not implemented"; | ||
| 380 | return; | ||
| 381 | } | ||
| 382 | if ( $cmd eq 'prior' ) { | ||
| 383 | warn "<$cmd> not implemented"; | ||
| 384 | return; | ||
| 385 | } | ||
| 386 | if ( $cmd eq 'null' ) { | ||
| 387 | warn "<$cmd> not implemented"; | ||
| 388 | return; | ||
| 389 | } | ||
| 390 | if ( $char_class{$cmd} ) { | ||
| 391 | # XXX - inlined char classes are not inheritable, but this should be ok | ||
| 392 | return to_genparser_string($char_class{$cmd}); | ||
| 393 | } | ||
| 394 | # capturing subrule | ||
| 395 | # <subrule ( param, param ) > | ||
| 396 | my ( $subrule, $param_list ) = split( /[\(\)]/, $cmd ); | ||
| 397 | $param_list = '' unless defined $param_list; | ||
| 398 | my @param = split( ',', $param_list ); | ||
| 399 | return ($named_capturing ? "$subrule <- " : '') . | ||
| 400 | rule_rename($subrule) . join '', map { " ($_)" } @param; | ||
| 401 | } | ||
| 402 | die "<$cmd> not implemented"; | ||
| 403 | } | ||
| 404 | |||
| 405 | 1; |