java.lang.Object
com.darkcollective.relix.lang.ast.ScriptPrinter

public final class ScriptPrinter extends Object
Renders a Script — or one Statement — back as .relix text.

The statement-level counterpart of PrettyPrinter, which does the same for a relational expression and which this delegates to for every RelNode and Operand it reaches.

The property this exists for

Printing is only useful if the text reads back as the same script: parse(print(s)) must equal s, modulo source locations. That is what makes a script a thing a program can hold, transform and hand back — and it is the same promise PrettyPrinter already carries for an expression, where it is load-bearing enough that AstEquivalence compares two nodes through it.

Without this, a session assembled by building an AST has no text form at all: the REPL's :save works only because it replays the source it captured, which a script that was never parsed does not have.

Formatting is not part of the promise

The output is normalised, not preserved: one statement per line, and a canonical spelling of each keyword. What is promised is that the text reads back as the same script, never that it matches the text someone originally wrote.

Comments

A parsed script keeps its comments (Script.comments()), and printing it puts each one back by the code it was next to: on its own line before the statement it preceded, after the ; it followed on the same line, or — for one inside a statement — immediately before the expression it preceded. A comment comes back as it was written, and in the order it was written; the code around it is normalised as ever. A blank line between statements is kept, as one. A comment after a row of a Markdown inline table comes back after that row. Inside a statement with no expression to stand by, a comment is printed just before the ;, which keeps it inside the statement.

Spelling

Operators are written as glyphs by default (σ, ⋈). With Spelling.KEYWORDS they are written in ASCII (SELECT, JOIN), the form language/spellings.md recommends for generated code; either form parses back to the same script.

  • Method Details

    • print

      public static String print(Script script)
      Renders a whole script, namespace first, one statement per line, its comments with it, and every operator as a glyph.
      Parameters:
      script - the script to render; must not be null
      Returns:
      the .relix text
    • print

      public static String print(Script script, Spelling spelling)
      Renders a whole script, namespace first, one statement per line, its comments with it, and every operator as spelling spells it.
      Parameters:
      script - the script to render; must not be null
      spelling - glyphs or ASCII keywords; must not be null
      Returns:
      the .relix text
      Since:
      1.0
    • print

      public static String print(Statement statement)
      Renders one statement, including its trailing semicolon.
      Parameters:
      statement - the statement to render; must not be null
      Returns:
      the .relix text for that statement
    • print

      public static String print(Statement statement, Spelling spelling)
      Renders one statement, including its trailing semicolon, every operator as spelling spells it.
      Parameters:
      statement - the statement to render; must not be null
      spelling - glyphs or ASCII keywords; must not be null
      Returns:
      the .relix text for that statement
      Since:
      1.0