1 package persistence.antlr; 2 3 8 9 import persistence.antlr.collections.impl.Vector; 10 11 import java.util.Hashtable ; 12 13 16 public class RuleBlock extends AlternativeBlock { 17 protected String ruleName; 18 protected String argAction = null; protected String throwsSpec = null; 20 protected String returnAction = null; protected RuleEndElement endNode; 23 protected boolean testLiterals = false; 25 26 Vector labeledElements; 29 protected boolean[] lock; protected Lookahead cache[]; 33 39 Hashtable exceptionSpecs; 41 protected boolean defaultErrorHandler = true; 43 protected String ignoreRule = null; 44 45 46 public RuleBlock(Grammar g, String r) { 47 super(g); 48 ruleName = r; 49 labeledElements = new Vector(); 50 cache = new Lookahead[g.maxk + 1]; 51 exceptionSpecs = new Hashtable (); 52 setAutoGen(g instanceof ParserGrammar); 53 } 54 55 56 public RuleBlock(Grammar g, String r, int line, boolean doAutoGen_) { 57 this(g, r); 58 this.line = line; 59 setAutoGen(doAutoGen_); 60 } 61 62 public void addExceptionSpec(ExceptionSpec ex) { 63 if (findExceptionSpec(ex.label) != null) { 64 if (ex.label != null) { 65 grammar.antlrTool.error("Rule '" + ruleName + "' already has an exception handler for label: " + ex.label); 66 } 67 else { 68 grammar.antlrTool.error("Rule '" + ruleName + "' already has an exception handler"); 69 } 70 } 71 else { 72 exceptionSpecs.put((ex.label == null ? "" : ex.label.getText()), ex); 73 } 74 } 75 76 public ExceptionSpec findExceptionSpec(Token label) { 77 return (ExceptionSpec)exceptionSpecs.get(label == null ? "" : label.getText()); 78 } 79 80 public ExceptionSpec findExceptionSpec(String label) { 81 return (ExceptionSpec)exceptionSpecs.get(label == null ? "" : label); 82 } 83 84 public void generate() { 85 grammar.generator.gen(this); 86 } 87 88 public boolean getDefaultErrorHandler() { 89 return defaultErrorHandler; 90 } 91 92 public RuleEndElement getEndElement() { 93 return endNode; 94 } 95 96 public String getIgnoreRule() { 97 return ignoreRule; 98 } 99 100 public String getRuleName() { 101 return ruleName; 102 } 103 104 public boolean getTestLiterals() { 105 return testLiterals; 106 } 107 108 public boolean isLexerAutoGenRule() { 109 return ruleName.equals("nextToken"); 110 } 111 112 public Lookahead look(int k) { 113 return grammar.theLLkAnalyzer.look(k, this); 114 } 115 116 public void prepareForAnalysis() { 117 super.prepareForAnalysis(); 118 lock = new boolean[grammar.maxk + 1]; 119 } 120 121 public void setDefaultErrorHandler(boolean value) { 123 defaultErrorHandler = value; 124 } 125 126 public void setEndElement(RuleEndElement re) { 127 endNode = re; 128 } 129 130 public void setOption(Token key, Token value) { 131 if (key.getText().equals("defaultErrorHandler")) { 132 if (value.getText().equals("true")) { 133 defaultErrorHandler = true; 134 } 135 else if (value.getText().equals("false")) { 136 defaultErrorHandler = false; 137 } 138 else { 139 grammar.antlrTool.error("Value for defaultErrorHandler must be true or false", grammar.getFilename(), key.getLine(), key.getColumn()); 140 } 141 } 142 else if (key.getText().equals("testLiterals")) { 143 if (!(grammar instanceof LexerGrammar)) { 144 grammar.antlrTool.error("testLiterals option only valid for lexer rules", grammar.getFilename(), key.getLine(), key.getColumn()); 145 } 146 else { 147 if (value.getText().equals("true")) { 148 testLiterals = true; 149 } 150 else if (value.getText().equals("false")) { 151 testLiterals = false; 152 } 153 else { 154 grammar.antlrTool.error("Value for testLiterals must be true or false", grammar.getFilename(), key.getLine(), key.getColumn()); 155 } 156 } 157 } 158 else if (key.getText().equals("ignore")) { 159 if (!(grammar instanceof LexerGrammar)) { 160 grammar.antlrTool.error("ignore option only valid for lexer rules", grammar.getFilename(), key.getLine(), key.getColumn()); 161 } 162 else { 163 ignoreRule = value.getText(); 164 } 165 } 166 else if (key.getText().equals("paraphrase")) { 167 if (!(grammar instanceof LexerGrammar)) { 168 grammar.antlrTool.error("paraphrase option only valid for lexer rules", grammar.getFilename(), key.getLine(), key.getColumn()); 169 } 170 else { 171 TokenSymbol ts = grammar.tokenManager.getTokenSymbol(ruleName); 173 if (ts == null) { 174 grammar.antlrTool.panic("cannot find token associated with rule " + ruleName); 175 } 176 ts.setParaphrase(value.getText()); 177 } 178 } 179 else if (key.getText().equals("generateAmbigWarnings")) { 180 if (value.getText().equals("true")) { 181 generateAmbigWarnings = true; 182 } 183 else if (value.getText().equals("false")) { 184 generateAmbigWarnings = false; 185 } 186 else { 187 grammar.antlrTool.error("Value for generateAmbigWarnings must be true or false", grammar.getFilename(), key.getLine(), key.getColumn()); 188 } 189 } 190 else { 191 grammar.antlrTool.error("Invalid rule option: " + key.getText(), grammar.getFilename(), key.getLine(), key.getColumn()); 192 } 193 } 194 195 public String toString() { 196 String s = " FOLLOW={"; 197 Lookahead cache[] = endNode.cache; 198 int k = grammar.maxk; 199 boolean allNull = true; 200 for (int j = 1; j <= k; j++) { 201 if (cache[j] == null) continue; 202 s += cache[j].toString(",", grammar.tokenManager.getVocabulary()); 203 allNull = false; 204 if (j < k && cache[j + 1] != null) s += ";"; 205 } 206 s += "}"; 207 if (allNull) s = ""; 208 return ruleName + ": " + super.toString() + " ;" + s; 209 } 210 } 211 | Popular Tags |