rbs-inline 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -5
- data/Rakefile +12 -0
- data/lib/rbs/inline/annotation_parser/tokenizer.rb +361 -0
- data/lib/rbs/inline/annotation_parser.rb +548 -326
- data/lib/rbs/inline/ast/annotations.rb +427 -138
- data/lib/rbs/inline/ast/comment_lines.rb +17 -20
- data/lib/rbs/inline/ast/declarations.rb +64 -28
- data/lib/rbs/inline/ast/members.rb +129 -115
- data/lib/rbs/inline/ast/tree.rb +37 -22
- data/lib/rbs/inline/cli.rb +12 -12
- data/lib/rbs/inline/node_utils.rb +1 -1
- data/lib/rbs/inline/parser.rb +134 -65
- data/lib/rbs/inline/version.rb +1 -1
- data/lib/rbs/inline/writer.rb +195 -118
- data/lib/rbs/inline.rb +1 -0
- data/sig/generated/rbs/inline/annotation_parser/tokenizer.rbs +221 -0
- data/sig/generated/rbs/inline/annotation_parser.rbs +148 -92
- data/sig/generated/rbs/inline/ast/annotations.rbs +141 -37
- data/sig/generated/rbs/inline/ast/comment_lines.rbs +8 -4
- data/sig/generated/rbs/inline/ast/declarations.rbs +26 -10
- data/sig/generated/rbs/inline/ast/members.rbs +26 -15
- data/sig/generated/rbs/inline/ast/tree.rbs +23 -17
- data/sig/generated/rbs/inline/cli.rbs +3 -3
- data/sig/generated/rbs/inline/node_utils.rbs +1 -1
- data/sig/generated/rbs/inline/parser.rbs +35 -18
- data/sig/generated/rbs/inline/writer.rbs +54 -22
- metadata +4 -2
data/lib/rbs/inline/writer.rb
CHANGED
@@ -3,25 +3,33 @@
|
|
3
3
|
module RBS
|
4
4
|
module Inline
|
5
5
|
class Writer
|
6
|
-
|
7
|
-
|
6
|
+
# @rbs!
|
7
|
+
# interface _Content
|
8
|
+
# def <<: (RBS::AST::Declarations::t | RBS::AST::Members::t) -> void
|
9
|
+
#
|
10
|
+
# def concat: (Array[RBS::AST::Declarations::t | RBS::AST::Members::t]) -> void
|
11
|
+
# end
|
12
|
+
|
13
|
+
attr_reader :output #: String
|
14
|
+
attr_reader :writer #: RBS::Writer
|
8
15
|
|
9
16
|
# @rbs buffer: String
|
10
|
-
def initialize(buffer = +"")
|
17
|
+
def initialize(buffer = +"") #: void
|
11
18
|
@output = buffer
|
12
19
|
@writer = RBS::Writer.new(out: StringIO.new(buffer))
|
13
20
|
end
|
14
21
|
|
15
22
|
# @rbs uses: Array[AST::Annotations::Use]
|
16
23
|
# @rbs decls: Array[AST::Declarations::t]
|
17
|
-
|
24
|
+
# @rbs rbs_decls: Array[RBS::AST::Declarations::t]
|
25
|
+
def self.write(uses, decls, rbs_decls) #: void
|
18
26
|
writer = Writer.new()
|
19
|
-
writer.write(uses, decls)
|
27
|
+
writer.write(uses, decls, rbs_decls)
|
20
28
|
writer.output
|
21
29
|
end
|
22
30
|
|
23
|
-
# @rbs lines:
|
24
|
-
# @rbs
|
31
|
+
# @rbs *lines: String
|
32
|
+
# @rbs return: void
|
25
33
|
def header(*lines)
|
26
34
|
lines.each do |line|
|
27
35
|
writer.out.puts("# " + line)
|
@@ -31,8 +39,10 @@ module RBS
|
|
31
39
|
|
32
40
|
# @rbs uses: Array[AST::Annotations::Use]
|
33
41
|
# @rbs decls: Array[AST::Declarations::t]
|
34
|
-
# @rbs
|
35
|
-
|
42
|
+
# @rbs rbs_decls: Array[RBS::AST::Declarations::t] --
|
43
|
+
# Top level `rbs!` declarations
|
44
|
+
# @rbs return: void
|
45
|
+
def write(uses, decls, rbs_decls)
|
36
46
|
use_dirs = uses.map do |use|
|
37
47
|
RBS::AST::Directives::Use.new(
|
38
48
|
clauses: use.clauses,
|
@@ -40,54 +50,60 @@ module RBS
|
|
40
50
|
)
|
41
51
|
end
|
42
52
|
|
43
|
-
rbs =
|
44
|
-
|
53
|
+
rbs = [] #: Array[RBS::AST::Declarations::t]
|
54
|
+
|
55
|
+
decls.each do |decl|
|
56
|
+
translate_decl(
|
57
|
+
decl,
|
58
|
+
rbs #: Array[RBS::AST::Declarations::t | RBS::AST::Members::t]
|
59
|
+
)
|
45
60
|
end
|
46
61
|
|
47
|
-
|
62
|
+
rbs.concat(rbs_decls)
|
63
|
+
|
64
|
+
writer.write(
|
65
|
+
use_dirs + rbs
|
66
|
+
)
|
48
67
|
end
|
49
68
|
|
50
69
|
# @rbs decl: AST::Declarations::t
|
51
|
-
# @rbs
|
52
|
-
|
70
|
+
# @rbs rbs: _Content
|
71
|
+
# @rbs return: void
|
72
|
+
def translate_decl(decl, rbs)
|
53
73
|
case decl
|
54
74
|
when AST::Declarations::ClassDecl
|
55
|
-
translate_class_decl(decl)
|
75
|
+
translate_class_decl(decl, rbs)
|
56
76
|
when AST::Declarations::ModuleDecl
|
57
|
-
translate_module_decl(decl)
|
77
|
+
translate_module_decl(decl, rbs)
|
58
78
|
when AST::Declarations::ConstantDecl
|
59
|
-
translate_constant_decl(decl)
|
79
|
+
translate_constant_decl(decl, rbs)
|
80
|
+
when AST::Declarations::BlockDecl
|
81
|
+
if decl.module_class_annotation
|
82
|
+
case decl.module_class_annotation
|
83
|
+
when AST::Annotations::ModuleDecl
|
84
|
+
translate_module_block_decl(decl, rbs)
|
85
|
+
when AST::Annotations::ClassDecl
|
86
|
+
translate_class_block_decl(decl, rbs)
|
87
|
+
end
|
88
|
+
end
|
60
89
|
end
|
61
90
|
end
|
62
91
|
|
63
92
|
# @rbs decl: AST::Declarations::ClassDecl
|
64
|
-
# @rbs
|
65
|
-
|
93
|
+
# @rbs rbs: _Content
|
94
|
+
# @rbs return: void
|
95
|
+
def translate_class_decl(decl, rbs)
|
66
96
|
return unless decl.class_name
|
67
97
|
|
68
98
|
if decl.comments
|
69
|
-
comment = RBS::AST::Comment.new(string: decl.comments.content, location: nil)
|
99
|
+
comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil)
|
70
100
|
end
|
71
101
|
|
72
102
|
members = [] #: Array[RBS::AST::Members::t | RBS::AST::Declarations::t]
|
73
103
|
|
74
|
-
decl.members
|
75
|
-
if member.is_a?(AST::Members::Base)
|
76
|
-
if rbs_member = translate_member(member, decl)
|
77
|
-
members.concat rbs_member
|
78
|
-
end
|
79
|
-
end
|
104
|
+
translate_members(decl.members, nil, members)
|
80
105
|
|
81
|
-
|
82
|
-
members.concat translate_singleton_decl(member)
|
83
|
-
elsif member.is_a?(AST::Declarations::Base)
|
84
|
-
if rbs = translate_decl(member)
|
85
|
-
members << rbs
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
RBS::AST::Declarations::Class.new(
|
106
|
+
rbs << RBS::AST::Declarations::Class.new(
|
91
107
|
name: decl.class_name,
|
92
108
|
type_params: decl.type_params,
|
93
109
|
members: members,
|
@@ -98,36 +114,46 @@ module RBS
|
|
98
114
|
)
|
99
115
|
end
|
100
116
|
|
117
|
+
# @rbs members: Array[AST::Declarations::t | AST::Members::t]
|
118
|
+
# @rbs decl: AST::Declarations::SingletonClassDecl?
|
119
|
+
# @rbs rbs: _Content
|
120
|
+
# @rbs return: void
|
121
|
+
def translate_members(members, decl, rbs)
|
122
|
+
members.each do |member|
|
123
|
+
case member
|
124
|
+
when AST::Members::Base
|
125
|
+
translate_member(member, decl, rbs)
|
126
|
+
when AST::Declarations::SingletonClassDecl
|
127
|
+
translate_singleton_decl(member, rbs)
|
128
|
+
when AST::Declarations::BlockDecl
|
129
|
+
if member.module_class_annotation
|
130
|
+
translate_decl(member, rbs)
|
131
|
+
else
|
132
|
+
translate_members(member.members, decl, rbs)
|
133
|
+
end
|
134
|
+
when AST::Declarations::ClassDecl, AST::Declarations::ModuleDecl, AST::Declarations::ConstantDecl
|
135
|
+
translate_decl(member, rbs)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
101
140
|
# @rbs decl: AST::Declarations::ModuleDecl
|
102
|
-
# @rbs
|
103
|
-
|
141
|
+
# @rbs rbs: _Content
|
142
|
+
# @rbs return: void
|
143
|
+
def translate_module_decl(decl, rbs)
|
104
144
|
return unless decl.module_name
|
105
145
|
|
106
146
|
if decl.comments
|
107
|
-
comment = RBS::AST::Comment.new(string: decl.comments.content, location: nil)
|
147
|
+
comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil)
|
108
148
|
end
|
109
149
|
|
110
150
|
members = [] #: Array[RBS::AST::Members::t | RBS::AST::Declarations::t]
|
111
151
|
|
112
|
-
decl.members
|
113
|
-
if member.is_a?(AST::Members::Base)
|
114
|
-
if rbs_member = translate_member(member, decl)
|
115
|
-
members.concat rbs_member
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
if member.is_a?(AST::Declarations::SingletonClassDecl)
|
120
|
-
members.concat translate_singleton_decl(member)
|
121
|
-
elsif member.is_a?(AST::Declarations::Base)
|
122
|
-
if rbs = translate_decl(member)
|
123
|
-
members << rbs
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
152
|
+
translate_members(decl.members, nil, members)
|
127
153
|
|
128
154
|
self_types = decl.module_selfs.map { _1.constraint }.compact
|
129
155
|
|
130
|
-
RBS::AST::Declarations::Module.new(
|
156
|
+
rbs << RBS::AST::Declarations::Module.new(
|
131
157
|
name: decl.module_name,
|
132
158
|
type_params: decl.type_params,
|
133
159
|
members: members,
|
@@ -139,15 +165,16 @@ module RBS
|
|
139
165
|
end
|
140
166
|
|
141
167
|
# @rbs decl: AST::Declarations::ConstantDecl
|
142
|
-
# @rbs
|
143
|
-
|
168
|
+
# @rbs rbs: _Content
|
169
|
+
# @rbs return: void
|
170
|
+
def translate_constant_decl(decl, rbs)
|
144
171
|
return unless decl.constant_name
|
145
172
|
|
146
173
|
if decl.comments
|
147
|
-
comment = RBS::AST::Comment.new(string: decl.comments.content, location: nil)
|
174
|
+
comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil)
|
148
175
|
end
|
149
176
|
|
150
|
-
RBS::AST::Declarations::Constant.new(
|
177
|
+
rbs << RBS::AST::Declarations::Constant.new(
|
151
178
|
name: decl.constant_name,
|
152
179
|
type: decl.type,
|
153
180
|
comment: comment,
|
@@ -156,95 +183,87 @@ module RBS
|
|
156
183
|
end
|
157
184
|
|
158
185
|
# @rbs decl: AST::Declarations::SingletonClassDecl
|
159
|
-
# @rbs
|
160
|
-
|
161
|
-
|
162
|
-
|
186
|
+
# @rbs rbs: _Content
|
187
|
+
# @rbs return: void
|
188
|
+
def translate_singleton_decl(decl, rbs)
|
163
189
|
decl.members.each do |member|
|
164
190
|
if member.is_a?(AST::Members::Base)
|
165
|
-
|
166
|
-
members.concat rbs_member
|
167
|
-
end
|
191
|
+
translate_member(member, decl, rbs)
|
168
192
|
end
|
169
193
|
end
|
170
|
-
|
171
|
-
members
|
172
194
|
end
|
173
195
|
|
174
196
|
# @rbs member: AST::Members::t
|
175
|
-
# @rbs decl: AST::Declarations::
|
176
|
-
#
|
177
|
-
|
197
|
+
# @rbs decl: AST::Declarations::SingletonClassDecl? --
|
198
|
+
# The surrouding singleton class definition
|
199
|
+
# @rbs rbs: _Content
|
200
|
+
# @rbs return void
|
201
|
+
def translate_member(member, decl, rbs)
|
178
202
|
case member
|
179
203
|
when AST::Members::RubyDef
|
180
204
|
if member.comments
|
181
|
-
comment = RBS::AST::Comment.new(string: member.comments.content, location: nil)
|
205
|
+
comment = RBS::AST::Comment.new(string: member.comments.content(trim: true), location: nil)
|
182
206
|
end
|
183
207
|
|
184
208
|
kind = method_kind(member, decl)
|
185
209
|
|
186
210
|
if member.override_annotation
|
187
|
-
|
188
|
-
RBS::AST::Members::MethodDefinition.new(
|
189
|
-
name: member.method_name,
|
190
|
-
kind: kind,
|
191
|
-
overloads: [],
|
192
|
-
annotations: [],
|
193
|
-
location: nil,
|
194
|
-
comment: comment,
|
195
|
-
overloading: true,
|
196
|
-
visibility: member.visibility
|
197
|
-
)
|
198
|
-
]
|
199
|
-
end
|
200
|
-
|
201
|
-
[
|
202
|
-
RBS::AST::Members::MethodDefinition.new(
|
211
|
+
rbs << RBS::AST::Members::MethodDefinition.new(
|
203
212
|
name: member.method_name,
|
204
213
|
kind: kind,
|
205
|
-
overloads:
|
206
|
-
annotations:
|
214
|
+
overloads: [],
|
215
|
+
annotations: [],
|
207
216
|
location: nil,
|
208
217
|
comment: comment,
|
209
|
-
overloading:
|
218
|
+
overloading: true,
|
210
219
|
visibility: member.visibility
|
211
220
|
)
|
212
|
-
|
221
|
+
return
|
222
|
+
end
|
223
|
+
|
224
|
+
rbs << RBS::AST::Members::MethodDefinition.new(
|
225
|
+
name: member.method_name,
|
226
|
+
kind: kind,
|
227
|
+
overloads: member.method_overloads,
|
228
|
+
annotations: member.method_annotations,
|
229
|
+
location: nil,
|
230
|
+
comment: comment,
|
231
|
+
overloading: member.overloading?,
|
232
|
+
visibility: member.visibility
|
233
|
+
)
|
213
234
|
when AST::Members::RubyAlias
|
214
235
|
if member.comments
|
215
|
-
comment = RBS::AST::Comment.new(string: member.comments.content, location: nil)
|
236
|
+
comment = RBS::AST::Comment.new(string: member.comments.content(trim: true), location: nil)
|
216
237
|
end
|
217
238
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
)
|
227
|
-
]
|
239
|
+
rbs << RBS::AST::Members::Alias.new(
|
240
|
+
new_name: member.new_name,
|
241
|
+
old_name: member.old_name,
|
242
|
+
kind: :instance,
|
243
|
+
annotations: [],
|
244
|
+
location: nil,
|
245
|
+
comment: comment
|
246
|
+
)
|
228
247
|
when AST::Members::RubyMixin
|
229
|
-
|
248
|
+
if m = member.rbs
|
249
|
+
rbs << m
|
250
|
+
end
|
230
251
|
when AST::Members::RubyAttr
|
231
|
-
member.rbs
|
252
|
+
if m = member.rbs
|
253
|
+
rbs.concat m
|
254
|
+
end
|
232
255
|
when AST::Members::RubyPrivate
|
233
|
-
|
234
|
-
RBS::AST::Members::Private.new(location: nil)
|
235
|
-
]
|
256
|
+
rbs << RBS::AST::Members::Private.new(location: nil)
|
236
257
|
when AST::Members::RubyPublic
|
237
|
-
|
238
|
-
RBS::AST::Members::Public.new(location: nil)
|
239
|
-
]
|
258
|
+
rbs << RBS::AST::Members::Public.new(location: nil)
|
240
259
|
when AST::Members::RBSIvar
|
241
|
-
|
242
|
-
|
243
|
-
|
260
|
+
if m = member.rbs
|
261
|
+
rbs << m
|
262
|
+
end
|
244
263
|
when AST::Members::RBSEmbedded
|
245
264
|
case members = member.members
|
246
265
|
when Array
|
247
|
-
members
|
266
|
+
rbs.concat members
|
248
267
|
end
|
249
268
|
end
|
250
269
|
end
|
@@ -265,10 +284,10 @@ module RBS
|
|
265
284
|
# ```
|
266
285
|
#
|
267
286
|
# @rbs member: AST::Members::RubyDef
|
268
|
-
# @rbs decl: AST::Declarations::
|
269
|
-
# @rbs
|
287
|
+
# @rbs decl: AST::Declarations::SingletonClassDecl?
|
288
|
+
# @rbs return: RBS::AST::Members::MethodDefinition::kind
|
270
289
|
def method_kind(member, decl)
|
271
|
-
return :singleton if decl
|
290
|
+
return :singleton if decl
|
272
291
|
|
273
292
|
case member.node.receiver
|
274
293
|
when Prism::SelfNode
|
@@ -277,6 +296,64 @@ module RBS
|
|
277
296
|
:instance
|
278
297
|
end
|
279
298
|
end
|
299
|
+
|
300
|
+
# @rbs block: AST::Declarations::BlockDecl
|
301
|
+
# @rbs rbs: _Content
|
302
|
+
# @rbs return: void
|
303
|
+
def translate_module_block_decl(block, rbs)
|
304
|
+
annotation = block.module_class_annotation
|
305
|
+
annotation.is_a?(AST::Annotations::ModuleDecl) or raise
|
306
|
+
|
307
|
+
return unless annotation.name
|
308
|
+
|
309
|
+
if block.comments
|
310
|
+
comment = RBS::AST::Comment.new(string: block.comments.content(trim: true), location: nil)
|
311
|
+
end
|
312
|
+
|
313
|
+
members = [] #: Array[RBS::AST::Members::t | RBS::AST::Declarations::t]
|
314
|
+
|
315
|
+
translate_members(block.members, nil, members)
|
316
|
+
|
317
|
+
self_types = annotation.self_types
|
318
|
+
|
319
|
+
rbs << RBS::AST::Declarations::Module.new(
|
320
|
+
name: annotation.name,
|
321
|
+
type_params: annotation.type_params,
|
322
|
+
members: members,
|
323
|
+
self_types: self_types,
|
324
|
+
annotations: [],
|
325
|
+
location: nil,
|
326
|
+
comment: comment
|
327
|
+
)
|
328
|
+
end
|
329
|
+
|
330
|
+
# @rbs block: AST::Declarations::BlockDecl
|
331
|
+
# @rbs rbs: _Content
|
332
|
+
# @rbs return: void
|
333
|
+
def translate_class_block_decl(block, rbs)
|
334
|
+
annotation = block.module_class_annotation
|
335
|
+
annotation.is_a?(AST::Annotations::ClassDecl) or raise
|
336
|
+
|
337
|
+
return unless annotation.name
|
338
|
+
|
339
|
+
if block.comments
|
340
|
+
comment = RBS::AST::Comment.new(string: block.comments.content(trim: true), location: nil)
|
341
|
+
end
|
342
|
+
|
343
|
+
members = [] #: Array[RBS::AST::Members::t | RBS::AST::Declarations::t]
|
344
|
+
|
345
|
+
translate_members(block.members, nil, members)
|
346
|
+
|
347
|
+
rbs << RBS::AST::Declarations::Class.new(
|
348
|
+
name: annotation.name,
|
349
|
+
type_params: annotation.type_params,
|
350
|
+
members: members,
|
351
|
+
super_class: annotation.super_class,
|
352
|
+
annotations: [],
|
353
|
+
location: nil,
|
354
|
+
comment: comment
|
355
|
+
)
|
356
|
+
end
|
280
357
|
end
|
281
358
|
end
|
282
359
|
end
|
data/lib/rbs/inline.rb
CHANGED
@@ -0,0 +1,221 @@
|
|
1
|
+
# Generated from lib/rbs/inline/annotation_parser/tokenizer.rb with RBS::Inline
|
2
|
+
|
3
|
+
module RBS
|
4
|
+
module Inline
|
5
|
+
class AnnotationParser
|
6
|
+
module Tokens
|
7
|
+
K_RETURN: ::Symbol
|
8
|
+
|
9
|
+
K_INHERITS: ::Symbol
|
10
|
+
|
11
|
+
K_AS: ::Symbol
|
12
|
+
|
13
|
+
K_OVERRIDE: ::Symbol
|
14
|
+
|
15
|
+
K_USE: ::Symbol
|
16
|
+
|
17
|
+
K_MODULE_SELF: ::Symbol
|
18
|
+
|
19
|
+
K_GENERIC: ::Symbol
|
20
|
+
|
21
|
+
K_IN: ::Symbol
|
22
|
+
|
23
|
+
K_OUT: ::Symbol
|
24
|
+
|
25
|
+
K_UNCHECKED: ::Symbol
|
26
|
+
|
27
|
+
K_SELF: ::Symbol
|
28
|
+
|
29
|
+
K_SKIP: ::Symbol
|
30
|
+
|
31
|
+
K_YIELDS: ::Symbol
|
32
|
+
|
33
|
+
K_MODULE: ::Symbol
|
34
|
+
|
35
|
+
K_CLASS: ::Symbol
|
36
|
+
|
37
|
+
K_COLON2: ::Symbol
|
38
|
+
|
39
|
+
K_COLON: ::Symbol
|
40
|
+
|
41
|
+
K_LBRACKET: ::Symbol
|
42
|
+
|
43
|
+
K_RBRACKET: ::Symbol
|
44
|
+
|
45
|
+
K_COMMA: ::Symbol
|
46
|
+
|
47
|
+
K_STAR2: ::Symbol
|
48
|
+
|
49
|
+
K_STAR: ::Symbol
|
50
|
+
|
51
|
+
K_MINUS2: ::Symbol
|
52
|
+
|
53
|
+
K_LT: ::Symbol
|
54
|
+
|
55
|
+
K_DOT3: ::Symbol
|
56
|
+
|
57
|
+
K_DOT: ::Symbol
|
58
|
+
|
59
|
+
K_ARROW: ::Symbol
|
60
|
+
|
61
|
+
K_LBRACE: ::Symbol
|
62
|
+
|
63
|
+
K_LPAREN: ::Symbol
|
64
|
+
|
65
|
+
K_AMP: ::Symbol
|
66
|
+
|
67
|
+
K_QUESTION: ::Symbol
|
68
|
+
|
69
|
+
K_VBAR: ::Symbol
|
70
|
+
|
71
|
+
K_EOF: ::Symbol
|
72
|
+
|
73
|
+
# `@rbs!`
|
74
|
+
K_RBSE: ::Symbol
|
75
|
+
|
76
|
+
# `@rbs`
|
77
|
+
K_RBS: ::Symbol
|
78
|
+
|
79
|
+
T_UIDENT: ::Symbol
|
80
|
+
|
81
|
+
T_IFIDENT: ::Symbol
|
82
|
+
|
83
|
+
T_LVAR: ::Symbol
|
84
|
+
|
85
|
+
# The body of comment string following `--`
|
86
|
+
T_COMMENT: ::Symbol
|
87
|
+
|
88
|
+
# Type/method type source
|
89
|
+
T_SOURCE: ::Symbol
|
90
|
+
|
91
|
+
# Block type source
|
92
|
+
T_BLOCKSTR: ::Symbol
|
93
|
+
|
94
|
+
# `!` local variable
|
95
|
+
T_ELVAR: ::Symbol
|
96
|
+
|
97
|
+
T_ATIDENT: ::Symbol
|
98
|
+
|
99
|
+
T_ANNOTATION: ::Symbol
|
100
|
+
|
101
|
+
T_WHITESPACE: ::Symbol
|
102
|
+
end
|
103
|
+
|
104
|
+
class Tokenizer
|
105
|
+
include Tokens
|
106
|
+
|
107
|
+
KEYWORDS: Hash[String, Symbol]
|
108
|
+
|
109
|
+
KW_RE: ::Regexp
|
110
|
+
|
111
|
+
PUNCTS: Hash[String, Symbol]
|
112
|
+
|
113
|
+
PUNCTS_RE: Regexp
|
114
|
+
|
115
|
+
attr_reader scanner: StringScanner
|
116
|
+
|
117
|
+
# Tokens that comes after the current position
|
118
|
+
#
|
119
|
+
# This is a four tuple of tokens.
|
120
|
+
#
|
121
|
+
# 1. The first array is a trivia tokens before current position
|
122
|
+
# 2. The second token is the first lookahead token after the current position
|
123
|
+
# 3. The third array is a trivia tokens between the first lookahead and the second lookahead
|
124
|
+
# 4. The fourth token is the second lookahead token
|
125
|
+
attr_reader lookahead_tokens: [ Array[token], token?, Array[token], token? ]
|
126
|
+
|
127
|
+
# Token that comes after the current position
|
128
|
+
# @rbs %a{pure}
|
129
|
+
%a{pure}
|
130
|
+
def lookahead1: () -> token?
|
131
|
+
|
132
|
+
# Token that comes after `lookahead1`
|
133
|
+
# @rbs %a{pure}
|
134
|
+
%a{pure}
|
135
|
+
def lookahead2: () -> token?
|
136
|
+
|
137
|
+
# Returns the current char position of the first lookahead token
|
138
|
+
#
|
139
|
+
# ```
|
140
|
+
# __ foo ___ bar baz
|
141
|
+
# ^^ Trivia tokens before lookahead1
|
142
|
+
# ^ #current_position
|
143
|
+
# ^^^ lookahead1
|
144
|
+
# ^^^ Trivia tokens between lookahead1 and lookahead2
|
145
|
+
# ^^^ lookahead2
|
146
|
+
# ^ <= scanner.charpos
|
147
|
+
# ```
|
148
|
+
def current_position: () -> Integer
|
149
|
+
|
150
|
+
def lookaheads: () -> Array[Symbol?]
|
151
|
+
|
152
|
+
# @rbs scanner: StringScanner
|
153
|
+
# @rbs return: void
|
154
|
+
def initialize: (StringScanner scanner) -> void
|
155
|
+
|
156
|
+
# Advances the scanner
|
157
|
+
#
|
158
|
+
# @rbs tree: AST::Tree -- Tree to insert trivia tokens
|
159
|
+
# @rbs eat: bool -- true to add the current lookahead token into the tree
|
160
|
+
# @rbs return: void
|
161
|
+
def advance: (AST::Tree tree, ?eat: bool) -> void
|
162
|
+
|
163
|
+
# @rbs (AST::Tree?) -> String
|
164
|
+
def consume_trivias: (AST::Tree?) -> String
|
165
|
+
|
166
|
+
# Returns true if the scanner cannot consume next token
|
167
|
+
def stuck?: () -> bool
|
168
|
+
|
169
|
+
# Skips characters
|
170
|
+
#
|
171
|
+
# This method ensures the `current_position` will be the given `position`.
|
172
|
+
#
|
173
|
+
# @rbs position: Integer -- The new position
|
174
|
+
# @rbs tree: AST::Tree -- Tree to insert trivia tokens
|
175
|
+
# @rbs return: void
|
176
|
+
def reset: (Integer position, AST::Tree tree) -> void
|
177
|
+
|
178
|
+
def rest: () -> String
|
179
|
+
|
180
|
+
# Consume given token type and inserts the token to the tree or `nil`
|
181
|
+
#
|
182
|
+
# @rbs *types: Symbol
|
183
|
+
# @rbs tree: AST::Tree
|
184
|
+
# @rbs return: void
|
185
|
+
def consume_token: (*Symbol types, tree: AST::Tree) -> void
|
186
|
+
|
187
|
+
# Consume given token type and inserts the token to the tree or raise
|
188
|
+
#
|
189
|
+
# @rbs *types: Symbol
|
190
|
+
# @rbs tree: AST::Tree
|
191
|
+
# @rbs return: void
|
192
|
+
def consume_token!: (*Symbol types, tree: AST::Tree) -> void
|
193
|
+
|
194
|
+
# Test if current token has specified `type`
|
195
|
+
#
|
196
|
+
# @rbs *types: Symbol
|
197
|
+
# @rbs return: bool
|
198
|
+
def type?: (*Symbol types) -> bool
|
199
|
+
|
200
|
+
# Test if lookahead2 token have specified `type`
|
201
|
+
#
|
202
|
+
# @rbs *types: Symbol -- The type of the lookahead2 token
|
203
|
+
# @rbs return: bool
|
204
|
+
def type2?: (*Symbol types) -> bool
|
205
|
+
|
206
|
+
# Ensure current token is one of the specified in types
|
207
|
+
#
|
208
|
+
# @rbs *types: Symbol
|
209
|
+
# @rbs return: void
|
210
|
+
def type!: (*Symbol types) -> void
|
211
|
+
|
212
|
+
# Reset the current_token to incoming comment `--`
|
213
|
+
#
|
214
|
+
# Reset to the end of the input if `--` token cannot be found.
|
215
|
+
#
|
216
|
+
# @rbs return: String -- String that is skipped
|
217
|
+
def skip_to_comment: () -> String
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|