Skip to content

Commit

Permalink
- Fixed some tracking of lexical state. Fixes bug #249.
Browse files Browse the repository at this point in the history
Also improved debugging against ruby 2.4 output by a lot
[git-p4: depot-paths = "//src/ruby_parser/dev/": change = 11363]
  • Loading branch information
zenspider committed Jun 16, 2017
1 parent c0cad19 commit 3490521
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
4 changes: 4 additions & 0 deletions lib/rp_extensions.rb
Expand Up @@ -38,6 +38,10 @@ class String
# reduces tIDENTIFIER.

attr_accessor :lineno

def clean_caller
self.sub(File.dirname(__FILE__), ".").sub(/:in.*/, "")
end if $DEBUG
end

require "sexp"
Expand Down
22 changes: 19 additions & 3 deletions lib/ruby_lexer.rb
Expand Up @@ -92,6 +92,17 @@ class RubyLexer
attr_accessor :string_buffer
attr_accessor :string_nest

if $DEBUG then
alias lex_state= lex_state=
def lex_state=o
return if @lex_state == o
c = caller.first
c = caller[1] if c =~ /\bresult\b/
warn "lex_state: %p -> %p from %s" % [@lex_state, o, c.clean_caller]
@lex_state = o
end
end

# Last token read via next_token.
attr_accessor :token

Expand All @@ -105,6 +116,10 @@ class RubyLexer

def initialize v = 18
self.version = v
@lex_state = :expr_none

self.cmdarg = RubyParserStuff::StackState.new(:cmdarg, $DEBUG)
self.cond = RubyParserStuff::StackState.new(:cond, $DEBUG)

reset
end
Expand Down Expand Up @@ -559,6 +574,7 @@ def process_paren text

self.paren_nest += 1

# TODO: add :expr_label to :expr_beg (set in expr_result below)
return expr_result(token, "(")
end

Expand Down Expand Up @@ -941,7 +957,7 @@ def reset
self.brace_nest = 0
self.command_start = true
self.comments = []
self.lex_state = nil
self.lex_state = :expr_none
self.lex_strterm = nil
self.lineno = 1
self.lpar_beg = nil
Expand All @@ -951,8 +967,8 @@ def reset
self.token = nil
self.extra_lineno = 0

self.cmdarg = RubyParserStuff::StackState.new(:cmdarg)
self.cond = RubyParserStuff::StackState.new(:cond)
self.cmdarg.reset
self.cond.reset
end

def result lex_state, token, text # :nodoc:
Expand Down
10 changes: 8 additions & 2 deletions lib/ruby_parser.yy
Expand Up @@ -999,14 +999,20 @@ rule
{
debug20 13, val, result
}
| tLPAREN_ARG expr
| tLPAREN_ARG
{
result = self.lexer.cmdarg.stack.dup
lexer.cmdarg.stack.replace [false] # TODO add api for these
}
expr
{
lexer.lex_state = :expr_endarg
}
rparen
{
warning "(...) interpreted as grouped expression"
result = val[1]
lexer.cmdarg.stack.replace val[1]
result = val[2]
}
| tLPAREN compstmt tRPAREN
{
Expand Down
18 changes: 12 additions & 6 deletions lib/ruby_parser_extras.rb
Expand Up @@ -1322,23 +1322,27 @@ class StackState
attr_reader :stack
attr_accessor :debug

def initialize(name)
def initialize name, debug=false
@name = name
@stack = [false]
@debug = false
@debug = debug
end

def reset
@stack = [false]
warn "#{name}_stack(set): 0" if debug
end

def inspect
"StackState(#{@name}, #{@stack.inspect})"
end

def is_in_state
p :stack_is_in_state => [name, @stack.last, caller.first] if debug
@stack.last
end

def lexpop
p :stack_lexpop => caller.first if debug
warn "#{name}_stack.lexpop" if debug
raise if @stack.size == 0
a = @stack.pop
b = @stack.pop
Expand All @@ -1347,14 +1351,16 @@ def lexpop

def pop
r = @stack.pop
p :stack_pop => [name, r, @stack, caller.first] if debug
warn "#{name}_stack.pop" if debug
@stack.push false if @stack.size == 0
r
end

def push val
@stack.push val
p :stack_push => [name, @stack, caller.first] if debug
c = caller.first
c = caller[1] if c =~ /expr_result/
warn "#{name}_stack(push): #{val} at line #{c.clean_caller}" if debug
nil
end

Expand Down
14 changes: 14 additions & 0 deletions test/test_ruby_parser.rb
Expand Up @@ -3256,6 +3256,20 @@ def test_iter_array_curly

assert_parse rb, pt
end

def test_bug_249
rb = "mount (Class.new do\ndef initialize\nend\n end).new, :at => 'endpoint'"
pt = s(:call, nil, :mount,
s(:call,
s(:iter,
s(:call, s(:const, :Class), :new),
0,
s(:defn, :initialize, s(:args), s(:nil))),
:new),
s(:hash, s(:lit, :at), s(:str, "endpoint")))

assert_parse rb, pt
end
end

module TestRubyParserShared22Plus
Expand Down

0 comments on commit 3490521

Please sign in to comment.