-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
token.rb
120 lines (93 loc) · 2.12 KB
/
token.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true
module RuboCop
module AST
# A basic wrapper around Parser's tokens.
class Token
attr_reader :pos, :type, :text
def self.from_parser_token(parser_token)
type, details = parser_token
text, range = details
new(range, type, text)
end
def initialize(pos, type, text)
@pos = pos
@type = type
# Parser token "text" may be an Integer
@text = text.to_s
end
def line
@pos.line
end
def column
@pos.column
end
def begin_pos
@pos.begin_pos
end
def end_pos
@pos.end_pos
end
def to_s
"[[#{line}, #{column}], #{type}, #{text.inspect}]"
end
# Checks if there is whitespace after token
def space_after?
pos.source_buffer.source.match(/\G\s/, end_pos)
end
# Checks if there is whitespace before token
def space_before?
position = begin_pos.zero? ? begin_pos : begin_pos - 1
pos.source_buffer.source.match(/\G\s/, position)
end
## Type Predicates
def comment?
type == :tCOMMENT
end
def semicolon?
type == :tSEMI
end
def left_array_bracket?
type == :tLBRACK
end
def left_ref_bracket?
type == :tLBRACK2
end
def left_bracket?
%i[tLBRACK tLBRACK2].include?(type)
end
def right_bracket?
type == :tRBRACK
end
def left_brace?
type == :tLBRACE
end
def left_curly_brace?
type == :tLCURLY
end
def right_curly_brace?
type == :tRCURLY
end
def left_parens?
%i[tLPAREN tLPAREN2].include?(type)
end
def right_parens?
type == :tRPAREN
end
def comma?
type == :tCOMMA
end
def regexp_dots?
%i[tDOT2 tDOT3].include?(type)
end
def rescue_modifier?
type == :kRESCUE_MOD
end
def end?
type == :kEND
end
def equal_sign?
%i[tEQL tOP_ASGN].include?(type)
end
end
end
end