1 22 23 package org.javacc.parser; 24 import java.util.Vector ; 25 26 30 31 public class RChoice extends RegularExpression { 32 33 37 public java.util.Vector choices = new java.util.Vector (); 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(); 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 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.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 |