Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove 'tern' stack state, which was never read. #105

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 8 additions & 25 deletions lib/ruby18_parser.y
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -241,13 +241,9 @@ rule
self.env.extend(:dynamic) self.env.extend(:dynamic)
result = self.lexer.lineno result = self.lexer.lineno
} }
opt_block_var opt_block_var compstmt tRCURLY
{ {
result = self.env.dynamic.keys result = new_iter nil, val[2], val[3]
}
compstmt tRCURLY
{
result = new_iter nil, val[2], val[4]
self.env.unextend self.env.unextend
} }


Expand Down Expand Up @@ -699,7 +695,6 @@ rule
} }
| arg tEH arg tCOLON arg | arg tEH arg tCOLON arg
{ {
lexer.tern.pop
result = s(:if, val[0], val[2], val[4]) result = s(:if, val[0], val[2], val[4])
} }
| primary | primary
Expand Down Expand Up @@ -1286,14 +1281,10 @@ rule
self.env.extend :dynamic self.env.extend :dynamic
result = self.lexer.lineno result = self.lexer.lineno
} }
opt_block_var opt_block_var compstmt kEND
{
result = self.env.dynamic.keys
}
compstmt kEND
{ {
vars = val[2] vars = val[2]
body = val[4] body = val[3]
result = new_iter nil, vars, body result = new_iter nil, vars, body
result.line = val[1] result.line = val[1]


Expand Down Expand Up @@ -1350,15 +1341,11 @@ rule
self.env.extend :dynamic self.env.extend :dynamic
result = self.lexer.lineno result = self.lexer.lineno
} }
opt_block_var opt_block_var compstmt tRCURLY
{
result = self.env.dynamic.keys
}
compstmt tRCURLY
{ {
# REFACTOR # REFACTOR
args = val[2] args = val[2]
body = val[4] body = val[3]
result = new_iter nil, args, body result = new_iter nil, args, body
self.env.unextend self.env.unextend
result.line = val[1] result.line = val[1]
Expand All @@ -1368,14 +1355,10 @@ rule
self.env.extend :dynamic self.env.extend :dynamic
result = self.lexer.lineno result = self.lexer.lineno
} }
opt_block_var opt_block_var compstmt kEND
{
result = self.env.dynamic.keys
}
compstmt kEND
{ {
args = val[2] args = val[2]
body = val[4] body = val[3]
result = new_iter nil, args, body result = new_iter nil, args, body
self.env.unextend self.env.unextend
result.line = val[1] result.line = val[1]
Expand Down
39 changes: 11 additions & 28 deletions lib/ruby19_parser.y
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -253,13 +253,9 @@ rule
self.env.extend(:dynamic) self.env.extend(:dynamic)
result = self.lexer.lineno result = self.lexer.lineno
} }
opt_block_param opt_block_param compstmt tRCURLY
{ {
result = self.env.dynamic.keys result = new_iter nil, val[2], val[3]
}
compstmt tRCURLY
{
result = new_iter nil, val[2], val[4]
result.line = val[1] result.line = val[1]


self.env.unextend self.env.unextend
Expand Down Expand Up @@ -764,7 +760,6 @@ rule
} }
| arg tEH arg opt_nl tCOLON arg | arg tEH arg opt_nl tCOLON arg
{ {
lexer.tern.pop
result = s(:if, val[0], val[2], val[5]) result = s(:if, val[0], val[2], val[5])
} }
| primary | primary
Expand Down Expand Up @@ -1399,14 +1394,10 @@ rule
self.env.extend :dynamic self.env.extend :dynamic
result = self.lexer.lineno result = self.lexer.lineno
} }
opt_block_param opt_block_param compstmt kEND
{
result = self.env.dynamic.keys
}
compstmt kEND
{ {
args = val[2] args = val[2]
body = val[4] body = val[3]
result = new_iter nil, args, body result = new_iter nil, args, body
result.line = val[1] result.line = val[1]


Expand Down Expand Up @@ -1480,13 +1471,9 @@ rule
self.env.extend :dynamic self.env.extend :dynamic
result = self.lexer.lineno result = self.lexer.lineno
} }
opt_block_param opt_block_param compstmt tRCURLY
{ {
result = self.env.dynamic.keys _, line, args, body, _ = val
}
compstmt tRCURLY
{
_, line, args, _, body, _ = val


result = new_iter nil, args, body result = new_iter nil, args, body
result.line = line result.line = line
Expand All @@ -1498,13 +1485,9 @@ rule
self.env.extend :dynamic self.env.extend :dynamic
result = self.lexer.lineno result = self.lexer.lineno
} }
opt_block_param opt_block_param compstmt kEND
{
result = self.env.dynamic.keys
}
compstmt kEND
{ {
_, line, args, _, body, _ = val _, line, args, body, _ = val


result = new_iter nil, args, body result = new_iter nil, args, body
result.line = line result.line = line
Expand Down Expand Up @@ -1769,9 +1752,9 @@ keyword_variable: kNIL { result = s(:nil) }
| kFALSE { result = s(:false) } | kFALSE { result = s(:false) }
| k__FILE__ { result = s(:str, self.file) } | k__FILE__ { result = s(:str, self.file) }
| k__LINE__ { result = s(:lit, lexer.src.current_line) } | k__LINE__ { result = s(:lit, lexer.src.current_line) }
| k__ENCODING__ | k__ENCODING__
{ {
result = result =
if defined? Encoding then if defined? Encoding then
s(:const, Encoding::UTF_8) s(:const, Encoding::UTF_8)
else else
Expand Down
18 changes: 1 addition & 17 deletions lib/ruby_lexer.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class RubyLexer
attr_accessor :command_start attr_accessor :command_start
attr_accessor :cmdarg attr_accessor :cmdarg
attr_accessor :cond attr_accessor :cond
attr_accessor :tern # TODO: rename ternary damnit... wtf
attr_accessor :nest attr_accessor :nest


ESC_RE = /\\((?>[0-7]{1,3}|x[0-9a-fA-F]{1,2}|M-[^\\]|(C-|c)[^\\]|[^0-7xMCc]))/ ESC_RE = /\\((?>[0-7]{1,3}|x[0-9a-fA-F]{1,2}|M-[^\\]|(C-|c)[^\\]|[^0-7xMCc]))/
Expand Down Expand Up @@ -240,7 +239,6 @@ def initialize v = 18
self.version = v self.version = v
self.cond = RubyParserStuff::StackState.new(:cond) self.cond = RubyParserStuff::StackState.new(:cond)
self.cmdarg = RubyParserStuff::StackState.new(:cmdarg) self.cmdarg = RubyParserStuff::StackState.new(:cmdarg)
self.tern = RubyParserStuff::StackState.new(:tern)
self.nest = 0 self.nest = 0
@comments = [] @comments = []


Expand Down Expand Up @@ -715,7 +713,6 @@ def yylex # 826 lines
elsif src.scan(/[\]\)\}]/) then elsif src.scan(/[\]\)\}]/) then
cond.lexpop cond.lexpop
cmdarg.lexpop cmdarg.lexpop
tern.lexpop
self.lex_state = :expr_end self.lex_state = :expr_end
self.yacc_value = src.matched self.yacc_value = src.matched
result = { result = {
Expand Down Expand Up @@ -832,10 +829,8 @@ def yylex # 826 lines
rb_compile_error "unexpected '['" rb_compile_error "unexpected '['"
end end
elsif is_beg? then elsif is_beg? then
self.tern.push false
result = :tLBRACK result = :tLBRACK
elsif is_arg? && space_seen then elsif is_arg? && space_seen then
self.tern.push false
result = :tLBRACK result = :tLBRACK
else else
result = :tLBRACK2 result = :tLBRACK2
Expand Down Expand Up @@ -878,7 +873,6 @@ def yylex # 826 lines
elsif in_lex_state?(:expr_endarg) then elsif in_lex_state?(:expr_endarg) then
:tLBRACE_ARG # block (expr) :tLBRACE_ARG # block (expr)
else else
self.tern.push false
:tLBRACE # hash :tLBRACE # hash
end end


Expand Down Expand Up @@ -1034,7 +1028,6 @@ def yylex # 826 lines


if is_end? then if is_end? then
self.lex_state = ruby18 ? :expr_beg : :expr_value # HACK? self.lex_state = ruby18 ? :expr_beg : :expr_value # HACK?
self.tern.push true
self.yacc_value = "?" self.yacc_value = "?"
return :tEH return :tEH
end end
Expand All @@ -1059,12 +1052,10 @@ def yylex # 826 lines


# ternary # ternary
self.lex_state = ruby18 ? :expr_beg : :expr_value # HACK? self.lex_state = ruby18 ? :expr_beg : :expr_value # HACK?
self.tern.push true
self.yacc_value = "?" self.yacc_value = "?"
return :tEH return :tEH
elsif src.check(/\w(?=\w)/) then # ternary, also elsif src.check(/\w(?=\w)/) then # ternary, also
self.lex_state = :expr_beg self.lex_state = :expr_beg
self.tern.push true
self.yacc_value = "?" self.yacc_value = "?"
return :tEH return :tEH
end end
Expand Down Expand Up @@ -1263,18 +1254,15 @@ def yylex_paren18
if in_lex_state? :expr_cmdarg then if in_lex_state? :expr_cmdarg then
result = :tLPAREN_ARG result = :tLPAREN_ARG
elsif in_lex_state? :expr_arg then elsif in_lex_state? :expr_arg then
self.tern.push false
warning "don't put space before argument parentheses" warning "don't put space before argument parentheses"
end end
else
self.tern.push false
end end


result result
end end


def is_end? def is_end?
in_lex_state? :expr_end, :expr_endarg, :expr_endfn in_lex_state? :expr_end, :expr_endarg
end end


def is_arg? def is_arg?
Expand Down Expand Up @@ -1391,8 +1379,6 @@ def process_token(command_state)


return keyword.id0 if state == :expr_beg or state == :expr_value return keyword.id0 if state == :expr_beg or state == :expr_value


self.lex_state = :expr_beg if keyword.id0 != keyword.id1

return keyword.id1 return keyword.id1
end end
end end
Expand All @@ -1407,8 +1393,6 @@ def process_token(command_state)
else else
:expr_arg :expr_arg
end end
elsif ruby19 && in_lex_state?(:expr_fname) then
:expr_endfn
else else
:expr_end :expr_end
end end
Expand Down
40 changes: 1 addition & 39 deletions lib/ruby_parser_extras.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -234,19 +234,8 @@ def assignable(lhs, value = nil)
s(:cdecl, id) s(:cdecl, id)
else else
case self.env[id] case self.env[id]
when :lvar then when :lvar, :dvar, nil then
s(:lasgn, id) s(:lasgn, id)
when :dvar, nil then
if self.env.current[id] == :dvar then
s(:lasgn, id)
elsif self.env[id] == :dvar then
self.env.use(id)
s(:lasgn, id)
elsif ! self.env.dynamic? then
s(:lasgn, id)
else
s(:lasgn, id)
end
else else
raise "wtf? unknown type: #{self.env[id]}" raise "wtf? unknown type: #{self.env[id]}"
end end
Expand Down Expand Up @@ -1171,55 +1160,28 @@ def current
@env.first @env.first
end end


def dynamic
idx = @dyn.index false
@env[0...idx].reverse.inject { |env, scope| env.merge scope } || {}
end

def dynamic?
@dyn[0] != false
end

def extend dyn = false def extend dyn = false
@dyn.unshift dyn @dyn.unshift dyn
@env.unshift({}) @env.unshift({})
@use.unshift({})
end end


def initialize dyn = false def initialize dyn = false
@dyn = [] @dyn = []
@env = [] @env = []
@use = []
self.reset self.reset
end end


def reset def reset
@dyn.clear @dyn.clear
@env.clear @env.clear
@use.clear
self.extend self.extend
end end


def unextend def unextend
@dyn.shift @dyn.shift
@env.shift @env.shift
@use.shift
raise "You went too far unextending env" if @env.empty? raise "You went too far unextending env" if @env.empty?
end end

def use id
@env.each_with_index do |env, i|
if env[id] then
@use[i][id] = true
end
end
end

def used? id
idx = @dyn.index false # REFACTOR
u = @use[0...idx].reverse.inject { |env, scope| env.merge scope } || {}
u[id]
end
end end


class StackState class StackState
Expand Down
Loading