rbs-inline 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -3,127 +3,148 @@
3
3
  module RBS
4
4
  module Inline
5
5
  class AnnotationParser
6
+ # ParsingResut groups consecutive comments, which may contain several annotations
7
+ #
8
+ # *Consecutive comments* are comments are defined in below.
9
+ # They are basically comments that follows from the previous line, but there are some more requirements.
10
+ #
11
+ # ```ruby
12
+ # # Line 1
13
+ # # Line 2 #=> Line 1 and Line 2 are consecutive
14
+ #
15
+ # # Line 3
16
+ # # Line4 #=> Line 3 and Line 4 are not consecutive, because the starting column are different
17
+ #
18
+ # # Line 5
19
+ # foo() # Line 6 #=> Line 5 and Line 6 are not consecutive, because Line 6 has leading code
20
+ # ```
6
21
  class ParsingResult
7
22
  attr_reader comments: Array[Prism::Comment]
8
23
 
9
- attr_reader annotations: Array[AST::Annotations::t]
24
+ attr_reader annotations: Array[AST::Annotations::t | AST::CommentLines]
10
25
 
11
26
  attr_reader first_comment_offset: Integer
12
27
 
28
+ # : () { (AST::Annotations::t) -> void } -> void
29
+ # : () -> Enumerator[AST::Annotations::t, void]
30
+ def each_annotation: () { (AST::Annotations::t) -> void } -> void
31
+ | () -> Enumerator[AST::Annotations::t, void]
32
+
13
33
  # @rbs first_comment: Prism::Comment
14
- def initialize: (Prism::Comment first_comment) -> untyped
34
+ def initialize: (Prism::Comment first_comment) -> void
15
35
 
16
- # @rbs returns Range[Integer]
36
+ # @rbs return: Range[Integer]
17
37
  def line_range: () -> Range[Integer]
18
38
 
19
- # @rbs returns Prism::Comment
39
+ # @rbs return: Prism::Comment
20
40
  def last_comment: () -> Prism::Comment
21
41
 
22
42
  # @rbs comment: Prism::Comment
23
- # @rbs returns self?
43
+ # @rbs return: self?
24
44
  def add_comment: (Prism::Comment comment) -> self?
25
45
 
26
- # @rbs returns Array[[String, Prism::Comment]]
27
- def lines: () -> Array[[ String, Prism::Comment ]]
46
+ # @rbs trim: bool -- `true` to trim the leading whitespaces
47
+ def content: (?trim: bool) -> String
28
48
 
29
- # @rbs returns String
30
- def content: () -> String
49
+ def lines: () -> Array[String]
31
50
  end
32
51
 
52
+ include Tokens
53
+
33
54
  attr_reader input: Array[Prism::Comment]
34
55
 
35
56
  # @rbs input: Array[Prism::Comment]
36
- def initialize: (Array[Prism::Comment] input) -> untyped
57
+ def initialize: (Array[Prism::Comment] input) -> void
37
58
 
38
59
  # @rbs input: Array[Prism::Comment]
39
- # @rbs returns Array[ParsingResult]
60
+ # @rbs return: Array[ParsingResult]
40
61
  def self.parse: (Array[Prism::Comment] input) -> Array[ParsingResult]
41
62
 
42
- # @rbs returns Array[ParsingResult]
63
+ # @rbs return: Array[ParsingResult]
43
64
  def parse: () -> Array[ParsingResult]
44
65
 
45
66
  private
46
67
 
