partiql-syntax

    A PartiQL parser for MoonBit.

    partiql
    sql
    query
    parser
    ast
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    5 hours ago
    Downloads
    3

    #moonrockz/partiql-syntax

    A PartiQL parser and printer for MoonBit: queries (DQL), including graph MATCH (GPML), into a typed syntax tree with source spans, and back to canonical text.

    let statement = @syntax.parse("SELECT a FROM t WHERE a > 1")

    print writes a tree back as canonical text, and format does parse and print for a whole script, keeping its comments:

    let text = @syntax.format("select a from t -- note")
    // text == "SELECT a FROM t;\n-- note\n"

    With a width, both lay the text out to fit that line width:

    let text = @syntax.format("select a, b from t where x = 1", width=20)
    // text == "SELECT a, b\nFROM t\nWHERE x = 1;\n"

    parse_script_recovering does not stop at the first error: it returns the partial script, with Error nodes where the source is broken, and every error in source order:

    let r = @syntax.parse_script_recovering("SELECT a, , b FROM t; SELECT 1")
    // r.errors.length() == 1, and r.script holds both statements

    PackageContent
    moonrockz/partiql-syntaxparse(String) -> Statement raise ParseError, parse_script_recovering
    moonrockz/partiql-syntax/astthe syntax tree, Span, ParseError
    moonrockz/partiql-syntax/lexerlex(String) -> Array[Token], lex_recovering
    moonrockz/partiql-syntax/parserparse_statement, parse_expression, parse_script, parse_script_recovering
    moonrockz/partiql-syntax/printerprint, print_script

    Errors: ParseError::Syntax(message~, span~, expected~, found~) for invalid input. ParseError::Unsupported reports valid PartiQL that a later version parses: DML and DDL statements such as INSERT or CREATE. Span::line_column(source) gives 1-based line and column numbers.

    render prints a parse error for people, and to_sexp prints a tree as an S-expression:

    let source = "SELECT FROM t"
    let text = try {
    ignore(@syntax.parse(source))
    ""
    } catch {
    e => @syntax.render(e, source, name="query.sql")
    }
    // error: expected an expression, found keyword `FROM`
    // --> query.sql:1:8

    Input nested more than 100 levels deep (parentheses, subqueries, collections, graph groups) is a Syntax error, "nesting is too deep", on every target.

    See docs/architecture.md and docs/conformance.md in the repository.

    Comment

    A comment as written: -- note (without the line break) or /* note */.

    ParseError

    Why a statement could not be parsed.

    Position

    A 1-based line and a 1-based column counted in code points.

    Recovered

    The result of parse_script_recovering: the partial script and every error, in source order (spec M3d §3).

    Script

    The statements of a script, and the comments of its source in source order.

    Span

    A range of source text: UTF-16 code unit offsets, end exclusive.

    Statement

    A parsed statement. M2 parses queries (DQL).

    format

    fn format(source : String, width? : Int) -> String raise
    ParseError

    Parses source as a script and prints it, keeping the comments: canonical without width, the pretty layout at width with it.

    parse

    Parses one PartiQL statement: a query, with an optional final ;. Raises ParseError::Syntax for invalid input and ParseError::Unsupported for valid input that this version does not parse yet (DML and DDL statements such as INSERT or CREATE, which later milestones parse).

    parse_script

    Parses a script: statements separated by ;, with its comments.

    parse_script_recovering

    fn parse_script_recovering(source : String) ->
    Recovered

    Parses a script and recovers from syntax errors: the partial script and every error, in source order (spec M3d).

    position

    fn position(source : String, offset : Int) ->
    Position

    The 1-based line and code-point column of a UTF-16 offset.

    print

    fn print(statement :
    Statement
    , width? : Int) -> String

    The text of a statement (no comments, no final ;): canonical without width, the pretty layout at width with it.
    fn print_script(script :
    Script
    , width? : Int) -> String

    The text of a script, with its comments: canonical without width, the pretty layout at width with it.

    render

    fn render(error :
    ParseError
    , source : String, name~ : String, color? : Bool) -> String

    The rendered diagnostic of a parse error (spec M3c §5).

    to_sexp

    fn to_sexp(statement :
    Statement
    , width? : Int) -> String

    The S-expression of a statement (spec M3c §7).

    Source Files