Search code examples
rascal

Location and unclear parameters


The optional parameters for a Location seem to be duplicate information:

  • Offset/Length define the starting character and end character of the selection
  • Begin/end line/column seem to be used for the same thing

However, begin/end line/column don't seem to be working: the location at

|project://file.java|(0,100,<1,1>,<1,1>)

and the location at

|project://file.java|(0,100,<9,2>,<17,23>)

return both the same content (when shown using e.g. readFile() ).

This leads to two questions:

  • What is the use of the begin/end line/column if they aren't used for any practical reasons?

  • Is there a way to construct a location when only begin/end line/column are known, but not the offset/length? (without accessing the file)

I found a related question to this question (How to construct a location?), but the answer given there does not explain this.


Solution

    1. The offset/length and begin/end information are indeed the same information, however to reconstruct line/column information from offset/length information you always have to read the entire file. Because some editors only support line/column indexing, we chose to include the information where possible and avoid having to walk the entire file and count lines for jumping to a selection. Also that code would be OS/text file encoding dependent.
    2. The loc representation does not allow line/column to be set without the offset/length information by design, because we need offset/length at the very least for the API of the Eclipse editor and Emacs for example. The line/column stuff is additional optimization/caching and not the other way around.
    3. If you do need to represent line/column information temporarily inside a loc I would recommend using a query parameter like ?startLine=12&endLine=24. The system will store the information but not interpret it or use it.