KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javacc > parser > RChoice


1 /*
2  * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3  * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has
4  * intellectual property rights relating to technology embodied in the product
5  * that is described in this document. In particular, and without limitation,
6  * these intellectual property rights may include one or more of the U.S.
7  * patents listed at http://www.sun.com/patents and one or more additional
8  * patents or pending patent applications in the U.S. and in other countries.
9  * U.S. Government Rights - Commercial software. Government users are subject
10  * to the Sun Microsystems, Inc. standard license agreement and applicable
11  * provisions of the FAR and its supplements. Use is subject to license terms.
12  * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
13  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This
14  * product is covered and controlled by U.S. Export Control laws and may be
15  * subject to the export or import laws in other countries. Nuclear, missile,
16  * chemical biological weapons or nuclear maritime end uses or end users,
17  * whether direct or indirect, are strictly prohibited. Export or reexport
18  * to countries subject to U.S. embargo or to entities identified on U.S.
19  * export exclusion lists, including, but not limited to, the denied persons
20  * and specially designated nationals lists is strictly prohibited.
21  */

22
23 package org.javacc.parser;
24 import java.util.Vector JavaDoc;
25
26 /**
27  * Describes regular expressions which are choices from
28  * from among included regular expressions.
29  */

30
31 public class RChoice extends RegularExpression {
32
33   /**
34    * The list of choices of this regular expression. Each
35    * Vector component will narrow to RegularExpression.
36    */

37   public java.util.Vector JavaDoc choices = new java.util.Vector JavaDoc();
38
39   public Nfa GenerateNfa(boolean ignoreCase)
40   {
41      CompressCharLists();
42
43      if (choices.size() == 1)
44         return ((RegularExpression)choices.elementAt(0)).GenerateNfa(ignoreCase);
45
46      Nfa retVal = new Nfa();
47      NfaState startState = retVal.start;
48      NfaState finalState = retVal.end;
49
50      for (int i = 0; i < choices.size(); i++)
51      {
52         Nfa temp;
53         RegularExpression curRE = (RegularExpression)choices.elementAt(i);
54
55         temp = curRE.GenerateNfa(ignoreCase);
56
57         startState.AddMove(temp.start);
58         temp.end.AddMove(finalState);
59      }
60
61      return retVal;
62   }
63
64   void CompressCharLists()
65   {
66      CompressChoices(); // Unroll nested choices
67
RegularExpression curRE;
68      RCharacterList curCharList = null;
69
70      for (int i = 0; i < choices.size(); i++)
71      {
72         curRE = (RegularExpression)choices.elementAt(i);
73
74         while (curRE instanceof RJustName)
75            curRE = ((RJustName)curRE).regexpr;
76
77         if (curRE instanceof RStringLiteral &&
78             ((RStringLiteral)curRE).image.length() == 1)
79            choices.setElementAt(curRE = new RCharacterList(
80                       ((RStringLiteral)curRE).image.charAt(0)), i);
81
82         if (curRE instanceof RCharacterList)
83         {
84            if (((RCharacterList)curRE).negated_list)
85               ((RCharacterList)curRE).RemoveNegation();
86
87            Vector JavaDoc tmp = ((RCharacterList)curRE).descriptors;
88
89            if (curCharList == null)
90               choices.setElementAt(curRE = curCharList =
91                                     new RCharacterList(), i);
92            else
93               choices.removeElementAt(i--);
94
95            for (int j = tmp.size(); j-- > 0;)
96               curCharList.descriptors.addElement(tmp.elementAt(j));
97          }
98
99      }
100   }
101
102   void CompressChoices()
103   {
104      RegularExpression curRE;
105
106      for (int i = 0; i < choices.size(); i++)
107      {
108         curRE = (RegularExpression)choices.elementAt(i);
109
110         while (curRE instanceof RJustName)
111            curRE = ((RJustName)curRE).regexpr;
112
113         if (curRE instanceof RChoice)
114         {
115            choices.removeElementAt(i--);
116            for (int j = ((RChoice)curRE).choices.size(); j-- > 0;)
117               choices.addElement(((RChoice)curRE).choices.elementAt(j));
118         }
119      }
120   }
121
122   public void CheckUnmatchability()
123   {
124      RegularExpression curRE;
125      int numStrings = 0;
126
127      for (int i = 0; i < choices.size(); i++)
128      {
129         if (!(curRE = (RegularExpression)choices.elementAt(i)).private_rexp &&
130             //curRE instanceof RJustName &&
131
curRE.ordinal > 0 && curRE.ordinal < ordinal &&
132             LexGen.lexStates[curRE.ordinal] == LexGen.lexStates[ordinal])
133         {
134            if (label != null)
135               JavaCCErrors.warning(this, "Regular Expression choice : " +
136                  curRE.label + " can never be matched as : " + label);
137            else
138               JavaCCErrors.warning(this, "Regular Expression choice : " +
139                  curRE.label + " can never be matched as token of kind : " +
140                                                                       ordinal);
141         }
142
143         if (!curRE.private_rexp && curRE instanceof RStringLiteral)
144            numStrings++;
145      }
146   }
147
148 }
149
Popular Tags