Skip to content

Commit 6f75f7a

Browse files
authored
Merge pull request #3881 from ruby/private-const
Mark STATES as a private constant
2 parents 929aec1 + c78f742 commit 6f75f7a

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

lib/prism/lex_compat.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ def result
639639

640640
event = RIPPER.fetch(token.type)
641641
value = token.value
642-
lex_state = Translation::Ripper::Lexer::State.cached(lex_state)
642+
lex_state = Translation::Ripper::Lexer::State[lex_state]
643643

644644
token =
645645
case event
@@ -691,7 +691,7 @@ def result
691691
counter += { on_embexpr_beg: -1, on_embexpr_end: 1 }[current_event] || 0
692692
end
693693

694-
Translation::Ripper::Lexer::State.cached(result_value[current_index][1])
694+
Translation::Ripper::Lexer::State[result_value[current_index][1]]
695695
else
696696
previous_state
697697
end
@@ -828,7 +828,7 @@ def result
828828
def add_on_sp_tokens(tokens, source, data_loc, bom, eof_token)
829829
new_tokens = []
830830

831-
prev_token_state = Translation::Ripper::Lexer::State.cached(Translation::Ripper::EXPR_BEG)
831+
prev_token_state = Translation::Ripper::Lexer::State[Translation::Ripper::EXPR_BEG]
832832
prev_token_end = bom ? 3 : 0
833833

834834
tokens.each do |token|

lib/prism/translation/ripper/lexer.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class Ripper
99
class Lexer < Ripper # :nodoc:
1010
# :stopdoc:
1111
class State
12-
1312
attr_reader :to_int, :to_s
1413

1514
def initialize(i)
@@ -39,10 +38,12 @@ def allbits?(i) to_int.allbits?(i) end
3938
def anybits?(i) to_int.anybits?(i) end
4039
def nobits?(i) to_int.nobits?(i) end
4140

42-
# Instances are frozen and there are only a handful of them so we cache them here.
43-
STATES = Hash.new { |h,k| h[k] = State.new(k) }
41+
# Instances are frozen and there are only a handful of them so we
42+
# cache them here.
43+
STATES = Hash.new { |hash, key| hash[key] = State.new(key) }
44+
private_constant :STATES
4445

45-
def self.cached(i)
46+
def self.[](i)
4647
STATES[i]
4748
end
4849
end
@@ -54,7 +55,7 @@ def initialize(pos, event, tok, state, message = nil)
5455
@pos = pos
5556
@event = event
5657
@tok = tok
57-
@state = State.cached(state)
58+
@state = State[state]
5859
@message = message
5960
end
6061

0 commit comments

Comments
 (0)