From: lempinen Date: Thu, 2 May 2013 05:13:55 +0000 (+0000) Subject: Forgotten files for unit parser. (refs #4263) X-Git-Tag: 1.8.1~315 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=244e28c41a0e00c5fd3451db1cff071146d4b2de;p=simantics%2Fsysdyn.git Forgotten files for unit parser. (refs #4263) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27301 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ArrayDefinition.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ArrayDefinition.java new file mode 100644 index 00000000..4b46e718 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ArrayDefinition.java @@ -0,0 +1,102 @@ +/******************************************************************************* + * Copyright (c) 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.unitParser.nodes; + +import java.util.ArrayList; +import java.util.HashMap; + +import org.simantics.sysdyn.unitParser.UnitCheckingException; +import org.simantics.sysdyn.unitParser.UnitCheckingNode; +import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; + +/** + * See UnitCheckingNodeFactory for mapping + * @author Teemu Lempinen + * + */ +public class ArrayDefinition extends UnitCheckingNode { + + public ArrayDefinition(int id) { + super(id); + } + + + @Override + public UnitResult getUnits(HashMap units) throws UnitCheckingException { + UnitResult result = new UnitResult(); + result.setUnitType(UnitType.SCALAR); + + UnitCheckingNode base = null; + + if(jjtGetNumChildren() > 0 && jjtGetChild(0) instanceof FunctionArguments) { + FunctionArguments functionArguments = (FunctionArguments) jjtGetChild(0); + + for(UnitCheckingNode candidateNode : gatherExpressions(functionArguments, new ArrayList())) { + UnitResult candidateUnits = candidateNode.getUnits(units); + if(candidateUnits.getUnitType() == UnitType.SCALAR) { + continue; + } else if(base == null) { + base = candidateNode; + UnitType oldUnitType = result.getUnitType(); + result.appendResult(base.getUnits(units)); + + /* + * Make sure unit type persist + * {Var1, Var2, 1} would result in UnitType.SCALAR even though it should be UnitType.NORMAL + * because the last element has UnitType.SCALAR + */ + UnitType newUnitType = result.getUnitType(); + if(newUnitType == UnitType.SCALAR || newUnitType == UnitType.ANY || newUnitType == UnitType.DMNL) + result.setUnitType(oldUnitType); + continue; + } else { + if(!result.equals(candidateUnits)) { + throw new UnitCheckingException("Array elements do not have same units: " + + base.printNode() + " [" + result.getCleanFullUnit() + "] =/= " + + candidateNode.printNode() + " [" + candidateUnits.getCleanFullUnit() + "]" + ); + } + } + } + } + + return result; + } + + /** + * Gather all expressions of array definitions. + * + * void function_arguments() : { + * } { + * LOOKAHEAD(2) named_argument() ( "," function_arguments() )? + * | expression() ( "," function_arguments() | "for" for_indices() )? + * + * } + * @param functionArguments + * @param expressions + * @return + */ + private ArrayList gatherExpressions(UnitCheckingNode functionArguments, ArrayList expressions) { + if(functionArguments.jjtGetNumChildren() > 0) { + if(functionArguments.jjtGetChild(0) instanceof Expression) { + expressions.add(((UnitCheckingNode)functionArguments.jjtGetChild(0))); + } + } + + if(functionArguments.jjtGetNumChildren() > 1) { + gatherExpressions((UnitCheckingNode)functionArguments.jjtGetChild(1), expressions); + } + + return expressions; + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Expression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Expression.java new file mode 100644 index 00000000..eb3ec7f7 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Expression.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.unitParser.nodes; + +import org.simantics.sysdyn.unitParser.UnitCheckingNode; + +/** + * See UnitCheckingNodeFactory for mapping + * @author Teemu Lempinen + * + */ +public class Expression extends UnitCheckingNode { + + public Expression(int id) { + super(id); + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionArguments.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionArguments.java new file mode 100644 index 00000000..3921d3cf --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionArguments.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.unitParser.nodes; + +import org.simantics.sysdyn.unitParser.UnitCheckingNode; + +/** + * See UnitCheckingNodeFactory for mapping + * @author Teemu Lempinen + * + */ +public class FunctionArguments extends UnitCheckingNode { + + public FunctionArguments(int id) { + super(id); + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionCall.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionCall.java new file mode 100644 index 00000000..e165cc56 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionCall.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.unitParser.nodes; + +import java.util.HashMap; + +import org.simantics.sysdyn.unitParser.UnitCheckingException; +import org.simantics.sysdyn.unitParser.UnitCheckingNode; +import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; + +/** + * See UnitCheckingNodeFactory for mapping + * @author Teemu Lempinen + * + */ +public class FunctionCall extends UnitCheckingNode { + + public FunctionCall(int id) { + super(id); + } + + @Override + public UnitResult getUnits(HashMap units) throws UnitCheckingException { + UnitResult result = new UnitResult(); + result.setUnitType(UnitType.ANY); + return result; + } + +}