@@ -153,33 +153,12 @@ public struct Parser: TokenConsumer {
153
153
self . lexemes = Lexer . tokenize ( sourceBuffer)
154
154
self . currentToken = self . lexemes. advance ( )
155
155
}
156
- }
157
-
158
- // MARK: Inspecting Tokens
159
-
160
- extension Parser {
161
- /// Retrieves the token following the current token without consuming it.
162
- @_spi ( RawSyntax)
163
- public func peek( ) -> Lexer . Lexeme {
164
- return self . lexemes. peek ( )
165
- }
166
- }
167
-
168
- // MARK: Consuming Tokens
169
-
170
- extension Parser {
171
- /// After calling `consume(ifAnyFrom:)` we know which token we are positioned
172
- /// at based on that function's return type. This handle allows consuming that
173
- /// token.
174
- struct TokenConsumptionHandle {
175
- /// The kind that is expected to be consumed if the handle is eaten.
176
- var tokenKind : RawTokenKind
177
- }
178
156
179
157
@_spi ( RawSyntax)
180
158
public mutating func missingToken( _ kind: RawTokenKind ) -> RawTokenSyntax {
181
159
return RawTokenSyntax ( missing: kind, arena: arena)
182
160
}
161
+
183
162
/// Consumes the current token and advances the lexer to the next token.
184
163
///
185
164
/// - Returns: The token that was consumed.
@@ -195,7 +174,21 @@ extension Parser {
195
174
arena: arena
196
175
)
197
176
}
177
+ }
178
+
179
+ // MARK: Inspecting Tokens
198
180
181
+ extension Parser {
182
+ /// Retrieves the token following the current token without consuming it.
183
+ @_spi ( RawSyntax)
184
+ public func peek( ) -> Lexer . Lexeme {
185
+ return self . lexemes. peek ( )
186
+ }
187
+ }
188
+
189
+ // MARK: Consuming Tokens
190
+
191
+ extension Parser {
199
192
/// Consumes the current token and sets its kind to the given `TokenKind`,
200
193
/// then advances the lexer to the next token.
201
194
///
@@ -208,6 +201,11 @@ extension Parser {
208
201
return self . consumeAnyToken ( )
209
202
}
210
203
204
+ }
205
+
206
+ // MARK: Consuming Tokens with Lookahead
207
+
208
+ extension Parser {
211
209
/// If the current token has `kind1` and is followed by `kind2` consume both tokens and return them.
212
210
/// Otherwise, return `nil`.
213
211
@_spi ( RawSyntax)
@@ -233,54 +231,31 @@ extension Parser {
233
231
return nil
234
232
}
235
233
}
234
+ }
236
235
237
- /// Examines the current token and consumes it if its kind matches the
238
- /// given `TokenKind` and additionally satisfies `condition`. If a token was
239
- /// consumed, the result is that token, else the result is `nil`.
236
+ // MARK: Expecting Tokens
237
+
238
+ extension Parser {
239
+ /// Attempts to consume a token of the given kind, synthesizing a missing
240
+ /// token if the current token's kind does not match.
241
+ ///
242
+ /// This method does not try to eat unexpected until it finds the token of the specified `kind`.
243
+ /// In general, `expect` or `expectAny` should be preferred.
240
244
///
241
245
/// - Parameter kind: The kind of token to consume.
242
- /// - Returns: A token of the given kind if one was consumed, else `nil` .
246
+ /// - Returns: A token of the given kind.
243
247
@_spi ( RawSyntax)
244
- public mutating func consume(
245
- if kind: RawTokenKind ,
246
- where condition: ( Lexer . Lexeme , Self ) -> Bool
247
- ) -> Token ? {
248
- guard self . at ( kind) && condition ( self . currentToken, self ) else {
249
- return nil
250
- }
251
- return self . consumeAnyToken ( )
252
- }
253
-
254
- /// Examines the current token and consumes it if its kind is in `kinds` and
255
- /// additionally satisfies `condition`. If a token was consumed, the result is
256
- /// that token, else the result is `nil`.
257
- ///
258
- /// - Parameter kind: The kinds of token to consume.
259
- /// - Returns: A token of the given kind if one was consumed, else `nil`.
260
- public mutating func consume( ifAny kinds: RawTokenKind ... , where condition: ( Lexer . Lexeme , Parser ) -> Bool ) -> Token ? {
261
- for kind in kinds {
262
- if let consumed = self . consume ( if: kind, where: condition) {
263
- return consumed
264
- }
265
- }
266
- return nil
267
- }
268
-
269
- /// Checks whether the parser is currently positioned at any token in `Subset`.
270
- /// If this is the case, return the `Subset` case that the parser is positioned in
271
- /// as well as a handle to consume that token.
272
- func at< Subset: RawTokenKindSubset > ( anyIn subset: Subset . Type ) -> ( Subset , TokenConsumptionHandle ) ? {
273
- for kind in Subset . allCases {
274
- if self . at ( kind. rawTokenKind) && kind. accepts ( lexeme: currentToken, parser: self ) {
275
- return ( kind, TokenConsumptionHandle ( tokenKind: kind. rawTokenKind) )
248
+ public mutating func expectWithoutLookahead( _ kind: RawTokenKind , _ text: SyntaxText ? = nil ) -> RawTokenSyntax {
249
+ if self . currentToken. tokenKind == kind {
250
+ if let text = text {
251
+ if self . currentToken. tokenText == text {
252
+ return self . consumeAnyToken ( )
253
+ }
254
+ } else {
255
+ return self . consumeAnyToken ( )
276
256
}
277
257
}
278
- return nil
279
- }
280
-
281
- /// Eat a token that we know we are currently positioned at, based on `at(anyIn:)`.
282
- mutating func eat( _ handle: TokenConsumptionHandle ) -> RawTokenSyntax {
283
- return consume ( if: handle. tokenKind) !
258
+ return RawTokenSyntax ( missing: kind, arena: self . arena)
284
259
}
285
260
286
261
/// Attempts to consume a token of the given kind.
@@ -335,28 +310,6 @@ extension Parser {
335
310
}
336
311
return ( nil , RawTokenSyntax ( missing: defaultKind, arena: self . arena) )
337
312
}
338
-
339
- /// Attempts to consume a token of the given kind, synthesizing a missing
340
- /// token if the current token's kind does not match.
341
- ///
342
- /// This method does not try to eat unexpected until it finds the token of the specified `kind`.
343
- /// In general, `expect` or `expectAny` should be preferred.
344
- ///
345
- /// - Parameter kind: The kind of token to consume.
346
- /// - Returns: A token of the given kind.
347
- @_spi ( RawSyntax)
348
- public mutating func expectWithoutLookahead( _ kind: RawTokenKind , _ text: SyntaxText ? = nil ) -> RawTokenSyntax {
349
- if self . currentToken. tokenKind == kind {
350
- if let text = text {
351
- if self . currentToken. tokenText == text {
352
- return self . consumeAnyToken ( )
353
- }
354
- } else {
355
- return self . consumeAnyToken ( )
356
- }
357
- }
358
- return RawTokenSyntax ( missing: kind, arena: self . arena)
359
- }
360
313
}
361
314
362
315
// MARK: Spliting Tokens
0 commit comments