separatedBy method

Parser<List<T>> separatedBy(
  1. Parser other
)

Expects to see an infinite amounts of the pattern, separated by the other pattern.

Use this as a shortcut to parse arrays, parameter lists, etc.

Implementation

Parser<List<T>> separatedBy(Parser other) {
  var suffix = other.then(this).index(1).cast<T>();
  return then(suffix.star()).map((r) {
    var v = r.value;
    if (v == null || v.length < 2) {
      return [];
    }
    var preceding = v.isEmpty ? [] : (v[0] == null ? [] : [v[0]]);
    var out = List<T>.from(preceding);
    if (v[1] != null && v[1] != 'NULL') {
      v[1].forEach((element) {
        out.add(element as T);
      });
    }
    return out;
  });
}