2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.util.script;
21 import gov.nasa.jpf.util.script.ScriptElementContainer.SECIterator;
23 import java.io.StringReader;
26 * an interpreter that walks a ScriptElementContainer hierarchy, returning
27 * Events and Alternatives while expanding loops
29 public class SequenceInterpreter implements Cloneable {
31 ScriptElementContainer.SECIterator top;
33 public SequenceInterpreter (ScriptElementContainer seq) {
37 void push (SECIterator it) {
49 public ScriptElement getNext() {
51 ScriptElement e = top.next();
53 if ((e instanceof ScriptElementContainer) && !(e instanceof Alternative) ) {
54 push( ((ScriptElementContainer)e).iterator());
61 return (top != null) ? getNext() : null;
69 public Object clone() {
70 // has to deep copy all iterators
73 SequenceInterpreter si = (SequenceInterpreter) super.clone();
75 si.top = (SECIterator)top.clone();
78 } catch (CloneNotSupportedException nonsense) {
79 return null; // we are a Cloneable, so we don't get here
83 public boolean isDone() {
88 public static void main (String[] args) {
90 //String s = "a; b; c ";
91 //String s = "REPEAT 2 { e1, REPEAT 1 { r1, r2 }, e2 }";
92 //String s = "x, ANY {a1,a2}, y";
93 String s = "REPEAT 2 { start, ANY {a1,a2}, REPEAT 2 {r}, end }";
96 StringReader r = new StringReader(s);
99 ESParser parser = new ESParser("test", r);
100 Script script = parser.parse();
102 SequenceInterpreter si = new SequenceInterpreter(script);
104 for (ScriptElement e = si.getNext(); e != null; e = si.getNext()) {
105 System.out.println(e);
108 } catch (Throwable t){