Class Lexer

java.lang.Object
org.clafer.frontend.Lexer

public final class Lexer extends Object
Splits the explicit form of a Clafer model, as AstPrinter prints it, into tokens.

Whitespace and line breaks only separate tokens: nesting is in braces, and indentation carries no meaning. Comments (// ... to the end of the line and /* ... */) are set aside with their spans, for tools that show or keep them.

Lines and columns start at 1. A column counts UTF-16 code units, as the Language Server Protocol does by default, so a tab is one column. A span ends after its last character.

The lexer does not stop at an error: an unexpected character or an unterminated string becomes an TokenKind.ERROR token with a diagnostic, and lexing goes on after it.

  • Method Details

    • lex

      public static Lexer.Result lex(String source)
      Splits a source text into tokens.
      Parameters:
      source - the source text
      Returns:
      the tokens, the comments and the errors
    • unescape

      public static String unescape(String token)
      Decodes a string token: drops the quotes and replaces the escapes.
      Parameters:
      token - the text of a TokenKind.STRING token
      Returns:
      the string it denotes