Event _parseDocumentStart()

in lib/src/parser.dart [146:192]


  Event _parseDocumentStart() {
    var token = _scanner.peek()!;

    // libyaml requires any document beyond the first in the stream to have an
    // explicit document start indicator, but the spec allows it to be omitted
    // as long as there was an end indicator.

    // Parse extra document end indicators.
    while (token.type == TokenType.documentEnd) {
      token = _scanner.advance()!;
    }

    if (token.type != TokenType.versionDirective &&
        token.type != TokenType.tagDirective &&
        token.type != TokenType.documentStart &&
        token.type != TokenType.streamEnd) {
      // Parse an implicit document.
      _processDirectives();
      _states.add(_State.DOCUMENT_END);
      _state = _State.BLOCK_NODE;
      return DocumentStartEvent(token.span.start.pointSpan());
    }

    if (token.type == TokenType.streamEnd) {
      _state = _State.END;
      _scanner.scan();
      return Event(EventType.streamEnd, token.span);
    }

    // Parse an explicit document.
    var start = token.span;
    var pair = _processDirectives();
    var versionDirective = pair.first;
    var tagDirectives = pair.last;
    token = _scanner.peek()!;
    if (token.type != TokenType.documentStart) {
      throw YamlException('Expected document start.', token.span);
    }

    _states.add(_State.DOCUMENT_END);
    _state = _State.DOCUMENT_CONTENT;
    _scanner.scan();
    return DocumentStartEvent(start.expand(token.span),
        versionDirective: versionDirective,
        tagDirectives: tagDirectives,
        isImplicit: false);
  }