Is there a way to add 2 location with a specific line and column with each other?
Something like:
|project://MyProject/src/MyClass.java|(962,10,<39,2>,<39,12>) + |project://MyProject/src/MyClass.java|(975,53,<40,2>,<40,55>);
With as outcome:
|project://MyProject/src/MyClass.java|(962,66,<39,2>,<40,55>)
I am currently working with AST's and I want to have a selection of a block but I couldn't find how so I tried to add every statement's location but that won't work.
Building on Paul's answer, this also handles the typical case, where there is a "gap" (probably whitespace) between the second location and the first:
loc add(loc s, loc r) {
res = s;
res.end = r.end;
adjust = 0;
if (s.offset + s.length < r.offset) {
adjust = r.offset - (s.offset+s.length);
}
res.length = s.length + r.length + adjust;
return res;
}
You definitely have to be careful with this, though -- there are a bunch of cases and possible error conditions that this doesn't handle (e.g., locations overlap, locations passed in in the wrong order, locations are actually from different files). It should handle just "merging" locations for two adjacent constructs, though.