From a5bf0232c8fdd0f83e9c0106cf26ee45e7357921 Mon Sep 17 00:00:00 2001 From: lempinen Date: Thu, 24 Oct 2013 07:04:13 +0000 Subject: [PATCH] Unit validation for whitespaced variable names fixes #4424 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@28110 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/unitParser/UnitParser.java | 62 +++++++++++-------- .../simantics/sysdyn/unitParser/UnitParser.jj | 2 +- .../sysdyn/unitParser/UnitParser.jjt | 2 +- .../unitParser/nodes/ComponentIdentity.java | 15 +++++ .../sysdyn/unitParser/nodes/IfThenElse.java | 4 +- .../sysdyn/unitParser/nodes/Term.java | 23 +++++++ 6 files changed, 77 insertions(+), 31 deletions(-) diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.java index eff84e43..7b0111ab 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.java @@ -1110,6 +1110,14 @@ public class UnitParser/*@bgen(jjtree)*/implements UnitParserTreeConstants, Unit jjtn000.jjtSetFirstToken(getToken(1)); try { jj_consume_token(IDENT); + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case IDENT: + jj_consume_token(IDENT); + break; + default: + jj_la1[27] = jj_gen; + ; + } } finally { if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); @@ -1146,7 +1154,7 @@ public class UnitParser/*@bgen(jjtree)*/implements UnitParserTreeConstants, Unit function_arguments(); break; default: - jj_la1[27] = jj_gen; + jj_la1[28] = jj_gen; ; } parenthesis_close(); @@ -1187,7 +1195,7 @@ public class UnitParser/*@bgen(jjtree)*/implements UnitParserTreeConstants, Unit function_arguments(); break; default: - jj_la1[28] = jj_gen; + jj_la1[29] = jj_gen; ; } } else { @@ -1222,18 +1230,18 @@ public class UnitParser/*@bgen(jjtree)*/implements UnitParserTreeConstants, Unit for_indices(); break; default: - jj_la1[29] = jj_gen; + jj_la1[30] = jj_gen; jj_consume_token(-1); throw new ParseException(); } break; default: - jj_la1[30] = jj_gen; + jj_la1[31] = jj_gen; ; } break; default: - jj_la1[31] = jj_gen; + jj_la1[32] = jj_gen; jj_consume_token(-1); throw new ParseException(); } @@ -1275,7 +1283,7 @@ public class UnitParser/*@bgen(jjtree)*/implements UnitParserTreeConstants, Unit ; break; default: - jj_la1[32] = jj_gen; + jj_la1[33] = jj_gen; break label_7; } jj_consume_token(69); @@ -1317,7 +1325,7 @@ public class UnitParser/*@bgen(jjtree)*/implements UnitParserTreeConstants, Unit expression(); break; default: - jj_la1[33] = jj_gen; + jj_la1[34] = jj_gen; ; } } catch (Throwable jjte000) { @@ -1407,7 +1415,7 @@ void named_arguments() : { expression(); break; default: - jj_la1[34] = jj_gen; + jj_la1[35] = jj_gen; ; } label_8: @@ -1417,7 +1425,7 @@ void named_arguments() : { ; break; default: - jj_la1[35] = jj_gen; + jj_la1[36] = jj_gen; break label_8; } jj_consume_token(69); @@ -1441,7 +1449,7 @@ void named_arguments() : { expression(); break; default: - jj_la1[36] = jj_gen; + jj_la1[37] = jj_gen; ; } } @@ -1482,7 +1490,7 @@ void named_arguments() : { ; break; default: - jj_la1[37] = jj_gen; + jj_la1[38] = jj_gen; break label_9; } jj_consume_token(69); @@ -1526,7 +1534,7 @@ void named_arguments() : { ; break; default: - jj_la1[38] = jj_gen; + jj_la1[39] = jj_gen; break label_10; } jj_consume_token(69); @@ -1567,7 +1575,7 @@ void named_arguments() : { jj_consume_token(67); break; default: - jj_la1[40] = jj_gen; + jj_la1[41] = jj_gen; if (jj_2_3(2147483647)) { function_call(); } else { @@ -1581,12 +1589,12 @@ void named_arguments() : { rangeIndex(); break; default: - jj_la1[39] = jj_gen; + jj_la1[40] = jj_gen; ; } break; default: - jj_la1[41] = jj_gen; + jj_la1[42] = jj_gen; jj_consume_token(-1); throw new ParseException(); } @@ -1629,7 +1637,7 @@ void named_arguments() : { jj_consume_token(IDENT); break; default: - jj_la1[42] = jj_gen; + jj_la1[43] = jj_gen; jj_consume_token(-1); throw new ParseException(); } @@ -1715,7 +1723,7 @@ void named_arguments() : { private Token jj_scanpos, jj_lastpos; private int jj_la; private int jj_gen; - final private int[] jj_la1 = new int[43]; + final private int[] jj_la1 = new int[44]; static private int[] jj_la1_0; static private int[] jj_la1_1; static private int[] jj_la1_2; @@ -1725,13 +1733,13 @@ void named_arguments() : { jj_la1_init_2(); } private static void jj_la1_init_0() { - jj_la1_0 = new int[] {0x1040,0x80001041,0x80001040,0x100000,0x0,0x0,0x400000,0x200,0x1000,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x40,0x0,0x40,0x0,0x0,0x0,0x80001040,0x0,0x200000,0x200000,0x80001040,0x0,0x0,0x80001040,0x0,0x80001040,0x0,0x0,0x0,0x0,0x0,0x0,}; + jj_la1_0 = new int[] {0x1040,0x80001041,0x80001040,0x100000,0x0,0x0,0x400000,0x200,0x1000,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x40,0x0,0x40,0x0,0x0,0x0,0x0,0x80001040,0x0,0x200000,0x200000,0x80001040,0x0,0x0,0x80001040,0x0,0x80001040,0x0,0x0,0x0,0x0,0x0,0x0,}; } private static void jj_la1_init_1() { - jj_la1_1 = new int[] {0x5000000a,0x5000000a,0x5000000a,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x2,0x50000008,0x2,0x0,0x0,0x0,0x5000000a,0x0,0x0,0x0,0x5000000a,0x0,0x200,0x5000000a,0x0,0x5000000a,0x0,0x0,0x0,0x0,0x0,0x0,}; + jj_la1_1 = new int[] {0x5000000a,0x5000000a,0x5000000a,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x2,0x50000008,0x2,0x0,0x0,0x0,0x0,0x5000000a,0x0,0x0,0x0,0x5000000a,0x0,0x200,0x5000000a,0x0,0x5000000a,0x0,0x0,0x0,0x0,0x0,0x0,}; } private static void jj_la1_init_2() { - jj_la1_2 = new int[] {0xf00f001,0xf00f001,0xf00f001,0x0,0x8,0x8,0x0,0x0,0x0,0xfc0,0xfc0,0xf000,0xf000,0xf000,0xf0000,0xf0000,0x300000,0x300000,0x50000,0xa0000,0x10,0xe000000,0x1000001,0xe000000,0x4,0x1,0x4,0xf00f001,0x20,0x20,0x20,0xf00f001,0x20,0x0,0xf00f001,0x20,0xf00f001,0x20,0x20,0x8,0x8,0x5000000,0x5000000,}; + jj_la1_2 = new int[] {0xf00f001,0xf00f001,0xf00f001,0x0,0x8,0x8,0x0,0x0,0x0,0xfc0,0xfc0,0xf000,0xf000,0xf000,0xf0000,0xf0000,0x300000,0x300000,0x50000,0xa0000,0x10,0xe000000,0x1000001,0xe000000,0x4,0x1,0x4,0x1000000,0xf00f001,0x20,0x20,0x20,0xf00f001,0x20,0x0,0xf00f001,0x20,0xf00f001,0x20,0x20,0x8,0x8,0x5000000,0x5000000,}; } final private JJCalls[] jj_2_rtns = new JJCalls[3]; private boolean jj_rescan = false; @@ -1748,7 +1756,7 @@ void named_arguments() : { token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 43; i++) jj_la1[i] = -1; + for (int i = 0; i < 44; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } @@ -1764,7 +1772,7 @@ void named_arguments() : { jj_ntk = -1; jjtree.reset(); jj_gen = 0; - for (int i = 0; i < 43; i++) jj_la1[i] = -1; + for (int i = 0; i < 44; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } @@ -1775,7 +1783,7 @@ void named_arguments() : { token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 43; i++) jj_la1[i] = -1; + for (int i = 0; i < 44; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } @@ -1787,7 +1795,7 @@ void named_arguments() : { jj_ntk = -1; jjtree.reset(); jj_gen = 0; - for (int i = 0; i < 43; i++) jj_la1[i] = -1; + for (int i = 0; i < 44; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } @@ -1797,7 +1805,7 @@ void named_arguments() : { token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 43; i++) jj_la1[i] = -1; + for (int i = 0; i < 44; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } @@ -1808,7 +1816,7 @@ void named_arguments() : { jj_ntk = -1; jjtree.reset(); jj_gen = 0; - for (int i = 0; i < 43; i++) jj_la1[i] = -1; + for (int i = 0; i < 44; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } @@ -1925,7 +1933,7 @@ void named_arguments() : { la1tokens[jj_kind] = true; jj_kind = -1; } - for (int i = 0; i < 43; i++) { + for (int i = 0; i < 44; i++) { if (jj_la1[i] == jj_gen) { for (int j = 0; j < 32; j++) { if ((jj_la1_0[i] & (1</*@bgen(jjtree)*/ +/*@egen*/ ()?/*@bgen(jjtree)*/ } finally { if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt index c6e3c4fa..c6428eec 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt @@ -210,7 +210,7 @@ void component_reference() : { } void component_identity() : { -} { +} { ()? } void function_call_args() : { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ComponentIdentity.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ComponentIdentity.java index c32b47f6..8d90375e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ComponentIdentity.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ComponentIdentity.java @@ -11,6 +11,7 @@ *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; +import org.simantics.sysdyn.unitParser.Token; import org.simantics.sysdyn.unitParser.UnitCheckingNode; /** @@ -23,5 +24,19 @@ public class ComponentIdentity extends UnitCheckingNode { public ComponentIdentity(int id) { super(id); } + + @Override + public String printNode() { + StringBuilder sb = new StringBuilder(); + Token token = jjtGetFirstToken(); + sb.append(token.image); + + while(token != null && !token.equals(jjtGetLastToken())) { + sb.append(" "); + token = token.next; + sb.append(token.image); + } + return sb.toString(); + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/IfThenElse.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/IfThenElse.java index f5942e4e..3529a15e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/IfThenElse.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/IfThenElse.java @@ -46,8 +46,8 @@ public class IfThenElse extends UnitCheckingNode { } else { if(!base.equals(candidateUnits)) { base.equals(candidateUnits); - throw new UnitCheckingException("Conditional results do not have same units: \n" + - printNode() + "\n" + + throw new UnitCheckingException("Conditional results do not have same units: " + + printNode() + " -> " + baseNode.printNode() + " [" + base.getFullUnit() + "] <-> " + candidateNode.printNode() + " [" + candidateUnits.getFullUnit() + "]" ); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Term.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Term.java index 33a37fd3..d8b454e6 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Term.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Term.java @@ -13,6 +13,7 @@ package org.simantics.sysdyn.unitParser.nodes; import java.util.HashMap; +import org.simantics.sysdyn.unitParser.Token; import org.simantics.sysdyn.unitParser.UnitCheckingException; import org.simantics.sysdyn.unitParser.UnitCheckingNode; import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; @@ -76,5 +77,27 @@ public class Term extends UnitCheckingNode { // System.out.println("Term: " + printNode() + " [" + result.getFullUnit() + " : " + result.getUnitType() + "]"); return result; } + + @Override + public String printNode() { + StringBuilder sb = new StringBuilder(); + Token token = jjtGetFirstToken(); + sb.append(token.image); + + Token prevToken = null; + while(token != null && !token.equals(jjtGetLastToken())) { + prevToken = token; + token = token.next; + if(!token.image.equals("[") + && !token.image.equals("]") + && !prevToken.image.equals("[") + && !prevToken.image.equals("]") + && !token.image.equals(".") + && !prevToken.image.equals(".")) + sb.append(" "); + sb.append(token.image); + } + return sb.toString(); + } } -- 2.47.1