| 1 | # -*- cperl -*- | ||
| 2 | |||
| 3 | use lib '../..'; # For running directly (to test) | ||
| 4 | |||
| 5 | package Parse::YALALR::Run; | ||
| 6 | |||
| 7 | use Parse::YALALR::Build; | ||
| 8 | use strict; | ||
| 9 | use vars qw($VERSION); | ||
| 10 | |||
| 11 | $VERSION = '0.02'; | ||
| 12 | |||
| 13 | sub run_parser { | ||
| 14 | my ($parser, $tok) = @_; | ||
| 15 | my $error = $parser->{error}; | ||
| 16 | my $end = $parser->{end}; | ||
| 17 | |||
| 18 | my $get_tok; | ||
| 19 | my $i; | ||
| 20 | |||
| 21 | if (ref $tok eq 'CODE') { | ||
| 22 | $get_tok = $tok; | ||
| 23 | } else { | ||
| 24 | $i = 0; | ||
| 25 | $get_tok = sub { @{$tok->[$i++] || [$parser->{end}, undef]}; }; | ||
| 26 | } | ||
| 27 | |||
| 28 | my ($next_tok, $next_val) = $get_tok->(); | ||
| 29 | print "Read token ", $parser->dump_sym($next_tok), "\n"; | ||
| 30 | |||
| 31 | my @state_stack = ($parser->{init_state}); | ||
| 32 | my @sym_stack; | ||
| 33 | my @val_stack; | ||
| 34 | |||
| 35 | my $state; | ||
| 36 | my $recovery_state = 0; | ||
| 37 | my $post_error_tok; | ||
| 38 | my $post_error_val; | ||
| 39 | my $using_post_error = 0; | ||
| 40 | |||
| 41 | while (1) { | ||
| 42 | $state = $state_stack[-1]; | ||
| 43 | |||
| 44 | print "\n"; | ||
| 45 | print "Syms: ", join(" ", map { $parser->dump_sym($_) } @sym_stack), "\n"; | ||
| 46 | print "Vals: ", join(" ", map { (defined) ? $_ : '<undef>' } @val_stack), "\n"; | ||
| 47 | print "States: ", join(" ", map { $_->{id}; } @state_stack ), " on "; | ||
| 48 | print $parser->dump_sym($next_tok), ": "; | ||
| 49 | |||
| 50 | my $action = $state->{actions}->[$next_tok]; | ||
| 51 | if ($recovery_state == 2) { | ||
| 52 | # If we can't use the next token, or the next token is the end | ||
| 53 | # token and the action is a shift (will this happen?), then | ||
| 54 | # eat tokens until we can do something. Or we run past the end. | ||
| 55 | if (!defined $action) { | ||
| 56 | if ($next_tok == $end) { | ||
| 57 | print "Unable to recover from error by EOF\n"; | ||
| 58 | return undef; | ||
| 59 | } | ||
| 60 | print "eat(".$parser->dump_sym($next_tok).")\n"; | ||
| 61 | ($next_tok, $next_val) = $get_tok->(); | ||
| 62 | print "Read token ", $parser->dump_sym($next_tok), "\n"; | ||
| 63 | next; | ||
| 64 | } else { | ||
| 65 | $recovery_state = 0; | ||
| 66 | print "recover(".$parser->dump_sym($next_tok)."), "; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | if (!defined $action) { | ||
| 71 | print "ERROR!!!\n"; | ||
| 72 | |||
| 73 | # Start recovering from error by popping states until one is | ||
| 74 | # reached that can do something on an error token | ||
| 75 | while (!defined $state->{actions}->[$error] && @state_stack > 0) { | ||
| 76 | print "Popping state $state_stack[-1]->{id}\n"; | ||
| 77 | pop(@state_stack); | ||
| 78 | pop(@sym_stack); | ||
| 79 | pop(@val_stack); | ||
| 80 | $state = $state_stack[-1]; | ||
| 81 | } | ||
| 82 | |||
| 83 | if (defined $post_error_tok && $post_error_tok == $end) { | ||
| 84 | print "Second attempt to error out at EOF, cannot catch\n"; | ||
| 85 | return undef; | ||
| 86 | } | ||
| 87 | |||
| 88 | # If we run out of states, it's an uncaught error | ||
| 89 | if (@state_stack == 0) { | ||
| 90 | print "UNCAUGHT ERROR\n"; | ||
| 91 | return undef; | ||
| 92 | } | ||
| 93 | |||
| 94 | # Otherwise, pretend we saw an error token. We'll also enter an | ||
| 95 | # error recovering state where we keep consuming input until we | ||
| 96 | # can perform another action. | ||
| 97 | $post_error_tok = $next_tok; | ||
| 98 | $post_error_val = $next_val; | ||
| 99 | $next_tok = $error; | ||
| 100 | # $next_val = undef; | ||
| 101 | $next_val = '<error>'; | ||
| 102 | $recovery_state = 1; # recovering, unused error | ||
| 103 | |||
| 104 | } elsif (ref $action) { | ||
| 105 | # Reduce | ||
| 106 | print "reduce ", $parser->dump_rule($action->[0]), "\n"; | ||
| 107 | # print " because ", ($parser->explain($state_stack[-1]{id}, | ||
| 108 | # $next_tok, | ||
| 109 | # 'reduce'))[1], "\n"; | ||
| 110 | |||
| 111 | my $v1; | ||
| 112 | if (defined(my $code = $parser->{rule_code}->{$action->[0]})) { | ||
| 113 | print "Executing code..."; | ||
| 114 | # @::v = @val_stack[-$action->[2] .. -1]; | ||
| 115 | # $v1 = eval($$code); | ||
| 116 | $v1 = $code->(@val_stack[-$action->[2] .. -1]); | ||
| 117 | print "value is $v1, $@\n"; | ||
| 118 | } else { | ||
| 119 | $v1 = $val_stack[-$action->[2]]; | ||
| 120 | } | ||
| 121 | $#state_stack -= $action->[2]; | ||
| 122 | $#sym_stack -= $action->[2]; | ||
| 123 | $#val_stack -= $action->[2]; | ||
| 124 | $state = $state_stack[-1]; | ||
| 125 | |||
| 126 | if ($action->[1] == $parser->{startsym}) { | ||
| 127 | print "Parse completed successfully.\n"; | ||
| 128 | print "Final value: $v1\n"; | ||
| 129 | return 1; | ||
| 130 | } | ||
| 131 | |||
| 132 | push(@sym_stack, $action->[1]); | ||
| 133 | push(@val_stack, $v1); | ||
| 134 | my $statenum = $state->{actions}->[$action->[1]]; | ||
| 135 | push(@state_stack, $parser->{states}->[$statenum]); | ||
| 136 | } else { | ||
| 137 | # shift | ||
| 138 | print "shift, goto state $parser->{states}->[$action]{id}\n"; | ||
| 139 | # print " because ", ($parser->explain($state_stack[-1]{id}, | ||
| 140 | # $next_tok, | ||
| 141 | # 'shift'))[1], "\n"; | ||
| 142 | # print " and if it helps, state $state_stack[-1]{id} kernel is ", | ||
| 143 | # $parser->dump_kernel($state_stack[-1]), "\n"; | ||
| 144 | push(@sym_stack, $next_tok); | ||
| 145 | push(@val_stack, $next_val); | ||
| 146 | push(@state_stack, $parser->{states}->[$action]); | ||
| 147 | if ($next_tok == $error) { | ||
| 148 | $recovery_state = 2; # recovering, used error | ||
| 149 | ($next_tok, $next_val) = ($post_error_tok, $post_error_val); | ||
| 150 | } else { | ||
| 151 | if ($next_tok == $end) { | ||
| 152 | print "Unable to recover from error by EOF\n"; | ||
| 153 | return undef; | ||
| 154 | } | ||
| 155 | ($next_tok, $next_val) = $get_tok->(); | ||
| 156 | } | ||
| 157 | print "Read token ", $parser->dump_sym($next_tok), "\n"; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | sub new { | ||
| 163 | my ($class, $parser, $tok, $callbacks) = @_; | ||
| 164 | |||
| 165 | my $tokenizer = $tok; | ||
| 166 | if (ref $tokenizer ne 'CODE') { | ||
| 167 | my $i = 0; | ||
| 168 | $tokenizer = sub { @{$tok->[$i++] || [$parser->{end}, undef]}; }; | ||
| 169 | } | ||
| 170 | |||
| 171 | my $self = { parser => $parser, | ||
| 172 | callbacks => $callbacks, | ||
| 173 | states => [ $parser->{init_state} ], | ||
| 174 | symbols => [], | ||
| 175 | values => [], | ||
| 176 | tokenizer => $tokenizer, | ||
| 177 | recovery_state => 0, | ||
| 178 | using_post_error => 0, | ||
| 179 | next_tok => undef, | ||
| 180 | next_val => undef, | ||
| 181 | post_error_tok => undef, | ||
| 182 | post_error_val => undef, | ||
| 183 | init => 0, | ||
| 184 | }; | ||
| 185 | bless $self, (ref $class || $class); | ||
| 186 | } | ||
| 187 | |||
| 188 | # Callbacks: | ||
| 189 | # read(token) | ||
| 190 | # discard(token) | ||
| 191 | # recover(token) | ||
| 192 | # error(token) | ||
| 193 | # popstate(state,symbol,value) | ||
| 194 | # fatal(message) | ||
| 195 | # exec(code,value) | ||
| 196 | # reduce(action) | ||
| 197 | # done(action) | ||
| 198 | # shift(action,token) | ||
| 199 | # | ||
| 200 | sub parse { | ||
| 201 | my Parse::YALALR::Run $self = shift; | ||
| 202 | my Parse::YALALR::Parser $parser = $self->{parser}; | ||
| 203 | |||
| 204 | my $error = $parser->{error}; | ||
| 205 | my $end = $parser->{end}; | ||
| 206 | my $get_tok = $self->{tokenizer}; | ||
| 207 | |||
| 208 | my $state_stack = $self->{states}; | ||
| 209 | my $sym_stack = $self->{symbols}; | ||
| 210 | my $val_stack = $self->{values}; | ||
| 211 | |||
| 212 | while (1) { | ||
| 213 | my $state = $state_stack->[-1]; | ||
| 214 | |||
| 215 | my ($next_tok, $next_val); | ||
| 216 | |||
| 217 | if (!defined $self->{next_tok}) { | ||
| 218 | ($self->{next_tok}, $self->{next_val}) = $get_tok->(); | ||
| 219 | return if !$self->{callbacks}->{read}->($self, $self->{next_tok}); | ||
| 220 | } | ||
| 221 | |||
| 222 | ($next_tok, $next_val) = ($self->{next_tok}, $self->{next_val}); | ||
| 223 | |||
| 224 | my $action = $state->{actions}->[$next_tok]; | ||
| 225 | print "ACTION := ".(defined $action ? $action : "<undef>")."\n"; | ||
| 226 | if (!defined $action && $self->{recovery_state} == 1) { | ||
| 227 | # If we can't use the next token, or the next token is the end | ||
| 228 | # token and the action is a shift (will this happen?), then | ||
| 229 | # eat tokens until we can do something. Or we run past the end. | ||
| 230 | if (!defined $action) { | ||
| 231 | if ($next_tok == $end) { | ||
| 232 | die "Unable to recover from error by EOF\n"; | ||
| 233 | } | ||
| 234 | return if !$self->{callbacks}->{discard}->($self, $next_tok); | ||
| 235 | undef $self->{next_tok}; | ||
| 236 | next; | ||
| 237 | } else { | ||
| 238 | $self->{recovery_state} = 0; | ||
| 239 | return if !$self->{callbacks}->{recover}->($self, $next_tok); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | if (!defined $action) { | ||
| 244 | print "A\n"; | ||
| 245 | my $continue = $self->{callbacks}->{error}->($self, $next_tok); | ||
| 246 | print "B\n"; | ||
| 247 | |||
| 248 | # Start recovering from error by popping states until one is | ||
| 249 | # reached that can do something on an error token | ||
| 250 | print "error action: ".($state->{actions}->[$error] || "<undef>")."\n"; | ||
| 251 | print "sym stack: ".(0+@$sym_stack)."\n"; | ||
| 252 | print "next tok: ".$parser->dump_sym($next_tok)."\n"; | ||
| 253 | |||
| 254 | while (!defined $state->{actions}->[$error] && @$sym_stack > 0) { | ||
| 255 | print "C\n"; | ||
| 256 | return if !$self->{callbacks}->{popstate}->($self, | ||
| 257 | $state_stack->[-1], | ||
| 258 | $sym_stack->[-1], | ||
| 259 | $val_stack->[-1]); | ||
| 260 | print "D\n"; | ||
| 261 | pop(@$state_stack); | ||
| 262 | pop(@$sym_stack); | ||
| 263 | pop(@$val_stack); | ||
| 264 | $state = $state_stack->[-1]; | ||
| 265 | } | ||
| 266 | |||
| 267 | print "E\n"; | ||
| 268 | if (defined $self->{post_error_tok} | ||
| 269 | && $self->{post_error_tok} == $end) | ||
| 270 | { | ||
| 271 | print "F\n"; | ||
| 272 | $self->{callbacks}->{fatal}->($self, "second attempt to error out at EOF"); | ||
| 273 | return; | ||
| 274 | } | ||
| 275 | |||
| 276 | print "G\n"; | ||
| 277 | # If we run out of symbols, it's an uncaught error | ||
| 278 | if (@$sym_stack == 0) { | ||
| 279 | $self->{callbacks}->{fatal}->($self, "uncaught error"); | ||
| 280 | return; | ||
| 281 | } | ||
| 282 | |||
| 283 | print "H\n"; | ||
| 284 | # Otherwise, pretend we saw an error token. We'll also enter an | ||
| 285 | # error recovering state where we keep consuming input until we | ||
| 286 | # can perform another action. | ||
| 287 | $self->{post_error_tok} = $next_tok; | ||
| 288 | $self->{post_error_val} = $next_val; | ||
| 289 | $next_tok = $error; | ||
| 290 | $next_val = '<error>'; | ||
| 291 | $self->{recovery_state} = 1; # recovering, unused error | ||
| 292 | print "I\n"; | ||
| 293 | return if !$continue; | ||
| 294 | |||
| 295 | } elsif (ref $action) { | ||
| 296 | # Reduce | ||
| 297 | my $v1; | ||
| 298 | if (defined(my $code = $parser->{rule_code}->{$action->[0]})) { | ||
| 299 | $v1 = $code->(@$val_stack[-$action->[2] .. -1]); | ||
| 300 | $self->{callbacks}->{exec}->($self, $code, $v1); | ||
| 301 | } else { | ||
| 302 | $v1 = $val_stack->[-$action->[2]]; | ||
| 303 | $self->{callbacks}->{exec}->($self, undef, $v1); | ||
| 304 | } | ||
| 305 | my $oldstate = $state_stack->[-1]; | ||
| 306 | $#$state_stack -= $action->[2]; | ||
| 307 | $#$sym_stack -= $action->[2]; | ||
| 308 | $#$val_stack -= $action->[2]; | ||
| 309 | $state = $state_stack->[-1]; | ||
| 310 | |||
| 311 | if ($action->[1] == $parser->{startsym}) { | ||
| 312 | $self->{callbacks}->{done}->($self, $action); | ||
| 313 | return 1; | ||
| 314 | } | ||
| 315 | |||
| 316 | push(@$sym_stack, $action->[1]); | ||
| 317 | push(@$val_stack, $v1); | ||
| 318 | my $statenum = $state->{actions}->[$action->[1]]; | ||
| 319 | push(@$state_stack, $parser->{states}->[$statenum]); | ||
| 320 | return if !$self->{callbacks}->{reduce}->($self, $next_tok, $oldstate, $action, $v1, $statenum); | ||
| 321 | } else { | ||
| 322 | # shift, goto state $parser->{states}->[$action]{id} | ||
| 323 | push(@$sym_stack, $next_tok); | ||
| 324 | push(@$val_stack, $next_val); | ||
| 325 | my $tostate = $parser->{states}->[$action]; | ||
| 326 | my $oldstate = $state_stack->[-1]; | ||
| 327 | push(@$state_stack, $tostate); | ||
| 328 | |||
| 329 | my $continue = | ||
| 330 | $self->{callbacks}->{shift}->($self, $oldstate, $next_tok, $tostate); | ||
| 331 | |||
| 332 | if ($next_tok == $error) { | ||
| 333 | $self->{recovery_state} = 2; # recovering, used error | ||
| 334 | ($next_tok, $next_val) = | ||
| 335 | ($self->{post_error_tok}, $self->{post_error_val}); | ||
| 336 | return if !$self->{callbacks}->{recover}->($self, $action); | ||
| 337 | |||
| 338 | } else { | ||
| 339 | if ($next_tok == $end) { | ||
| 340 | print "Unable to recover from error by EOF\n"; | ||
| 341 | return undef; | ||
| 342 | } | ||
| 343 | undef $self->{next_tok}; | ||
| 344 | } | ||
| 345 | |||
| 346 | return if !$continue; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | 1; | ||
| 352 | __END__ |