Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/sablecc/sablecc/Symbol.java


1   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2    * This file is part of SableCC.                             *
3    * See the file "LICENSE" for copyright information and the  *
4    * terms and conditions for copying, distribution and        *
5    * modification of SableCC.                                  *
6    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7   
8   package org.sablecc.sablecc;
9   
10  import java.util.*;
11  import com.sun.java.util.collections.*;
12  import java.util.Vector;
13  
14  final class Symbol implements Comparable
15  {
16      private static Vector terminals;
17      private static Vector nonterminals;
18      private static TreeMap names;
19  
20      private static boolean modified_ = true;
21      private static Symbol[] symbols_;
22      private static Symbol[] terminals_;
23      private static Symbol[] nonterminals_;
24  
25      final String name;
26      final String errorName;
27      final boolean terminal;
28      final int index;
29  
30      static {
31          reinit();
32      }
33  
34      Symbol(String name, String errorName, boolean terminal)
35      {
36          if(names.get(name) != null)
37          {
38              throw new IllegalArgumentException("The symbol " + name + " aready exists.");
39          }
40  
41          if(terminal)
42          {
43              terminals.addElement(this);
44              this.index = terminals.indexOf(this);
45          }
46          else
47          {
48              nonterminals.addElement(this);
49              this.index = nonterminals.indexOf(this);
50          }
51  
52          this.name = name;
53          this.errorName = errorName;
54          this.terminal = terminal;
55          names.put(name, this);
56          modified_ = true;
57      }
58  
59      public static void reinit()
60      {
61          terminals = new Vector();
62          nonterminals = new Vector();
63          names = new TreeMap(StringComparator.instance);
64          modified_ = true;
65          symbols_ = null;
66          terminals_ = null;
67          nonterminals_ = null;
68      }
69  
70      static Symbol symbol(String name)
71      {
72          return (Symbol) names.get(name);
73      }
74  
75      static Symbol symbol(int index, boolean terminal)
76      {
77          if(terminal)
78          {
79              return (Symbol) terminals.elementAt(index);
80          }
81          else
82          {
83              return (Symbol) nonterminals.elementAt(index);
84          }
85      }
86  
87      private static void computeArrays()
88      {
89          symbols_ = new Symbol[terminals.size() + nonterminals.size()];
90          terminals_ = new Symbol[terminals.size()];
91          nonterminals_ = new Symbol[nonterminals.size()];
92  
93          terminals.copyInto(terminals_);
94          nonterminals.copyInto(nonterminals_);
95          System.arraycopy(terminals_, 0, symbols_, 0, terminals_.length);
96          System.arraycopy(nonterminals_, 0, symbols_, terminals_.length, nonterminals_.length);
97  
98          modified_ = false;
99      }
100 
101     static Symbol[] symbols()
102     {
103         if(modified_)
104         {
105             computeArrays();
106         }
107 
108         return symbols_;
109     }
110 
111     static Symbol[] terminals()
112     {
113         if(modified_)
114         {
115             computeArrays();
116         }
117 
118         return terminals_;
119     }
120 
121     static Symbol[] nonterminals()
122     {
123         if(modified_)
124         {
125             computeArrays();
126         }
127 
128         return nonterminals_;
129     }
130 
131     public String toString()
132     {
133         return name;
134     }
135 
136     public int compareTo(Object object)
137     {
138         Symbol symbol = (Symbol) object;
139 
140         if(terminal ^ symbol.terminal)
141         {
142             if(terminal)
143             {
144                 return 1;
145             }
146 
147             return -1;
148         }
149 
150         return index - symbol.index;
151     }
152 }
153 
154