]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/errors/Locations.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / errors / Locations.java
1 package org.simantics.scl.compiler.errors;
2
3 public class Locations {
4
5     public static final long NO_LOCATION = 0x7fffffff80000000L;
6     
7     public static long location(int begin, int end) {
8         return (((long)begin) << 32) | (((long)end) );
9     }
10     
11     public static int beginOf(long location) {
12         return (int)(location >>> 32);
13     }
14     
15     public static int endOf(long location) {
16         return (int)location;
17     }
18     
19     public static int length(long location) {
20         return endOf(location) - beginOf(location);
21     }
22
23     
24     public static long combine(long a, long b) {
25         return location(Math.min(beginOf(a), beginOf(b)), Math.max(endOf(a), endOf(b)));
26     }
27     
28     public static int compare(long a, long b) {
29         if(a < b) 
30             return -1;
31         if(a > b) 
32             return 1;
33         return 0;
34     }
35
36     public static String annotatate(String annotationBegin, String annotationEnd, String formula, long location) {
37         if(location == NO_LOCATION)
38             return annotationBegin + formula + annotationEnd;
39         int begin = beginOf(location);
40         int end = endOf(location);
41         return formula.substring(0, begin) + annotationBegin + formula.substring(begin, end) + annotationEnd + formula.substring(end);
42     }
43     
44     public static long sublocation(long location, int localBegin, int localEnd) {
45         int begin = beginOf(location);
46         int end = endOf(location);
47         if(localEnd > end-begin)
48             localEnd = begin-end;
49         return location(begin+localBegin, begin+localEnd);
50     }
51
52     public static String toString(long location) {
53         if(location == NO_LOCATION)
54             return "NO LOCATION";
55         else
56             return beginOf(location) + "-" + endOf(location);
57     }
58 }