1 18 19 package org.objectweb.jac.core.parsers.acc; 20 21 import java.util.Iterator ; 22 import java.util.Vector ; 23 import org.apache.log4j.Logger; 24 25 28 public class NonTerminal extends SyntaxElement { 29 static Logger logger = Logger.getLogger("acc.parser"); 30 31 Vector children = new Vector (); 32 33 public NonTerminal(String name, SyntaxElement[] children) { 34 super(name, 35 children[0].getLeft(), 36 children[children.length-1].getRight()); 37 for (int i=0; i<children.length; i++) { 38 addChild(children[i]); 39 } 40 } 41 42 public NonTerminal(String name, SyntaxElement child) { 43 super(name,child.getLeft(),child.getRight()); 44 addChild(child); 45 } 46 47 public NonTerminal(String name) { 48 super(name,Integer.MAX_VALUE,-1); 49 } 50 51 54 public void addChild(SyntaxElement se) { 55 children.add(se); 56 childAdded(se); 57 } 58 59 62 public void insertChild(SyntaxElement se) { 63 children.add(0,se); 64 childAdded(se); 65 } 66 67 70 public SyntaxElement getChild(int index) { 71 return (SyntaxElement)children.get(index); 72 } 73 74 77 public SyntaxElement getChild(String name) { 78 Iterator it = children.iterator(); 79 while (it.hasNext()) { 80 SyntaxElement child = (SyntaxElement)it.next(); 81 if (child.getName().equals(name)) { 82 return child; 83 } 84 } 85 return null; 86 } 87 88 protected void childAdded(SyntaxElement se) { 89 se.setParent(this); 90 if (se.getLeft()<left) 91 left = se.getLeft(); 92 if (se.getRight()>right) 93 right = se.getRight(); 94 } 95 96 102 public Terminal getTerminalAt(int position) { 103 Iterator it = children.iterator(); 104 while (it.hasNext()) { 105 SyntaxElement se = (SyntaxElement)it.next(); 106 if (se.getLeft()<=position && se.getRight()>=position) { 107 logger.debug("Found "+se+" at "+position); 108 if (se instanceof Terminal) 109 return (Terminal)se; 110 else 111 return ((NonTerminal)se).getTerminalAt(position); 112 } 113 } 114 return null; 115 } 116 117 118 124 public SyntaxElement getSyntaxElementAt(int position) { 125 Iterator it = children.iterator(); 126 while (it.hasNext()) { 127 SyntaxElement se = (SyntaxElement)it.next(); 128 if (se.getLeft()<=position && se.getRight()>=position) { 129 logger.debug("Found "+se+" at "+position); 130 if (se instanceof Terminal) 131 return se; 132 else 133 return ((NonTerminal)se).getSyntaxElementAt(position); 134 } 135 } 136 if (getLeft()<=position && getRight()>=position) 137 return this; 138 else 139 return null; 140 } 141 142 } 143 | Popular Tags |