]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Expression parser. Expression field listener checks the correctness of the expression.
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 12 Jan 2010 15:00:12 +0000 (15:00 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 12 Jan 2010 15:00:12 +0000 (15:00 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13486 ac1ea38d-2e2b-0410-8846-a27921b304fc

12 files changed:
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/AuxiliaryExpressionViewFactor.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ConstantExpressionViewFactor.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ParameterExpressionViewFactor.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionViewFactor.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java
org.simantics.sysdyn/META-INF/MANIFEST.MF
org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj
org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/TestExpressionParser.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/LookupExpression.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java

index e756dbf6072e4be7bb67fd78651e89cdc368979a..4d523cd1b43fc05aa630f71232d26d160fef5933 100644 (file)
@@ -7,8 +7,6 @@ import org.eclipse.jface.layout.GridLayoutFactory;
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.events.FocusAdapter;\r
 import org.eclipse.swt.events.FocusEvent;\r
-import org.eclipse.swt.events.KeyAdapter;\r
-import org.eclipse.swt.events.KeyEvent;\r
 import org.eclipse.swt.graphics.Point;\r
 import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.swt.widgets.Label;\r
@@ -54,19 +52,8 @@ public class AuxiliaryExpressionViewFactor implements IExpressionViewFactor {
                 lastSelection = expression.getSelection();      \r
             }\r
         });\r
-\r
-        expression.addKeyListener(new KeyAdapter() {          \r
-\r
-            @Override\r
-            public void keyPressed(KeyEvent e) {\r
-                if(e.keyCode == SWT.ESC) {\r
-                    expression.setText(equation);\r
-                }\r
-            }\r
-\r
-        });\r
-\r
         \r
+        expression.addKeyListener(new ExpressionFieldKeyListener(equation));\r
     }\r
 \r
     @Override\r
index 4d0a1726e542f06afa75cf4015d9429d4df409cb..a3c24fec7540b4589c377afe72af5a2d849224f4 100644 (file)
@@ -7,8 +7,6 @@ import org.eclipse.jface.layout.GridLayoutFactory;
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.events.FocusAdapter;\r
 import org.eclipse.swt.events.FocusEvent;\r
-import org.eclipse.swt.events.KeyAdapter;\r
-import org.eclipse.swt.events.KeyEvent;\r
 import org.eclipse.swt.graphics.Point;\r
 import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.swt.widgets.Label;\r
@@ -56,16 +54,7 @@ public class ConstantExpressionViewFactor implements IExpressionViewFactor {
             }\r
         });\r
 \r
-        expression.addKeyListener(new KeyAdapter() {          \r
-\r
-            @Override\r
-            public void keyPressed(KeyEvent e) {\r
-                if(e.keyCode == SWT.ESC) {\r
-                    expression.setText(equation);\r
-                }\r
-            }\r
-\r
-        });\r
+        expression.addKeyListener(new ExpressionFieldKeyListener(equation));\r
 \r
 \r
     }\r
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 (file)
index 0000000..a199f75
--- /dev/null
@@ -0,0 +1,45 @@
+package org.simantics.sysdyn.ui.equation.expressions;\r
+\r
+import java.io.StringReader;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.events.KeyListener;\r
+import org.eclipse.swt.graphics.Color;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
+import org.simantics.sysdyn.expressionParser.ParseException;\r
+\r
+public class ExpressionFieldKeyListener implements KeyListener {\r
+\r
+    String originalText; \r
+    \r
+    public ExpressionFieldKeyListener(String originalText) {\r
+        this.originalText = originalText;        \r
+    }\r
+    @Override\r
+    public void keyPressed(KeyEvent e) {   \r
+        if(e.keyCode == SWT.ESC && e.widget instanceof Text) {\r
+            ((Text)e.widget).setText(originalText);\r
+            ((Text)e.widget).setSelection(originalText.length());\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public void keyReleased(KeyEvent e) {     \r
+        if(e.widget instanceof Text) {\r
+            Text text = (Text)e.widget;\r
+            \r
+            ExpressionParser parser = new ExpressionParser(\r
+                    new StringReader(text.getText())\r
+            );\r
+            try {\r
+                parser.expr();\r
+                text.setBackground(new Color(text.getDisplay(), 255, 255, 255));\r
+            } catch (ParseException e1) {\r
+                text.setBackground(new Color(text.getDisplay(), 255, 230, 230));\r
+            }\r
+        }\r
+    }\r
+\r
+}\r
index cb60a2303599225fa9a9a1277a101d19d50a6bcc..aee644370be6426d67a0554e9705507013dcfdce 100644 (file)
@@ -7,8 +7,6 @@ import org.eclipse.jface.layout.GridLayoutFactory;
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.events.FocusAdapter;\r
 import org.eclipse.swt.events.FocusEvent;\r
-import org.eclipse.swt.events.KeyAdapter;\r
-import org.eclipse.swt.events.KeyEvent;\r
 import org.eclipse.swt.graphics.Point;\r
 import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.swt.widgets.Label;\r
@@ -56,16 +54,7 @@ public class ParameterExpressionViewFactor implements IExpressionViewFactor {
             }\r
         });\r
 \r
