]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.basicexpression/src/org/simantics/basicexpression/Expressions.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.basicexpression / src / org / simantics / basicexpression / Expressions.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.basicexpression;\r
13 \r
14 import java.io.PushbackReader;\r
15 import java.io.StringReader;\r
16 \r
17 import org.simantics.basicexpression.analysis.DepthFirstAdapter;\r
18 import org.simantics.basicexpression.lexer.Lexer;\r
19 import org.simantics.basicexpression.node.Start;\r
20 import org.simantics.basicexpression.parser.Parser;\r
21 \r
22 public class Expressions {\r
23 \r
24         private static final boolean DEBUG = false;\r
25         \r
26     public static void evaluate(String expression, DepthFirstAdapter evaluator) {\r
27         \r
28         try\r
29         {\r
30             // Create a Parser instance.\r
31             Parser p =\r
32                 new Parser(\r
33                         new Lexer(\r
34                                 new PushbackReader(new StringReader(expression))));\r
35 \r
36             // Parse the input.\r
37             Start tree = p.parse();\r
38             \r
39             if(DEBUG)\r
40                 tree.apply(new PrettyPrintExpressionVisitor());\r
41             \r
42 //            System.out.println("tree=" + tree.getClass().getName());\r
43             \r
44             // Apply the translation.\r
45             tree.apply(evaluator);\r
46 \r
47         }\r
48         catch(Exception e)\r
49         {\r
50                 e.printStackTrace();\r
51                 evaluator.except(e);\r
52         }        \r
53         \r
54     }\r
55 \r
56 }\r