rename Node->StateNode
[sbp.git] / src / edu / berkeley / sbp / Element.java
1 // (C) 2006-2007 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp;
4 import java.util.*;
5
6 /**
7  *  <font color=green>
8  *  the root superclass for all components of the grammar (terminals,
9  *  nonterminals, literals, etc)
10  *  </font>
11  */
12 public abstract class Element implements SequenceOrElement {
13
14     /** sorry, you can't make up new, custom elements */
15     Element() { }
16
17     /** a more verbose version of toString() for displaying whole grammars */
18     abstract StringBuffer toString(StringBuffer sb);
19
20     /** a slow and inefficient epsilon-ness checker used when constructing parse trees (see Union.epsilonForm()) */
21     public static boolean possiblyEpsilon(SequenceOrElement e) {
22         if (e instanceof Atom) return false;
23         if (e instanceof Sequence) {
24             Sequence s = (Sequence)e;
25             for(Sequence.Pos p = s.firstp(); !p.isLast(); p = p.next())
26                 if (!possiblyEpsilon(p.element()))
27                     return false;
28             return true;
29         }
30         if (e instanceof Union) {
31             Union u = (Union)e;
32             if (u.visiting) return true;
33             try {
34                 u.visiting = true;
35                 for(Sequence s : u)
36                     if (possiblyEpsilon(s))
37                         return true;
38                 return false;
39             } finally {
40                 u.visiting = false;
41             }
42         }
43         throw new Error();
44     }
45     boolean visiting = false;
46 }