Class ClaferParser

java.lang.Object
org.clafer.frontend.ClaferParser

public final class ClaferParser extends Object
Parses a model in the full Clafer language into its Syntax tree, as the compiler parses it (clafer.cf, generated by BNFC and Happy, and Language.Clafer.parse).

It is a recursive descent over the grammar, one method per level of expression, which takes the choices that Happy takes:

  • an expression goes on as long as it can, so that in [a -b] the - is a subtraction, and a not must go on with in; a list of expressions has no separator;
  • a quantifier starts a declaration, some x; y : X | e, when disj, or a name and ; or :, follow it;
  • a cardinality is taken whenever one can start, e.g., the 2..3 of a 2..3 b is the card of a;
  • a clafer must start on a new line, after its previous sibling clafer.
The layout is resolved first (Layout), so the parser reads blocks as braces, and lines of siblings with a separator between them.

The parser does not stop at an error: it reports it, skips to the next line at the same or a lower indentation, and goes on, so that a model with errors on several lines reports all of them, and the tree has the rest.

  • Method Details

    • parse

      public static ClaferParser.Result parse(String source)
      Parses a model.
      Parameters:
      source - the text of the model
      Returns:
      its tree, comments and errors