47
- # @rbs result: ParsingResult
48
- # @rbs block: ^(Array[Prism::Comment]) -> void
49
- # @rbs returns void
50
- def each_annotation_paragraph: (ParsingResult result) { (Array[Prism::Comment]) -> void } -> void
51
-
52
- class Tokenizer
53
- attr_reader scanner: StringScanner
54
-
55
- attr_reader current_token: token?
56
-
57
- KEYWORDS: Hash[String, Symbol]
58
-
59
- KW_RE: ::Regexp
60
-
61
- PUNCTS: Hash[String, Symbol]
62
-
63
- PUNCTS_RE: Regexp
64
-
65
- # @rbs scanner: StringScanner
66
- # @rbs returns void
67
- def initialize: (StringScanner scanner) -> void
68
-
69
- # @rbs tree: AST::Tree
70
- # @rbs returns token?
71
- def advance: (AST::Tree tree) -> token?
72
-
73
- # Consume given token type and inserts the token to the tree or `nil`
74
- #
75
- # @rbs type: Array[Symbol]
76
- # @rbs tree: AST::Tree
77
- # @rbs returns void
78
- def consume_token: (*untyped types, tree: AST::Tree) -> void
79
-
80
- # Consume given token type and inserts the token to the tree or raise
81
- #
82
- # @rbs type: Array[Symbol]
83
- # @rbs tree: AST::Tree
84
- # @rbs returns void
85
- def consume_token!: (*untyped types, tree: AST::Tree) -> void
68
+ # Test if the comment is an annotation comment
69
+ #
70
+ # - Returns `nil` if the comment is not an annotation.
71
+ # - Returns `true` if the comment is `#:` or `#[` annotation. (Offset is `1`)
72
+ # - Returns Integer if the comment is `#@rbs` annotation. (Offset is the number of leading spaces including `#`)
73
+ #
74
+ # : (Prism::Comment) -> (Integer | true | nil)
75
+ def annotation_comment?: (Prism::Comment) -> (Integer | true | nil)
86
76
 
87
- # Test if current token has specified `type`
88
- #
89
- # @rbs type: Array[Symbol]
90
- # @rbs returns bool
91
- def type?: (*Symbol type) -> bool
77
+ # Split lines of comments in `result` into paragraphs
78
+ #
79
+ # A paragraph consists of:
80
+ #
81
+ # * An annotation syntax constructs -- starting with `@rbs` or `::`, or
82
+ # * A lines something else
83
+ #
84
+ # Yields an array of comments, and a boolean indicating if the comments may be an annotation.
85
+ #
86
+ # : (ParsingResult) { (Array[Prism::Comment], bool is_annotation) -> void } -> void
87
+ def each_annotation_paragraph: (ParsingResult) { (Array[Prism::Comment], bool is_annotation) -> void } -> void
92
88
 
93
- # Ensure current token is one of the specified in types
94
- #
95
- # @rbs types: Array[Symbol]
96
- # @rbs returns void
97
- def type!: (*Symbol types) -> void
89
+ # The first annotation line is already detected and consumed.
90
+ # The annotation comment is already in `comments`.
91
+ #
92
+ # @rbs comments: Array[Prism::Comment] -- Annotation comments
93
+ # @rbs lines: Array[Prism::Comment] -- Lines to be consumed
94
+ # @rbs offset: Integer -- Offset of the first character of the first annotation comment from the `#` (>= 1)
95
+ # @rbs allow_empty_lines: bool -- `true` if empty line is allowed inside the annotation comments
96
+ # @rbs &block: (Array[Prism::Comment], bool is_annotation) -> void
97
+ # @rbs return: void
98
+ def yield_annotation: (Array[Prism::Comment] comments, Array[Prism::Comment] lines, Integer offset, allow_empty_lines: bool) { (Array[Prism::Comment], bool is_annotation) -> void } -> void
99
+
100
+ # The first line is NOT consumed.
101
+ #
102
+ # The `comments` may be empty.
103
+ #
104
+ # @rbs comments: Array[Prism::Comment] -- Leading comments
105
+ # @rbs lines: Array[Prism::Comment] -- Lines to be consumed
106
+ # @rbs &block: (Array[Prism::Comment], bool is_annotation) -> void
107
+ # @rbs return: void
108
+ def yield_paragraph: (Array[Prism::Comment] comments, Array[Prism::Comment] lines) { (Array[Prism::Comment], bool is_annotation) -> void } -> void
98
109
 
99
- # Reset the current_token to incoming comment `--`
100
- #
101
- # Reset to the end of the input if `--` token cannot be found.
102
- #
103
- # @rbs returns String -- String that is skipped
104
- def skip_to_comment: () -> String
105
- end
110
+ # Consumes empty lines between annotation lines
111
+ #
112
+ # An empty line is already detected and consumed.
113
+ # The line is already removed from `lines` and put in `empty_comments`.
114
+ #
115
+ # Note that the arguments, `comments`, `empty_comments`, and `lines` are modified in place.
116
+ #
117
+ # @rbs comments: Array[Prism::Comment] -- Non empty annotation comments
118
+ # @rbs empty_comments: Array[Prism::Comment] -- Empty comments that may be part of the annotation
119
+ # @rbs lines: Array[Prism::Comment] -- Lines
120
+ # @rbs offset: Integer -- Offset of the first character of the annotation
121
+ # @rbs &block: (Array[Prism::Comment], bool is_annotation) -> void
122
+ # @rbs return: void
123
+ def yield_empty_annotation: (Array[Prism::Comment] comments, Array[Prism::Comment] empty_comments, Array[Prism::Comment] lines, Integer offset) { (Array[Prism::Comment], bool is_annotation) -> void } -> void
106
124
 
107
125
  # @rbs comments: AST::CommentLines
108
- # @rbs returns AST::Annotations::t?
126
+ # @rbs return: AST::Annotations::t?
109
127
  def parse_annotation: (AST::CommentLines comments) -> AST::Annotations::t?
110
128
 
111
129
  # @rbs tokenizer: Tokenizer
112
- # @rbs returns AST::Tree
130
+ # @rbs return: AST::Tree
113
131
  def parse_var_decl: (Tokenizer tokenizer) -> AST::Tree
114
132
 
115
133
  # @rbs tokenizer: Tokenizer
116
- # @rbs returns AST::Tree
134
+ # @rbs return: AST::Tree
117
135
  def parse_return_type_decl: (Tokenizer tokenizer) -> AST::Tree
118
136
 
119
137
  # @rbs tokenizer: Tokenizer
120
- # @rbs returns AST::Tree
138
+ # @rbs return: AST::Tree
121
139
  def parse_comment: (Tokenizer tokenizer) -> AST::Tree
122
140
 
123
141
  # @rbs tokenizer: Tokenizer
124
- # @rbs returns AST::Tree
142
+ # @rbs return: AST::Tree
125
143
  def parse_type_app: (Tokenizer tokenizer) -> AST::Tree
126
144
 
145
+ # @rbs (Tokenizer) -> AST::Tree
146
+ def parse_method_type_annotation: (Tokenizer) -> AST::Tree
147
+
127
148
  # Parse a RBS method type or type and returns it
128
149
  #
129
150
  # It tries parsing a method type, and then parsing a type if failed.
@@ -134,9 +155,20 @@ module RBS
134
155
  #
135
156
  # @rbs tokenizer: Tokenizer
136
157
  # @rbs parent_tree: AST::Tree
137
- # @rbs returns MethodType | AST::Tree | Types::t | nil
158
+ # @rbs return: MethodType | AST::Tree | Types::t | nil
138
159
  def parse_type_method_type: (Tokenizer tokenizer, AST::Tree parent_tree) -> (MethodType | AST::Tree | Types::t | nil)
139
160
 
161
+ # Parse a RBS method type
162
+ #
163
+ # If parsing failed, it returns a Tree(`:type_syntax_error), consuming all of the remaining input.
164
+ #
165
+ # Note that this doesn't recognize `--` comment unlike `parse_type`.
166
+ #
167
+ # @rbs tokenizer: Tokenizer
168
+ # @rbs parent_tree: AST::Tree
169
+ # @rbs return: MethodType | AST::Tree
170
+ def parse_method_type: (Tokenizer tokenizer, AST::Tree parent_tree) -> (MethodType | AST::Tree)
171
+
140
172
  # Parse a RBS type and returns it
141
173
  #
142
174
  # If parsing failed, it returns a Tree(`:type_syntax_error), consuming
@@ -152,27 +184,27 @@ module RBS
152
184
  #
153
185
  # @rbs tokenizer: Tokenizer
154
186
  # @rbs parent_tree: AST::Tree
155
- # @rbs returns Types::t | AST::Tree | nil
187
+ # @rbs return: Types::t | AST::Tree | nil
156
188
  def parse_type: (Tokenizer tokenizer, AST::Tree parent_tree) -> (Types::t | AST::Tree | nil)
157
189
 
158
190
  # @rbs tokenizer: Tokenizer
159
- # @rbs returns AST::Tree
191
+ # @rbs return: AST::Tree
160
192
  def parse_rbs_annotation: (Tokenizer tokenizer) -> AST::Tree
161
193
 
162
- # @rbs tokznier: Tokenizer
163
- # @rbs returns AST::Tree
164
- def parse_inherits: (untyped tokenizer) -> AST::Tree
194
+ # @rbs tokenizer: Tokenizer
195
+ # @rbs return: AST::Tree
196
+ def parse_inherits: (Tokenizer tokenizer) -> AST::Tree
165
197
 
166
198
  # Parse `@rbs override` annotation
167
199
  #
168
200
  # @rbs tokenizer: Tokenizer
