/
Explorer.java
437 lines (337 loc) · 12.7 KB
/
Explorer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright (c) 2003 Compaq Corporation. All rights reserved.
// Last modified on Fri 2 Mar 2007 at 15:51:59 PST by lamport
/***************************************************************************
* 2 Mar 2007: enum <- Enum *
***************************************************************************/
package tla2sany.explorer;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import tla2sany.semantic.ExternalModuleTable;
import tla2sany.semantic.FormalParamNode;
import tla2sany.semantic.Generator;
import tla2sany.semantic.OpDefOrDeclNode;
import tla2sany.semantic.SemanticNode;
import tla2sany.semantic.SymbolNode;
import tla2sany.utilities.Vector;
import util.UniqueString;
/*
* TL - 2014
* Executing the SANY jar with the -d flag enters debugging mode (which is the explorer)
* Here one can type mt to see all semantical objects parsed and one can enter the object id to see
* a more elaborated output
*
*/
// This class implements the explorer tool for traversing
// and examining the semantic graph and associated data structures
// of a TLA specification
public class Explorer {
public Generator generator;
// Next three vars used in reading commands from keyboard
private final InputStreamReader inStream = new InputStreamReader(System.in);
private final int inCapacity = 100;
private StringBuffer input = new StringBuffer(inCapacity);
private int lineLength;
// semNodesTable contains various nodes in the semantic graph keyed
// by their UIDs
private final Hashtable<Integer, ExploreNode> semNodesTable = new Hashtable<>();
// variables used in parsing commands
private int ntokens;
private StringTokenizer inputTokens;
private String firstToken, secondToken;
private Integer icmd, icmd2 = null;
private ExploreNode obj;
private ExternalModuleTable mt;
// Constructor
public Explorer(ExternalModuleTable mtarg) {
mt = mtarg;
}
/*
* Reads one line from "inStream" into "input". Returns "false" if there is an
* EOF; "true" otherwise
*/
private boolean getLine() {
try {
lineLength = 0;
input.setLength(inCapacity);
do {
input.setCharAt(lineLength, (char) inStream.read());
lineLength++;
} while (input.charAt(lineLength - 1) != '\n' & lineLength < inCapacity);
input.setLength(lineLength);
} catch (EOFException e) {
return false;
} catch (IOException e) {
System.out.println("***I/O exception on keyboard input; " + e);
System.exit(-1);
}
if (lineLength >= inCapacity) {
System.out.println("Input line too long; line limited to " + inCapacity + " chars. Line ignored.");
input.setLength(0);
}
return true;
}
// Integer command
private void printNode(int depth) {
// See if the object requested is already in the table
if ((obj = (ExploreNode) semNodesTable.get(icmd)) != null) {
// Print tree to depth of icmd2
System.out.println(((ExploreNode) obj).toString(depth));
System.out.print("\n" + ((ExploreNode) obj).levelDataToString());
} else {
// object requested is not in semNodesTable
System.out.println("No such node encountered yet");
}
} // end method
private void lookUpAndPrintSyntaxTree(String symbName) {
Vector<SymbolNode> symbolVect = new Vector<>(8); // Initial room for 8 symbols with same name
// Collect in Vector symbols all SymbolNodes in the semNodesTable whose name ==
// symbName
for (Enumeration<ExploreNode> Enum = semNodesTable.elements(); Enum.hasMoreElements();) {
ExploreNode semNode = Enum.nextElement();
if (semNode instanceof SymbolNode
&& ((SymbolNode) semNode).getName() == UniqueString.uniqueStringOf(symbName)) {
symbolVect.addElement((SymbolNode) semNode);
}
}
// Print them all
for (int i = 0; i < symbolVect.size(); i++) {
SymbolNode sym = (SymbolNode) (symbolVect.elementAt(i));
((SemanticNode) (sym)).getTreeNode().printST(0);
System.out.println();
}
}
private void lookUpAndPrintDef(String symbName) {
Vector<SymbolNode> symbolVect = new Vector<>(8); // Initial room for 8 symbols with same name
// Collect in Vector symbols all SymbolNodes in the semNodesTable whose name ==
// symbName
for (Enumeration<ExploreNode> Enum = semNodesTable.elements(); Enum.hasMoreElements();) {
ExploreNode semNode = Enum.nextElement();
if (semNode instanceof SymbolNode
&& ((SymbolNode) semNode).getName() == UniqueString.uniqueStringOf(symbName)) {
symbolVect.addElement((SymbolNode) semNode);
}
}
// Print them all
for (int i = 0; i < symbolVect.size(); i++) {
SymbolNode sym = (SymbolNode) (symbolVect.elementAt(i));
if (sym instanceof OpDefOrDeclNode) {
if (((OpDefOrDeclNode) sym).getOriginallyDefinedInModuleNode() != null) {
System.out.print(
"Module: " + ((OpDefOrDeclNode) sym).getOriginallyDefinedInModuleNode().getName() + "\n");
} else {
System.out.print("Module: " + "null" + "\n");
}
} else if (sym instanceof FormalParamNode) {
System.out.print("Module: " + ((FormalParamNode) sym).getModuleNode().getName());
}
System.out.println(((ExploreNode) (symbolVect.elementAt(i))).toString(100));
System.out.println();
}
}
private void levelDataPrint(String symbName) {
Vector<SymbolNode> symbolVect = new Vector<>(8); // Initial room for 8 symbols with same name
// Collect in Vector symbols all SymbolNodes in the semNodesTable whose name ==
// symbName
for (Enumeration<ExploreNode> Enum = semNodesTable.elements(); Enum.hasMoreElements();) {
ExploreNode semNode = Enum.nextElement();
if (semNode instanceof SymbolNode
&& ((SymbolNode) semNode).getName() == UniqueString.uniqueStringOf(symbName)) {
symbolVect.addElement((SymbolNode) semNode);
}
}
// Print them all
for (int i = 0; i < symbolVect.size(); i++) {
SymbolNode sym = (SymbolNode) (symbolVect.elementAt(i));
if (sym instanceof OpDefOrDeclNode) {
if (((OpDefOrDeclNode) sym).getOriginallyDefinedInModuleNode() != null) {
System.out.print(
"Module: " + ((OpDefOrDeclNode) sym).getOriginallyDefinedInModuleNode().getName() + "\n");
} else {
System.out.print("Module: " + "null" + "\n");
}
} else if (sym instanceof FormalParamNode) {
System.out.print("Module: " + ((FormalParamNode) sym).getModuleNode().getName() + "\n");
}
System.out.println(((ExploreNode) (sym)).levelDataToString());
System.out.println();
}
}
private void executeCommand() throws ExplorerQuitException {
// At this point icmd (firsToken) may be null, but icmd2
// (second token) is always non-null
// Integers as commands start printing at the node having icmd == UID;
// non-integer commands do something else
if (icmd != null) { // first token is an integer
printNode(icmd2.intValue());
} else { // the first token is not an integer
// non-integer commands
if (firstToken.toLowerCase().startsWith("qu")) {
// "quit" command
throw new ExplorerQuitException();
} else if (firstToken.toLowerCase().equals("mt")) {
// Print the semantic graph, rooted in the Module Table
// excluding built-ins and ops defined in module Naturals
if (icmd2 != null) {
mt.printExternalModuleTable(icmd2.intValue(), false);
} else {
mt.printExternalModuleTable(2, false);
}
} else if (firstToken.toLowerCase().equals("mt*")) {
// Print the semantic graph, rooted in the Module Table
// including builtins and ops defined in Naturals
if (icmd2 != null) {
mt.printExternalModuleTable(icmd2.intValue(), true);
} else {
mt.printExternalModuleTable(2, true);
}
} else if (firstToken.equalsIgnoreCase("dot")) {
dotSemanticGraph();
} else if (firstToken.toLowerCase().startsWith("cst")) {
printSyntaxTree();
} else if (firstToken.toLowerCase().startsWith("s")) {
if (secondToken != null) {
lookUpAndPrintSyntaxTree(secondToken);
} else {
System.out.println("***Error: You must indicate what name you want to print the syntax tree of.");
}
} else if (firstToken.toLowerCase().startsWith("d")) {
if (secondToken != null) {
lookUpAndPrintDef(secondToken);
} else {
System.out.println("***Error: You must indicate what name you want to print the definition of.");
}
} else if (firstToken.toLowerCase().startsWith("l")) {
if (secondToken != null) {
levelDataPrint(secondToken);
} else {
System.out.println("***Error: You must indicate what name you want to print the level data of.");
}
} else {
// unknown command
System.out.println("Unknown command: " + firstToken.toString());
return;
}
} // end else
}
private void parseAndExecuteCommand() throws ExplorerQuitException {
icmd = null;
icmd2 = null;
ntokens = 0;
// Do nothing if cmd line contains no tokens
if (!inputTokens.hasMoreElements())
return;
// Process first token
ntokens++;
firstToken = (String) (inputTokens.nextElement());
// Try parsing first token as an Integer
try {
icmd = Integer.valueOf(firstToken);
} catch (Exception e) {
}
// Process second token (if present)
if (inputTokens.hasMoreElements()) {
ntokens++;
secondToken = (String) (inputTokens.nextElement());
// Try parsing second token as an Integer
try {
icmd2 = Integer.valueOf(secondToken);
} catch (Exception e) {
}
}
// A single token command defaults the depth to 20, except for
// "mt" command, which defaults to 2
if (ntokens < 2 || (icmd2 != null && icmd2.intValue() < 0)) {
if (firstToken.toLowerCase().startsWith("mt")) {
icmd2 = new Integer(2);
} else {
icmd2 = new Integer(4);
}
}
if (inputTokens.hasMoreElements()) {
System.out.println("Command has too many tokens");
return;
}
executeCommand();
} // end method
public void dotSemanticGraph() {
final DotExplorerVisitor visitor = new DotExplorerVisitor(mt.getRootModule());
mt.getRootModule().walkGraph(visitor.getTable(), visitor);
visitor.done();
}
public void printSyntaxTree() {
int key;
// Prepare to iterate over ExternalModuleTable entries
Iterator<ExternalModuleTable.ExternalModuleTableEntry> modules = mt.moduleHashTable.values().iterator();
ExternalModuleTable.ExternalModuleTableEntry mte;
// For each entry ExternalModuleTableEntry mte in the ExternalModuleTable mt ...
while (modules.hasNext()) {
key = -1;
mte = modules.next();
// Did the module parse correctly?
if (mte != null) {
if (mte.getModuleNode() != null) {
key = mte.getModuleNode().getUid();
// Make an entry in the semNodesTable for this ModuleNode
semNodesTable.put(key, mte.getModuleNode());
// Print the concrete syntax tree for this ExternalModuleTableEntry
System.out.println("\n*** Concrete Syntax Tree for Module " + key);
tla2sany.st.TreeNode stn = mte.getModuleNode().getTreeNode();
stn.printST(0); // Zero indentation level
System.out.println("\n*** End of concrete syntax tree for Module " + key);
} else {
System.out.println("\n*** Null ExternalModuleTableEntry. "
+ "\n*** Next module did not parse, and cannot be printed.");
}
} else {
System.out.println("*** Null SemanticNode in ExternalModuleTableEntry. "
+ "/n*** Next module did not parse, and cannot be printed.");
}
}
}
public void main(final String[] args) throws ExplorerQuitException {
if (mt == null) {
System.out.println("*** module table == null in Explorer.main() ***");
return;
}
final List<String> asList = Arrays.asList(args);
// Passing single token commands as command line parameter skips Explorer's
// interpreter mode.
if (asList.contains("cst")) {
inputTokens = new StringTokenizer("cst");
parseAndExecuteCommand();
System.exit(0);
} else if (asList.contains("dot")) {
inputTokens = new StringTokenizer("dot");
parseAndExecuteCommand();
System.exit(0);
} else if (asList.contains("mt")) {
inputTokens = new StringTokenizer("mt");
parseAndExecuteCommand();
System.exit(0);
} else if (asList.contains("mt*")) {
inputTokens = new StringTokenizer("mt*");
parseAndExecuteCommand();
System.exit(0);
} else {
// Get all semNodes in semNodeTable
mt.walkGraph(semNodesTable);
// Print initial user input prompt
System.out.println("\n\n*** TLA+ semantic graph exploration tool v 1.0 (DRJ)");
System.out.print("\n>>");
// Main command interpreter loop
while (getLine()) {
inputTokens = new StringTokenizer(input.toString());
parseAndExecuteCommand();
// Print next user prompt
System.out.print("\n>>");
} // end while
}
} // end main() method
} // end class