parse<T> static method

DOMAction<T>? parse<T>(
  1. DOMActionExecutor<T> executor,
  2. String? actionLine
)

Implementation

static DOMAction<T>? parse<T>(
    DOMActionExecutor<T> executor, String? actionLine) {
  if (actionLine == null) return null;
  actionLine = actionLine.trim();
  if (actionLine.isEmpty) return null;

  var matches = _regexpActionCapture.allMatches(actionLine);

  var actions = <DOMAction<T>>[];

  DOMAction<T>? rootAction;
  DOMAction<T>? lastAction;

  var endPos = 0;
  for (var match in matches) {
    var part = match.group(0)!;

    if (match.start > endPos) {
      var prevChar = actionLine.substring(endPos, match.start).trim();

      if (prevChar == ';') {
        actions.add(rootAction!);
        rootAction = null;
        lastAction = null;
      } else {
        throw ArgumentError("Can't parse DOMAction line: $actionLine");
      }
    }

    var sel = match.group(1);
    var call = match.group(2);

    DOMAction<T> action;

    if (sel != null) {
      var selMatch = _regexpSelCapture.firstMatch(part)!;
      var id = selMatch.group(1)!;
      action = DOMActionSelect<T>(executor, id);
    } else if (call != null) {
      var callMatch = _regexpCallCapture.firstMatch(part)!;
      var name = callMatch.group(1)!;
      var parametersLine = callMatch.group(2);
      var parameters = parseParameters(executor, parametersLine);
      action = DOMActionCall<T>(executor, name, parameters);
    } else {
      throw ArgumentError("Can't parse DOMAction line: $actionLine");
    }

    rootAction ??= action;

    if (lastAction != null) {
      lastAction.next = action;
    }

    lastAction = action;

    endPos = match.end;
  }

  if (actions.isNotEmpty) {
    actions.add(rootAction!);
    return DOMActionList(executor, actions);
  }

  return rootAction;
}