Coverage Report - com.sdmetrics.math.ExpressionParser - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpressionParser
99%
274/276
99%
227/228
5,607
ExpressionParser$ParserException
100%
2/2
N/A
5,607
 
 1  
 /*
 2  
  * SDMetrics Open Core for UML design measurement
 3  
  * Copyright (c) Juergen Wuest
 4  
  * To contact the author, see <http://www.sdmetrics.com/Contact.html>.
 5  
  * 
 6  
  * This file is part of the SDMetrics Open Core.
 7  
  * 
 8  
  * SDMetrics Open Core is free software: you can redistribute it and/or modify
 9  
  * it under the terms of the GNU Affero General Public License as
 10  
  * published by the Free Software Foundation, either version 3 of the
 11  
  * License, or (at your option) any later version.
 12  
     
 13  
  * SDMetrics Open Core is distributed in the hope that it will be useful,
 14  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  
  * GNU Affero General Public License for more details.
 17  
  *
 18  
  * You should have received a copy of the GNU Affero General Public License
 19  
  * along with SDMetrics Open Core.  If not, see <http://www.gnu.org/licenses/>.
 20  
  *
 21  
  */
 22  
 package com.sdmetrics.math;
 23  
 
 24  
 import java.util.ArrayList;
 25  
 import java.util.Collections;
 26  
 import java.util.List;
 27  
 import java.util.Set;
 28  
 
 29  
 /** Parses metric, set, and condition expressions. */
 30  
 public class ExpressionParser {
 31  
 
 32  
         /** Special token: opening parenthesis. */
 33  82
         private static final ExpressionNode OPENINGPARENTHESIS = new ExpressionNode(
 34  41
                         null, "(");
 35  
         /** Special token: closing parenthesis. */
 36  82
         private static final ExpressionNode CLOSINGPARENTHESIS = new ExpressionNode(
 37  41
                         null, ")");
 38  
         /** Special token: expression list separator. */
 39  82
         private static final ExpressionNode EXPRESSIONLISTSEPARATOR = new ExpressionNode(
 40  41
                         null, ",");
 41  
         /** Special token: end of expression. */
 42  41
         private static final ExpressionNode ENDNODE = new ExpressionNode(null, "");
 43  
 
 44  
         /** The expression to parse. */
 45  
         private String expression;
 46  
         /** Position in the expression string to process next. */
 47  
         private int currentPos;
 48  
         /** Node representation of the current parse position. */
 49  
         private ExpressionNode currentNode;
 50  
 
 51  
         /** Error message in case of syntax errors. */
 52  
         private String errorMessage;
 53  
 
 54  
         /** The set of names that the parser recognizes as functions. */
 55  
         private Set<String> functionNames;
 56  
         /**
 57  
          * The set of names that the parser recognizes as relations with high
 58  
          * precedence.
 59  
          */
 60  
         private Set<String> highPrecedenceRelationNames;
 61  
         /**
 62  
          * The set of names that the parser recognizes as relations with low
 63  
          * precedence.
 64  
          */
 65  
         private Set<String> lowPrecedenceRelationNames;
 66  
 
 67  
         /**
 68  
          * Creates a new parser instance without custom functions and relations.
 69  
          * 
 70  
          * @since 2.3
 71  
          */
 72  
         public ExpressionParser() {
 73  293
                 this(Collections.<String> emptySet(), Collections.<String> emptySet(),
 74  293
                                 Collections.<String> emptySet());
 75  293
         }
 76  
 
 77  
         /**
 78  
          * Creates a new parser instance with custom functions and relations. Note:
 79  
          * the feature to provide custom relations will probably be removed in
 80  
          * future versions of SDMetrics, and the existing special relation operators
 81  
          * (onlist, topmost etc) be replaced with functions.
 82  
          * 
 83  
          * @param functionNames The set of names the parser recognizes as function
 84  
          *        calls (e.g. ln, exp, ...)
 85  
          * @param highPrecedenceRelationNames The set of names the parser recognizes
 86  
          *        as high precedence relations. These relations have the same
 87  
          *        precedence as the comparison relations (
 88  
          *        <code>&gt;, &lt;, &gt;=, &lt;=</code>)
 89  
          * @param lowPrecedenceRelationNames The set of names the parser recognizes
 90  
          *        as low precedence relations. These relations all have the same
 91  
          *        precedence which is lower than the <code>and</code> relation
 92  
          *        (&amp;)
 93  
          * @since 2.3
 94  
          */
 95  594
         public ExpressionParser(Set<String> functionNames,
 96  
                         Set<String> highPrecedenceRelationNames,
 97  
                         Set<String> lowPrecedenceRelationNames) {
 98  594
                 this.functionNames = functionNames;
 99  594
                 this.highPrecedenceRelationNames = highPrecedenceRelationNames;
 100  594
                 this.lowPrecedenceRelationNames = lowPrecedenceRelationNames;
 101  594
         }
 102  
 
 103  
         /**
 104  
          * Parses an expression and returns its operator tree.
 105  
          * 
 106  
          * @param expr The expression to parse.
 107  
          * @return Root node of the operator tree for the expression, or
 108  
          *         <code>null</code> if the expression could not be parsed.
 109  
          */
 110  
         public ExpressionNode parseExpression(String expr) {
 111  21549
                 expression = expr;
 112  21549
                 currentPos = 0;
 113  21549
                 errorMessage = "";
 114  21549
                 ExpressionNode result = null;
 115  
                 try {
 116  21549
                         result = parseExpression();
 117  21535
                         if (currentPos < expression.length() || currentNode != ENDNODE) {
 118  6
                                 throw new ParserException(
 119  6
                                                 "Unexpected token past end of expression: "
 120  6
                                                                 + currentNode.getValue(), currentPos);
 121  
                         }
 122  17
                 } catch (ParserException ex) {
 123  17
                         errorMessage = ex.getMessage();
 124  17
                         result = null;
 125  
                 }
 126  21549
                 return result;
 127  
         }
 128  
 
 129  
         /**
 130  
          * Retrieves a string describing the syntax error that occurred during
 131  
          * expression parsing.
 132  
          * 
 133  
          * @return Description of the parse error.
 134  
          */
 135  
         public String getErrorInfo() {
 136  374
                 return errorMessage;
 137  
         }
 138  
 
 139  
         /**
 140  
          * Exception thrown by the expression parser on syntax errors.
 141  
          */
 142  
         static class ParserException extends RuntimeException {
 143  
                 private static final long serialVersionUID = 4537409434255097180L;
 144  
 
 145  
                 /**
 146  
                  * @param message Description of the syntax error.
 147  
                  * @param pos Index of the location of the error in the expression
 148  
                  *        string.
 149  
                  */
 150  
                 ParserException(String message, int pos) {
 151  17
                         super("Parse error at position " + pos + ": " + message);
 152  17
                 }
 153  
         }
 154  
 
 155  
         /**
 156  
          * Top level parser function to parse an arbitrary expression.
 157  
          * 
 158  
          * @return Root node of the operator tree for the expression.
 159  
          */
 160  
         private ExpressionNode parseExpression() {
 161  23686
                 return lowPrecedenceRelationExpression();
 162  
         }
 163  
 
 164  
         /**
 165  
          * Parses a low precedence relation (operators upto, topmost).
 166  
          * 
 167  
          * @return Root node of the corresponding operator tree.
 168  
          */
 169  
         private ExpressionNode lowPrecedenceRelationExpression() {
 170  23686
                 ExpressionNode result = orExpression();
 171  47349
                 while (currentNode.isOperation()
 172  9
                                 && isLowPrecedenceRelation(currentNode.getValue())) {
 173  7
                         String operator = currentNode.getValue();
 174  7
                         result = handleRightHandSide(result, operator, orExpression());
 175  
                 }
 176  23671
                 return result;
 177  
         }
 178  
 
 179  
         /**
 180  
          * Tests if a string is the name of a function.
 181  
          * 
 182  
          * @param s String to test.
 183  
          * @return <code>true</code> if s is a function, else <code>false</code>.
 184  
          */
 185  
         private boolean isFunction(String s) {
 186  25316
                 return functionNames.contains(s);
 187  
         }
 188  
 
 189  
         /**
 190  
          * Tests if a string is the name of a high precedence relation.
 191  
          * 
 192  
          * @param s String to test.
 193  
          * @return <code>true</code> if s is a high precedence relation, else
 194  
          *         <code>false</code>.
 195  
          */
 196  
         private boolean isHighPrecedenceRelation(String s) {
 197  23864
                 return highPrecedenceRelationNames.contains(s);
 198  
         }
 199  
 
 200  
         /**
 201  
          * Tests if a string is the name of a low precedence relation.
 202  
          * 
 203  
          * @param s String to test.
 204  
          * @return <code>true</code> if s is a low precedence relation, else
 205  
          *         <code>false</code>.
 206  
          */
 207  
         private boolean isLowPrecedenceRelation(String s) {
 208  21883
                 return lowPrecedenceRelationNames.contains(s);
 209  
         }
 210  
 
 211  
         private boolean isCurrentNodeValue(String value) {
 212  37968
                 return value.equals(currentNode.getValue());
 213  
         }
 214  
 
 215  
         private ExpressionNode handleRightHandSide(ExpressionNode lhs,
 216  
                         String operator, ExpressionNode rhs) {
 217  4694
                 if (rhs == null)
 218  0
                         throw new ParserException("Missing right hand side for operator "
 219  0
                                         + operator, currentPos);
 220  4694
                 return new ExpressionNode(operator, new ExpressionNode[] { lhs, rhs });
 221  
         }
 222  
 
 223  
         /**
 224  
          * Parses an "or" expression.
 225  
          * 
 226  
          * @return Root node of the corresponding operator tree.
 227  
          */
 228  
         private ExpressionNode orExpression() {
 229  23693
                 ExpressionNode result = andExpression();
 230  47500
                 while (currentNode.isOperation() && isCurrentNodeValue("|")) {
 231  144
                         result = handleRightHandSide(result, "|", andExpression());
 232  
                 }
 233  23678
                 return result;
 234  
         }
 235  
 
 236  
         /**
 237  
          * Parse an "and" expression.
 238  
          * 
 239  
          * @return Root node of the corresponding operator tree.
 240  
          */
 241  
         private ExpressionNode andExpression() {
 242  23837
                 ExpressionNode result = equalExpression();
 243  47669
                 while (currentNode.isOperation() && isCurrentNodeValue("&")) {
 244  25
                         result = handleRightHandSide(result, "&", equalExpression());
 245  
                 }
 246  23822
                 return result;
 247  
         }
 248  
 
 249  
         /**
 250  
          * Parses an "equal" expression (operators = and !=).
 251  
          * 
 252  
          * @return Root node of the corresponding operator tree.
 253  
          */
 254  
         private ExpressionNode equalExpression() {
 255  23862
                 ExpressionNode result = relExpression();
 256  23850
                 if (currentNode.isOperation()
 257  1420
                                 && (isCurrentNodeValue("=") || isCurrentNodeValue("!="))) {
 258  1257
                         String operator = currentNode.getValue();
 259  1257
                         result = handleRightHandSide(result, operator, relExpression());
 260  
                 }
 261  23847
                 return result;
 262  
         }
 263  
 
 264  
         /**
 265  
          * Parses a high precedence relational expression (comparators &gt;, &ge;
 266  
          * etc and relations "startswith", "endswith", "onlist").
 267  
          * 
 268  
          * @return Root node of the corresponding operator tree.
 269  
          */
 270  
         private ExpressionNode relExpression() {
 271  25119
                 ExpressionNode result = addExpression();
 272  25105
                 if (currentNode.isOperation()
 273  2936
                                 && (isCurrentNodeValue("<") || isCurrentNodeValue(">")
 274  3438
                                                 || isCurrentNodeValue("<=") || isCurrentNodeValue(">=") || isHighPrecedenceRelation(currentNode
 275  3424
                                                 .getValue()))) {
 276  1502
                         String operator = currentNode.getValue();
 277  1502
                         result = handleRightHandSide(result, operator, addExpression());
 278  
                 }
 279  25104
                 return result;
 280  
         }
 281  
 
 282  
         /**
 283  
          * Parses an additive expression (binary operators + and -).
 284  
          * 
 285  
          * @return Root node of the corresponding operator tree.
 286  
          */
 287  
         private ExpressionNode addExpression() {
 288  26621
                 ExpressionNode result = multExpression();
 289  54090
                 while (currentNode.isOperation()
 290  3809
                                 && (isCurrentNodeValue("+") || isCurrentNodeValue("-"))) {
 291  872
                         String operator = currentNode.getValue();
 292  872
                         result = handleRightHandSide(result, operator, multExpression());
 293  
                 }
 294  26606
                 return result;
 295  
         }
 296  
 
 297  
         /**
 298  
          * Parses a multiplicative expression (operators *, /).
 299  
          * 
 300  
          * @return Root node of the corresponding operator tree.
 301  
          */
 302  
         private ExpressionNode multExpression() {
 303  27493
                 ExpressionNode result = powExpression();
 304  54981
                 while (currentNode.isOperation()
 305  3834
                                 && (isCurrentNodeValue("/") || isCurrentNodeValue("*"))) {
 306  25
                         String operator = currentNode.getValue();
 307  25
                         result = handleRightHandSide(result, operator, powExpression());
 308  
                 }
 309  27478
                 return result;
 310  
         }
 311  
 
 312  
         /**
 313  
          * Parses a power expression term (operators ^, -&gt;).
 314  
          * 
 315  
          * @return Root node of the corresponding operator tree.
 316  
          */
 317  
         private ExpressionNode powExpression() {
 318  27518
                 ExpressionNode result = dotExpression();
 319  55035
                 while (currentNode.isOperation()
 320  3863
                                 && (isCurrentNodeValue("^") || isCurrentNodeValue("->"))) {
 321  29
                         String operator = currentNode.getValue();
 322  29
                         result = handleRightHandSide(result, operator, dotExpression());
 323  
                 }
 324  27503
                 return result;
 325  
         }
 326  
 
 327  
         /**
 328  
          * Parses a dot expression "a.b"
 329  
          * 
 330  
          * @return Root node of the corresponding operator tree.
 331  
          */
 332  
         private ExpressionNode dotExpression() {
 333  27547
                 ExpressionNode result = unaryExpression();
 334  55907
                 while (currentNode.isOperation() && (isCurrentNodeValue("."))) {
 335  843
                         result = handleRightHandSide(result, ".", unaryExpression());
 336  
                 }
 337  27532
                 return result;
 338  
         }
 339  
 
 340  
         /**
 341  
          * Parses a unary expression (constants, prefixes +/-/!, function calls,
 342  
          * parenthesis).
 343  
          * 
 344  
          * @return Node representing the unary expression.
 345  
          */
 346  
         private ExpressionNode unaryExpression() {
 347  
 
 348  28775
                 nextToken();
 349  28771
                 String currentValue = currentNode.getValue();
 350  
 
 351  28771
                 if (currentNode.isNumberConstant() || currentNode.isStringConstant()
 352  22640
                                 || currentNode.isIdentifier()) {
 353  26719
                         ExpressionNode constant = currentNode;
 354  26719
                         nextToken();
 355  26719
                         return constant;
 356  2052
                 } else if (currentNode.isOperation() && (isFunction(currentValue))) {
 357  
 
 358  1387
                         ExpressionNode fctCall = currentNode;
 359  1387
                         List<ExpressionNode> argList = new ArrayList<ExpressionNode>();
 360  1387
                         String operator = currentValue;
 361  1387
                         int startPos = currentPos;
 362  1387
                         nextToken();
 363  1387
                         if (currentNode != OPENINGPARENTHESIS)
 364  2
                                 throw new ParserException(
 365  2
                                                 "Expected opening parenthesis following function call to '"
 366  2
                                                                 + operator + "'.", currentPos);
 367  
                         do {
 368  1863
                                 argList.add(parseExpression());
 369  1863
                         } while (currentNode == EXPRESSIONLISTSEPARATOR);
 370  1386
                         if (currentNode != CLOSINGPARENTHESIS)
 371  2
                                 throw new ParserException(
 372  2
                                                 "Missing closing parenthesis following function call to '"
 373  1
                                                                 + operator + "' at position " + startPos + ".",
 374  1
                                                 currentPos);
 375  2770
                         fctCall.setValue(NodeType.OPERATION, operator,
 376  1385
                                         argList.toArray(new ExpressionNode[argList.size()]));
 377  
 
 378  1385
                         nextToken();
 379  1385
                         return fctCall;
 380  
 
 381  665
                 } else if (currentNode.isOperation() && "-".equals(currentValue)
 382  540
                                 || "+".equals(currentValue) || "!".equals(currentValue)) {
 383  385
                         ExpressionNode unary = currentNode;
 384  770
                         unary.setValue(NodeType.OPERATION, currentValue,
 385  385
                                         new ExpressionNode[] { unaryExpression() });
 386  385
                         return unary;
 387  280
                 } else if (currentNode == OPENINGPARENTHESIS) {
 388  274
                         int startIndex = currentPos;
 389  274
                         ExpressionNode temp = parseExpression();
 390  
 
 391  273
                         if (currentNode == CLOSINGPARENTHESIS) {
 392  271
                                 nextToken();
 393  271
                                 return temp;
 394  
                         }
 395  4
                         throw new ParserException(
 396  4
                                         "Missing closing parenthesis opened at position "
 397  4
                                                         + startIndex + ".", currentPos);
 398  6
                 } else if (currentNode == ENDNODE) {
 399  4
                         throw new ParserException("Unexpected end of expression.",
 400  2
                                         currentPos);
 401  
                 } else {
 402  8
                         throw new ParserException("Unexpected token: " + currentValue,
 403  4
                                         currentPos);
 404  
                 }
 405  
         }
 406  
 
 407  
         /**
 408  
          * Consumes a token, leave information about it in {@link #currentType} and
 409  
          * {@link #currentValue} variables.
 410  
          */
 411  
         private void nextToken() {
 412  120235
                 while (currentPos < expression.length()
 413  40161
                                 && Character.isSpaceChar(expression.charAt(currentPos)))
 414  3161
                         currentPos++;
 415  
 
 416  58537
                 if (currentPos >= expression.length()) {
 417  21537
                         currentNode = ENDNODE;
 418  21537
                 } else if (expression.charAt(currentPos) == '+'
 419  36139
                                 || expression.charAt(currentPos) == '-'
 420  35986
                                 || expression.charAt(currentPos) == '*'
 421  35967
                                 || expression.charAt(currentPos) == '^'
 422  35948
                                 || expression.charAt(currentPos) == '/'
 423  35942
                                 || expression.charAt(currentPos) == '='
 424  35279
                                 || expression.charAt(currentPos) == '|'
 425  35253
                                 || expression.charAt(currentPos) == '!'
 426  34406
                                 || expression.charAt(currentPos) == '<'
 427  34387
                                 || expression.charAt(currentPos) == '>'
 428  34337
                                 || expression.charAt(currentPos) == '.'
 429  33494
                                 || expression.charAt(currentPos) == '&') {
 430  3531
                         handleSymbolicOperatorToken();
 431  3531
                 } else if (expression.charAt(currentPos) == '(') {
 432  1660
                         currentPos++;
 433  1660
                         currentNode = OPENINGPARENTHESIS;
 434  1660
                 } else if (expression.charAt(currentPos) == ')') {
 435  1658
                         currentPos++;
 436  1658
                         currentNode = CLOSINGPARENTHESIS;
 437  1658
                 } else if (expression.charAt(currentPos) == ',') {
 438  477
                         currentPos++;
 439  477
                         currentNode = EXPRESSIONLISTSEPARATOR;
 440  477
                 } else if (expression.charAt(currentPos) == '\'') {
 441  7143
                         currentNode = new ExpressionNode(NodeType.STRING,
 442  3572
                                         getStringConstant());
 443  3571
                 } else if (Character.isDigit(expression.charAt(currentPos))) {
 444  5122
                         currentNode = new ExpressionNode(NodeType.NUMBER,
 445  2562
                                         getNumberConstant());
 446  2560
                 } else {
 447  23540
                         handleIdentifier();
 448  
                 }
 449  58533
         }
 450  
 
 451  
         private void handleSymbolicOperatorToken() {
 452  3531
                 String currentValue = String.valueOf(expression.charAt(currentPos));
 453  3531
                 currentPos++;
 454  3531
                 if ("&".equals(currentValue)) {
 455  
                         // In case some XML do not translate &amp; and &lt;in quoted
 456  
                         // text, we accept &amp; as "and" operator and &lt; as "less
 457  
                         // than" operator
 458  25
                         if (currentPos + 3 < expression.length()) {
 459  28
                                 if ("amp;".equals(expression.substring(currentPos,
 460  28
                                                 currentPos + 4))) {
 461  1
                                         currentPos = currentPos + 4; // skip "amp;" part
 462  1
                                 } else if (expression.substring(currentPos, currentPos + 3)
 463  13
                                                 .equals("lt;")) {
 464  1
                                         currentPos = currentPos + 3;
 465  1
                                         currentValue = "<"; // skip "lt;" part
 466  
                                 }
 467  
                         }
 468  
                 }
 469  
                 // check >=, <= !=
 470  3531
                 if (currentPos < expression.length()
 471  3529
                                 && expression.charAt(currentPos) == '=') {
 472  609
                         if ("<".equals(currentValue) || ">".equals(currentValue)
 473  596
                                         || "!".equals(currentValue)) {
 474  608
                                 currentValue += expression.charAt(currentPos);
 475  608
                                 currentPos++;
 476  
                         }
 477  608
                 } else if (currentPos < expression.length()
 478  2920
                                 && expression.charAt(currentPos) == '>') {
 479  
                         // check ->
 480  10
                         if ("-".equals(currentValue)) {
 481  9
                                 currentValue += expression.charAt(currentPos);
 482  9
                                 currentPos++;
 483  
                         }
 484  
                 }
 485  3531
                 currentNode = new ExpressionNode(NodeType.OPERATION, currentValue);
 486  3531
         }
 487  
 
 488  
         private void handleIdentifier() {
 489  23540
                 String currentValue = getIdentifier();
 490  23539
                 NodeType currentType = NodeType.OPERATION;
 491  23539
                 if (currentValue.startsWith("fct_")) {
 492  
                         // In earlier versions, all functions had a prefix (e.g. fct_sqrt()
 493  
                         // instead of sqrt()). We support this prefix for backwards
 494  
                         // compatibility.
 495  3
                         String trail = currentValue.substring(4);
 496  3
                         if (isFunction(trail))
 497  2
                                 currentValue = trail;
 498  
                 }
 499  23539
                 if (isFunction(currentValue) || isHighPrecedenceRelation(currentValue)
 500  21874
                                 || isLowPrecedenceRelation(currentValue))
 501  1672
                         currentType = NodeType.OPERATION;
 502  21867
                 else if ("and".equals(currentValue)) {
 503  1
                         currentValue = "&";
 504  1
                 } else if ("or".equals(currentValue)) {
 505  118
                         currentValue = "|";
 506  118
                 } else if ("not".equals(currentValue)) {
 507  1
                         currentValue = "!";
 508  1
                 } else if ("lt".equals(currentValue)) {
 509  685
                         currentValue = "<";
 510  685
                 } else if ("gt".equals(currentValue)) {
 511  469
                         currentValue = ">";
 512  469
                 } else if ("le".equals(currentValue)) {
 513  1
                         currentValue = "<=";
 514  1
                 } else if ("ge".equals(currentValue)) {
 515  1
                         currentValue = ">=";
 516  1
                 } else if ("in".equals(currentValue)) {
 517  1
                         currentValue = "->";
 518  1
                 } else
 519  20590
                         currentType = NodeType.IDENTIFIER;
 520  
 
 521  23539
                 currentNode = new ExpressionNode(currentType, currentValue);
 522  23539
         }
 523  
 
 524  
         /**
 525  
          * Parses a number constant in the expression string.
 526  
          * 
 527  
          * @return The number constant, as a string.
 528  
          */
 529  
         private String getNumberConstant() {
 530  2562
                 int startIndex = currentPos;
 531  
 
 532  8351
                 while (currentPos < expression.length()
 533  3556
                                 && Character.isDigit(expression.charAt(currentPos))) {
 534  3227
                         currentPos++;
 535  
                 }
 536  2562
                 if (currentPos < expression.length()
 537  329
                                 && expression.charAt(currentPos) == '.') {
 538  23
                         currentPos++;
 539  23
                         int fractionStart = currentPos;
 540  95
                         while (currentPos < expression.length()
 541  64
                                         && Character.isDigit(expression.charAt(currentPos))) {
 542  49
                                 currentPos++;
 543  
                         }
 544  23
                         if (fractionStart == currentPos)
 545  2
                                 throw new ParserException(
 546  1
                                                 "Missing fraction part of number constant.", currentPos);
 547  
                 }
 548  2561
                 if (currentPos < expression.length()
 549  320
                                 && (expression.charAt(currentPos) == 'e' || expression
 550  317
                                                 .charAt(currentPos) == 'E')) {
 551  6
                         currentPos++;
 552  6
                         if (currentPos < expression.length()
 553  5
                                         && (expression.charAt(currentPos) == '+' || expression
 554  4
                                                         .charAt(currentPos) == '-')) {
 555  4
                                 currentPos++;
 556  
                         }
 557  6
                         int exponentStart = currentPos;
 558  17
                         while (currentPos < expression.length()
 559  6
                                         && Character.isDigit(expression.charAt(currentPos))) {
 560  5
                                 currentPos++;
 561  
                         }
 562  6
                         if (exponentStart == currentPos)
 563  2
                                 throw new ParserException(
 564  1
                                                 "Missing exponent of scientific notation number constant.",
 565  1
                                                 currentPos);
 566  
                 }
 567  
 
 568  2560
                 return expression.substring(startIndex, currentPos);
 569  
         }
 570  
 
 571  
         /**
 572  
          * Parses an identifier in the expression string.
 573  
          * 
 574  
          * @return the identifier.
 575  
          */
 576  
         private String getIdentifier() {
 577  23540
                 int startIndex = currentPos;
 578  217701
                 while (currentPos < expression.length()
 579  178944
                                 && isIdentifierCharacter(expression.charAt(currentPos))) {
 580  170621
                         currentPos++;
 581  
                 }
 582  23540
                 if (startIndex == currentPos)
 583  2
                         throw new ParserException("Unexpected character: "
 584  1
                                         + expression.charAt(currentPos), startIndex + 1);
 585  23539
                 return expression.substring(startIndex, currentPos);
 586  
         }
 587  
 
 588  
         /**
 589  
          * Tests if a character is a valid identifier character.
 590  
          * 
 591  
          * @param c Character to test.
 592  
          * @return <code>true</code> if the character is a letter, digit, or
 593  
          *         underscore.
 594  
          */
 595  
         public static boolean isIdentifierCharacter(char c) {
 596  178982
                 return Character.isLetter(c) || Character.isDigit(c) || c == '_';
 597  
         }
 598  
 
 599  
         /**
 600  
          * Parses a string constant in the expression string. String constants are
 601  
          * enclosed by apostrophes.
 602  
          * 
 603  
          * @return The string constant without the enclosing apostrophes.
 604  
          */
 605  
         private String getStringConstant() {
 606  
 
 607  3572
                 currentPos++; // skip leading apostrophe
 608  3572
                 int startIndex = currentPos;
 609  40027
                 while (currentPos < expression.length()
 610  36454
                                 && expression.charAt(currentPos) != '\'') {
 611  32883
                         currentPos++;
 612  
                 }
 613  3572
                 if (currentPos == expression.length()) {
 614  2
                         throw new ParserException(
 615  2
                                         "Missing closing apostrophe of string constant starting at position "
 616  2
                                                         + startIndex + ".", currentPos);
 617  
                 }
 618  3571
                 currentPos++; // skip the closing apostrophe
 619  
 
 620  3571
                 return expression.substring(startIndex, currentPos - 1);
 621  
         }
 622  
 }