169
- # @rbs returns AST::Tree
201
+ # @rbs return: AST::Tree
170
202
  def parse_override: (Tokenizer tokenizer) -> AST::Tree
171
203
 
172
204
  # Parse `@rbs use [CLAUSES]` annotation
173
205
  #
174
206
  # @rbs tokenizer: Tokenizer
175
- # @rbs returns AST::Tree
207
+ # @rbs return: AST::Tree
176
208
  def parse_use: (Tokenizer tokenizer) -> AST::Tree
177
209
 
178
210
  # Parses use clause
@@ -185,11 +217,11 @@ module RBS
185
217
  # * [`::`?, [UIDENT) `::`]*, `*`]
186
218
  #
187
219
  # @rbs tokenizer: Tokenizer
188
- # @rbs returns AST::Tree
220
+ # @rbs return: AST::Tree
189
221
  def parse_use_clause: (Tokenizer tokenizer) -> AST::Tree
190
222
 
191
223
  # @rbs tokenizer: Tokenizer
192
- # @rbs returns AST::Tree
224
+ # @rbs return: AST::Tree
193
225
  def parse_module_self: (Tokenizer tokenizer) -> AST::Tree
194
226
 
195
227
  # Yield the block and return the resulting tree if tokenizer has current token of `types`
@@ -197,26 +229,50 @@ module RBS
197
229
  # ```rb
198
230
  # # Test if tokenize has `--` token, then parse comment or insert `nil` to tree
199
231
  #
200
- # tree << parse_optional(tokenizer, :kMINUS2) do
232
+ # tree << parse_optional(tokenizer, K_MINUS2) do
201
233
  # parse_comment(tokenizer)
202
234
  # end
203
235
  # ```
204
236
  #
237
+ # If `tree:` is given, it consumes trivia tokens before yielding the block.
238
+ #
205
239
  # @rbs tokenizer: Tokenizer
206
- # @rbs types: Array[Symbol]
207
- # @rbs block: ^() -> AST::Tree
208
- # @rbs returns AST::Tree?
209
- def parse_optional: (Tokenizer tokenizer, *Symbol types) { () -> AST::Tree } -> AST::Tree?
240
+ # @rbs *types: Symbol
241
+ # @rbs tree: AST::Tree? -- the parent tree to consume leading trivia tokens
242
+ # @rbs &block: () -> AST::Tree
243
+ # @rbs return: AST::Tree?
244
+ def parse_optional: (Tokenizer tokenizer, *Symbol types, ?tree: AST::Tree?) { () -> AST::Tree } -> AST::Tree?
210
245
 
211
246
  # @rbs tokenizer: Tokenizer
212
- # @rbs returns AST::Tree
247
+ # @rbs return: AST::Tree
213
248
  def parse_generic: (Tokenizer tokenizer) -> AST::Tree
214
249
 
215
- # :: (Tokenizer) -> AST::Tree
250
+ # @rbs (Tokenizer) -> AST::Tree
251
+ def parse_type_param: (Tokenizer) -> AST::Tree
252
+
253
+ # : (Tokenizer) -> AST::Tree
216
254
  def parse_ivar_type: (Tokenizer) -> AST::Tree
217
255
 
218
- # :: (Tokenizer) -> AST::Tree
219
- def parse_yields: (Tokenizer) -> AST::Tree
256
+ # : (Tokenizer) -> AST::Tree
257
+ def parse_splat_param_type: (Tokenizer) -> AST::Tree
258
+
259
+ # : (Tokenizer) -> AST::Tree
260
+ def parse_block_type: (Tokenizer) -> AST::Tree
261
+
262
+ # @rbs (Tokenizer) -> AST::Tree
263
+ def parse_module_decl: (Tokenizer) -> AST::Tree
264
+
265
+ # @rbs (Tokenizer) -> AST::Tree
266
+ def parse_class_decl: (Tokenizer) -> AST::Tree
267
+
268
+ # @rbs (Tokenizer) -> AST::Tree
269
+ def parse_module_name: (Tokenizer) -> AST::Tree
270
+
271
+ # @rbs (Tokenizer) -> AST::Tree
272
+ def parse_type_params: (Tokenizer) -> AST::Tree
273
+
274
+ # @rbs (Tokenizer) -> AST::Tree
275
+ def parse_module_selfs: (Tokenizer) -> AST::Tree
220
276
  end
221
277
  end
222
278
  end