parse method

RegExp parse()

Implementation

RegExp parse() {
  var b = StringBuffer();
  var parts = Queue<String>();

  while (!_scanner.isDone) {
    if (_scanner.scan('|')) {
      if (parts.isEmpty) {
        throw _formatExc('No hostname parts found before "|".');
      } else {
        var next = _parseHostnamePart();
        if (next == null) {
          throw _formatExc('No hostname parts found after "|".');
        } else {
          var prev = parts.removeLast();
          parts.addLast('(($prev)|($next))');
        }
      }
    } else {
      var part = _parseHostnamePart();
      if (part != null) {
        if (_scanner.scan('.')) {
          var subPart = _parseHostnamePart(shouldThrow: false);
          while (subPart != null) {
            part += '\\.' + subPart;
            if (_scanner.scan('.')) {
              subPart = _parseHostnamePart(shouldThrow: false);
            } else {
              break;
            }
          }
        }

        parts.add(part);
      }
    }
  }

  while (parts.isNotEmpty) {
    b.write(parts.removeFirst());
  }

  if (b.isEmpty) {
    throw _formatExc('Invalid or empty hostname.');
  } else {
    return RegExp('^$b\$', caseSensitive: false);
  }
}