process method

TagserResult? process(
  1. ProcessMessage msg,
  2. TagserContext context
)

Implementation

TagserResult? process(ProcessMessage msg, TagserContext context) {
  final charCode = msg.charCode;

  if (charCode == charEos) {
    if (_openedTag != null) {
      return TagserResult(
        err: TagserError(
          code: errorEndOfTag,
          text: getError(errorEndOfTag, {
            "tag": _openedTag!.name,
            "line": _openedTag!.line,
            "symbol": _openedTag!.symbol,
          }),
        ),
      );
    }
    _text = _text.trim();

    if (_text.isNotEmpty) {
      tags.add(
        Tag(
            name: '',
            type: typeText,
            body: _text,
            symbol: context.symbol ?? 0,
            line: context.line),
      );
      _text = '';
    }
  } else if (_opened == true) {
    _opened = false;

    if (TagserUtils.isAvailableCharacter(charCode)) {
      return TagserResult(
        state: TagState(),
        message: InitMessage(
          charCode: charCode,
        ),
      );
    } else if (charCode == charSlash) {
      if (_openedTag != null) {
        return TagserResult(
          state: CloseTag(_openedTag!.name ?? ''),
          message: InitMessage(
            charCode: charCode,
          ),
        );
      }

      return TagserResult(
          err: TagserError(
              code: errorSourceDocumentMalformed,
              text: getError(errorSourceDocumentMalformed, null)));
    } else {
      return TagserResult(
          err: TagserError(
              code: errorTagMalformed,
              text: getError(errorTagMalformed, null)));
    }
  } else if (_escape == true) {
    _text += String.fromCharCode(charCode!);
  } else if (charCode == charBackSlash) {
    _escape = true;
  } else if (charCode == charOpenBracket) {
    _opened = true;

    _text = _text.trim();

    if (_text.isNotEmpty) {
      tags.add(
        Tag(
            name: '',
            type: typeText,
            body: _text,
            symbol: context.symbol ?? 0,
            line: context.line),
      );
      _text = '';
    }
  } else {
    _text += String.fromCharCode(charCode!);
  }

  return null;
}