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