comparison src/main/gov/nasa/jpf/util/script/SequenceInterpreter.java @ 0:61d41facf527

initial v8 import (history reset)
author Peter Mehlitz <Peter.C.Mehlitz@nasa.gov>
date Fri, 23 Jan 2015 10:14:01 -0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:61d41facf527
1 /*
2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
4 * All rights reserved.
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0.
11 *
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.
17 */
18
19 package gov.nasa.jpf.util.script;
20
21 import gov.nasa.jpf.util.script.ScriptElementContainer.SECIterator;
22
23 import java.io.StringReader;
24
25 /**
26 * an interpreter that walks a ScriptElementContainer hierarchy, returning
27 * Events and Alternatives while expanding loops
28 */
29 public class SequenceInterpreter implements Cloneable {
30
31 ScriptElementContainer.SECIterator top;
32
33 public SequenceInterpreter (ScriptElementContainer seq) {
34 top = seq.iterator();
35 }
36
37 void push (SECIterator it) {
38 it.prev = top;
39 top = it;
40 }
41
42 SECIterator pop () {
43 if (top != null) {
44 top = top.getPrev();
45 }
46 return top;
47 }
48
49 public ScriptElement getNext() {
50 if (top != null) {
51 ScriptElement e = top.next();
52 if (e != null) {
53 if ((e instanceof ScriptElementContainer) && !(e instanceof Alternative) ) {
54 push( ((ScriptElementContainer)e).iterator());
55 return getNext();
56 } else {
57 return e;
58 }
59 } else {
60 pop();
61 return (top != null) ? getNext() : null;
62 }
63 } else {
64 return null;
65 }
66 }
67
68 @Override
69 public Object clone() {
70 // has to deep copy all iterators
71
72 try {
73 SequenceInterpreter si = (SequenceInterpreter) super.clone();
74 if (top != null) {
75 si.top = (SECIterator)top.clone();
76 }
77 return si;
78 } catch (CloneNotSupportedException nonsense) {
79 return null; // we are a Cloneable, so we don't get here
80 }
81 }
82
83 public boolean isDone() {
84 return (top == null);
85 }
86
87 //---- test driver
88 public static void main (String[] args) {
89 //String s = "";
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 }";
94
95
96 StringReader r = new StringReader(s);
97
98 try {
99 ESParser parser = new ESParser("test", r);
100 Script script = parser.parse();
101
102 SequenceInterpreter si = new SequenceInterpreter(script);
103
104 for (ScriptElement e = si.getNext(); e != null; e = si.getNext()) {
105 System.out.println(e);
106 }
107
108 } catch (Throwable t){
109 t.printStackTrace();
110 }
111 }
112 }