1 15 package org.apache.hivemind.conditional; 16 17 import org.apache.hivemind.util.Defense; 18 19 26 public class Parser 27 { 28 private String _input; 29 30 private Lexer _lexer; 31 32 private Token _nextToken; 33 34 private boolean _onDeck; 35 36 39 private static final Evaluator NOT_EVALUATOR = new NotEvaluator(); 40 41 private static final Evaluator OR_EVALUATOR = new OrEvaluator(); 42 43 private static final Evaluator AND_EVALUATOR = new AndEvaluator(); 44 45 public Node parse(String input) 46 { 47 Defense.notNull(input, "input"); 48 49 try 50 { 51 _input = input; 52 _lexer = new Lexer(input); 53 54 Node result = expression(); 55 56 Token token = next(); 57 58 if (token != null) 59 throw new RuntimeException (ConditionalMessages.unparsedToken(token, _input)); 60 61 return result; 62 } 63 finally 64 { 65 _input = null; 66 _nextToken = null; 67 _lexer = null; 68 _onDeck = false; 69 } 70 } 71 72 private Token next() 73 { 74 Token result = _onDeck ? _nextToken : _lexer.next(); 75 76 _onDeck = false; 77 _nextToken = null; 78 79 return result; 80 } 81 82 private Token match(TokenType expected) 83 { 84 Token actual = next(); 85 86 if (actual == null) 87 throw new RuntimeException (ConditionalMessages.unexpectedEndOfInput(_input)); 88 89 if (actual.getType() != expected) 90 throw new RuntimeException (ConditionalMessages.unexpectedToken(expected, actual 91 .getType(), _input)); 92 93 return actual; 94 } 95 96 private Token peek() 97 { 98 if (! _onDeck) 99 { 100 _nextToken = _lexer.next(); 101 _onDeck = true; 102 } 103 104 return _nextToken; 105 } 106 107 private TokenType peekType() 108 { 109 Token next = peek(); 110 111 return next == null ? null : next.getType(); 112 } 113 114 private boolean isPeek(TokenType type) 115 { 116 return peekType() == type; 117 } 118 119 private Node expression() 120 { 121 Node lnode = term(); 122 123 if (isPeek(TokenType.OR)) 124 { 125 next(); 126 127 Node rnode = expression(); 128 129 return new NodeImpl(lnode, rnode, OR_EVALUATOR); 130 } 131 132 if (isPeek(TokenType.AND)) 133 { 134 next(); 135 136 Node rnode = expression(); 137 138 return new NodeImpl(lnode, rnode, AND_EVALUATOR); 139 } 140 141 return lnode; 142 } 143 144 private Node term() 145 { 146 if (isPeek(TokenType.OPAREN)) 147 { 148 next(); 149 150 Node result = expression(); 151 152 match(TokenType.CPAREN); 153 154 return result; 155 } 156 157 if (isPeek(TokenType.NOT)) 158 { 159 next(); 160 161 match(TokenType.OPAREN); 162 163 Node expression = expression(); 164 165 match(TokenType.CPAREN); 166 167 return new NodeImpl(expression, null, NOT_EVALUATOR); 168 } 169 170 if (isPeek(TokenType.PROPERTY)) 171 { 172 next(); 173 174 Token symbolToken = match(TokenType.SYMBOL); 175 176 Evaluator ev = new PropertyEvaluator(symbolToken.getValue()); 177 178 return new NodeImpl(ev); 179 } 180 181 if (isPeek(TokenType.CLASS)) 182 { 183 next(); 184 185 Token symbolToken = match(TokenType.SYMBOL); 186 187 Evaluator ev = new ClassNameEvaluator(symbolToken.getValue()); 188 189 return new NodeImpl(ev); 190 } 191 192 throw new RuntimeException (ConditionalMessages.unparsedToken(next(), _input)); 193 } 194 } | Popular Tags |