Class: RuboCop::AST::Token
- Inherits:
-
Object
- Object
- RuboCop::AST::Token
- Defined in:
- lib/rubocop/ast/token.rb
Overview
A basic wrapper around Parser’s tokens.
Instance Attribute Summary collapse
-
#pos ⇒ Object
readonly
Returns the value of attribute pos.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #begin_pos ⇒ Object
- #column ⇒ Object
- #comma? ⇒ Boolean
-
#comment? ⇒ Boolean
Type Predicates.
- #end? ⇒ Boolean
- #end_pos ⇒ Object
- #equal_sign? ⇒ Boolean
-
#initialize(pos, type, text) ⇒ Token
constructor
A new instance of Token.
- #left_array_bracket? ⇒ Boolean
- #left_brace? ⇒ Boolean
- #left_bracket? ⇒ Boolean
- #left_curly_brace? ⇒ Boolean
- #left_parens? ⇒ Boolean
- #left_ref_bracket? ⇒ Boolean
- #line ⇒ Object
- #rescue_modifier? ⇒ Boolean
- #right_bracket? ⇒ Boolean
- #right_curly_brace? ⇒ Boolean
- #right_parens? ⇒ Boolean
- #semicolon? ⇒ Boolean
-
#space_after? ⇒ Boolean
Checks if there is whitespace after token.
-
#space_before? ⇒ Boolean
Checks if there is whitespace before token.
- #to_s ⇒ Object
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
#pos ⇒ Object (readonly)
Returns the value of attribute pos.
7 8 9 |
# File 'lib/rubocop/ast/token.rb', line 7 def pos @pos end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
7 8 9 |
# File 'lib/rubocop/ast/token.rb', line 7 def text @text end |
#type ⇒ Object (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_pos ⇒ Object
30 31 32 |
# File 'lib/rubocop/ast/token.rb', line 30 def begin_pos @pos.begin_pos end |
#column ⇒ Object
26 27 28 |
# File 'lib/rubocop/ast/token.rb', line 26 def column @pos.column end |
#comma? ⇒ Boolean
99 100 101 |
# File 'lib/rubocop/ast/token.rb', line 99 def comma? type == :tCOMMA end |
#comment? ⇒ Boolean
Type Predicates
55 56 57 |
# File 'lib/rubocop/ast/token.rb', line 55 def comment? type == :tCOMMENT end |
#end? ⇒ Boolean
107 108 109 |
# File 'lib/rubocop/ast/token.rb', line 107 def end? type == :kEND end |
#end_pos ⇒ Object
34 35 36 |
# File 'lib/rubocop/ast/token.rb', line 34 def end_pos @pos.end_pos end |
#equal_sign? ⇒ 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
63 64 65 |
# File 'lib/rubocop/ast/token.rb', line 63 def left_array_bracket? type == :tLBRACK end |
#left_brace? ⇒ Boolean
79 80 81 |
# File 'lib/rubocop/ast/token.rb', line 79 def left_brace? type == :tLBRACE end |
#left_bracket? ⇒ 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
83 84 85 |
# File 'lib/rubocop/ast/token.rb', line 83 def left_curly_brace? type == :tLCURLY end |
#left_parens? ⇒ 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
67 68 69 |
# File 'lib/rubocop/ast/token.rb', line 67 def left_ref_bracket? type == :tLBRACK2 end |
#line ⇒ Object
22 23 24 |
# File 'lib/rubocop/ast/token.rb', line 22 def line @pos.line end |
#rescue_modifier? ⇒ Boolean
103 104 105 |
# File 'lib/rubocop/ast/token.rb', line 103 def rescue_modifier? type == :kRESCUE_MOD end |
#right_bracket? ⇒ Boolean
75 76 77 |
# File 'lib/rubocop/ast/token.rb', line 75 def right_bracket? type == :tRBRACK end |
#right_curly_brace? ⇒ Boolean
87 88 89 |
# File 'lib/rubocop/ast/token.rb', line 87 def right_curly_brace? type == :tRCURLY end |
#right_parens? ⇒ Boolean
95 96 97 |
# File 'lib/rubocop/ast/token.rb', line 95 def right_parens? type == :tRPAREN end |
#semicolon? ⇒ 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
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
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_s ⇒ Object
38 39 40 |
# File 'lib/rubocop/ast/token.rb', line 38 def to_s "[[#{line}, #{column}], #{type}, #{text.inspect}]" end |