1 package persistence.antlr; 2 3 8 9 import persistence.antlr.collections.impl.Vector; 10 11 12 class AlternativeBlock extends AlternativeElement { 13 protected String initAction = null; protected Vector alternatives; 16 protected String label; 18 protected int alti, altj; protected int analysisAlt; 22 protected boolean hasAnAction = false; protected boolean hasASynPred = false; 25 protected int ID = 0; protected static int nblks; boolean not = false; 29 boolean greedy = true; boolean greedySet = false; 32 protected boolean doAutoGen = true; 34 protected boolean warnWhenFollowAmbig = true; 36 protected boolean generateAmbigWarnings = true; 41 public AlternativeBlock(Grammar g) { 42 super(g); 43 alternatives = new Vector(5); 44 this.not = false; 45 nblks++; 46 ID = nblks; 47 } 48 49 public AlternativeBlock(Grammar g, Token start, boolean not) { 50 super(g, start); 51 alternatives = new Vector(5); 52 this.not = not; 55 nblks++; 56 ID = nblks; 57 } 58 59 public void addAlternative(Alternative alt) { 60 alternatives.appendElement(alt); 61 } 62 63 public void generate() { 64 grammar.generator.gen(this); 65 } 66 67 public Alternative getAlternativeAt(int i) { 68 return (Alternative)alternatives.elementAt(i); 69 } 70 71 public Vector getAlternatives() { 72 return alternatives; 73 } 74 75 public boolean getAutoGen() { 76 return doAutoGen; 77 } 78 79 public String getInitAction() { 80 return initAction; 81 } 82 83 public String getLabel() { 84 return label; 85 } 86 87 public Lookahead look(int k) { 88 return grammar.theLLkAnalyzer.look(k, this); 89 } 90 91 public void prepareForAnalysis() { 92 for (int i = 0; i < alternatives.size(); i++) { 93 Alternative a = (Alternative)alternatives.elementAt(i); 95 a.cache = new Lookahead[grammar.maxk + 1]; 96 a.lookaheadDepth = GrammarAnalyzer.LOOKAHEAD_DEPTH_INIT; 97 } 98 } 99 100 104 public void removeTrackingOfRuleRefs(Grammar g) { 105 for (int i = 0; i < alternatives.size(); i++) { 106 Alternative alt = getAlternativeAt(i); 107 AlternativeElement elem = alt.head; 108 while (elem != null) { 109 if (elem instanceof RuleRefElement) { 110 RuleRefElement rr = (RuleRefElement)elem; 111 RuleSymbol rs = (RuleSymbol)g.getSymbol(rr.targetRule); 112 if (rs == null) { 113 grammar.antlrTool.error("rule " + rr.targetRule + " referenced in (...)=>, but not defined"); 114 } 115 else { 116 rs.references.removeElement(rr); 117 } 118 } 119 else if (elem instanceof AlternativeBlock) { ((AlternativeBlock)elem).removeTrackingOfRuleRefs(g); 121 } 122 elem = elem.next; 123 } 124 } 125 } 126 127 public void setAlternatives(Vector v) { 128 alternatives = v; 129 } 130 131 public void setAutoGen(boolean doAutoGen_) { 132 doAutoGen = doAutoGen_; 133 } 134 135 public void setInitAction(String initAction_) { 136 initAction = initAction_; 137 } 138 139 public void setLabel(String label_) { 140 label = label_; 141 } 142 143 public void setOption(Token key, Token value) { 144 if (key.getText().equals("warnWhenFollowAmbig")) { 145 if (value.getText().equals("true")) { 146 warnWhenFollowAmbig = true; 147 } 148 else if (value.getText().equals("false")) { 149 warnWhenFollowAmbig = false; 150 } 151 else { 152 grammar.antlrTool.error("Value for warnWhenFollowAmbig must be true or false", grammar.getFilename(), key.getLine(), key.getColumn()); 153 } 154 } 155 else if (key.getText().equals("generateAmbigWarnings")) { 156 if (value.getText().equals("true")) { 157 generateAmbigWarnings = true; 158 } 159 else if (value.getText().equals("false")) { 160 generateAmbigWarnings = false; 161 } 162 else { 163 grammar.antlrTool.error("Value for generateAmbigWarnings must be true or false", grammar.getFilename(), key.getLine(), key.getColumn()); 164 } 165 } 166 else if (key.getText().equals("greedy")) { 167 if (value.getText().equals("true")) { 168 greedy = true; 169 greedySet = true; 170 } 171 else if (value.getText().equals("false")) { 172 greedy = false; 173 greedySet = true; 174 } 175 else { 176 grammar.antlrTool.error("Value for greedy must be true or false", grammar.getFilename(), key.getLine(), key.getColumn()); 177 } 178 } 179 else { 180 grammar.antlrTool.error("Invalid subrule option: " + key.getText(), grammar.getFilename(), key.getLine(), key.getColumn()); 181 } 182 } 183 184 public String toString() { 185 String s = " ("; 186 if (initAction != null) { 187 s += initAction; 188 } 189 for (int i = 0; i < alternatives.size(); i++) { 190 Alternative alt = getAlternativeAt(i); 191 Lookahead cache[] = alt.cache; 192 int k = alt.lookaheadDepth; 193 if (k == GrammarAnalyzer.LOOKAHEAD_DEPTH_INIT) { 195 } 196 else if (k == GrammarAnalyzer.NONDETERMINISTIC) { 197 s += "{?}:"; 198 } 199 else { 200 s += " {"; 201 for (int j = 1; j <= k; j++) { 202 s += cache[j].toString(",", grammar.tokenManager.getVocabulary()); 203 if (j < k && cache[j + 1] != null) s += ";"; 204 } 205 s += "}:"; 206 } 207 AlternativeElement p = alt.head; 209 String pred = alt.semPred; 210 if (pred != null) { 211 s += pred; 212 } 213 while (p != null) { 214 s += p; 215 p = p.next; 216 } 217 if (i < (alternatives.size() - 1)) { 218 s += " |"; 219 } 220 } 221 s += " )"; 222 return s; 223 } 224 225 } 226 | Popular Tags |