Class ClaferLexer

java.lang.Object
org.clafer.frontend.ClaferLexer

public final class ClaferLexer extends Object
Splits a model in the full Clafer language into tokens, as the compiler's lexer does (clafer.cf, generated by BNFC and Alex).

The rules are the compiler's, and so is the longest match:

  • a name starts with a letter, and goes on with letters, digits, _ and '; a reserved word, e.g., some or X, is a TokenKind.KEYWORD. A letter is an ASCII letter or a Latin-1 letter, À to ÿ without × and ÷;
  • a number is an integer, a real with digits on both sides of the point, or a double, a real with an exponent (1.5e-3);
  • a string has the escapes \" \\ \n \t, and may span lines;
  • comments (// to the end of the line, /* */) and escapes ([alloy| |], [choco| |]) are set aside, with their spans.
The compiler's character classes are those of Latin-1, so a character after U+00FF is an error anywhere, in a string or a comment too. A column counts characters, and a tab moves to the next column after a multiple of 8, as in the compiler's messages.

The lexer does not stop at an error, as the compiler's does: an unexpected character is reported and skipped, and an unterminated string or comment is reported and ends the input.

  • Method Details

    • lex

      public static ClaferLexer.Result lex(String source)
      Splits a model into tokens.
      Parameters:
      source - the text of the model
      Returns:
      the tokens, the comments and escapes, and the errors