For what I am trying to do right now I need to get the line number out of the loc
type.
|file:///randomfile.java|(0,11817,<1,0>,<185,1>)
I can get the first part by using top(...) but how do I get the second part and more specific : the line number, I believe in this case it would be line #1.
PS can someone direct me towards information on the meaning of all numbers, can't seem to find it in the documentation.
To get the line number, assuming the location is called l
, you would use l.begin.line
.
More generally, given the location |file:///randomfile.java|(0,11817,<1,0>,<185,1>)
, the numbers represent (offset, length, < startingLine, startingColumn >, < endingLine, endingColumn> )
, so:
0
is the offset
into the given file where this location begins;11817
is the length
covered by the locationThese allow you to use a location to identify part of a file, for instance a specific class or method definition.
1
is the first line (startingLine
) included in the location0
is the first column (startingColumn
) included in the location185
is the last line (endingLine
) included in the location1
is the last column (endingColumn
) included in the locationThe offset and length define the actual part of the file covered by the location sufficiently, so you can view these as optional, and they don't need to be included. It also isn't always obvious, at least with the column, what the proper value is, since tab characters could be expanded to multiple characters in an editor.
To access each of these parts of the location, you use the following:
rascal>|file:///randomfile.java|(0,11817,<1,0>,<185,1>).offset;
int: 0
rascal>|file:///randomfile.java|(0,11817,<1,0>,<185,1>).length;
int: 11817
rascal>|file:///randomfile.java|(0,11817,<1,0>,<185,1>).begin;
tuple[int line,int column]: <1,0>
rascal>|file:///randomfile.java|(0,11817,<1,0>,<185,1>).begin.line;
int: 1
rascal>|file:///randomfile.java|(0,11817,<1,0>,<185,1>).begin.column;
int: 0
rascal>|file:///randomfile.java|(0,11817,<1,0>,<185,1>).end;
tuple[int line,int column]: <185,1>
rascal>|file:///randomfile.java|(0,11817,<1,0>,<185,1>).end.line;
int: 185
rascal>|file:///randomfile.java|(0,11817,<1,0>,<185,1>).end.column;
int: 1
You can find more details here, including documentation on how to access the other parts of the location, such as the scheme used in the URI or the name of the referenced resource/file: http://tutor.rascal-mpl.org/Rascal/Rascal.html#/Rascal/Expressions/Values/Location/Location.html