Class: RuboCop::AST::Token

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

Overview

A basic wrapper around Parser’s tokens.

Constant Summary collapse

LEFT_PAREN_TYPES =
%i[tLPAREN tLPAREN2].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pos, type, text) ⇒ Token

Returns a new instance of Token.



17
18
19
20
21
22
# File 'lib/rubocop/ast/token.rb', line 17

def initialize(pos, type, text)
  @pos = pos
  @type = type
  # Parser token "text" may be an Integer
  @text = text.to_s
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos.



9
10
11
# File 'lib/rubocop/ast/token.rb', line 9

def pos
  @pos
end

#textObject (readonly)

Returns the value of attribute text.



9
10
11
# File 'lib/rubocop/ast/token.rb', line 9

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/rubocop/ast/token.rb', line 9

def type
  @type
end

Class Method Details

.from_parser_token(parser_token) ⇒ Object



11
12
13
14
15
# File 'lib/rubocop/ast/token.rb', line 11

def self.from_parser_token(parser_token)
  type, details = parser_token
  text, range = details
  new(range, type, text)
end

Instance Method Details

#begin_posObject



32
33
34
# File 'lib/rubocop/ast/token.rb', line 32

def begin_pos
  @pos.begin_pos
end

#columnObject



28
29
30
# File 'lib/rubocop/ast/token.rb', line 28

def column
  @pos.column
end

#comma?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/rubocop/ast/token.rb', line 101

def comma?
  type == :tCOMMA
end

#comment?Boolean

Type Predicates

Returns:

  • (Boolean)


57
58
59
# File 'lib/rubocop/ast/token.rb', line 57

def comment?
  type == :tCOMMENT
end

#dot?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/rubocop/ast/token.rb', line 105

def dot?
  type == :tDOT
end

#end?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/rubocop/ast/token.rb', line 117

def end?
  type == :kEND
end

#end_posObject



36
37
38
# File 'lib/rubocop/ast/token.rb', line 36

def end_pos
  @pos.end_pos
end

#equal_sign?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/rubocop/ast/token.rb', line 121

def equal_sign?
  %i[tEQL tOP_ASGN].include?(type)
end

#left_array_bracket?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/rubocop/ast/token.rb', line 65

def left_array_bracket?
  type == :tLBRACK
end

#left_brace?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/rubocop/ast/token.rb', line 81

def left_brace?
  type == :tLBRACE
end

#left_bracket?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/rubocop/ast/token.rb', line 73

def left_bracket?
  %i[tLBRACK tLBRACK2].include?(type)
end

#left_curly_brace?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/rubocop/ast/token.rb', line 85

def left_curly_brace?
  type == :tLCURLY || type == :tLAMBEG
end

#left_parens?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/rubocop/ast/token.rb', line 93

def left_parens?
  LEFT_PAREN_TYPES.include?(type)
end

#left_ref_bracket?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/rubocop/ast/token.rb', line 69

def left_ref_bracket?
  type == :tLBRACK2
end

#lineObject



24
25
26
# File 'lib/rubocop/ast/token.rb', line 24

def line
  @pos.line
end

#new_line?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/rubocop/ast/token.rb', line 125

def new_line?
  type == :tNL
end

#regexp_dots?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/rubocop/ast/token.rb', line 109

def regexp_dots?
  %i[tDOT2 tDOT3].include?(type)
end

#rescue_modifier?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/rubocop/ast/token.rb', line 113

def rescue_modifier?
  type == :kRESCUE_MOD
end

#right_bracket?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/rubocop/ast/token.rb', line 77

def right_bracket?
  type == :tRBRACK
end

#right_curly_brace?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/rubocop/ast/token.rb', line 89

def right_curly_brace?
  type == :tRCURLY
end

#right_parens?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/rubocop/ast/token.rb', line 97

def right_parens?
  type == :tRPAREN
end

#semicolon?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/rubocop/ast/token.rb', line 61

def semicolon?
  type == :tSEMI
end

#space_after?Boolean

Checks if there is whitespace after token

Returns:

  • (Boolean)


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

def space_after?
  pos.source_buffer.source.match(/\G\s/, end_pos)
end

#space_before?Boolean

Checks if there is whitespace before token

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/rubocop/ast/token.rb', line 50

def space_before?
  position = begin_pos.zero? ? begin_pos : begin_pos - 1
  pos.source_buffer.source.match(/\G\s/, position)
end

#to_sObject



40
41
42
# File 'lib/rubocop/ast/token.rb', line 40

def to_s
  "[[#{line}, #{column}], #{type}, #{text.inspect}]"
end