Search code examples
parsingcommentsrascal

Rascal fetch methods without comments


I'm looking for a way to avoid comments during parsing. Here's my issue.

First I fetch all methods from a M3 model, like so:

public set[loc] getMethodLocations(M3 model){
  locations = { <x,y> | <x,y> <- model@containment,
  x.scheme=="java+class",
  y.scheme=="java+method" ||
  y.scheme=="java+constructor" };
  set[loc] methodLocations = { a | a <- range(locations) };
  return methodLocations;
}

Then I want to iterate over the fetched locations, like so:

set[loc] AllMethodsAsLoc = getMethodLocations(model);

for( methodAsLoc <- AllMethodsAsLoc ) {
  MethodDec m = parse(#MethodDec, methodAsLoc);
};

My issue is that the parsing seems to fail with a ParseError when a fetched method has comments at the location. How can I not include comments when fetching or how can I ignore comments during the parsing?

I'm new at this and learning, so please excuse my ignorance.

Any help is appreciated.

Rob


Solution

  • Excellent question. Because MethodDec is not a "start" non-terminal it does not accept whitespace or comments before and after the actual MethodDec. So either we should trim the whitespace off somehow, or we could make a new non-terminal which can accept the layout.

    The latter solution is the better IMHO:

    start syntax MyTop = MethodDec method;
    
    start[MyTop] theTop = parse(start[MyTop], methodAsLoc);
    MyTop t = theTop.top
    MethodDec dec = t.method;
    
    // or more directly
    dec = parse(start[MyTop], methodAsLoc).top.method;