-        expression.addKeyListener(new KeyAdapter() {          \r
-\r
-            @Override\r
-            public void keyPressed(KeyEvent e) {\r
-                if(e.keyCode == SWT.ESC) {\r
-                    expression.setText(equation);\r
-                }\r
-            }\r
-\r
-        });\r
+        expression.addKeyListener(new ExpressionFieldKeyListener(equation));\r
 \r
         \r
     }\r
index 7a920ace347b73f75d9c688f87c8ca931c352458..3b2e036fbbe035d7cb6da8d9a26f455a7414cbfa 100644 (file)
@@ -8,8 +8,6 @@ import org.eclipse.jface.layout.GridLayoutFactory;
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.events.FocusAdapter;\r
 import org.eclipse.swt.events.FocusEvent;\r
-import org.eclipse.swt.events.KeyAdapter;\r
-import org.eclipse.swt.events.KeyEvent;\r
 import org.eclipse.swt.graphics.Point;\r
 import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.swt.widgets.Label;\r
@@ -71,16 +69,7 @@ public class StockExpressionViewFactor implements IExpressionViewFactor {
             }\r
         });\r
 \r
-        expression.addKeyListener(new KeyAdapter() {          \r
-\r
-            @Override\r
-            public void keyPressed(KeyEvent e) {\r
-                if(e.keyCode == SWT.ESC) {\r
-                    expression.setText(initialEquation);\r
-                }                \r
-            }\r
-\r
-        });\r
+        expression.addKeyListener(new ExpressionFieldKeyListener(initialEquation));\r
 \r
     }\r
 \r
index 6308759b7120cd012459097da565f4ac2e8c45de..e0c6212a2d1fac7d767af861ea90917f21f29c20 100644 (file)
@@ -7,8 +7,6 @@ import org.eclipse.jface.layout.GridLayoutFactory;
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.events.FocusAdapter;\r
 import org.eclipse.swt.events.FocusEvent;\r
-import org.eclipse.swt.events.KeyAdapter;\r
-import org.eclipse.swt.events.KeyEvent;\r
 import org.eclipse.swt.graphics.Point;\r
 import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.swt.widgets.Label;\r
@@ -63,16 +61,7 @@ public class WithLookupExpressionViewFactor implements IExpressionViewFactor {
             }\r
         });\r
 \r
-        expression.addKeyListener(new KeyAdapter() {          \r
-\r
-            @Override\r
-            public void keyPressed(KeyEvent e) {\r
-                if(e.keyCode == SWT.ESC) {\r
-                    expression.setText(equation);\r
-                }                \r
-            }\r
-\r
-        });\r
+        expression.addKeyListener(new ExpressionFieldKeyListener(equation));\r
 \r
         lookupLabel = new Label(parent, SWT.NONE);\r
         lookupLabel.setFont(FONT);\r
