willExpressionChangeCharOnFailure method

bool willExpressionChangeCharOnFailure(
  1. Expression node,
  2. Set<Expression> processed
)
inherited

Implementation

bool willExpressionChangeCharOnFailure(
    Expression node, Set<Expression> processed) {
  if (!processed.add(node)) {
    return true;
  }

  switch (node.kind) {
    case ExpressionKind.andPredicate:
    case ExpressionKind.anyCharacter:
    case ExpressionKind.characterClass:
    case ExpressionKind.literal:
    case ExpressionKind.notPredicate:
      return false;
    case ExpressionKind.capture:
    case ExpressionKind.optional:
    case ExpressionKind.oneOrMore:
    case ExpressionKind.zeroOrMore:
      final single = node as SingleExpression;
      return willExpressionChangeCharOnFailure(single.expression, processed);
    case ExpressionKind.nonterminal:
    case ExpressionKind.subterminal:
    case ExpressionKind.terminal:
      final symbol = node as SymbolExpression;
      return willExpressionChangeCharOnFailure(symbol.expression!, processed);
    case ExpressionKind.orderedChoice:
      final choice = node as OrderedChoiceExpression;
      final expressions = choice.expressions;
      for (final expression in expressions) {
        if (willExpressionChangeCharOnFailure(expression, processed)) {
          return true;
        }
      }

      return false;
    case ExpressionKind.sequence:
      final sequence = node as SequenceExpression;
      final expressions = sequence.expressions;
      var count = 0;
      var skipOtional = false;
      for (final expression in expressions) {
        switch (expression.kind) {
          case ExpressionKind.andPredicate:
          case ExpressionKind.notPredicate:
            continue;
          default:
        }

        if (expression.isOptional && skipOtional) {
          continue;
        }

        if (count > 0) {
          return true;
        }

        skipOtional = true;
        count++;
      }

      return false;
  }
}