1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/racc/parser.rb: synchronize with Racc 1.4.4.

* ext/racc/cparse/cparse.c: ditto.
* ext/racc/cparse/cparse.c (parse_main): should abort when the length of LR state stack <=1, not ==0.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4890 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
aamine 2003-11-03 13:55:01 +00:00
parent a9da115662
commit 7bf9e6d2a1
3 changed files with 272 additions and 282 deletions

View file

@ -1,47 +1,51 @@
#
# parser.rb
#
# Copyright (c) 1999-2002 Minero Aoki <aamine@loveruby.net>
# Copyright (c) 1999-2003 Minero Aoki <aamine@loveruby.net>
#
# This program is free software.
# You can distribute/modify this program under the same terms of ruby.
# This program is free software.
# You can distribute/modify this program under the same terms of ruby.
#
# As a special exception, when this code is copied by Racc
# into a Racc output file, you may use that output file
# without restriction.
# As a special exception, when this code is copied by Racc
# into a Racc output file, you may use that output file
# without restriction.
#
# $Id$
# $raccId: parser.rb,v 1.4 2003/11/03 13:41:47 aamine Exp $
#
unless defined?(NotImplementedError)
NotImplementedError = NotImplementError
end
module Racc
class ParseError < StandardError; end
end
unless defined? ::ParseError then
unless defined?(::ParseError)
ParseError = Racc::ParseError
end
module Racc
unless defined? Racc_No_Extentions then
unless defined?(Racc_No_Extentions)
Racc_No_Extentions = false
end
class Parser
Racc_Runtime_Version = '1.4.2'
Racc_Runtime_Revision = '$Revision$'.split(/\s+/)[1]
Racc_Runtime_Version = '1.4.4'
Racc_Runtime_Revision = '$raccRevision: 1.4 $'.split[1]
Racc_Runtime_Core_Version_R = '1.4.2'
Racc_Runtime_Core_Revision_R = '$Revision$'.split(/\s+/)[1]
Racc_Runtime_Core_Version_R = '1.4.4'
Racc_Runtime_Core_Revision_R = '$raccRevision: 1.4 $'.split[1]
begin
require 'racc/cparse'
# Racc_Runtime_Core_Version_C = (defined in extention)
Racc_Runtime_Core_Revision_C = Racc_Runtime_Core_Id_C.split(/\s+/)[2]
unless new.respond_to? :_racc_do_parse_c, true then
Racc_Runtime_Core_Revision_C = Racc_Runtime_Core_Id_C.split[2]
unless new.respond_to?(:_racc_do_parse_c, true)
raise LoadError, 'old cparse.so'
end
if Racc_No_Extentions then
if Racc_No_Extentions
raise LoadError, 'selecting ruby version of racc runtime core'
end
@ -58,36 +62,26 @@ module Racc
Racc_Runtime_Type = 'ruby'
end
def self.racc_runtime_type
def Parser.racc_runtime_type
Racc_Runtime_Type
end
private
def _racc_setup
t = self.class
unless t::Racc_debug_parser then
@yydebug = false
end
@yydebug = false unless defined? @yydebug
if @yydebug then
@racc_debug_out = $stderr unless defined? @racc_debug_out
@yydebug = false unless self.class::Racc_debug_parser
@yydebug = false unless defined?(@yydebug)
if @yydebug
@racc_debug_out = $stderr unless defined?(@racc_debug_out)
@racc_debug_out ||= $stderr
end
arg = t::Racc_arg
if arg.size < 14 then
arg[13] = true
end
arg = self.class::Racc_arg
arg[13] = true if arg.size < 14
arg
end
def _racc_init_sysvars
@racc_state = [ 0 ]
@racc_state = [0]
@racc_tstack = []
@racc_vstack = []
@ -100,13 +94,12 @@ module Racc
@racc_error_status = 0
end
###
### do_parse
###
def do_parse
__send__ Racc_Main_Parsing_Routine, _racc_setup(), false
__send__(Racc_Main_Parsing_Routine, _racc_setup(), false)
end
def next_token
@ -123,48 +116,43 @@ module Racc
tok = act = i = nil
nerr = 0
catch( :racc_end_parse ) {
while true do
if i = action_pointer[ @racc_state[-1] ] then
if @racc_read_next then
if @racc_t != 0 then # not EOF
tok, @racc_val = next_token()
unless tok then # EOF
@racc_t = 0
else
@racc_t = (token_table[tok] or 1) # error token
catch(:racc_end_parse) {
while true
if i = action_pointer[@racc_state[-1]]
if @racc_read_next
if @racc_t != 0 # not EOF
tok, @racc_val = next_token()
unless tok # EOF
@racc_t = 0
else
@racc_t = (token_table[tok] or 1) # error token
end
racc_read_token(@racc_t, tok, @racc_val) if @yydebug
@racc_read_next = false
end
racc_read_token( @racc_t, tok, @racc_val ) if @yydebug
@racc_read_next = false
end
end
i += @racc_t
if i >= 0 and act = action_table[i] and
action_check[i] == @racc_state[-1] then
;
i += @racc_t
unless i >= 0 and
act = action_table[i] and
action_check[i] == @racc_state[-1]
act = action_default[@racc_state[-1]]
end
else
act = action_default[ @racc_state[-1] ]
act = action_default[@racc_state[-1]]
end
while act = _racc_evalact(act, arg)
;
end
else
act = action_default[ @racc_state[-1] ]
end
while act = _racc_evalact( act, arg ) do end
end
}
end
###
### yyparse
###
def yyparse( recv, mid )
__send__ Racc_YY_Parse_Method, recv, mid, _racc_setup(), true
__send__(Racc_YY_Parse_Method, recv, mid, _racc_setup(), true)
end
def _racc_yyparse_rb( recv, mid, arg, c_debug )
@ -179,16 +167,15 @@ module Racc
i = nil
nerr = 0
catch( :racc_end_parse ) {
until i = action_pointer[ @racc_state[-1] ] do
while act = _racc_evalact(
action_default[ @racc_state[-1] ], arg ) do end
catch(:racc_end_parse) {
until i = action_pointer[@racc_state[-1]]
while act = _racc_evalact(action_default[@racc_state[-1]], arg)
;
end
end
recv.__send__( mid ) do |tok, val|
recv.__send__(mid) do |tok, val|
# $stderr.puts "rd: tok=#{tok}, val=#{val}"
unless tok then
unless tok
@racc_t = 0
else
@racc_t = (token_table[tok] or 1) # error token
@ -197,38 +184,40 @@ module Racc
@racc_read_next = false
i += @racc_t
if i >= 0 and act = action_table[i] and
action_check[i] == @racc_state[-1] then
# $stderr.puts "01: act=#{act}"
else
act = action_default[ @racc_state[-1] ]
unless i >= 0 and
act = action_table[i] and
action_check[i] == @racc_state[-1]
act = action_default[@racc_state[-1]]
# $stderr.puts "02: act=#{act}"
# $stderr.puts "curstate=#{@racc_state[-1]}"
else
# $stderr.puts "01: act=#{act}"
end
while act = _racc_evalact( act, arg ) do end
while act = _racc_evalact(act, arg)
;
end
while not (i = action_pointer[ @racc_state[-1] ]) or
while not (i = action_pointer[@racc_state[-1]]) or
not @racc_read_next or
@racc_t == 0 do # $
if i and i += @racc_t and
i >= 0 and
act = action_table[i] and
action_check[i] == @racc_state[-1] then
# $stderr.puts "03: act=#{act}"
;
else
@racc_t == 0 # $
unless i and i += @racc_t and
i >= 0 and
act = action_table[i] and
action_check[i] == @racc_state[-1]
act = action_default[@racc_state[-1]]
# $stderr.puts "04: act=#{act}"
act = action_default[ @racc_state[-1] ]
else
# $stderr.puts "03: act=#{act}"
end
while act = _racc_evalact(act, arg)
;
end
while act = _racc_evalact( act, arg ) do end
end
end
}
end
###
### common
###
@ -241,34 +230,30 @@ module Racc
reduce_n, use_result, * = arg
nerr = 0 # tmp
if act > 0 and act < shift_n then
if act > 0 and act < shift_n
#
# shift
#
if @racc_error_status > 0 then
if @racc_error_status > 0
@racc_error_status -= 1 unless @racc_t == 1 # error token
end
@racc_vstack.push @racc_val
@racc_state.push act
@racc_read_next = true
if @yydebug then
if @yydebug
@racc_tstack.push @racc_t
racc_shift( @racc_t, @racc_tstack, @racc_vstack )
racc_shift @racc_t, @racc_tstack, @racc_vstack
end
elsif act < 0 and act > -reduce_n then
elsif act < 0 and act > -reduce_n
#
# reduce
#
code = catch( :racc_jump ) {
@racc_state.push _racc_do_reduce( arg, act )
code = catch(:racc_jump) {
@racc_state.push _racc_do_reduce(arg, act)
false
}
if code then
if code
case code
when 1 # yyerror
@racc_user_yyerror = true # user_yyerror
@ -280,60 +265,56 @@ nerr = 0 # tmp
end
end
elsif act == shift_n then
elsif act == shift_n
#
# accept
#
racc_accept if @yydebug
throw :racc_end_parse, @racc_vstack[0]
elsif act == -reduce_n then
elsif act == -reduce_n
#
# error
#
case @racc_error_status
when 0
unless arg[21] then # user_yyerror
unless arg[21] # user_yyerror
nerr += 1
on_error @racc_t, @racc_val, @racc_vstack
end
when 3
if @racc_t == 0 then # is $
if @racc_t == 0 # is $
throw :racc_end_parse, nil
end
@racc_read_next = true
end
@racc_user_yyerror = false
@racc_error_status = 3
while true do
if i = action_pointer[ @racc_state[-1] ] then
while true
if i = action_pointer[@racc_state[-1]]
i += 1 # error token
if i >= 0 and
(act = action_table[i]) and
action_check[i] == @racc_state[-1] then
break
if i >= 0 and
(act = action_table[i]) and
action_check[i] == @racc_state[-1]
break
end
end
throw :racc_end_parse, nil if @racc_state.size < 2
throw :racc_end_parse, nil if @racc_state.size <= 1
@racc_state.pop
@racc_vstack.pop
if @yydebug then
if @yydebug
@racc_tstack.pop
racc_e_pop( @racc_state, @racc_tstack, @racc_vstack )
racc_e_pop @racc_state, @racc_tstack, @racc_vstack
end
end
return act
else
raise RuntimeError, "[Racc Bug] unknown action #{act.inspect}"
end
racc_next_state( @racc_state[-1], @racc_state ) if @yydebug
racc_next_state(@racc_state[-1], @racc_state) if @yydebug
nil
end
@ -353,36 +334,35 @@ nerr = 0 # tmp
method_id = reduce_table[i+2]
void_array = []
tmp_t = tstack[ -len, len ] if @yydebug
tmp_v = vstack[ -len, len ]
tstack[ -len, len ] = void_array if @yydebug
vstack[ -len, len ] = void_array
state[ -len, len ] = void_array
tmp_t = tstack[-len, len] if @yydebug
tmp_v = vstack[-len, len]
tstack[-len, len] = void_array if @yydebug
vstack[-len, len] = void_array
state[-len, len] = void_array
# tstack must be updated AFTER method call
if use_result then
if use_result
vstack.push __send__(method_id, tmp_v, vstack, tmp_v[0])
else
vstack.push __send__(method_id, tmp_v, vstack)
end
tstack.push reduce_to
racc_reduce( tmp_t, reduce_to, tstack, vstack ) if @yydebug
racc_reduce(tmp_t, reduce_to, tstack, vstack) if @yydebug
k1 = reduce_to - nt_base
if i = goto_pointer[ k1 ] then
if i = goto_pointer[k1]
i += state[-1]
if i >= 0 and (curstate = goto_table[i]) and goto_check[i] == k1 then
if i >= 0 and (curstate = goto_table[i]) and goto_check[i] == k1
return curstate
end
end
goto_default[ k1 ]
goto_default[k1]
end
def on_error( t, val, vstack )
raise ParseError, sprintf("\nparse error on value %s (%s)",
val.inspect,
token_to_str(t) || '?')
val.inspect, token_to_str(t) || '?')
end
def yyerror
@ -397,8 +377,9 @@ nerr = 0 # tmp
@racc_error_status = 0
end
#
# for debugging output
#
def racc_read_token( t, tok, val )
@racc_debug_out.print 'read '
@ -416,7 +397,7 @@ nerr = 0 # tmp
def racc_reduce( toks, sim, tstack, vstack )
out = @racc_debug_out
out.print 'reduce '
if toks.empty? then
if toks.empty?
out.print ' <none>'
else
toks.each {|t| out.print ' ', racc_token2str(t) }
@ -463,7 +444,7 @@ nerr = 0 # tmp
def racc_token2str( tok )
self.class::Racc_token_to_s_table[tok] or
raise RuntimeError, "[Racc Bug] can't convert token #{tok} to string"
raise RuntimeError, "[Racc Bug] can't convert token #{tok} to string"
end
def token_to_str( t )