@@ -94,16 +83,7 @@ public class WithLookupExpressionViewFactor implements IExpressionViewFactor {
             }\r
         });\r
 \r
-        lookup.addKeyListener(new KeyAdapter() {          \r
-\r
-            @Override\r
-            public void keyPressed(KeyEvent e) {\r
-                if(e.keyCode == SWT.ESC) {\r
-                    lookup.setText(lookupTable);\r
-                }                \r
-            }\r
-\r
-        });\r
+        lookup.addKeyListener(new ExpressionFieldKeyListener(lookupTable));\r
         \r
     }\r
 \r
index 6e6d17f2e45a90bc324fdfd39cd7d5524d7b12ed..e7dfc0715ad595d3079d0230ebed79c9201ccd5f 100644 (file)
@@ -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
index f73d5e17b946e86f4c75aad2e9d50431e946eece..a4495209a7f2521bc0ff1f488aaf878d77313c14 100644 (file)
@@ -32,9 +32,9 @@ TOKEN:
 | "=" | ":=" \r
 | "false" | "true" | "end" | "annotation"\r
 | "each" | "fine" | "redeclare" | "replaceable"\r
+| <IDENT: ["a"-"z","A"-"Z","_"] (["a"-"z","A"-"Z","_","0"-"9"])* >\r
 | <STRING: "\"" (~["\"", "\\", "\n"] | "\\" ~["\n"])* "\"">\r
     { matchedToken.image = matchedToken.image.substring(1,matchedToken.image.length()-1); }\r
