extension PureYAML.Parsing.Scanner { /// Rejects a tab that appears in separation position only when it supplies /// indentation for a nested block node. YAML 1.2 allows a tab as in-line /// separation (s-white) before a plain or quoted scalar value, but never as /// indentation (s-indent). The two are distinguished by what follows the tab: /// a block-entry `-`, an explicit-key `?`, or a scalar that is itself a mapping /// key (a compact nested mapping) all start a nested block node whose column the /// tab would be indenting, so they are rejected; a plain scalar *value* is /// ordinary separation and is allowed. /// /// Examples (yaml-test-suite): `--1` and `foo: bar` are valid (scalar /// value follows), while `--`, `?key:`, and `:key:` are rejected /// (a nested indicator or key follows). func rejectTabSeparation(_ state: inout State) throws { guard state.flowDepth == 0, state.simpleKeyAllowed, state.reader.peek() == "\t" else { return } if tabSeparationPrecedesNestedBlockNode(state) { throw PureYAML.Parsing.ParseError.tabIndentation(line: state.reader.mark.line) } } /// True when, after the tab(s) and spaces under the reader, the next token starts /// a nested block node (a `-` block entry, a `?` explicit key, or a plain scalar /// immediately followed by a `:` mapping-value indicator). private func tabSeparationPrecedesNestedBlockNode(_ state: State) -> Bool { var offset = 0 while state.reader.peek(offset: offset) == "\t" || state.reader.peek(offset: offset) == " " { offset += 1 } guard let character = state.reader.peek(offset: offset) else { return false } if character == "-", isIndicatorBoundary(state.reader.peek(offset: offset + 1)) { return true } if character == "?", isIndicatorBoundary(state.reader.peek(offset: offset + 1)) { return true } return plainScalarIsMappingKey(in: state, startingAt: offset) } /// Scans a prospective plain scalar starting at `offset` (in lookahead only) and /// reports whether it is immediately followed by a `:` block mapping-value /// indicator, i.e. the scalar is a nested mapping key rather than a value. private func plainScalarIsMappingKey(in state: State, startingAt offset: Int) -> Bool { var cursor = offset while let character = state.reader.peek(offset: cursor) { if isLineBreak(character) { return false } if character == ":", isBlockMappingValueBoundary(state.reader.peek(offset: cursor + 1)) { return true } if character == " ", state.reader.peek(offset: cursor + 1) == "#" { return false } cursor += 1 } return false } }