Search code examples
javarascal

M3: how to identify syntax errors in Java projects


I'm writing some analysis rules using the Java M3 model, loaded with createM3FromDirectory. If the Java code contains syntax errors, I want to abort and not run my analysis, because then the model might be in an inconsistent state.

M3.messages usually contains a lot of errors even for good projects, because of project dependencies like junit, which createM3FromDirectory can not take into account. These errors are fine, my analysis works anyway.

How can I reliably identify if Rascal encountered syntax errrors when loading a model?

For the moment I'm using the following helper function.

set[Message] syntaxErrors(M3 model) =
  { e
  | e:error(msg, _) <- model.messages
  , /^syntax error/i := msg
  };

Is that a reliable way?


Solution

  • Yes, that's reliable enough IMHO.

    As an aside, pattern matches can be arbitrarily nested, like so:

    set[Message] syntaxErrors(M3 model) =
      { e | e:error(/^syntax error/i, _) <- model.messages}
    

    As another aside, you can also pass a classpath to createM3FromDirectory in order to make sure the JUnit dependencies are properly resolved (and other dependencies). If you need to construct such a classpath for a maven project, you might want to try: https://github.com/cwi-swat/rascal-java-build-manager