Search code examples
rascal

Constructing a location from a string


I receive the following string to my webserver: "|project://Detector/src/exporter.rsc|(1762,28,<45,10>,<45,38>)" using the toLocation() function to convert this to a location will result into an MalFormedURI error.

I can use the toLocation() function to convert the string loc myLoc = toLocation("project://Detector/src/exporter.rsc"); to a location. With a regular expression I can extract "0,10,<2,0>,<4,10>". But now the problem is I can do: myLoc(0,10,<2,0>,<4,10>), but I can't do myLoc("0,10,<2,0>,<4,10>"). I could extract all this position data with regular expressions and then add it to the location but that will result in nasty code.

What is the best practice in converting a string with position data eg. "|project://Detector/src/exporter.rsc|(1762,28,<45,10>,<45,38>)" to a location containing position data.


Solution

  • I think the most robust way is this:

    import ValueIO;
    str theString = "|project://Detector/src/exporter.rsc|(1762,28,\<45,10\>,\<45,38\>)";
    theLoc = readTextValueString(#loc, theString);
    

    The function may throw parse errors which you can catch and handle but I'm guessing that's not going to happen with your setup.