-| <IDENT: ["a"-"z","A"-"Z","_"] (["a"-"z","A"-"Z","_","0"-"9"])* >\r
 | <UNSIGNED_INTEGER: (["0"-"9"])+ >\r
 | <UNSIGNED_NUMBER:  \r
     ( <UNSIGNED_INTEGER> "." (<UNSIGNED_INTEGER>)? (["e","E"] <UNSIGNED_INTEGER>)?\r
@@ -50,6 +50,50 @@ TOKEN:
 // [ add_op ] -> ( add_op() )?\r
 // { add_op term } -> ( add_op() term() )*\r
 \r
+void expr() : {\r
+} {\r
+       simple_expression() <EOF> \r
+       | "if" expression() "then" expression() ( "elseif" expression() "then" expression() )*\r
+       "else" expression() <EOF> \r
+}\r
+\r
+void expression() : {\r
+} {\r
+       simple_expression()\r
+       | "if" expression() "then" expression() ( "elseif" expression() "then" expression() )*\r
+       "else" expression()\r
+}\r
+\r
+void simple_expression() : {\r
+} {\r
+       logical_expression() ( ":" logical_expression() ( ":" logical_expression() )? )?\r
+}      \r
+\r
+void logical_expression() : {\r
+} {\r
+       logical_term() ( "or" logical_term() )*\r
+}      \r
+\r
+void logical_term() : {\r
+} {\r
+       logical_factor() ( "and" logical_factor() )*\r
+}\r
+\r
+void logical_factor() : {\r
+} {\r
+       ( "not" )? relation()\r
+}\r
+\r
+void relation() : {\r
+} {\r
+       arithmetic_expression() ( rel_op() arithmetic_expression() )?\r
+}\r
+\r
+void rel_op() : {\r
+} {\r
+       "<" | "<=" | ">" | ">=" | "==" | "<>"\r
+}\r
+\r
 void arithmetic_expression() : {\r
 } {\r
     (add_op())? term() (add_op() term())*\r
@@ -62,8 +106,12 @@ void add_op() : {
 \r
 void term() : {\r
 } {\r
-    // TODO primary -> factor\r
-    primary() ( mul_op() primary() )*\r
+    factor() ( mul_op() factor() )*\r
+}\r
+\r
+void factor() : {\r
+} {\r
+       primary() ( "^" | ".^" primary() )?\r
 }\r
 \r
 void mul_op() : {\r
@@ -73,15 +121,50 @@ void mul_op() : {
 \r
 void primary() : {\r
 } {\r
-    <UNSIGNED_NUMBER>\r
+       <UNSIGNED_NUMBER>\r
   | <UNSIGNED_INTEGER>\r
   | <STRING>\r
   | "false"\r
   | "true"\r
-  // | name() function_call_args()\r
-  // | component_reference()\r
+  |   LOOKAHEAD(2) name() function_call_args()\r
+  | component_reference()\r
   // | "(" output_expression_list() ")"\r
   // | "[" expression_list() { ";" expression_list() } "]"\r
-  // | "{" function_arguments() "}"\r
+  //| "{" function_arguments() "}"\r
   | "end"\r
+}\r
+\r
+void name() : {\r
+} {\r
+       <IDENT> ( "." name() )?\r
+}\r
+\r
+void component_reference() : {\r
+} {\r
+       //IDENT [ array_subscripts ] [ "." component_reference ]\r
+       <IDENT> ( "." component_reference() )?\r
+}\r
+\r
+\r
+void function_call_args() : {\r
+} {\r
+       "(" ( function_arguments() )? ")"\r
+}\r
+\r
+void function_arguments() : {\r
+} {\r
+       //expression [ "," function_arguments | for for_indices ]\r
+       //| named_arguments\r
+        LOOKAHEAD(2) expression() ( "," function_arguments())?\r
+       | named_arguments()\r
+}      \r
+\r
+void named_arguments() : {\r
+} {\r
+       named_argument() ( "," named_arguments() )?\r
+}\r
+\r
+void named_argument() : {\r
+} {    \r
+       <IDENT> "=" expression()\r
 }
\ No newline at end of file
index d1813d44bab160403ce8112c884b08912392cbdd..0eefa89b3d279438882f5a34d7ed7a337183ea51 100644 (file)
@@ -9,7 +9,7 @@ public class TestExpressionParser {
                 new StringReader(string)\r
         );\r
         try {\r
-            parser.arithmetic_expression();\r
+            parser.expr();\r
         } catch (ParseException e) {\r
             System.out.println("While parsing " + string + ":");\r
             // TODO Auto-generated catch block\r
@@ -18,8 +18,8 @@ public class TestExpressionParser {
     }\r
     \r
     public static void main(String[] args) {\r
-        parse("1 + 2");\r
-        parse("1 + 2 + ");\r
-        parse("1 * 2");\r
+        parse("1 + m2ma +");\r
+        parse("ter2e + moro");\r
+        parse("moro * sqr(4.0) + min(3, 2)");\r
     }\r
 }\r
index ae090efca8d9231b298697ba550818d86619a816..cb3348593fc9b97d73e16598320fa50a52c08bd7 100644 (file)
@@ -1,5 +1,8 @@
 package org.simantics.sysdyn.representation.expressions;\r
 \r
-public class DelayExpression {\r
+import org.simantics.objmap.annotations.GraphType;\r
+\r
+@GraphType("http://www.simantics.org/Sysdyn#Delayxpression")\r
+public class DelayExpression extends Expression {\r
 \r
 }\r
index 65f194499ba50b359bc931eafc5cf34e878d5029..ccb925d8998ea7e1851e8dec3844fc1db1cd171b 100644 (file)
@@ -1,5 +1,8 @@
 package org.simantics.sysdyn.representation.expressions;\r
 \r
-public class LookupExpression {\r
+import org.simantics.objmap.annotations.GraphType;\r
+\r
+@GraphType("http://www.simantics.org/Sysdyn#LookupExpression")\r
+public class LookupExpression extends Expression {\r
 \r
 }\r
index e337fa66623448e42f4b5dd11bd0d0500fb67b18..0899b122ee36078908900f17cca8147679ff2510 100644 (file)
@@ -1,5 +1,9 @@
 package org.simantics.sysdyn.representation.expressions;\r
 \r
-public class WithLookupExpression {\r
+import org.simantics.objmap.annotations.GraphType;\r
+\r
+\r
+@GraphType("http://www.simantics.org/Sysdyn#WithLookupExpression")\r
+public class WithLookupExpression extends Expression {\r
 \r
 }\r