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.
Constant Summary collapse
- LEFT_PAREN_TYPES =
i[tLPAREN tLPAREN2].freeze
- LEFT_CURLY_TYPES =
i[tLCURLY tLAMBEG].freeze
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.
- #dot? ⇒ Boolean
- #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
- #new_line? ⇒ Boolean
- #regexp_dots? ⇒ Boolean
- #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.
18 19 20 21 22 23 |
# File 'lib/rubocop/ast/token.rb', line 18 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.
10 11 12 |
# File 'lib/rubocop/ast/token.rb', line 10 def pos @pos end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
10 11 12 |
# File 'lib/rubocop/ast/token.rb', line 10 def text @text end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
10 11 12 |
# File 'lib/rubocop/ast/token.rb', line 10 def type @type end |
Class Method Details
.from_parser_token(parser_token) ⇒ Object
12 13 14 15 16 |
# File 'lib/rubocop/ast/token.rb', line 12 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
33 34 35 |
# File 'lib/rubocop/ast/token.rb', line 33 def begin_pos @pos.begin_pos end |
#column ⇒ Object
29 30 31 |
# File 'lib/rubocop/ast/token.rb', line 29 def column @pos.column end |
#comma? ⇒ Boolean
102 103 104 |
# File 'lib/rubocop/ast/token.rb', line 102 def comma? type == :tCOMMA end |
#comment? ⇒ Boolean
Type Predicates
58 59 60 |
# File 'lib/rubocop/ast/token.rb', line 58 def comment? type == :tCOMMENT end |
#dot? ⇒ Boolean
106 107 108 |
# File 'lib/rubocop/ast/token.rb', line 106 def dot? type == :tDOT end |
#end? ⇒ Boolean
118 119 120 |
# File 'lib/rubocop/ast/token.rb', line 118 def end? type == :kEND end |
#end_pos ⇒ Object
37 38 39 |
# File 'lib/rubocop/ast/token.rb', line 37 def end_pos @pos.end_pos end |
#equal_sign? ⇒ Boolean
122 123 124 |
# File 'lib/rubocop/ast/token.rb', line 122 def equal_sign? i[tEQL tOP_ASGN].include?(type) end |
#left_array_bracket? ⇒ Boolean
66 67 68 |
# File 'lib/rubocop/ast/token.rb', line 66 def left_array_bracket? type == :tLBRACK end |
#left_brace? ⇒ Boolean
82 83 84 |
# File 'lib/rubocop/ast/token.rb', line 82 def left_brace? type == :tLBRACE end |
#left_bracket? ⇒ Boolean
74 75 76 |
# File 'lib/rubocop/ast/token.rb', line 74 def left_bracket? i[tLBRACK tLBRACK2].include?(type) end |
#left_curly_brace? ⇒ Boolean
86 87 88 |
# File 'lib/rubocop/ast/token.rb', line 86 def left_curly_brace? LEFT_CURLY_TYPES.include?(type) end |
#left_parens? ⇒ Boolean
94 95 96 |
# File 'lib/rubocop/ast/token.rb', line 94 def left_parens? LEFT_PAREN_TYPES.include?(type) end |
#left_ref_bracket? ⇒ Boolean
70 71 72 |
# File 'lib/rubocop/ast/token.rb', line 70 def left_ref_bracket? type == :tLBRACK2 end |
#line ⇒ Object
25 26 27 |
# File 'lib/rubocop/ast/token.rb', line 25 def line @pos.line end |
#new_line? ⇒ Boolean
126 127 128 |
# File 'lib/rubocop/ast/token.rb', line 126 def new_line? type == :tNL end |
#regexp_dots? ⇒ Boolean
110 111 112 |
# File 'lib/rubocop/ast/token.rb', line 110 def regexp_dots? i[tDOT2 tDOT3].include?(type) end |
#rescue_modifier? ⇒ Boolean
114 115 116 |
# File 'lib/rubocop/ast/token.rb', line 114 def rescue_modifier? type == :kRESCUE_MOD end |
#right_bracket? ⇒ Boolean
78 79 80 |
# File 'lib/rubocop/ast/token.rb', line 78 def right_bracket? type == :tRBRACK end |
#right_curly_brace? ⇒ Boolean
90 91 92 |
# File 'lib/rubocop/ast/token.rb', line 90 def right_curly_brace? type == :tRCURLY end |
#right_parens? ⇒ Boolean
98 99 100 |
# File 'lib/rubocop/ast/token.rb', line 98 def right_parens? type == :tRPAREN end |
#semicolon? ⇒ Boolean
62 63 64 |
# File 'lib/rubocop/ast/token.rb', line 62 def semicolon? type == :tSEMI end |
#space_after? ⇒ Boolean
Checks if there is whitespace after token
46 47 48 |
# File 'lib/rubocop/ast/token.rb', line 46 def space_after? pos.source_buffer.source.match(/\G\s/, end_pos) end |
#space_before? ⇒ Boolean
Checks if there is whitespace before token
51 52 53 54 |
# File 'lib/rubocop/ast/token.rb', line 51 def space_before? position = begin_pos.zero? ? begin_pos : begin_pos - 1 pos.source_buffer.source.match(/\G\s/, position) end |
#to_s ⇒ Object
41 42 43 |
# File 'lib/rubocop/ast/token.rb', line 41 def to_s "[[#{line}, #{column}], #{type}, #{text.inspect}]" end |