Skip to content

Commit d7da198

Browse files
Earlopainmatzbot
authored andcommitted
[ruby/syntax_suggest] Rename lex to token where appropriate
I had a bit of trouble following what is going on. Lexing produces tokens, this renames to reflect that. ruby/syntax_suggest@d3386ab6f4
1 parent 3a5bb48 commit d7da198

File tree

6 files changed

+65
-65
lines changed

6 files changed

+65
-65
lines changed

lib/syntax_suggest/clean_document.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ def join_heredoc!
182182
start_index_stack = []
183183
heredoc_beg_end_index = []
184184
lines.each do |line|
185-
line.lex.each do |lex_value|
186-
case lex_value.type
185+
line.tokens.each do |token|
186+
case token.type
187187
when :on_heredoc_beg
188188
start_index_stack << line.index
189189
when :on_heredoc_end
@@ -273,7 +273,7 @@ def join_groups(groups)
273273

274274
# Join group into the first line
275275
@document[line.index] = CodeLine.new(
276-
lex: lines.map(&:lex).flatten,
276+
tokens: lines.map(&:tokens).flatten,
277277
line: lines.join,
278278
index: line.index
279279
)
@@ -282,7 +282,7 @@ def join_groups(groups)
282282
lines[1..].each do |line|
283283
# The above lines already have newlines in them, if add more
284284
# then there will be double newline, use an empty line instead
285-
@document[line.index] = CodeLine.new(line: "", index: line.index, lex: [])
285+
@document[line.index] = CodeLine.new(line: "", index: line.index, tokens: [])
286286
end
287287
end
288288
self

lib/syntax_suggest/code_line.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ class CodeLine
2727
# Returns an array of CodeLine objects
2828
# from the source string
2929
def self.from_source(source)
30-
lex_array_for_line = LexAll.new(source: source).each_with_object(Hash.new { |h, k| h[k] = [] }) { |lex, hash| hash[lex.line] << lex }
30+
tokens_for_line = LexAll.new(source: source).each_with_object(Hash.new { |h, k| h[k] = [] }) { |token, hash| hash[token.line] << token }
3131
source.lines.map.with_index do |line, index|
3232
CodeLine.new(
3333
line: line,
3434
index: index,
35-
lex: lex_array_for_line[index + 1]
35+
tokens: tokens_for_line[index + 1]
3636
)
3737
end
3838
end
3939

40-
attr_reader :line, :index, :lex, :line_number, :indent
41-
def initialize(line:, index:, lex:)
42-
@lex = lex
40+
attr_reader :line, :index, :tokens, :line_number, :indent
41+
def initialize(line:, index:, tokens:)
42+
@tokens = tokens
4343
@line = line
4444
@index = index
4545
@original = line
@@ -180,12 +180,12 @@ def ignore_newline_not_beg?
180180
# expect(lines.first.trailing_slash?).to eq(true)
181181
#
182182
def trailing_slash?
183-
last = @lex.last
183+
last = @tokens.last
184184

185185
# Older versions of prism diverged slightly from Ripper in compatibility mode
186186
case last&.type
187187
when :on_sp
188-
last.token == TRAILING_SLASH
188+
last.value == TRAILING_SLASH
189189
when :on_tstring_end
190190
true
191191
else
@@ -209,21 +209,21 @@ def trailing_slash?
209209
end_count = 0
210210

211211
@ignore_newline_not_beg = false
212-
@lex.each do |lex|
213-
kw_count += 1 if lex.is_kw?
214-
end_count += 1 if lex.is_end?
212+
@tokens.each do |token|
213+
kw_count += 1 if token.is_kw?
214+
end_count += 1 if token.is_end?
215215

216-
if lex.type == :on_ignored_nl
217-
@ignore_newline_not_beg = !lex.expr_beg?
216+
if token.type == :on_ignored_nl
217+
@ignore_newline_not_beg = !token.expr_beg?
218218
end
219219

