Class: RuboCop::AST::Token

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

Overview

A basic wrapper around Parser’s tokens.

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.



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

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.



7
8
9
# File 'lib/rubocop/ast/token.rb', line 7

def pos
  @pos
end

#textObject (readonly)

Returns the value of attribute text.



7
8
9
# File 'lib/rubocop/ast/token.rb', line 7

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/rubocop/ast/token.rb', line 7

def type
  @type
end

Class Method Details

.from_parser_token(parser_token) ⇒ Object



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

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

Instance Method Details

#begin_posObject



30
31
32
# File 'lib/rubocop/ast/token.rb', line 30

def begin_pos
  @pos.begin_pos
end

#columnObject



26
27
28
# File 'lib/rubocop/ast/token.rb', line 26

def column
  @pos.column
end

#comma?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/rubocop/ast/token.rb', line 99

def comma?
  type == :tCOMMA
end

#comment?Boolean

Type Predicates

Returns:

  • (Boolean)


55
56
57
# File 'lib/rubocop/ast/token.rb', line 55

def comment?
  type == :tCOMMENT
end

#end?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/rubocop/ast/token.rb', line 107

def end?
  type == :kEND
end

#end_posObject



34
35
36
# File 'lib/rubocop/ast/token.rb', line 34

def end_pos
  @pos.end_pos
end

#equal_sign?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/rubocop/ast/token.rb', line 111

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

#left_array_bracket?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rubocop/ast/token.rb', line 63

def left_array_bracket?
  type == :tLBRACK
end

#left_brace?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/rubocop/ast/token.rb', line 79

def left_brace?
  type == :tLBRACE
end

#left_bracket?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/rubocop/ast/token.rb', line 71

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

#left_curly_brace?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/rubocop/ast/token.rb', line 83

def left_curly_brace?
  type == :tLCURLY
end

#left_parens?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/rubocop/ast/token.rb', line 91

def left_parens?
  %i[tLPAREN tLPAREN2].include?(type)
end

#left_ref_bracket?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/rubocop/ast/token.rb', line 67

def left_ref_bracket?
  type == :tLBRACK2
end

#lineObject



22
23
24
# File 'lib/rubocop/ast/token.rb', line 22

def line
  @pos.line
end

#rescue_modifier?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/rubocop/ast/token.rb', line 103

def rescue_modifier?
  type == :kRESCUE_MOD
end

#right_bracket?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/rubocop/ast/token.rb', line 75

def right_bracket?
  type == :tRBRACK
end

#right_curly_brace?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/rubocop/ast/token.rb', line 87

def right_curly_brace?
  type == :tRCURLY
end

#right_parens?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/rubocop/ast/token.rb', line 95

def right_parens?
  type == :tRPAREN
end

#semicolon?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rubocop/ast/token.rb', line 59

def semicolon?
  type == :tSEMI
end

#space_after?Boolean

Checks if there is whitespace after token

Returns:

  • (Boolean)


43
44
45
# File 'lib/rubocop/ast/token.rb', line 43

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)


48
49
50
51
# File 'lib/rubocop/ast/token.rb', line 48

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

#to_sObject



38
39
40
# File 'lib/rubocop/ast/token.rb', line 38

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