From 64c883d045494da9b9c4af9a9bbfd2a9965d0c74 Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 12 Jan 2010 15:00:12 +0000 Subject: [PATCH] Expression parser. Expression field listener checks the correctness of the expression. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13486 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../AuxiliaryExpressionViewFactor.java | 15 +-- .../ConstantExpressionViewFactor.java | 13 +-- .../ExpressionFieldKeyListener.java | 45 +++++++++ .../ParameterExpressionViewFactor.java | 13 +-- .../StockExpressionViewFactor.java | 13 +-- .../WithLookupExpressionViewFactor.java | 24 +---- org.simantics.sysdyn/META-INF/MANIFEST.MF | 1 + .../expressionParser/ExpressionParser.jj | 97 +++++++++++++++++-- .../TestExpressionParser.java | 8 +- .../expressions/DelayExpression.java | 5 +- .../expressions/LookupExpression.java | 5 +- .../expressions/WithLookupExpression.java | 6 +- 12 files changed, 159 insertions(+), 86 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/AuxiliaryExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/AuxiliaryExpressionViewFactor.java index e756dbf6..4d523cd1 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/AuxiliaryExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/AuxiliaryExpressionViewFactor.java @@ -7,8 +7,6 @@ import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.events.FocusAdapter; import org.eclipse.swt.events.FocusEvent; -import org.eclipse.swt.events.KeyAdapter; -import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; @@ -54,19 +52,8 @@ public class AuxiliaryExpressionViewFactor implements IExpressionViewFactor { lastSelection = expression.getSelection(); } }); - - expression.addKeyListener(new KeyAdapter() { - - @Override - public void keyPressed(KeyEvent e) { - if(e.keyCode == SWT.ESC) { - expression.setText(equation); - } - } - - }); - + expression.addKeyListener(new ExpressionFieldKeyListener(equation)); } @Override diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ConstantExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ConstantExpressionViewFactor.java index 4d0a1726..a3c24fec 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ConstantExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ConstantExpressionViewFactor.java @@ -7,8 +7,6 @@ import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.events.FocusAdapter; import org.eclipse.swt.events.FocusEvent; -import org.eclipse.swt.events.KeyAdapter; -import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; @@ -56,16 +54,7 @@ public class ConstantExpressionViewFactor implements IExpressionViewFactor { } }); - expression.addKeyListener(new KeyAdapter() { - - @Override - public void keyPressed(KeyEvent e) { - if(e.keyCode == SWT.ESC) { - expression.setText(equation); - } - } - - }); + expression.addKeyListener(new ExpressionFieldKeyListener(equation)); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java new file mode 100644 index 00000000..a199f75b --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java @@ -0,0 +1,45 @@ +package org.simantics.sysdyn.ui.equation.expressions; + +import java.io.StringReader; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.events.KeyListener; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.widgets.Text; +import org.simantics.sysdyn.expressionParser.ExpressionParser; +import org.simantics.sysdyn.expressionParser.ParseException; + +public class ExpressionFieldKeyListener implements KeyListener { + + String originalText; + + public ExpressionFieldKeyListener(String originalText) { + this.originalText = originalText; + } + @Override + public void keyPressed(KeyEvent e) { + if(e.keyCode == SWT.ESC && e.widget instanceof Text) { + ((Text)e.widget).setText(originalText); + ((Text)e.widget).setSelection(originalText.length()); + } + } + + @Override + public void keyReleased(KeyEvent e) { + if(e.widget instanceof Text) { + Text text = (Text)e.widget; + + ExpressionParser parser = new ExpressionParser( + new StringReader(text.getText()) + ); + try { + parser.expr(); + text.setBackground(new Color(text.getDisplay(), 255, 255, 255)); + } catch (ParseException e1) { + text.setBackground(new Color(text.getDisplay(), 255, 230, 230)); + } + } + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ParameterExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ParameterExpressionViewFactor.java index cb60a230..aee64437 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ParameterExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ParameterExpressionViewFactor.java @@ -7,8 +7,6 @@ import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.events.FocusAdapter; import org.eclipse.swt.events.FocusEvent; -import org.eclipse.swt.events.KeyAdapter; -import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; @@ -56,16 +54,7 @@ public class ParameterExpressionViewFactor implements IExpressionViewFactor { } }); - expression.addKeyListener(new KeyAdapter() { - - @Override - public void keyPressed(KeyEvent e) { - if(e.keyCode == SWT.ESC) { - expression.setText(equation); - } - } - - }); + expression.addKeyListener(new ExpressionFieldKeyListener(equation)); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionViewFactor.java index 7a920ace..3b2e036f 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionViewFactor.java @@ -8,8 +8,6 @@ import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.events.FocusAdapter; import org.eclipse.swt.events.FocusEvent; -import org.eclipse.swt.events.KeyAdapter; -import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; @@ -71,16 +69,7 @@ public class StockExpressionViewFactor implements IExpressionViewFactor { } }); - expression.addKeyListener(new KeyAdapter() { - - @Override - public void keyPressed(KeyEvent e) { - if(e.keyCode == SWT.ESC) { - expression.setText(initialEquation); - } - } - - }); + expression.addKeyListener(new ExpressionFieldKeyListener(initialEquation)); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java index 6308759b..e0c6212a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java @@ -7,8 +7,6 @@ import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.events.FocusAdapter; import org.eclipse.swt.events.FocusEvent; -import org.eclipse.swt.events.KeyAdapter; -import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; @@ -63,16 +61,7 @@ public class WithLookupExpressionViewFactor implements IExpressionViewFactor { } }); - expression.addKeyListener(new KeyAdapter() { - - @Override - public void keyPressed(KeyEvent e) { - if(e.keyCode == SWT.ESC) { - expression.setText(equation); - } - } - - }); + expression.addKeyListener(new ExpressionFieldKeyListener(equation)); lookupLabel = new Label(parent, SWT.NONE); lookupLabel.setFont(FONT); @@ -94,16 +83,7 @@ public class WithLookupExpressionViewFactor implements IExpressionViewFactor { } }); - lookup.addKeyListener(new KeyAdapter() { - - @Override - public void keyPressed(KeyEvent e) { - if(e.keyCode == SWT.ESC) { - lookup.setText(lookupTable); - } - } - - }); + lookup.addKeyListener(new ExpressionFieldKeyListener(lookupTable)); } diff --git a/org.simantics.sysdyn/META-INF/MANIFEST.MF b/org.simantics.sysdyn/META-INF/MANIFEST.MF index 6e6d17f2..e7dfc071 100644 --- a/org.simantics.sysdyn/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn/META-INF/MANIFEST.MF @@ -7,6 +7,7 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Require-Bundle: org.simantics.objmap;bundle-version="0.1.0", org.simantics.db;bundle-version="0.6.2" Export-Package: org.simantics.sysdyn, + org.simantics.sysdyn.expressionParser, org.simantics.sysdyn.modelica, org.simantics.sysdyn.representation, org.simantics.sysdyn.representation.visitors diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj index f73d5e17..a4495209 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj @@ -32,9 +32,9 @@ TOKEN: | "=" | ":=" | "false" | "true" | "end" | "annotation" | "each" | "fine" | "redeclare" | "replaceable" +| | { matchedToken.image = matchedToken.image.substring(1,matchedToken.image.length()-1); } -| | | "." ()? (["e","E"] )? @@ -50,6 +50,50 @@ TOKEN: // [ add_op ] -> ( add_op() )? // { add_op term } -> ( add_op() term() )* +void expr() : { +} { + simple_expression() + | "if" expression() "then" expression() ( "elseif" expression() "then" expression() )* + "else" expression() +} + +void expression() : { +} { + simple_expression() + | "if" expression() "then" expression() ( "elseif" expression() "then" expression() )* + "else" expression() +} + +void simple_expression() : { +} { + logical_expression() ( ":" logical_expression() ( ":" logical_expression() )? )? +} + +void logical_expression() : { +} { + logical_term() ( "or" logical_term() )* +} + +void logical_term() : { +} { + logical_factor() ( "and" logical_factor() )* +} + +void logical_factor() : { +} { + ( "not" )? relation() +} + +void relation() : { +} { + arithmetic_expression() ( rel_op() arithmetic_expression() )? +} + +void rel_op() : { +} { + "<" | "<=" | ">" | ">=" | "==" | "<>" +} + void arithmetic_expression() : { } { (add_op())? term() (add_op() term())* @@ -62,8 +106,12 @@ void add_op() : { void term() : { } { - // TODO primary -> factor - primary() ( mul_op() primary() )* + factor() ( mul_op() factor() )* +} + +void factor() : { +} { + primary() ( "^" | ".^" primary() )? } void mul_op() : { @@ -73,15 +121,50 @@ void mul_op() : { void primary() : { } { - + | | | "false" | "true" - // | name() function_call_args() - // | component_reference() + | LOOKAHEAD(2) name() function_call_args() + | component_reference() // | "(" output_expression_list() ")" // | "[" expression_list() { ";" expression_list() } "]" - // | "{" function_arguments() "}" + //| "{" function_arguments() "}" | "end" +} + +void name() : { +} { + ( "." name() )? +} + +void component_reference() : { +} { + //IDENT [ array_subscripts ] [ "." component_reference ] + ( "." component_reference() )? +} + + +void function_call_args() : { +} { + "(" ( function_arguments() )? ")" +} + +void function_arguments() : { +} { + //expression [ "," function_arguments | for for_indices ] + //| named_arguments + LOOKAHEAD(2) expression() ( "," function_arguments())? + | named_arguments() +} + +void named_arguments() : { +} { + named_argument() ( "," named_arguments() )? +} + +void named_argument() : { +} { + "=" expression() } \ No newline at end of file diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/TestExpressionParser.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/TestExpressionParser.java index d1813d44..0eefa89b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/TestExpressionParser.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/TestExpressionParser.java @@ -9,7 +9,7 @@ public class TestExpressionParser { new StringReader(string) ); try { - parser.arithmetic_expression(); + parser.expr(); } catch (ParseException e) { System.out.println("While parsing " + string + ":"); // TODO Auto-generated catch block @@ -18,8 +18,8 @@ public class TestExpressionParser { } public static void main(String[] args) { - parse("1 + 2"); - parse("1 + 2 + "); - parse("1 * 2"); + parse("1 + m2ma +"); + parse("ter2e + moro"); + parse("moro * sqr(4.0) + min(3, 2)"); } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java index ae090efc..cb334859 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java @@ -1,5 +1,8 @@ package org.simantics.sysdyn.representation.expressions; -public class DelayExpression { +import org.simantics.objmap.annotations.GraphType; + +@GraphType("http://www.simantics.org/Sysdyn#Delayxpression") +public class DelayExpression extends Expression { } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/LookupExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/LookupExpression.java index 65f19449..ccb925d8 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/LookupExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/LookupExpression.java @@ -1,5 +1,8 @@ package org.simantics.sysdyn.representation.expressions; -public class LookupExpression { +import org.simantics.objmap.annotations.GraphType; + +@GraphType("http://www.simantics.org/Sysdyn#LookupExpression") +public class LookupExpression extends Expression { } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java index e337fa66..0899b122 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java @@ -1,5 +1,9 @@ package org.simantics.sysdyn.representation.expressions; -public class WithLookupExpression { +import org.simantics.objmap.annotations.GraphType; + + +@GraphType("http://www.simantics.org/Sysdyn#WithLookupExpression") +public class WithLookupExpression extends Expression { } -- 2.47.1