220220
if in_oneliner_def.nil?
221-
in_oneliner_def = :ENDFN if lex.state.allbits?(Ripper::EXPR_ENDFN)
222-
elsif lex.state.allbits?(Ripper::EXPR_ENDFN)
221+
in_oneliner_def = :ENDFN if token.state.allbits?(Ripper::EXPR_ENDFN)
222+
elsif token.state.allbits?(Ripper::EXPR_ENDFN)
223223
# Continue
224-
elsif lex.state.allbits?(Ripper::EXPR_BEG)
225-
in_oneliner_def = :BODY if lex.token == "="
226-
elsif lex.state.allbits?(Ripper::EXPR_END)
224+
elsif token.state.allbits?(Ripper::EXPR_BEG)
225+
in_oneliner_def = :BODY if token.value == "="
226+
elsif token.state.allbits?(Ripper::EXPR_END)
227227
# We found an endless method, count it
228228
oneliner_count += 1 if in_oneliner_def == :BODY
229229

lib/syntax_suggest/explain_syntax.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require_relative "left_right_lex_count"
3+
require_relative "left_right_token_count"
44

55
module SyntaxSuggest
66
class GetParseErrors
@@ -45,14 +45,14 @@ class ExplainSyntax
4545

4646
def initialize(code_lines:)
4747
@code_lines = code_lines
48-
@left_right = LeftRightLexCount.new
48+
@left_right = LeftRightTokenCount.new
4949
@missing = nil
5050
end
5151

5252
def call
5353
@code_lines.each do |line|
54-
line.lex.each do |lex|
55-
@left_right.count_lex(lex)
54+
line.tokens.each do |token|
55+
@left_right.count_token(token)
5656
end
5757
end
5858

lib/syntax_suggest/left_right_lex_count.rb renamed to lib/syntax_suggest/left_right_token_count.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ module SyntaxSuggest
99
#
1010
# Example:
1111
#
12-
# left_right = LeftRightLexCount.new
12+
# left_right = LeftRightTokenCount.new
1313
# left_right.count_kw
1414
# left_right.missing.first
1515
# # => "end"
1616
#
17-
# left_right = LeftRightLexCount.new
17+
# left_right = LeftRightTokenCount.new
1818
# source = "{ a: b, c: d" # Note missing '}'
19-
# LexAll.new(source: source).each do |lex|
20-
# left_right.count_lex(lex)
19+
# LexAll.new(source: source).each do |token|
20+
# left_right.count_token(token)
2121
# end
2222
# left_right.missing.first
2323
# # => "}"
24-
class LeftRightLexCount
24+
class LeftRightTokenCount
2525
def initialize
2626
@kw_count = 0
2727
@end_count = 0
@@ -49,14 +49,14 @@ def count_end
4949
#
5050
# Example:
5151
#
52-
# left_right = LeftRightLexCount.new
53-
# left_right.count_lex(LexValue.new(1, :on_lbrace, "{", Ripper::EXPR_BEG))
52+
# left_right = LeftRightTokenCount.new
53+
# left_right.count_token(Token.new(1, :on_lbrace, "{", Ripper::EXPR_BEG))
5454
# left_right.count_for_char("{")
5555
# # => 1
5656
# left_right.count_for_char("}")
5757
# # => 0
58-
def count_lex(lex)
59-
case lex.type
58+
def count_token(token)
59+
case token.type
6060
when :on_tstring_content
6161
# ^^^
6262
# Means it's a string or a symbol `"{"` rather than being
@@ -70,7 +70,7 @@ def count_lex(lex)
7070
# The start token will be the full thing `%Q{` but we
7171
# need to count it as if it's a `{`. Any token
7272
# can be used
73-
char = lex.token[-1]
73+
char = token.value[-1]
7474
@count_for_char[char] += 1 if @count_for_char.key?(char)
7575
when :on_embexpr_beg
7676
# ^^^
@@ -87,14 +87,14 @@ def count_lex(lex)
8787
# When we see `#{` count it as a `{` or we will
8888
# have a mis-match count.
8989
#
90-
case lex.token
90+
case token.value
9191
when "\#{"
9292
@count_for_char["{"] += 1
9393
end
9494
else
95-
@end_count += 1 if lex.is_end?
96-
@kw_count += 1 if lex.is_kw?
97-
@count_for_char[lex.token] += 1 if @count_for_char.key?(lex.token)
95+
@end_count += 1 if token.is_end?
96+
@kw_count += 1 if token.is_kw?
97+
@count_for_char[token.value] += 1 if @count_for_char.key?(token.value)
9898
end
9999
end
100100

