parse method

Resource parse(
  1. String source
)

Parse the full source into a Resource.

Implementation

Resource parse(String source) {
  final ps = FluentParserStream(source);
  ps.skipBlankBlock();

  final entries = <Entry>[];
  Comment? lastComment;

  while (ps.currentChar() != eof) {
    final entry = _getEntryOrJunk(ps);
    final blankLines = ps.skipBlankBlock();

    // Comments attach to a Message/Term iff they're separated by no blank
    // line. If the comment is followed by junk or by blank lines, it stays
    // a standalone Comment entry.
    if (entry is Comment &&
        entry.level == CommentLevel.regular &&
        blankLines.isEmpty &&
        ps.currentChar() != eof) {
      lastComment = entry;
    } else {
      if (lastComment != null) {
        // Attach the saved comment to the just-parsed Message or Term.
        if (entry is Message) {
          entries.add(
            Message(
              id: entry.id,
              value: entry.value,
              attributes: entry.attributes,
              comment: lastComment,
              span: _spanIfTracking(
                _messageStart(entry, lastComment),
                ps.index,
              ),
            ),
          );
        } else if (entry is Term) {
          entries.add(
            Term(
              id: entry.id,
              value: entry.value,
              attributes: entry.attributes,
              comment: lastComment,
              span: _spanIfTracking(_termStart(entry, lastComment), ps.index),
            ),
          );
        } else {
          entries.add(lastComment);
          entries.add(entry);
        }
        lastComment = null;
      } else {
        entries.add(entry);
      }
    }
  }

  // Trailing comment that never attached to anything stays as standalone.
  if (lastComment != null) entries.add(lastComment);

  return Resource(entries, span: _spanIfTracking(0, source.length));
}