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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if swift(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif
extension Parser {
/// Token lookahead for the parser.
///
/// Parser lookahead functions nearly identically to parsing, except the
/// resulting functions do not construct syntax trees and may skip an
/// arbitrary number of tokens ahead in the input stream. Instances of
/// ``Lookahead`` are distinct from their parent ``Parser`` instances, so
/// any tokens they consume will not be reflected in the parent parser.
struct Lookahead {
var lexemes: Lexer.LexemeSequence
var currentToken: Lexer.Lexeme
/// Number of tokens this ``Lookahead`` has consumed from where it was started,
/// i.e. how far it looked ahead.
var tokensConsumed: Int = 0
/// The Swift version as which this source file should be parsed.
let swiftVersion: SwiftVersion
/// The experimental features that have been enabled in the underlying
/// parser.
let experimentalFeatures: ExperimentalFeatures
private init(
lexemes: Lexer.LexemeSequence,
currentToken: Lexer.Lexeme,
swiftVersion: SwiftVersion,
experimentalFeatures: ExperimentalFeatures
) {
self.lexemes = lexemes
self.currentToken = currentToken
self.swiftVersion = swiftVersion
self.experimentalFeatures = experimentalFeatures
}
fileprivate init(cloning other: Parser) {
self.init(
lexemes: other.lexemes,
currentToken: other.currentToken,
swiftVersion: other.swiftVersion,
experimentalFeatures: other.experimentalFeatures
)
}
/// Initiates a lookahead session from the current point in this
/// lookahead session.
func lookahead() -> Lookahead {
return Lookahead(
lexemes: self.lexemes,
currentToken: self.currentToken,
swiftVersion: self.swiftVersion,
experimentalFeatures: self.experimentalFeatures
)
}
}
/// Initiates a lookahead session from the current point in this parse.
func lookahead() -> Lookahead {
return Lookahead(cloning: self)
}
func withLookahead<T>(_ body: (_: inout Lookahead) -> T) -> T {
var lookahead = lookahead()
return body(&lookahead)
}
}
extension Parser.Lookahead: TokenConsumer {
/// Consumes the current token, and asserts that it matches `spec`.
///
/// If the token kind did not match, this function will abort. It is useful
/// to insert structural invariants during parsing.
///
/// - Parameter kind: The kind of token to consume.
/// - Returns: A token of the given kind.
mutating func eat(_ spec: TokenSpec) -> Token {
return self.consume(if: spec)!
}
#if SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION
var shouldRecordAlternativeTokenChoices: Bool { false }
mutating func recordAlternativeTokenChoice(for lexeme: Lexer.Lexeme, choices: [TokenSpec]) {}
#endif
}
extension Parser.Lookahead {
func peek() -> Lexer.Lexeme {
return self.lexemes.peek()
}
}
extension Parser.Lookahead {
mutating func missingToken(_ kind: RawTokenKind, text: SyntaxText?) {
// do nothing
}
mutating func consumeAnyToken() {
tokensConsumed += 1
self.currentToken = self.lexemes.advance()
}
mutating func consumeAnyToken(remapping: RawTokenKind) {
self.consumeAnyToken()
}
/// Consume tokens of lower precedence than `spec` until reaching a token that
/// matches that `spec`. The token that matches `spec` is also consumed.
mutating func consume(to spec: TokenSpec) {
if self.consume(if: spec) != nil {
return
}
var lookahead = self.lookahead()
if lookahead.canRecoverTo(spec) != nil {
for _ in 0..<lookahead.tokensConsumed {
self.consumeAnyToken()
}
self.consumeAnyToken()
}
}
/// Consumes a given token, or splits the current token into a leading token
/// matching the given token and a trailing token and consumes the leading
/// token.
///
/// <TOKEN> ... -> consumePrefix(<TOK>) -> [ <TOK> ] <EN> ...
mutating func consumePrefix(_ prefix: SyntaxText, as tokenKind: RawTokenKind) {
precondition(
tokenKind.defaultText == nil || prefix == tokenKind.defaultText!,
"If tokenKind has a defaultText, the prefix needs to match it"
)
let tokenText = self.currentToken.tokenText
if tokenText == prefix {
return self.consumeAnyToken()
}
precondition(tokenText.hasPrefix(prefix))
self.currentToken = self.lexemes.resetForSplit(
splitToken: self.currentToken,
consumedPrefix: self.currentToken.leadingTriviaByteLength + prefix.count
)
}
}
// MARK: Skipping Tokens
extension Parser.Lookahead {
mutating func skipTypeAttribute() {
// These are keywords that we accept as attribute names.
guard self.at(.identifier) || self.at(.keyword(.in), .keyword(.inout)) else {
return
}
// Determine which attribute it is.
if let (attr, handle) = self.at(anyIn: TypeAttribute.self) {
// Ok, it is a valid attribute, eat it, and then process it.
self.eat(handle)
switch attr {
case .convention, .isolated:
self.skipSingle()
default:
break
}
return
}
if let (_, handle) = self.at(anyIn: Parser.DeclarationAttributeWithSpecialSyntax.self) {
// This is a valid decl attribute so they should have put it on the decl
// instead of the type.
//
// Recover by eating @foo(...)
self.eat(handle)
if self.at(.leftParen) {
var lookahead = self.lookahead()
lookahead.skipSingle()
// If we found '->', or 'throws' after paren, it's likely a parameter
// of function type.
guard lookahead.at(.arrow) || lookahead.at(.keyword(.throws), .keyword(.rethrows), .keyword(.throw)) else {
self.skipSingle()
return
}
}
return
}
_ = self.canParseCustomAttribute()
return
}
}
extension Parser.Lookahead {
mutating func consumeAttributeList() -> Bool {
guard self.at(.atSign) else {
return false
}
while let _ = self.consume(if: .atSign) {
// Consume qualified names that may or may not involve generic arguments.
repeat {
self.consume(if: .identifier, .keyword(.rethrows))
// We don't care whether this succeeds or fails to eat generic
// parameters.
_ = self.consumeGenericArguments()
} while self.consume(if: .period) != nil
if self.consume(if: .leftParen) != nil {
while !self.at(.endOfFile, .rightParen, .poundEndif) {
self.skipSingle()
}
self.consume(if: .rightParen)
}
}
return true
}
/// Tries consuming a `#if` directive that only contains attributes.
/// Returns `true` if that was successful and `false` if
/// - we are not at a valid `#if` directive (e.g. `#endif` is missing)
/// - the directive contained non-attributes
/// - the directive did not contain any attributes
mutating func consumeIfConfigOfAttributes() -> Bool {
precondition(self.at(.poundIf))
var didSeeAnyAttributes = false
var poundIfLoopProgress = LoopProgressCondition()
repeat {
precondition(self.at(.poundIf, .poundElse, .poundElseif))
self.consumeAnyToken()
// <expression> after `#if` or `#elseif`
self.skipUntilEndOfLine()
var attributesLoopProgress = LoopProgressCondition()
ATTRIBUTE_LOOP: while self.hasProgressed(&attributesLoopProgress) {
switch self.currentToken.rawTokenKind {
case .atSign:
didSeeAnyAttributes = true
_ = self.consumeAttributeList()
case .poundIf:
_ = self.consumeIfConfigOfAttributes()
default:
break ATTRIBUTE_LOOP
}
}
} while self.at(.poundElseif, .poundElse) && self.hasProgressed(£IfLoopProgress)
return didSeeAnyAttributes && self.atStartOfLine && self.consume(if: .poundEndif) != nil
}
}
// MARK: Lookahead
extension Parser.Lookahead {
mutating func atStartOfGetSetAccessor() -> Bool {
precondition(self.at(.leftBrace), "not checking a brace?")
// The only case this can happen is if the accessor label is immediately after
// a brace (possibly preceded by attributes). "get" is implicit, so it can't
// be checked for. Conveniently however, get/set properties are not allowed
// to have initializers, so we don't have an ambiguity, we just have to check
// for observing accessors.
//
// If we have a 'didSet' or a 'willSet' label, disambiguate immediately as
// an accessor block.
let nextToken = self.peek()
if TokenSpec(.didSet) ~= nextToken || TokenSpec(.willSet) ~= nextToken || TokenSpec(.`init`) ~= nextToken {
return true
}
// If we don't have attributes, then it cannot be an accessor block.
if nextToken.rawTokenKind != .atSign {
return false
}
// Eat the "{".
var lookahead = self.lookahead()
lookahead.eat(.leftBrace)
// Eat attributes, if present.
while lookahead.consume(if: .atSign) != nil {
guard lookahead.consume(if: .identifier) != nil else {
return false
}
// Eat paren after attribute name; e.g. @foo(x)
if lookahead.at(.leftParen) {
lookahead.skipSingle()
}
}
// Check if we have 'didSet'/'willSet' or 'init' after attributes.
return lookahead.at(.keyword(.didSet), .keyword(.willSet), .keyword(.`init`))
}
}
// MARK: Skipping Tokens
extension Parser.Lookahead {
mutating func skipUntil(_ t1: TokenSpec, _ t2: TokenSpec) {
return skip(initialState: .skipUntil(t1, t2))
}
mutating func skipUntilEndOfLine() {
while !self.at(.endOfFile) && !self.atStartOfLine {
self.skipSingle()
}
}
mutating func skipSingle() {
return skip(initialState: .skipSingle)
}
// Note: We don't need to treat string quotes as bracketed tokens because:
// - If we skip over the opening quote, we also automatically skip over
// closing quote since it has the same token kind
// - It is very unlikely that we look for string segments, so we
// automatically skip over those as individual tokens
// - String interpolation contains parentheses, so it automatically skips
// until the closing parenthesis.
private enum BracketedTokens: TokenSpecSet {
case leftParen
case leftBrace
case leftSquare
case poundIf
case poundElse
case poundElseif
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch lexeme.rawTokenKind {
case .leftParen: self = .leftParen
case .leftBrace: self = .leftBrace
case .leftSquare: self = .leftSquare
case .poundIf: self = .poundIf
case .poundElse: self = .poundElse
case .poundElseif: self = .poundElseif
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .leftParen: return .leftParen
case .leftBrace: return .leftBrace
case .leftSquare: return .leftSquare
case .poundIf: return .poundIf
case .poundElse: return .poundElse
case .poundElseif: return .poundElseif
}
}
}
private enum SkippingState {
/// Equivalent to a call to `skipSingle`. Skip a single token.
/// If that token is bracketed, skip until the closing bracket
case skipSingle
/// Execute code after skipping bracketed tokens detected from `skipSingle`.
case skipSinglePost(start: BracketedTokens)
/// Skip until either `t1` or `t2`.
case skipUntil(_ t1: TokenSpec, _ t2: TokenSpec)
}
/// A non-recursive function to skip tokens.
private mutating func skip(initialState: SkippingState) {
var stack: [SkippingState] = [initialState]
while let state = stack.popLast() {
switch state {
case .skipSingle:
let t = self.at(anyIn: BracketedTokens.self)
switch t {
case (.leftParen, let handle)?:
self.eat(handle)
stack += [.skipSinglePost(start: .leftParen), .skipUntil(.rightParen, .rightBrace)]
case (.leftBrace, let handle)?:
self.eat(handle)
stack += [.skipSinglePost(start: .leftBrace), .skipUntil(.rightBrace, .rightBrace)]
case (.leftSquare, let handle)?:
self.eat(handle)
stack += [.skipSinglePost(start: .leftSquare), .skipUntil(.rightSquare, .rightSquare)]
case (.poundIf, let handle)?,
(.poundElse, let handle)?,
(.poundElseif, let handle)?:
self.eat(handle)
// skipUntil also implicitly stops at tok::pound_endif.
stack += [.skipSinglePost(start: t!.0), .skipUntil(.poundElse, .poundElseif)]
case nil:
self.consumeAnyToken()
}
case .skipSinglePost(start: let start):
switch start {
case .leftParen:
self.consume(if: .rightParen)
case .leftBrace:
self.consume(if: .rightBrace)
case .leftSquare:
self.consume(if: .rightSquare)
case .poundIf, .poundElse, .poundElseif:
if self.at(.poundElse, .poundElseif) {
stack += [.skipSingle]
} else {
self.consume(if: .poundElseif)
}
return
}
case .skipUntil(let t1, let t2):
if !self.at(.endOfFile, t1, t2) && !self.at(.poundEndif, .poundElse, .poundElseif) {
stack += [.skipUntil(t1, t2), .skipSingle]
}
}
}
}
}
|