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