Class: SyntaxSuggest::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_suggest/token.rb

Overview

Value object for accessing lex values

This lex:

[IDENTIFIER(1,0)-(1,8)("describe"), 32]

Would translate into:

lex.location # => (1,0)-(1,8)
lex.type # => :IDENTIFIER
lex.token # => "describe"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prism_token, previous_prism_token, visitor) ⇒ Token

Returns a new instance of Token.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/syntax_suggest/token.rb', line 24

def initialize(prism_token, previous_prism_token, visitor)
  @location = prism_token.location
  @type = prism_token.type
  @value = prism_token.value

  # Prism lexes `:module` as SYMBOL_BEGIN, KEYWORD_MODULE
  # https://github.com/ruby/prism/issues/3940
  symbol_content = previous_prism_token&.type == :SYMBOL_BEGIN
  @is_kw = KW_TYPES.include?(@type)
  @is_kw = false if symbol_content || visitor.endless_def_keyword_offsets.include?(@location.start_offset)
  @is_end = @type == :KEYWORD_END
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



16
17
18
# File 'lib/syntax_suggest/token.rb', line 16

def location
  @location
end

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/syntax_suggest/token.rb', line 16

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



16
17
18
# File 'lib/syntax_suggest/token.rb', line 16

def value
  @value
end

Instance Method Details

#is_end?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/syntax_suggest/token.rb', line 41

def is_end?
  @is_end
end

#is_kw?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/syntax_suggest/token.rb', line 45

def is_kw?
  @is_kw
end

#lineObject



37
38
39
# File 'lib/syntax_suggest/token.rb', line 37

def line
  @location.start_line
end