All checks were successful
No Cursor attribution / Scan commits (push) Successful in 36s
224 lines
6.3 KiB
Swift
224 lines
6.3 KiB
Swift
extension PureYAML.Parsing {
|
|
private final class TokenScannerStateBox {
|
|
var state: Scanner.State
|
|
|
|
init(state: Scanner.State) {
|
|
self.state = state
|
|
}
|
|
}
|
|
|
|
struct TokenCursor {
|
|
private var tokens: [Token]
|
|
private(set) var index: Int
|
|
private var previousToken: Token?
|
|
private var scanner: Scanner?
|
|
private var scannerBox: TokenScannerStateBox?
|
|
|
|
init(tokens: [Token]) {
|
|
self.tokens = tokens.filter { !$0.kind.isComment }
|
|
index = 0
|
|
previousToken = nil
|
|
scanner = nil
|
|
scannerBox = nil
|
|
}
|
|
|
|
init(yaml: String, scanner: Scanner = .init()) {
|
|
var state = Scanner.State(reader: Reader(yaml))
|
|
state.append(.streamStart)
|
|
tokens = state.tokens.filter { !$0.kind.isComment }
|
|
state.tokens.removeAll(keepingCapacity: true)
|
|
index = 0
|
|
previousToken = nil
|
|
self.scanner = scanner
|
|
scannerBox = TokenScannerStateBox(state: state)
|
|
}
|
|
|
|
var current: Token? {
|
|
guard tokens.indices.contains(index) else {
|
|
return nil
|
|
}
|
|
return tokens[index]
|
|
}
|
|
|
|
var previous: Token? {
|
|
previousToken
|
|
}
|
|
|
|
mutating func peek(offset: Int = 1) throws -> Token? {
|
|
try ensureAvailable(offset: offset)
|
|
let targetIndex = index + offset
|
|
guard tokens.indices.contains(targetIndex) else {
|
|
return nil
|
|
}
|
|
return tokens[targetIndex]
|
|
}
|
|
|
|
mutating func advance() throws -> Token? {
|
|
try ensureAvailable(offset: 0)
|
|
guard tokens.indices.contains(index) else {
|
|
return nil
|
|
}
|
|
let token = tokens[index]
|
|
previousToken = token
|
|
index += 1
|
|
compactConsumedTokens()
|
|
try ensureAvailable(offset: 0)
|
|
return token
|
|
}
|
|
|
|
private mutating func ensureAvailable(offset: Int) throws {
|
|
while tokens.count <= index + offset, scannerBox != nil {
|
|
try scanMoreTokens()
|
|
}
|
|
}
|
|
|
|
private mutating func scanMoreTokens() throws {
|
|
guard let scanner, let scannerBox else {
|
|
return
|
|
}
|
|
|
|
// Scan at least one token, and keep scanning without draining while a
|
|
// flow collection is open: a flow `:` may splice a `mappingKey` into an
|
|
// already-scanned earlier token (the simple-key mechanism), so the whole
|
|
// flow collection's tokens must stay in `state.tokens` until it closes
|
|
// for the retroactive index to remain valid. The same holds once a flow
|
|
// collection has closed at block level but a block-mapping `:` might
|
|
// still splice a `mappingKey` before its opener (`[a, b]: value`), so
|
|
// also hold the buffer while a block flow simple key is pending.
|
|
while scannerBox.state.tokens.isEmpty
|
|
|| scannerBox.state.flowDepth > 0
|
|
|| scannerBox.state.blockFlowSimpleKey != nil {
|
|
if scannerBox.state.reader.isAtEnd {
|
|
if scannerBox.state.hasPendingDirective {
|
|
throw PureYAML.Parsing.ParseError.directiveWithoutDocument(
|
|
line: scannerBox.state.reader.mark.line,
|
|
)
|
|
}
|
|
scanner.closeIndentation(&scannerBox.state)
|
|
scannerBox.state.append(.streamEnd)
|
|
drainScannedTokens(from: &scannerBox.state)
|
|
self.scannerBox = nil
|
|
return
|
|
}
|
|
|
|
// Drive the same scanning step the batch scanner uses, so the two
|
|
// paths share one source of truth for line breaks, blank lines,
|
|
// indentation, and `simpleKeyAllowed` handling.
|
|
let step = try scanner.scanStep(&scannerBox.state)
|
|
if case .scannedMarker = step, !scannerBox.state.tokens.isEmpty {
|
|
break
|
|
}
|
|
}
|
|
|
|
drainScannedTokens(from: &scannerBox.state)
|
|
}
|
|
|
|
private mutating func drainScannedTokens(from state: inout Scanner.State) {
|
|
tokens.append(contentsOf: state.tokens.filter { !$0.kind.isComment })
|
|
state.tokens.removeAll(keepingCapacity: true)
|
|
}
|
|
|
|
private mutating func compactConsumedTokens() {
|
|
guard index > 4096 else {
|
|
return
|
|
}
|
|
tokens.removeFirst(index)
|
|
index = 0
|
|
}
|
|
}
|
|
}
|
|
|
|
extension PureYAML.Parsing.TokenKind {
|
|
var isAlias: Bool {
|
|
if case .alias = self {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
var isNodeProperty: Bool {
|
|
switch self {
|
|
case .anchor, .tag:
|
|
true
|
|
default:
|
|
false
|
|
}
|
|
}
|
|
|
|
var isBlockEntry: Bool {
|
|
if case .blockEntry = self {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
var isComment: Bool {
|
|
if case .comment = self {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
var isDedent: Bool {
|
|
if case .dedent = self {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
var isDocumentEnd: Bool {
|
|
if case .documentEnd = self {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
var isDocumentStart: Bool {
|
|
if case .documentStart = self {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
var isFlowTerminator: Bool {
|
|
switch self {
|
|
case .flowEntry, .flowMappingEnd, .flowSequenceEnd:
|
|
true
|
|
default:
|
|
false
|
|
}
|
|
}
|
|
|
|
var isIndent: Bool {
|
|
if case .indent = self {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
var isMappingKey: Bool {
|
|
if case .mappingKey = self {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
var isMappingValue: Bool {
|
|
if case .mappingValue = self {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
var isStreamEnd: Bool {
|
|
if case .streamEnd = self {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
var isTerminator: Bool {
|
|
isDedent || isDocumentEnd || isDocumentStart || isFlowTerminator || isStreamEnd
|
|
}
|
|
}
|