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
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
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
}\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
--- /dev/null
+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
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
}\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
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
}\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
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
}\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
}\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
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
| "=" | ":=" \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
// [ 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
\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
\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
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
}\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
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
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
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