Search code examples
rascal

Setting line attributes for loc type in Rascal?


The syntax for a location (loc) is defined (according to tutor.rascal.org) as: | Uri | ( O, L, <BL,BC>, <EL,EC> )

where

  1. Uri is an arbitrary Uniform Resource Identifier (URI).
  2. O and L are integer expressions, where O gives the offset to the beginning of the file and 'L' gives the length of the file.
  3. BL and BC are integers denoting the beginning line and column.
  4. EC and EL are integers denoting the ending line and column.

I am currently trying to copy an instance of loc and modify the values (BL, BC, EC, EL). I'm having quite some trouble doing it. I've tried.

  1. Pattern matching a loc parameter using the syntax definition.
  2. Attempting to access fields as if they were qualified names.

Evidently none of these worked out. I am unable to find too much more about doing this in the Rascal documentation. Can I get any pointers as to how to access these values?

Thanks!


Solution

  • Accessing and modifying the fields of source locations is done in the same way as for values of other data types. See the documentation for Source Location for all fields that are available.

    Here is an example:

    rascal>l = |project://rascal/src/org/rascalmpl/library/lang/rascal/types/CheckTypes.rsc|(243216,14,<4598,8>,<4598,22>);
    loc: |project://rascal/src/org/rascalmpl/library/lang/rascal/types/CheckTypes.rsc|(243216,14,<4598,8>,<4598,22>)
    rascal>l.begin.line
    int: 4598
    rascal>l.begin.column
    int: 8
    rascal>l.begin.line=3;
    loc: |project://rascal/src/org/rascalmpl/library/lang/rascal/types/CheckTypes.rsc|(243216,14,<3,8>,<4598,22>)
    

    First I initialize l to some source location, next I access the line and column of the begin of this source location. Finally I set the begin line to 3.

    Hope this helps.