1 21 22 package net.percederberg.grammatica.parser; 23 24 import java.util.ArrayList ; 25 26 44 public class Analyzer { 45 46 49 public Analyzer() { 50 } 51 52 68 public Node analyze(Node node) throws ParserLogException { 69 ParserLogException log = new ParserLogException(); 70 71 node = analyze(node, log); 72 if (log.getErrorCount() > 0) { 73 throw log; 74 } 75 return node; 76 } 77 78 92 private Node analyze(Node node, ParserLogException log) { 93 Production prod; 94 int errorCount; 95 96 errorCount = log.getErrorCount(); 97 if (node instanceof Production) { 98 prod = (Production) node; 99 prod = new Production(prod.getPattern()); 100 try { 101 enter(prod); 102 } catch (ParseException e) { 103 log.addError(e); 104 } 105 for (int i = 0; i < node.getChildCount(); i++) { 106 try { 107 child(prod, analyze(node.getChildAt(i), log)); 108 } catch (ParseException e) { 109 log.addError(e); 110 } 111 } 112 try { 113 return exit(prod); 114 } catch (ParseException e) { 115 if (errorCount == log.getErrorCount()) { 116 log.addError(e); 117 } 118 } 119 } else { 120 node.removeAllValues(); 121 try { 122 enter(node); 123 } catch (ParseException e) { 124 log.addError(e); 125 } 126 try { 127 return exit(node); 128 } catch (ParseException e) { 129 if (errorCount == log.getErrorCount()) { 130 log.addError(e); 131 } 132 } 133 } 134 return null; 135 } 136 137 146 protected void enter(Node node) throws ParseException { 147 } 148 149 162 protected Node exit(Node node) throws ParseException { 163 return node; 164 } 165 166 178 protected void child(Production node, Node child) 179 throws ParseException { 180 181 node.addChild(child); 182 } 183 184 197 protected Node getChildAt(Node node, int pos) throws ParseException { 198 Node child; 199 200 if (node == null) { 201 throw new ParseException( 202 ParseException.INTERNAL_ERROR, 203 "attempt to read 'null' parse tree node", 204 -1, 205 -1); 206 } 207 child = node.getChildAt(pos); 208 if (child == null) { 209 throw new ParseException( 210 ParseException.INTERNAL_ERROR, 211 "node '" + node.getName() + "' has no child at " + 212 "position " + pos, 213 node.getStartLine(), 214 node.getStartColumn()); 215 } 216 return child; 217 } 218 219 233 protected Node getChildWithId(Node node, int id) 234 throws ParseException { 235 236 Node child; 237 238 if (node == null) { 239 throw new ParseException( 240 ParseException.INTERNAL_ERROR, 241 "attempt to read 'null' parse tree node", 242 -1, 243 -1); 244 } 245 for (int i = 0; i < node.getChildCount(); i++) { 246 child = node.getChildAt(i); 247 if (child != null && child.getId() == id) { 248 return child; 249 } 250 } 251 throw new ParseException( 252 ParseException.INTERNAL_ERROR, 253 "node '" + node.getName() + "' has no child with id " + id, 254 node.getStartLine(), 255 node.getStartColumn()); 256 } 257 258 270 protected Object getValue(Node node, int pos) throws ParseException { 271 Object value; 272 273 if (node == null) { 274 throw new ParseException( 275 ParseException.INTERNAL_ERROR, 276 "attempt to read 'null' parse tree node", 277 -1, 278 -1); 279 } 280 value = node.getValue(pos); 281 if (value == null) { 282 throw new ParseException( 283 ParseException.INTERNAL_ERROR, 284 "node '" + node.getName() + "' has no value at " + 285 "position " + pos, 286 node.getStartLine(), 287 node.getStartColumn()); 288 } 289 return value; 290 } 291 292 306 protected int getIntValue(Node node, int pos) throws ParseException { 307 Object value; 308 309 value = getValue(node, pos); 310 if (value instanceof Integer ) { 311 return ((Integer ) value).intValue(); 312 } else { 313 throw new ParseException( 314 ParseException.INTERNAL_ERROR, 315 "node '" + node.getName() + "' has no integer value " + 316 "at position " + pos, 317 node.getStartLine(), 318 node.getStartColumn()); 319 } 320 } 321 322 336 protected String getStringValue(Node node, int pos) 337 throws ParseException { 338 339 Object value; 340 341 value = getValue(node, pos); 342 if (value instanceof String ) { 343 return (String ) value; 344 } else { 345 throw new ParseException( 346 ParseException.INTERNAL_ERROR, 347 "node '" + node.getName() + "' has no string value " + 348 "at position " + pos, 349 node.getStartLine(), 350 node.getStartColumn()); 351 } 352 } 353 354 363 protected ArrayList getChildValues(Node node) { 364 ArrayList result = new ArrayList (); 365 Node child; 366 ArrayList values; 367 368 for (int i = 0; i < node.getChildCount(); i++) { 369 child = node.getChildAt(i); 370 values = child.getAllValues(); 371 if (values != null) { 372 result.addAll(values); 373 } 374 } 375 return result; 376 } 377 } 378 | Popular Tags |