regexrascalclair

How can I negate a regular expression?


I want to know a field if it does not match a certain string:

Case 1: When the previous node is not *_pcm.h, then l1 gives the path to file l2.

Case 2: When there is no l1 or case 1 is not true, then l is what I need

visit(sec) {
  // case 1
  case \sources(_, "_SOURCES", [*_, l1:\sourceList(_, _, _, p:/_pcm.h/i, _, _),
                l2:\sourceList(_, _, _, n:/_pcm.c/i, _, _), *_]): { 
                    name = determinePath(p, n, f); 
  }
  // case 2
  case \sources(_, "_SOURCES", [*_,  l:\sourceList(_, _, _, n:/_pcm.c/i, _, _) , *_]): {
                    path = getPath(f); 
  }
}

Instead of p:/_pcm.h/i I tried p:!/_pcm.h/i but this does not work


Solution

  • You can not directly express this negation inside a pattern, but here is a workaround by adding a when condition to the case in the case you are transforming (i.e., using => between pattern and action):

    data D = d1(str s) | d2(int n);
    
    D transform(D subject){
        return visit(subject){
                    case d1(str s) => d1(s + s) when /abc/ !:= s
        }
    }
    

    In the case of a more complex case, you can to the test and use a fail to let the case fail.

    case d1(str s): { if(/abc/ !:= s) fail; ... }
    

    Hope this solves your problem.