From: Hannu Niemistö Date: Thu, 29 Dec 2016 07:44:00 +0000 (+0200) Subject: Proper error message, when pattern compilation fails X-Git-Tag: v1.26.0~4 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=fc1c765795517b3e77a1956fb2ae98a770d2beec;p=simantics%2Fplatform.git Proper error message, when pattern compilation fails Also, added CHR regression tests to regression test suite. refs #6900 Change-Id: Idac2dc44b9afa74b5ea48e53d1f317a3497bd2bb --- diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRQuery.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRQuery.java index 8bcd14b38..9e2bf6f34 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRQuery.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRQuery.java @@ -1,94 +1,94 @@ -package org.simantics.scl.compiler.elaboration.chr; - -import org.simantics.scl.compiler.elaboration.chr.plan.PostCommitOp; -import org.simantics.scl.compiler.elaboration.chr.plan.PreCommitOp; -import org.simantics.scl.compiler.elaboration.chr.planning.QueryPlanningContext; -import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext; -import org.simantics.scl.compiler.elaboration.contexts.TranslationContext; -import org.simantics.scl.compiler.elaboration.contexts.TypingContext; -import org.simantics.scl.compiler.elaboration.expressions.Expression; -import org.simantics.scl.compiler.elaboration.expressions.Variable; -import org.simantics.scl.compiler.elaboration.expressions.VariableProcedure; -import org.simantics.scl.compiler.elaboration.expressions.printing.ExpressionToStringVisitor; -import org.simantics.scl.compiler.errors.Locations; -import org.simantics.scl.compiler.internal.parsing.Symbol; - -import gnu.trove.map.hash.TObjectIntHashMap; -import gnu.trove.set.hash.THashSet; -import gnu.trove.set.hash.TIntHashSet; - -public class CHRQuery extends Symbol { - public CHRLiteral[] literals; - - public CHRQuery(CHRLiteral[] literals) { - this.literals = literals; - } - - public void resolve(TranslationContext context) { - for(CHRLiteral literal : literals) - literal.resolve(context); - } - - public void collectRefs(TObjectIntHashMap allRefs, TIntHashSet refs) { - for(CHRLiteral literal : literals) - literal.collectRefs(allRefs, refs); - } - - public void checkType(TypingContext context) { - for(CHRLiteral literal : literals) - literal.checkType(context); - } - - public void collectVars(TObjectIntHashMap allVars, TIntHashSet vars) { - for(CHRLiteral literal : literals) - literal.collectVars(allVars, vars); - } - - public void forVariables(VariableProcedure procedure) { - for(CHRLiteral literal : literals) - literal.forVariables(procedure); - } - - public void collectFreeVariables(THashSet vars) { - for(CHRLiteral literal : literals) - literal.collectFreeVariables(vars); - } - - public void setLocationDeep(long loc) { - if(location == Locations.NO_LOCATION) { - this.location = loc; - for(CHRLiteral literal : literals) - literal.setLocationDeep(loc); - } - } - - public void createQueryPlan(QueryPlanningContext context, Expression inputFact, int activeLiteralId) { - for(int i=0;i allRefs, TIntHashSet refs) { + for(CHRLiteral literal : literals) + literal.collectRefs(allRefs, refs); + } + + public void checkType(TypingContext context) { + for(CHRLiteral literal : literals) + literal.checkType(context); + } + + public void collectVars(TObjectIntHashMap allVars, TIntHashSet vars) { + for(CHRLiteral literal : literals) + literal.collectVars(allVars, vars); + } + + public void forVariables(VariableProcedure procedure) { + for(CHRLiteral literal : literals) + literal.forVariables(procedure); + } + + public void collectFreeVariables(THashSet vars) { + for(CHRLiteral literal : literals) + literal.collectFreeVariables(vars); + } + + public void setLocationDeep(long loc) { + if(location == Locations.NO_LOCATION) { + this.location = loc; + for(CHRLiteral literal : literals) + literal.setLocationDeep(loc); + } + } + + public boolean createQueryPlan(QueryPlanningContext context, Expression inputFact, int activeLiteralId) { + for(int i=0;i allRefs, TIntHashSet refs) { - head.collectRefs(allRefs, refs); - body.collectRefs(allRefs, refs); - } - - public void checkType(TypingContext context) { - for(Variable variable : existentialVariables) - variable.setType(Types.metaVar(Kinds.STAR)); - head.checkType(context); - body.checkType(context); - } - - public void collectVars(TObjectIntHashMap allVars, TIntHashSet vars) { - head.collectVars(allVars, vars); - body.collectVars(allVars, vars); - } - - public void forVariables(VariableProcedure procedure) { - head.forVariables(procedure); - body.forVariables(procedure); - } - - public void collectFreeVariables(THashSet vars) { - head.collectFreeVariables(vars); - body.collectFreeVariables(vars); - } - - public void setLocationDeep(long loc) { - if(location == Locations.NO_LOCATION) { - this.location = loc; - head.setLocationDeep(loc); - body.setLocationDeep(loc); - } - } - - public void simplify(SimplificationContext context) { - head.simplify(context); - body.simplify(context); - } - - public void compile(CompilationContext compilationContext, CHRConstraint initConstraint) { - boolean hasActiveLiteral = false; - for(int i=0;i allRefs, TIntHashSet refs) { + head.collectRefs(allRefs, refs); + body.collectRefs(allRefs, refs); + } + + public void checkType(TypingContext context) { + for(Variable variable : existentialVariables) + variable.setType(Types.metaVar(Kinds.STAR)); + head.checkType(context); + body.checkType(context); + } + + public void collectVars(TObjectIntHashMap allVars, TIntHashSet vars) { + head.collectVars(allVars, vars); + body.collectVars(allVars, vars); + } + + public void forVariables(VariableProcedure procedure) { + head.forVariables(procedure); + body.forVariables(procedure); + } + + public void collectFreeVariables(THashSet vars) { + head.collectFreeVariables(vars); + body.collectFreeVariables(vars); + } + + public void setLocationDeep(long loc) { + if(location == Locations.NO_LOCATION) { + this.location = loc; + head.setLocationDeep(loc); + body.setLocationDeep(loc); + } + } + + public void simplify(SimplificationContext context) { + head.simplify(context); + body.simplify(context); + } + + public void compile(CompilationContext compilationContext, CHRConstraint initConstraint) { + boolean hasActiveLiteral = false; + for(int i=0;i variables; - TObjectIntHashMap variableMap; - ArrayList> itemsContainingVariable; - ArrayList planOps = new ArrayList(); - - public QueryPlanningContext(CompilationContext compilationContext, Variable[] variables) { - this.compilationContext = compilationContext; - this.variables = new ArrayList(variables.length*2); - this.variableMap = new TObjectIntHashMap(variables.length, Constants.DEFAULT_LOAD_FACTOR, -1); - itemsContainingVariable = new ArrayList>(variables.length*2); - for(Variable variable : variables) - addVariable(variable); - } - - private void addVariable(Variable variable) { - int id = variables.size(); - variables.add(variable); - variableMap.put(variable, id); - itemsContainingVariable.add(new ArrayList(2)); - } - - public void add(CHRLiteral literal, int secondaryPriority) { - if(literal.relation instanceof SpecialCHRRelation) { - switch((SpecialCHRRelation)literal.relation) { - case CHECK: - addCheck(literal.location, literal.parameters[0], secondaryPriority); - return; - case EQUALS: - addGenericEquals(literal.location, literal.parameters[0], literal.parameters[1], secondaryPriority); - return; - case MEMBER: - addMember(literal.location, literal.parameters[0], literal.parameters[1], secondaryPriority); - return; - case EXECUTE: - throw new InternalCompilerError(literal.location, "EXECUTE constraint is not allowed in query compilation."); - } - } - - addGenericConstraint(literal, secondaryPriority); - } - - private TIntHashSet getVars(Expression expression, int initialCapacity) { - TIntHashSet variableSet = new TIntHashSet(initialCapacity); - expression.collectVars(variableMap, variableSet); - return variableSet; - } - - private TIntHashSet[] getVars(Expression[] expressions, int initialCapacity) { - TIntHashSet[] variableSets = new TIntHashSet[expressions.length]; - for(int i=0;i getPlanOps() { - return planOps; - } - - private final TIntProcedure BIND_PROCEDURE = new TIntProcedure() { - @Override - public boolean execute(int variableId) { - ArrayList l = itemsContainingVariable.get(variableId); - for(PrePlanItem item : l) - item.variableSolved(QueryPlanningContext.this, variableId); - l.clear(); - return true; - } - }; - - public void bind(TIntHashSet variableSet) { - variableSet.forEach(BIND_PROCEDURE); - } - - public void addPlanOp(PlanOp planOp) { - planOps.add(planOp); - } - - public CompilationContext getCompilationContext() { - return compilationContext; - } - - public void activate(CHRLiteral literal, Expression inputFact, int secondaryPriority) { - Variable[] variables = new Variable[literal.parameters.length]; - for(int i=0;i variables; + TObjectIntHashMap variableMap; + ArrayList> itemsContainingVariable; + ArrayList planOps = new ArrayList(); + + public QueryPlanningContext(CompilationContext compilationContext, Variable[] variables) { + this.compilationContext = compilationContext; + this.variables = new ArrayList(variables.length*2); + this.variableMap = new TObjectIntHashMap(variables.length, Constants.DEFAULT_LOAD_FACTOR, -1); + itemsContainingVariable = new ArrayList>(variables.length*2); + for(Variable variable : variables) + addVariable(variable); + } + + private void addVariable(Variable variable) { + int id = variables.size(); + variables.add(variable); + variableMap.put(variable, id); + itemsContainingVariable.add(new ArrayList(2)); + } + + public void add(CHRLiteral literal, int secondaryPriority) { + if(literal.relation instanceof SpecialCHRRelation) { + switch((SpecialCHRRelation)literal.relation) { + case CHECK: + addCheck(literal.location, literal.parameters[0], secondaryPriority); + return; + case EQUALS: + addGenericEquals(literal.location, literal.parameters[0], literal.parameters[1], secondaryPriority); + return; + case MEMBER: + addMember(literal.location, literal.parameters[0], literal.parameters[1], secondaryPriority); + return; + case EXECUTE: + throw new InternalCompilerError(literal.location, "EXECUTE constraint is not allowed in query compilation."); + } + } + + addGenericConstraint(literal, secondaryPriority); + } + + private TIntHashSet getVars(Expression expression, int initialCapacity) { + TIntHashSet variableSet = new TIntHashSet(initialCapacity); + expression.collectVars(variableMap, variableSet); + return variableSet; + } + + private TIntHashSet[] getVars(Expression[] expressions, int initialCapacity) { + TIntHashSet[] variableSets = new TIntHashSet[expressions.length]; + for(int i=0;i getPlanOps() { + return planOps; + } + + private final TIntProcedure BIND_PROCEDURE = new TIntProcedure() { + @Override + public boolean execute(int variableId) { + ArrayList l = itemsContainingVariable.get(variableId); + for(PrePlanItem item : l) + item.variableSolved(QueryPlanningContext.this, variableId); + l.clear(); + return true; + } + }; + + public void bind(TIntHashSet variableSet) { + variableSet.forEach(BIND_PROCEDURE); + } + + public void addPlanOp(PlanOp planOp) { + planOps.add(planOp); + } + + public CompilationContext getCompilationContext() { + return compilationContext; + } + + public void activate(CHRLiteral literal, Expression inputFact, int secondaryPriority) { + Variable[] variables = new Variable[literal.parameters.length]; + for(int i=0;i