]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/parser/unparsing/DataTypeToAst.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / parser / unparsing / DataTypeToAst.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/parser/unparsing/DataTypeToAst.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/parser/unparsing/DataTypeToAst.java
new file mode 100644 (file)
index 0000000..0fd53b1
--- /dev/null
@@ -0,0 +1,218 @@
+/*******************************************************************************\r
+ *  Copyright (c) 2010 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
+ *      VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.parser.unparsing;
+
+import gnu.trove.set.hash.THashSet;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.simantics.databoard.parser.ast.type.AstArrayType;\r
+import org.simantics.databoard.parser.ast.type.AstComponent;\r
+import org.simantics.databoard.parser.ast.type.AstRecordType;\r
+import org.simantics.databoard.parser.ast.type.AstTupleType;\r
+import org.simantics.databoard.parser.ast.type.AstType;\r
+import org.simantics.databoard.parser.ast.type.AstTypeDefinition;\r
+import org.simantics.databoard.parser.ast.type.AstTypeReference;\r
+import org.simantics.databoard.parser.ast.type.AstUnionType;\r
+import org.simantics.databoard.parser.repository.DataTypeRepository;\r
+import org.simantics.databoard.type.ArrayType;\r
+import org.simantics.databoard.type.BooleanType;\r
+import org.simantics.databoard.type.ByteType;\r
+import org.simantics.databoard.type.Component;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.type.Datatype.Visitor;\r
+import org.simantics.databoard.type.DoubleType;\r
+import org.simantics.databoard.type.FloatType;\r
+import org.simantics.databoard.type.IntegerType;\r
+import org.simantics.databoard.type.LongType;\r
+import org.simantics.databoard.type.MapType;\r
+import org.simantics.databoard.type.NumberType;\r
+import org.simantics.databoard.type.OptionalType;\r
+import org.simantics.databoard.type.RecordType;\r
+import org.simantics.databoard.type.StringType;\r
+import org.simantics.databoard.type.UnionType;\r
+import org.simantics.databoard.type.VariantType;\r
+
+/**
+ * Converts data type to abstract syntax tree.
+ * 
+ * @author Hannu Niemistö
+ */
+public class DataTypeToAst implements Visitor<AstType> {
+
+       List<AstTypeDefinition> typeDefinitions = new ArrayList<AstTypeDefinition>();
+       DataTypeRepository repo = new DataTypeRepository();
+       THashSet<Datatype> underConstruction = new THashSet<Datatype>();
+
+       int id = 0;
+
+       public DataTypeToAst(DataTypeRepository repo) {
+               this.repo = repo;
+       }
+       
+       private String freshTypeName() {
+               return "Temp" + (++id);
+       }
+       
+       public AstType addDefinition(String name, Datatype type) {
+               repo.add(name, type);
+               AstType ast = type.accept(this);                
+               typeDefinitions.add(new AstTypeDefinition(name, type.accept(this)));
+               return ast;
+       }
+       
+       public AstType visit(Datatype type) {
+               if(repo.contains(type))
+                       return new AstTypeReference(repo.get(type));
+               if(underConstruction.contains(type)) {
+                       String name = repo.get(type);
+                       if (name==null) {
+                               name = freshTypeName();
+                               repo.add(name, type);
+                       }
+                       underConstruction.remove(type);
+                       return new AstTypeReference(name);
+               }
+               else {
+                       underConstruction.add(type);
+                       AstType ast = type.accept(this);
+                       if(!underConstruction.remove(type)) {
+                               String name = repo.get(type);
+                               typeDefinitions.add(new AstTypeDefinition(name, ast));
+                               return new AstTypeReference(name);
+                       }
+                       else
+                               return ast;
+               }
+       }
+       
+       @Override
+       public AstType visit(ArrayType b) {
+               return new AstArrayType(visit(b.componentType), 
+                               b.getLength()==null ? null : b.getLength().getLower().smallestIncludedInteger(),
+                           b.getLength()==null ? null : b.getLength().getUpper().greatestIncludedInteger());
+       }
+
+       @Override
+       public AstType visit(BooleanType b) {
+               return new AstTypeReference("Boolean");
+       }
+
+       @Override
+       public AstType visit(DoubleType b) {
+               AstTypeReference ref = new AstTypeReference("Double");\r
+               // XXX
+               if( b.metadata.containsKey( NumberType.KEY_RANGE ) )
+                       ref.addAttribute("range", b.metadata.get( NumberType.KEY_RANGE ) );             
+               if( b.metadata.containsKey( NumberType.KEY_UNIT ) )
+                       ref.addAttribute("unit", b.metadata.get( NumberType.KEY_UNIT ));
+               return ref;
+       }
+
+       @Override
+       public AstType visit(FloatType b) {
+               AstTypeReference ref = new AstTypeReference("Float");
+               // XXX\r
+               if( b.metadata.containsKey( NumberType.KEY_RANGE ) )\r
+                       ref.addAttribute("range", b.metadata.get( NumberType.KEY_RANGE ) );             \r
+               if( b.metadata.containsKey( NumberType.KEY_UNIT ) )\r
+                       ref.addAttribute("unit", b.metadata.get( NumberType.KEY_UNIT ));\r
+               return ref;
+       }
+
+       @Override
+       public AstType visit(IntegerType b) {
+               AstTypeReference ref = new AstTypeReference("Integer");
+               // XXX\r
+               if( b.metadata.containsKey( NumberType.KEY_RANGE ) )\r
+                       ref.addAttribute("range", b.metadata.get( NumberType.KEY_RANGE ) );             \r
+               if( b.metadata.containsKey( NumberType.KEY_UNIT ) )\r
+                       ref.addAttribute("unit", b.metadata.get( NumberType.KEY_UNIT ));\r
+               return ref;
+       }
+
+       @Override
+       public AstType visit(ByteType b) {
+               return new AstTypeReference("Byte");
+       }
+
+       @Override
+       public AstType visit(LongType b) {
+               AstTypeReference ref = new AstTypeReference("Long");
+               // XXX\r
+               if( b.metadata.containsKey( NumberType.KEY_RANGE ) )\r
+                       ref.addAttribute("range", b.metadata.get( NumberType.KEY_RANGE ) );             \r
+               if( b.metadata.containsKey( NumberType.KEY_UNIT ) )\r
+                       ref.addAttribute("unit", b.metadata.get( NumberType.KEY_UNIT ));\r
+               return ref;
+       }
+
+       @Override
+       public AstType visit(OptionalType b) {
+               return new AstTypeReference("Optional", visit(b.getComponentType()));
+       }
+
+       @Override
+       public AstType visit(RecordType b) {
+               if(b.isTupleType()) {
+                       AstTupleType tuple = new AstTupleType(new ArrayList<AstType>(b.getComponentCount()));
+                       for(Component component : b.getComponents()) 
+                               tuple.addComponent(visit(component.type));
+                       return tuple;
+               }
+               else {
+                       AstRecordType record = new AstRecordType(b.isReferable(), new ArrayList<AstComponent>(b.getComponentCount()));
+                       for(Component component : b.getComponents()) 
+                               record.addComponent(component.name, visit(component.type));
+                       return record;
+               }
+       }
+
+       @Override
+       public AstType visit(StringType b) {
+               AstTypeReference ref = new AstTypeReference("String");\r
+               // XXX
+               if( b.metadata.containsKey( StringType.KEY_MIMETYPE ) )
+                       ref.addAttribute("mimeType", b.metadata.get( StringType.KEY_MIMETYPE ));
+               if( b.metadata.containsKey( StringType.KEY_LENGTH ) )
+                       ref.addAttribute("length", b.metadata.get( StringType.KEY_LENGTH ));
+               if( b.metadata.containsKey( StringType.KEY_PATTERN ))
+                       ref.addAttribute("pattern", b.metadata.get( StringType.KEY_PATTERN ));
+               return ref;
+       }
+
+       @Override
+       public AstType visit(UnionType b) {
+               AstUnionType union = new AstUnionType(new ArrayList<AstComponent>(b.components.length));
+               for(Component component : b.components) 
+                       union.addComponent(component.name, visit(component.type));
+               return union;
+       }       
+       
+       @Override
+       public AstType visit(VariantType b) {
+               AstTypeReference ref = new AstTypeReference("Variant");
+               return ref;
+       }
+       
+       public List<AstTypeDefinition> getTypeDefinitions() {
+               return typeDefinitions;
+       }
+
+       @Override
+       public AstType visit(MapType b) {
+               AstTypeReference ref = new AstTypeReference("Map", visit(b.keyType), visit(b.valueType));
+               return ref;
+       }
+       
+}