Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libjava / classpath / tools / gnu / classpath / tools / gjdoc / expr / Evaluator.java
1 /* gnu.classpath.tools.gjdoc.expr.Evaluator
2    Copyright (C) 2004, 2012 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 package gnu.classpath.tools.gjdoc.expr;
39
40 import com.sun.javadoc.FieldDoc;
41
42 import java.io.StringReader;
43 import java.math.BigInteger;
44 import antlr.RecognitionException;
45 import antlr.TokenStreamException;
46 import java.util.Set;
47
48 public class Evaluator
49 {
50    /**
51     *  Try to evaluate the given Java expression in the context of the
52     *  given environment.
53     *
54     *  @param expression the Java expression to evaluate. The
55     *  expression string must not include a terminating semicolon.
56     *
57     *  @param source the FieldDoc (part of) whose constant field value
58     *  expression is being evaluated.  Used to prevent circular
59     *  references.
60     *
61     *  @param environment callback hook used by the Evaluator to query
62     *  the value of static fields referenced in the expression.
63     *
64     *  @return a Java object representing the value of the expression,
65     *  or <code>null</code> if the expression evaluates to
66     *  <code>null</code>.
67     *
68     *  @throws IllegalExpressionException if the expression is
69     *  invalid, uses unsupported syntax constructs (e.g. method calls,
70     *  array access) or references unknown static fields.
71     */
72    public static Object evaluate(String expression,
73                                  Set<FieldDoc> visitedFields,
74                                  EvaluatorEnvironment environment)
75       throws IllegalExpressionException
76    {
77       try {
78          JavaLexer lexer = new JavaLexer(new StringReader(expression));
79          JavaRecognizer recognizer = new JavaRecognizer(lexer);
80          Expression e = recognizer.expression();
81          ConstantExpression value = e.evaluate(new Context(environment, visitedFields));
82          return value.asObject();
83       }
84       catch (RecognitionException e) {
85          throw new IllegalExpressionException(e);
86       }
87       catch (TokenStreamException e) {
88          throw new IllegalExpressionException(e);
89       }
90    }
91
92    static int parseInt(String stringValue)
93    {
94       int base = 10;
95
96       if (stringValue.startsWith("0x")) {
97          base = 16;
98          stringValue = stringValue.substring(2);
99       }
100       else if (stringValue.length() > 1 && stringValue.startsWith("0")) {
101          base = 8;
102          stringValue = stringValue.substring(1);
103       }
104       while (stringValue.length() > 1 && stringValue.startsWith("0")) {
105          stringValue = stringValue.substring(1);
106       }
107
108       if (10 == base) {
109          return Integer.parseInt(stringValue);
110       }
111       else {
112          long result = Long.parseLong(stringValue, base);
113
114          if (result > Integer.MAX_VALUE) {
115             result -= 0x100000000L;
116          }
117
118          if (result > Integer.MAX_VALUE) {
119             throw new NumberFormatException(result + " > " + Integer.MAX_VALUE);
120          }
121          else if (result < Integer.MIN_VALUE) {
122             throw new NumberFormatException(result + " < " + Integer.MIN_VALUE);
123          }
124          else {
125             return (int)result;
126          }
127       }
128    }
129
130    static long parseLong(String stringValue)
131    {
132       int base = 10;
133
134       if (stringValue.startsWith("0x")) {
135          base = 16;
136          stringValue = stringValue.substring(2);
137       }
138       else if (stringValue.length() > 1 && stringValue.startsWith("0")) {
139          base = 8;
140          stringValue = stringValue.substring(1);
141       }
142       while (stringValue.length() > 1 && stringValue.startsWith("0")) {
143          stringValue = stringValue.substring(1);
144       }
145
146       BigInteger bigInt = new BigInteger(stringValue, base);
147       long result = bigInt.longValue();
148       return result;
149    }
150 }