]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParserImpl.java
Merged changes from feature/scl to master.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / parsing / parser / SCLParserImpl.java
index bbf944f1e28b8c84cf78b00895fbca516158e8e4..7477c38900b7f7c05e8eef4dcc3c4757c64a8755 100644 (file)
@@ -41,7 +41,7 @@ import org.simantics.scl.compiler.elaboration.expressions.ETransformation;
 import org.simantics.scl.compiler.elaboration.expressions.ETypeAnnotation;
 import org.simantics.scl.compiler.elaboration.expressions.EVar;
 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
-import org.simantics.scl.compiler.elaboration.expressions.EWhen;
+import org.simantics.scl.compiler.elaboration.expressions.EViewPattern;
 import org.simantics.scl.compiler.elaboration.expressions.Expression;
 import org.simantics.scl.compiler.elaboration.expressions.GuardedExpression;
 import org.simantics.scl.compiler.elaboration.expressions.GuardedExpressionGroup;
@@ -51,6 +51,8 @@ import org.simantics.scl.compiler.elaboration.expressions.accessor.FieldAccessor
 import org.simantics.scl.compiler.elaboration.expressions.accessor.IdAccessor;
 import org.simantics.scl.compiler.elaboration.expressions.accessor.StringAccessor;
 import org.simantics.scl.compiler.elaboration.expressions.block.BindStatement;
+import org.simantics.scl.compiler.elaboration.expressions.block.CHRStatement;
+import org.simantics.scl.compiler.elaboration.expressions.block.ConstraintStatement;
 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
 import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;
@@ -220,6 +222,7 @@ public class SCLParserImpl extends SCLParser {
                 );
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     protected Object reduceClassDefinition() {
         int i=1;
@@ -273,6 +276,7 @@ public class SCLParserImpl extends SCLParser {
         return fundeps;
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     protected Object reduceInstanceDefinition() {
         int i=1;
@@ -298,6 +302,7 @@ public class SCLParserImpl extends SCLParser {
                 declarations);
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     protected Object reduceDerivingInstanceDefinition() {
         int i=2;
@@ -381,6 +386,7 @@ public class SCLParserImpl extends SCLParser {
         return get(0);
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     protected Object reduceImportJava() {
         return new DImportJavaAst(((Token)get(2)).text, (ArrayList<DeclarationAst>)get(4));
@@ -686,6 +692,14 @@ public class SCLParserImpl extends SCLParser {
         case_.setLhs(Locations.combine(patterns[0].location, patterns[patterns.length-1].location));
         return new ELambda(case_);
     }
+    
+    @Override
+    protected Object reduceLambdaMatch() {
+        Case[] cases = new Case[length()/2-1];
+        for(int i=0;i<cases.length;++i)
+            cases[i] = (Case)get(i*2+2);
+        return new ELambda(cases);
+    }
 
     @Override
     protected Object reduceLet() {
@@ -704,7 +718,7 @@ public class SCLParserImpl extends SCLParser {
         return new EIf(
                 (Expression)get(1),
                 (Expression)get(3),
-                (Expression)get(5));
+                length() == 6 ? (Expression)get(5) : null);
     }
 
     @Override
@@ -728,14 +742,7 @@ public class SCLParserImpl extends SCLParser {
     protected Object reduceSelect() {
         return new ESelect(((Token)get(0)).id, (Expression)get(1), new QConjunction((Query[])get(3)));
     }
-    
-    @Override
-    protected Object reduceWhen() {
-        return new EWhen(
-                new QConjunction((Query[])get(1)),
-                (Expression)get(3));
-    }
-    
+       
     @Override
     protected Object reduceEnforce() {
         return new EEnforce(new QConjunction((Query[])get(1)));
@@ -880,6 +887,7 @@ public class SCLParserImpl extends SCLParser {
         throw new UnsupportedOperationException();
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     protected void postReduce(Object reduced) {
         if(!(reduced instanceof Symbol))
@@ -887,6 +895,20 @@ public class SCLParserImpl extends SCLParser {
         Symbol sym = (Symbol)reduced;
         if(sym.location != Locations.NO_LOCATION || length() == 0)
             return;
+        Object first = get(0);
+        if(!(first instanceof Symbol)) {
+            if(first instanceof List) {
+                List<Object> ll = (List<Object>)first;
+                first = ll.get(0);
+            }
+            else {
+                Object[] ll = (Object[])first;
+                if(ll.length > 0)
+                    first = ll[0];
+                else
+                    first = get(1);
+            }
+        }
         Object last = get(length()-1);
         if(!(last instanceof Symbol)) {
             if(last instanceof List) {
@@ -901,7 +923,7 @@ public class SCLParserImpl extends SCLParser {
                     last = get(length()-2);
             }
         }
-        sym.location = (((Symbol)get(0)).location & 0xffffffff00000000L) 
+        sym.location = (((Symbol)first).location & 0xffffffff00000000L) 
                 | (((Symbol)last).location & 0xffffffffL);
         /*for(int i=0;i<length();++i) {
             Object obj = get(i);
@@ -1078,6 +1100,7 @@ public class SCLParserImpl extends SCLParser {
 
     private static final String[] EMPTY_STRING_ARRAY = new String[0];
     
+    @SuppressWarnings("unchecked")
     @Override
     protected Object reduceRuleDefinition() {
         String[] extendsNames = EMPTY_STRING_ARRAY;
@@ -1134,6 +1157,7 @@ public class SCLParserImpl extends SCLParser {
                 );
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     protected Object reduceRelationDefinition() {
         return new DRelationAst((Expression)get(0), 
@@ -1207,4 +1231,52 @@ public class SCLParserImpl extends SCLParser {
     protected Object reduceBasicEquation() {
         return new EqBasic((Expression)get(0), (Expression)get(2));
     }
+
+    @Override
+    protected Object reduceViewPattern() {
+        return new EViewPattern((Expression)get(1), (Expression)get(3));
+    }
+
+    @Override
+    protected Object reduceCHRStatement() {
+        return new CHRStatement((ListQualifier[])get(0), (ListQualifier[])get(2));
+    }
+
+    @Override
+    protected Object reduceConstraintStatement() {
+        TypeAst[] parameterTypes = new TypeAst[length()-2];
+        for(int i=0;i<parameterTypes.length;++i)
+            parameterTypes[i] = (TypeAst)get(2+i);
+        return new ConstraintStatement((Token)get(1), parameterTypes);
+    }
+
+    @Override
+    protected Object reduceCHRQuery() {
+        ListQualifier[] query = new ListQualifier[(length()+1)/2];
+        for(int i=0;i<query.length;++i)
+            query[i] = (ListQualifier)get(i*2);
+        return query;
+    }
+
+    /*
+    @Override
+    protected Object reduceWhen() {
+        return new EWhen(
+                new QConjunction((Query[])get(1)),
+                (Expression)get(3));
+    }*/
+    
+    @Override
+    protected Object reduceVerboseCHRQuery() {
+        ListQualifier[] query = new ListQualifier[(length()-1)/2];
+        for(int i=0;i<query.length;++i)
+            query[i] = (ListQualifier)get(i*2+1);
+        return query;
+    }
+    
+    @Override
+    protected Object reduceVerboseCHRStatement() {
+        return new CHRStatement((ListQualifier[])get(1), (ListQualifier[])get(3));
+    }
+
 }