From: jkauttio Date: Tue, 29 Sep 2015 13:53:30 +0000 (+0000) Subject: Handle if-then-else in xmile import, change default suffix for xmile files from ... X-Git-Tag: v1.29.0~55 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=5250527fd70e5ce43be135061e70b180900e6528;p=simantics%2Fsysdyn.git Handle if-then-else in xmile import, change default suffix for xmile files from .xml to .xmile refs #6057 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@31736 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/ExportHandler.java b/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/ExportHandler.java index 05aefed5..c8473fce 100644 --- a/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/ExportHandler.java +++ b/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/ExportHandler.java @@ -25,7 +25,7 @@ public class ExportHandler extends AbstractHandler { public Object execute(ExecutionEvent event) throws ExecutionException { // prompt the user for a file FileDialog dialog = new FileDialog(HandlerUtil.getActiveShell(event), SWT.SAVE); - dialog.setFilterExtensions(new String[] {"*.xml","*.*"}); + dialog.setFilterExtensions(new String[] {"*.xmile","*.*"}); dialog.setOverwrite(true); String fileName = dialog.open(); diff --git a/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/ImportHandler.java b/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/ImportHandler.java index 0703e3ca..ea4f0eaf 100644 --- a/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/ImportHandler.java +++ b/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/ImportHandler.java @@ -21,7 +21,7 @@ public class ImportHandler extends AbstractHandler { public Object execute(ExecutionEvent event) throws ExecutionException { // prompt the user for a file FileDialog dialog = new FileDialog(HandlerUtil.getActiveShell(event), SWT.OPEN); - dialog.setFilterExtensions(new String[] {"*.xml","*.*"}); + dialog.setFilterExtensions(new String[] {"*.xmile","*.*"}); String fileName = dialog.open(); if (fileName == null) { diff --git a/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/expressionParser/XmileExpressionParser.java b/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/expressionParser/XmileExpressionParser.java index 5539b522..2f14590b 100644 --- a/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/expressionParser/XmileExpressionParser.java +++ b/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/expressionParser/XmileExpressionParser.java @@ -10,8 +10,6 @@ import java.util.Map; import org.simantics.sysdyn.xmile.XmileUtil; public class XmileExpressionParser implements XmileExpressionParserConstants { - private static final boolean DEBUG = true; - public static String parse(String expression, Map subscripts) { XmileExpressionParser parser = new XmileExpressionParser(new StringReader(expression)); @@ -25,12 +23,46 @@ public class XmileExpressionParser implements XmileExpressionParserConstants { } final public String Expression(Map subscripts) throws ParseException {String e; - e = BinExpr(subscripts); + e = CondExpr(subscripts); jj_consume_token(0); {if ("" != null) return e;} throw new Error("Missing return statement in function"); } + final public String CondExpr(Map subscripts) throws ParseException {String condExpr = null; + String thenExpr = null; + String elseExpr = null; + String expr; + switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) { + case IF:{ + jj_consume_token(IF); + condExpr = BinExpr(subscripts); + jj_consume_token(THEN); + thenExpr = BinExpr(subscripts); + jj_consume_token(ELSE); + elseExpr = BinExpr(subscripts); +{if ("" != null) return "if "+condExpr+" then "+thenExpr+" else "+elseExpr;} + break; + } + case LNOT: + case PLUS: + case MINUS: + case LPAREN: + case NUMBER: + case IDENT: + case QIDENT:{ + expr = BinExpr(subscripts); +{if ("" != null) return expr;} + break; + } + default: + jj_la1[0] = jj_gen; + jj_consume_token(-1); + throw new ParseException(); + } + throw new Error("Missing return statement in function"); + } + final public String BinOp() throws ParseException { switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) { case LOR:{ @@ -94,7 +126,7 @@ public class XmileExpressionParser implements XmileExpressionParserConstants { break; } default: - jj_la1[0] = jj_gen; + jj_la1[1] = jj_gen; jj_consume_token(-1); throw new ParseException(); } @@ -123,7 +155,7 @@ public class XmileExpressionParser implements XmileExpressionParserConstants { break; } default: - jj_la1[1] = jj_gen; + jj_la1[2] = jj_gen; ; } {if ("" != null) return o != null ? l + o + r : l;} @@ -148,7 +180,7 @@ public class XmileExpressionParser implements XmileExpressionParserConstants { break; } default: - jj_la1[2] = jj_gen; + jj_la1[3] = jj_gen; jj_consume_token(-1); throw new ParseException(); } @@ -165,7 +197,7 @@ public class XmileExpressionParser implements XmileExpressionParserConstants { break; } default: - jj_la1[3] = jj_gen; + jj_la1[4] = jj_gen; ; } e = Atom(subscripts); @@ -182,7 +214,7 @@ public class XmileExpressionParser implements XmileExpressionParserConstants { break; } default: - jj_la1[4] = jj_gen; + jj_la1[5] = jj_gen; if (jj_2_1(2)) { s = Function(subscripts); {if ("" != null) return s;} @@ -196,13 +228,13 @@ public class XmileExpressionParser implements XmileExpressionParserConstants { } case LPAREN:{ jj_consume_token(LPAREN); - s = BinExpr(subscripts); + s = CondExpr(subscripts); jj_consume_token(RPAREN); {if ("" != null) return "(" + s + ")";} break; } default: - jj_la1[5] = jj_gen; + jj_la1[6] = jj_gen; jj_consume_token(-1); throw new ParseException(); } @@ -234,7 +266,7 @@ params.add(p); break; } default: - jj_la1[6] = jj_gen; + jj_la1[7] = jj_gen; break label_1; } jj_consume_token(COMMA); @@ -244,7 +276,7 @@ params.add(p); break; } default: - jj_la1[7] = jj_gen; + jj_la1[8] = jj_gen; ; } jj_consume_token(RPAREN); @@ -287,7 +319,7 @@ String name = XmileUtil.getSysdynFunction(t.image); break; } default: - jj_la1[8] = jj_gen; + jj_la1[9] = jj_gen; jj_consume_token(-1); throw new ParseException(); } @@ -326,13 +358,13 @@ String name = XmileUtil.getSysdynFunction(t.image); private Token jj_scanpos, jj_lastpos; private int jj_la; private int jj_gen; - final private int[] jj_la1 = new int[9]; + final private int[] jj_la1 = new int[10]; static private int[] jj_la1_0; static { jj_la1_init_0(); } private static void jj_la1_init_0() { - jj_la1_0 = new int[] {0x7fec,0x7fec,0xc10,0xc10,0x80000,0x310000,0x40000,0x390c10,0x300000,}; + jj_la1_0 = new int[] {0x1c90c10,0x7fec,0x7fec,0xc10,0xc10,0x400000,0x1810000,0x40000,0x1c10c10,0x1800000,}; } final private JJCalls[] jj_2_rtns = new JJCalls[1]; private boolean jj_rescan = false; @@ -349,7 +381,7 @@ String name = XmileUtil.getSysdynFunction(t.image); token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 9; i++) jj_la1[i] = -1; + for (int i = 0; i < 10; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } @@ -364,7 +396,7 @@ String name = XmileUtil.getSysdynFunction(t.image); token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 9; i++) jj_la1[i] = -1; + for (int i = 0; i < 10; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } @@ -375,7 +407,7 @@ String name = XmileUtil.getSysdynFunction(t.image); token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 9; i++) jj_la1[i] = -1; + for (int i = 0; i < 10; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } @@ -386,7 +418,7 @@ String name = XmileUtil.getSysdynFunction(t.image); token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 9; i++) jj_la1[i] = -1; + for (int i = 0; i < 10; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } @@ -396,7 +428,7 @@ String name = XmileUtil.getSysdynFunction(t.image); token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 9; i++) jj_la1[i] = -1; + for (int i = 0; i < 10; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } @@ -406,7 +438,7 @@ String name = XmileUtil.getSysdynFunction(t.image); token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 9; i++) jj_la1[i] = -1; + for (int i = 0; i < 10; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } @@ -519,12 +551,12 @@ String name = XmileUtil.getSysdynFunction(t.image); /** Generate ParseException. */ public ParseException generateParseException() { jj_expentries.clear(); - boolean[] la1tokens = new boolean[22]; + boolean[] la1tokens = new boolean[25]; if (jj_kind >= 0) { la1tokens[jj_kind] = true; jj_kind = -1; } - for (int i = 0; i < 9; i++) { + for (int i = 0; i < 10; i++) { if (jj_la1[i] == jj_gen) { for (int j = 0; j < 32; j++) { if ((jj_la1_0[i] & (1< subscripts) { XmileExpressionParser parser = new XmileExpressionParser(new StringReader(expression)); @@ -50,6 +48,9 @@ TOKEN : | | | +| +| +| } TOKEN : @@ -64,7 +65,19 @@ String Expression(Map subscripts) : String e; } { - e=BinExpr(subscripts) { return e; } + e=CondExpr(subscripts) { return e; } +} + +String CondExpr(Map subscripts) : +{ + String condExpr = null; + String thenExpr = null; + String elseExpr = null; + String expr; +} +{ + condExpr=BinExpr(subscripts) thenExpr=BinExpr(subscripts) elseExpr=BinExpr(subscripts) { return "if "+condExpr+" then "+thenExpr+" else "+elseExpr; } +| expr = BinExpr(subscripts) { return expr; } } String BinOp() : @@ -120,7 +133,7 @@ String Atom(Map subscripts) : t= { return t.image; } | LOOKAHEAD(2) s=Function(subscripts) { return s; } | s=Variable(subscripts) { return s; } -| s=BinExpr(subscripts) { return "(" + s + ")"; } +| s=CondExpr(subscripts) { return "(" + s + ")"; } } String Function(Map subscripts) : diff --git a/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/expressionParser/XmileExpressionParserConstants.java b/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/expressionParser/XmileExpressionParserConstants.java index 30c26e33..9a692a1b 100644 --- a/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/expressionParser/XmileExpressionParserConstants.java +++ b/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/expressionParser/XmileExpressionParserConstants.java @@ -45,11 +45,17 @@ public interface XmileExpressionParserConstants { /** RegularExpression Id. */ int COMMA = 18; /** RegularExpression Id. */ - int NUMBER = 19; + int IF = 19; /** RegularExpression Id. */ - int IDENT = 20; + int THEN = 20; /** RegularExpression Id. */ - int QIDENT = 21; + int ELSE = 21; + /** RegularExpression Id. */ + int NUMBER = 22; + /** RegularExpression Id. */ + int IDENT = 23; + /** RegularExpression Id. */ + int QIDENT = 24; /** Lexical state. */ int DEFAULT = 0; @@ -75,6 +81,9 @@ public interface XmileExpressionParserConstants { "\"(\"", "\")\"", "\",\"", + "\"IF\"", + "\"THEN\"", + "\"ELSE\"", "", "", "", diff --git a/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/expressionParser/XmileExpressionParserTokenManager.java b/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/expressionParser/XmileExpressionParserTokenManager.java index 35ef8a2f..ef1a6c57 100644 --- a/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/expressionParser/XmileExpressionParserTokenManager.java +++ b/org.simantics.sysdyn.xmile/src/org/simantics/sysdyn/xmile/expressionParser/XmileExpressionParserTokenManager.java @@ -18,22 +18,32 @@ private final int jjStopStringLiteralDfa_0(int pos, long active0){ switch (pos) { case 0: - if ((active0 & 0xc0L) != 0L) - return 1; - if ((active0 & 0x401cL) != 0L) + if ((active0 & 0x38401cL) != 0L) { - jjmatchedKind = 20; + jjmatchedKind = 23; return 9; } + if ((active0 & 0xc0L) != 0L) + return 1; return -1; case 1: - if ((active0 & 0x4018L) != 0L) + if ((active0 & 0x304018L) != 0L) { - jjmatchedKind = 20; + jjmatchedKind = 23; jjmatchedPos = 1; return 9; } - if ((active0 & 0x4L) != 0L) + if ((active0 & 0x80004L) != 0L) + return 9; + return -1; + case 2: + if ((active0 & 0x300000L) != 0L) + { + jjmatchedKind = 23; + jjmatchedPos = 2; + return 9; + } + if ((active0 & 0x4018L) != 0L) return 9; return -1; default : @@ -74,12 +84,18 @@ private int jjMoveStringLiteralDfa0_0(){ return jjMoveStringLiteralDfa1_0(0x200L); case 65: return jjMoveStringLiteralDfa1_0(0x8L); + case 69: + return jjMoveStringLiteralDfa1_0(0x200000L); + case 73: + return jjMoveStringLiteralDfa1_0(0x80000L); case 77: return jjMoveStringLiteralDfa1_0(0x4000L); case 78: return jjMoveStringLiteralDfa1_0(0x10L); case 79: return jjMoveStringLiteralDfa1_0(0x4L); + case 84: + return jjMoveStringLiteralDfa1_0(0x100000L); case 94: return jjStopAtPos(0, 15); default : @@ -100,6 +116,14 @@ private int jjMoveStringLiteralDfa1_0(long active0){ else if ((active0 & 0x200L) != 0L) return jjStopAtPos(1, 9); break; + case 70: + if ((active0 & 0x80000L) != 0L) + return jjStartNfaWithStates_0(1, 19, 9); + break; + case 72: + return jjMoveStringLiteralDfa2_0(active0, 0x100000L); + case 76: + return jjMoveStringLiteralDfa2_0(active0, 0x200000L); case 78: return jjMoveStringLiteralDfa2_0(active0, 0x8L); case 79: @@ -129,6 +153,10 @@ private int jjMoveStringLiteralDfa2_0(long old0, long active0){ else if ((active0 & 0x4000L) != 0L) return jjStartNfaWithStates_0(2, 14, 9); break; + case 69: + return jjMoveStringLiteralDfa3_0(active0, 0x100000L); + case 83: + return jjMoveStringLiteralDfa3_0(active0, 0x200000L); case 84: if ((active0 & 0x10L) != 0L) return jjStartNfaWithStates_0(2, 4, 9); @@ -138,6 +166,29 @@ private int jjMoveStringLiteralDfa2_0(long old0, long active0){ } return jjStartNfa_0(1, active0); } +private int jjMoveStringLiteralDfa3_0(long old0, long active0){ + if (((active0 &= old0)) == 0L) + return jjStartNfa_0(1, old0); + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { + jjStopStringLiteralDfa_0(2, active0); + return 3; + } + switch(curChar) + { + case 69: + if ((active0 & 0x200000L) != 0L) + return jjStartNfaWithStates_0(3, 21, 9); + break; + case 78: + if ((active0 & 0x100000L) != 0L) + return jjStartNfaWithStates_0(3, 20, 9); + break; + default : + break; + } + return jjStartNfa_0(2, active0); +} private int jjStartNfaWithStates_0(int pos, int kind, int state) { jjmatchedKind = kind; @@ -170,8 +221,8 @@ private int jjMoveNfa_0(int startState, int curPos) case 0: if ((0x3ff000000000000L & l) != 0L) { - if (kind > 19) - kind = 19; + if (kind > 22) + kind = 22; { jjCheckNAddStates(0, 4); } } else if (curChar == 34) @@ -201,8 +252,8 @@ private int jjMoveNfa_0(int startState, int curPos) case 4: if ((0x3ff000000000000L & l) == 0L) break; - if (kind > 19) - kind = 19; + if (kind > 22) + kind = 22; { jjCheckNAddTwoStates(4, 5); } break; case 6: @@ -212,15 +263,15 @@ private int jjMoveNfa_0(int startState, int curPos) case 7: if ((0x3ff000000000000L & l) == 0L) break; - if (kind > 19) - kind = 19; + if (kind > 22) + kind = 22; { jjCheckNAdd(7); } break; case 9: if ((0x3ff001000000000L & l) == 0L) break; - if (kind > 20) - kind = 20; + if (kind > 23) + kind = 23; jjstateSet[jjnewStateCnt++] = 9; break; case 10: @@ -238,35 +289,35 @@ private int jjMoveNfa_0(int startState, int curPos) { jjCheckNAddStates(8, 10); } break; case 15: - if (curChar == 34 && kind > 21) - kind = 21; + if (curChar == 34 && kind > 24) + kind = 24; break; case 16: if ((0x3ff000000000000L & l) == 0L) break; - if (kind > 19) - kind = 19; + if (kind > 22) + kind = 22; { jjCheckNAddStates(0, 4); } break; case 17: if ((0x3ff000000000000L & l) == 0L) break; - if (kind > 19) - kind = 19; + if (kind > 22) + kind = 22; { jjCheckNAddStates(11, 13); } break; case 18: if (curChar != 46) break; - if (kind > 19) - kind = 19; + if (kind > 22) + kind = 22; { jjCheckNAddTwoStates(19, 5); } break; case 19: if ((0x3ff000000000000L & l) == 0L) break; - if (kind > 19) - kind = 19; + if (kind > 22) + kind = 22; { jjCheckNAddTwoStates(19, 5); } break; case 20: @@ -287,8 +338,8 @@ private int jjMoveNfa_0(int startState, int curPos) case 0: if ((0x7fffffe07fffffeL & l) == 0L) break; - if (kind > 20) - kind = 20; + if (kind > 23) + kind = 23; { jjCheckNAdd(9); } break; case 5: @@ -298,8 +349,8 @@ private int jjMoveNfa_0(int startState, int curPos) case 9: if ((0x7fffffe87fffffeL & l) == 0L) break; - if (kind > 20) - kind = 20; + if (kind > 23) + kind = 23; { jjCheckNAdd(9); } break; case 11: @@ -363,7 +414,7 @@ static final int[] jjnextStates = { public static final String[] jjstrLiteralImages = { "", null, "\117\122", "\101\116\104", "\116\117\124", null, "\74", "\74\75", "\76", "\76\75", "\53", "\55", "\52", "\57", "\115\117\104", "\136", "\50", "\51", -"\54", null, null, null, }; +"\54", "\111\106", "\124\110\105\116", "\105\114\123\105", null, null, null, }; protected Token jjFillToken() { final Token t; @@ -541,7 +592,7 @@ public static final String[] lexStateNames = { "DEFAULT", }; static final long[] jjtoToken = { - 0x3ffffdL, + 0x1fffffdL, }; static final long[] jjtoSkip = { 0x2L,