From: Hannu Niemistö Date: Sun, 18 Jun 2017 17:58:11 +0000 (+0300) Subject: (refs #7316) Improved error locations for invalid field access X-Git-Tag: v1.31.0~306^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=d8e51c3b7d55403115dc83d0a9b2337378e91fb8;p=simantics%2Fplatform.git (refs #7316) Improved error locations for invalid field access Change-Id: Ie901cd59e0cf45ece1c99932d44ac1863da97565 --- diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TranslationContext.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TranslationContext.java index 0331f9a50..ac9299d77 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TranslationContext.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TranslationContext.java @@ -157,13 +157,16 @@ public class TranslationContext extends TypeTranslationContext implements Enviro return existentialFrames.get(size-1); } - private Expression resolveFieldAccess(Expression base, int pos, String name) { + private Expression resolveFieldAccess(long location, Expression base, int pos, String name) { while(pos != -1) { int p = findSeparator(name, pos+1); + int endPos = p==-1 ? name.length() : p; FieldAccessor accessor = new IdAccessor( name.charAt(pos), - name.substring(pos+1, p==-1 ? name.length() : p-1)); + name.substring(pos+1, endPos)); + accessor.location = Locations.sublocation(location, pos+1, endPos); base = new EFieldAccess(base, accessor); + base.location = Locations.sublocation(location, 0, endPos); pos = p; } return base; @@ -235,7 +238,7 @@ public class TranslationContext extends TypeTranslationContext implements Enviro if(begin == 0) { Expression result = resolveLocalVariable(location, part); if(result != null) - return end == -1 ? result : resolveFieldAccess(result, end, name); + return end == -1 ? result : resolveFieldAccess(location, result, end, name); // FIXME.. support for records if(localEnvironment != null) { @@ -266,7 +269,7 @@ public class TranslationContext extends TypeTranslationContext implements Enviro if(result == null) result = resolveValue(location, namespace, part); if(result != null) - return end == -1 ? result : resolveFieldAccess(result, end, name); + return end == -1 ? result : resolveFieldAccess(location, result, end, name); } reportResolveFailure(location, namespace, part); return new EError(location); diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EFieldAccess.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EFieldAccess.java index 0d3e2eeaf..a8d117f5d 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EFieldAccess.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EFieldAccess.java @@ -67,7 +67,7 @@ public class EFieldAccess extends SimplifiableExpression { return null; List accessors = context.getEnvironment().getFieldAccessors(idAccessor.fieldName); if(accessors == null) { - context.getErrorLog().log("Couldn't resolve accessor ." + idAccessor.fieldName + "."); + context.getErrorLog().log(idAccessor.location, "Couldn't resolve accessor ." + idAccessor.fieldName + "."); return new EError(location); } Expression accessorExpression; @@ -86,6 +86,10 @@ public class EFieldAccess extends SimplifiableExpression { public Type getType() { return accessors.get(index).getType(); } + @Override + public String toString() { + return accessors.get(index).toString(); + } }; } accessorExpression = new EAmbiguous(alternatives); diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/errors/Locations.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/errors/Locations.java index 49c5b03d4..3a77cf325 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/errors/Locations.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/errors/Locations.java @@ -40,4 +40,12 @@ public class Locations { int end = endOf(location); return formula.substring(0, begin) + annotationBegin + formula.substring(begin, end) + annotationEnd + formula.substring(end); } + + public static long sublocation(long location, int localBegin, int localEnd) { + int begin = beginOf(location); + int end = endOf(location); + if(localEnd > end-begin) + localEnd = begin-end; + return location(begin+localBegin, begin+localEnd); + } }