All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.ppg.generator.Sequence Maven / Gradle / Ivy

There is a newer version: 10.0.0.Beta3
Show newest version
package org.infinispan.ppg.generator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Several elements, useful when these repeat
 */
public class Sequence implements Element {
   final List elements = new ArrayList<>();

   @Override
   public String toString() {
      StringBuilder sb = new StringBuilder();
      sb.append("( ");
      for (Element e : elements) {
         sb.append(e).append(" ");
      }
      return sb.append(')').toString();
   };

   @Override
   public void addReferences(Set references) {
      for (Element e : elements) {
         e.addReferences(references);
      }
   }

   @Override
   public Machine.State addStates(Machine.State prev, Machine.State target, Machine machine, Grammar grammar, List ruleStack) {
      for (Iterator iterator = elements.iterator(); iterator.hasNext(); ) {
         Element e = iterator.next();
         Machine.State next = iterator.hasNext() ? machine.addState(ruleStack) : target;
         prev = e.addStates(prev, next, machine, grammar, ruleStack);
      }
      assert prev == target; // could fail if sequence is empty but that's illegal
      return prev;
   }

   @Override
   public String analyzeType(Grammar grammar) {
      return elements.get(elements.size() - 1).analyzeType(grammar);
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy