--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.unitParser.nodes;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+\r
+import org.simantics.sysdyn.unitParser.UnitCheckingException;\r
+import org.simantics.sysdyn.unitParser.UnitCheckingNode;\r
+import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType;\r
+\r
+/**\r
+ * See UnitCheckingNodeFactory for mapping\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class ArrayDefinition extends UnitCheckingNode {\r
+\r
+ public ArrayDefinition(int id) {\r
+ super(id);\r
+ }\r
+\r
+\r
+ @Override\r
+ public UnitResult getUnits(HashMap<String, String> units) throws UnitCheckingException {\r
+ UnitResult result = new UnitResult();\r
+ result.setUnitType(UnitType.SCALAR);\r
+\r
+ UnitCheckingNode base = null;\r
+\r
+ if(jjtGetNumChildren() > 0 && jjtGetChild(0) instanceof FunctionArguments) {\r
+ FunctionArguments functionArguments = (FunctionArguments) jjtGetChild(0);\r
+\r
+ for(UnitCheckingNode candidateNode : gatherExpressions(functionArguments, new ArrayList<UnitCheckingNode>())) {\r
+ UnitResult candidateUnits = candidateNode.getUnits(units);\r
+ if(candidateUnits.getUnitType() == UnitType.SCALAR) {\r
+ continue;\r
+ } else if(base == null) {\r
+ base = candidateNode;\r
+ UnitType oldUnitType = result.getUnitType();\r
+ result.appendResult(base.getUnits(units));\r
+\r
+ /*\r
+ * Make sure unit type persist\r
+ * {Var1, Var2, 1} would result in UnitType.SCALAR even though it should be UnitType.NORMAL \r
+ * because the last element has UnitType.SCALAR \r
+ */\r
+ UnitType newUnitType = result.getUnitType();\r
+ if(newUnitType == UnitType.SCALAR || newUnitType == UnitType.ANY || newUnitType == UnitType.DMNL)\r
+ result.setUnitType(oldUnitType);\r
+ continue;\r
+ } else {\r
+ if(!result.equals(candidateUnits)) {\r
+ throw new UnitCheckingException("Array elements do not have same units: " +\r
+ base.printNode() + " [" + result.getCleanFullUnit() + "] =/= " +\r
+ candidateNode.printNode() + " [" + candidateUnits.getCleanFullUnit() + "]"\r
+ );\r
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * Gather all expressions of array definitions.\r
+ * \r
+ * void function_arguments() : {\r
+ * } {\r
+ * LOOKAHEAD(2) named_argument() ( "," function_arguments() )?\r
+ * | expression() ( "," function_arguments() | "for" for_indices() )?\r
+ * \r
+ * }\r
+ * @param functionArguments\r
+ * @param expressions\r
+ * @return\r
+ */\r
+ private ArrayList<UnitCheckingNode> gatherExpressions(UnitCheckingNode functionArguments, ArrayList<UnitCheckingNode> expressions) {\r
+ if(functionArguments.jjtGetNumChildren() > 0) {\r
+ if(functionArguments.jjtGetChild(0) instanceof Expression) {\r
+ expressions.add(((UnitCheckingNode)functionArguments.jjtGetChild(0)));\r
+ }\r
+ }\r
+\r
+ if(functionArguments.jjtGetNumChildren() > 1) {\r
+ gatherExpressions((UnitCheckingNode)functionArguments.jjtGetChild(1), expressions);\r
+ }\r
+\r
+ return expressions;\r
+ }\r
+\r
+}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.unitParser.nodes;\r
+\r
+import org.simantics.sysdyn.unitParser.UnitCheckingNode;\r
+\r
+/**\r
+ * See UnitCheckingNodeFactory for mapping\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class Expression extends UnitCheckingNode {\r
+\r
+ public Expression(int id) {\r
+ super(id);\r
+ }\r
+\r
+}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.unitParser.nodes;\r
+\r
+import org.simantics.sysdyn.unitParser.UnitCheckingNode;\r
+\r
+/**\r
+ * See UnitCheckingNodeFactory for mapping\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class FunctionArguments extends UnitCheckingNode {\r
+\r
+ public FunctionArguments(int id) {\r
+ super(id);\r
+ }\r
+\r
+}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.unitParser.nodes;\r
+\r
+import java.util.HashMap;\r
+\r
+import org.simantics.sysdyn.unitParser.UnitCheckingException;\r
+import org.simantics.sysdyn.unitParser.UnitCheckingNode;\r
+import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType;\r
+\r
+/**\r
+ * See UnitCheckingNodeFactory for mapping\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class FunctionCall extends UnitCheckingNode {\r
+\r
+ public FunctionCall(int id) {\r
+ super(id);\r
+ }\r
+ \r
+ @Override\r
+ public UnitResult getUnits(HashMap<String, String> units) throws UnitCheckingException {\r
+ UnitResult result = new UnitResult();\r
+ result.setUnitType(UnitType.ANY);\r
+ return result;\r
+ }\r
+\r
+}\r