示例#1
0
  private static State replicateStateSequenceBranch(
      HashMap<Long, State> stateMap, State curRef, State startRef, State endRef) {
    if (stateMap == null || curRef == null) return null;

    State addTo = createOrGetStateFromMap(stateMap, curRef);

    for (Connection c : curRef.connections) {
      State newS = null;

      if (c.cyclic) {
        if (curRef.name == startRef.name) continue;

        newS = createOrGetStateFromMap(stateMap, c.to);
      } else {
        if (endRef != null && curRef.name == endRef.name) continue;

        newS = replicateStateSequenceBranch(stateMap, c.to, startRef, endRef);
      }

      if (newS == null) continue;

      addTo.connect(newS, c.character, c.cyclic);
    }

    return addTo;
  }
示例#2
0
  private static void _connectAll(StateQueue queue, State s, String str) {
    if (queue.toConnect.size() > 0) {
      for (State st : queue.toConnect) {
        st.isFinal = false;
        st.connect(s, str, false);
      }

      queue.toConnect.clear();
    }
  }
示例#3
0
  private void analysis(SimpleNode node) throws ParseException {
    if (node == null || node.jjtGetValue() == null) return;

    String name = node.toString();
    StateQueue queue = stateQueue.element();

    if (name.equals("Chars")) {
      // System.out.println(level + " Chars");

      State s = new State();

      _connectAll(queue, s, node.jjtGetValue().toString());

      queue.last.connect(s, node.jjtGetValue().toString(), false);
      queue.last = s;
    }
    //		else
    //		if( name.equals("Expression") )
    //		{
    //			State s = new State();
    //
    //
    //
    //			queue.last.connect(s, "aa", false);
    //			queue.last = s;
    //		}
    else if (name.equals("CharTypes")) {
      if (node.jjtGetValue().toString().equals(".")) {
        State s = new State();

        _connectAll(queue, s, node.jjtGetValue().toString());

        Connection nC = queue.last.connect(s, "<ANY>", false);

        nC.isAnyChar = true;
        queue.last = s;
      } else throw new ParseException("Invalid character received");
    } else if (name.equals("Quantifier")) {
      // System.out.println(level + " Quantifier");
      QuantifierState quantifier = (QuantifierState) node.jjtGetValue();

      long start = -1;
      long end = -1;

      switch (quantifier.type) {
        case ZERO_OR_MORE:
          queue.head.connect(queue.last, null, true);
          queue.last.connect(queue.head, null, true);

          return;

        case ONE_OR_MORE:
          queue.last.connect(queue.head, null, true);

          return;

        case ZERO_OR_ONE:
          queue.head.connect(queue.last, null, true);

          return;

        case EXACTLY_X:
          if (quantifier.value != null && quantifier.value < 1L)
            throw new ParseException("Quantifier repetition cannot be zero");

          start = quantifier.value;
          end = quantifier.value;

          break;

        case RANGED:
          if (quantifier.max != null && quantifier.max < 1L)
            throw new ParseException("Quantifier repetition cannot be zero");

          if (quantifier.value > quantifier.max)
            throw new ParseException(
                "Quantifier minimum repetitions cannot be greater than max repetition");

          start = quantifier.value;
          end = quantifier.max;

          break;
      }

      State newQueueLast = queue.last;

      ArrayList<State> ttttoConnect = new ArrayList<State>();

      if (start == 0) ttttoConnect.add(queue.head);

      for (long i = 1; i < end; i++) {
        StateQueue newQueue = replicateStateSequence(queue);

        if (i >= start && i < end) ttttoConnect.add(newQueue.head);

        newQueueLast.connect(newQueue.head, null, false);

        newQueueLast = newQueue.last;
      }
      queue.last = newQueueLast;

      for (State s : ttttoConnect) {
        if (queue.last != null) s.connect(queue.last, null, true);
      }
    }
  }