lib/syntax_suggest/lex_all.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# frozen_string_literal: true
22

33
module SyntaxSuggest
4-
# Lexes the whole source and wraps the tokens in `LexValue`.
4+
# Lexes the whole source and wraps the tokens in `Token`.
55
#
66
# Example usage:
77
#
8-
# lex = LexAll.new(source: source)
9-
# lex.each do |value|
10-
# puts value.line
8+
# tokens = LexAll.new(source: source)
9+
# tokens.each do |token|
10+
# puts token.line
1111
# end
1212
class LexAll
1313
include Enumerable
1414

1515
def initialize(source:)
16-
@lex = self.class.lex(source, 1)
17-
last_lex = nil
18-
@lex.map! { |elem|
19-
last_lex = LexValue.new(elem[0].first, elem[1], elem[2], elem[3], last_lex)
16+
@tokens = self.class.lex(source, 1)
17+
last_token = nil
18+
@tokens.map! { |elem|
19+
last_token = Token.new(elem[0].first, elem[1], elem[2], elem[3], last_token)
2020
}
2121
end
2222

@@ -25,24 +25,24 @@ def self.lex(source, line_number)
2525
end
2626

2727
def to_a
28-
@lex
28+
@tokens
2929
end
3030

3131
def each
32-
return @lex.each unless block_given?
33-
@lex.each do |x|
34-
yield x
32+
return @tokens.each unless block_given?
33+
@tokens.each do |token|
34+
yield token
3535
end
3636
end
3737

3838
def [](index)
39-
@lex[index]
39+
@tokens[index]
4040
end
4141

4242
def last
43-
@lex.last
43+
@tokens.last
4444
end
4545
end
4646
end
4747

48-
require_relative "lex_value"
48+
require_relative "token"
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@ module SyntaxSuggest
99
#
1010
# Would translate into:
1111
#
12-
# lex.line # => 1
13-
# lex.type # => :on_indent
14-
# lex.token # => "describe"
15-
class LexValue
16-
attr_reader :line, :type, :token, :state
12+
# token.line # => 1
13+
# token.type # => :on_indent
14+
# token.value # => "describe"
15+
class Token
16+
attr_reader :line, :type, :value, :state
1717

18-
def initialize(line, type, token, state, last_lex = nil)
18+
def initialize(line, type, value, state, last_token = nil)
1919
@line = line
2020
@type = type
21-
@token = token
21+
@value = value
2222
@state = state
2323

24-
set_kw_end(last_lex)
24+
set_kw_end(last_token)
2525
end
2626

27-
private def set_kw_end(last_lex)
27+
private def set_kw_end(last_token)
2828
@is_end = false
2929
@is_kw = false
3030
return if type != :on_kw
3131

32-
return if last_lex && last_lex.fname? # https://github.com/ruby/ruby/commit/776759e300e4659bb7468e2b97c8c2d4359a2953
32+
return if last_token && last_token.fname? # https://github.com/ruby/ruby/commit/776759e300e4659bb7468e2b97c8c2d4359a2953
3333

34-
case token
34+
case value
3535
when "if", "unless", "while", "until"
3636
# Only count if/unless when it's not a "trailing" if/unless
3737
# https://github.com/ruby/ruby/blob/06b44f819eb7b5ede1ff69cecb25682b56a1d60c/lib/irb/ruby-lex.rb#L374-L375

0 commit comments

Comments
 (0)