3 releases

0.1.2 Jan 1, 2024
0.1.1 Jan 1, 2024
0.1.0 Dec 30, 2023

#1439 in Database interfaces


Used in steeldb

Apache-2.0

7KB
62 lines

Documentation:

https://docs.rs/steeldb-parser/latest/steeldb_parser/

Source code:

https://github.com/paolorechia/steeldb/tree/main/steeldb-parser


lib.rs:

SteelDB Parser

This crate exposes functions that parse a subset of SQL, used by the SteelDB project.

You can find more information about the Database here: https://github.com/paolorechia/steeldb

Since this is still work in progress, not much is implemented.

Currently, the only exposed function is [parse_select], which takes an input string and returns the columns that were given in the SELECT clause.

This crate relies on lalrpop library: https://github.com/lalrpop/lalrpop

Examples

Good examples of this crate usage are found in the unit tests in lib.rs For instance:

    #[test]
    fn test_parse_select() {
        let result = parse_select("select brigadeiro, churros;".to_string()).unwrap();
        let v = vec!["brigadeiro".to_string(), "churros".to_string()];
        assert_eq!(v, result);
    }

Grammar Files

Note that lalrpop reads a file of the format .lalrpop where the parser grammar is defined, and generated during compilation-time the actual parser code, which is not displayed in the source code repository.

Here's the first implementation of the select clause:

grammar(v: &mut Vec<String>);

pub Select: () = {
    SELECT <c:Columns> SEMICOLON => {}
};

Columns: () = {
    <l:LITERAL> => v.push(l),
    Columns "," <l:LITERAL> => {
        v.push(l);
    }
}

SELECT: String = <s:r"select "> => s.to_string();
LITERAL: String = <s:r"[a-z\*_0-9]+"> => s.to_string();
SEMICOLON: String = <s:r";"> => s.to_string();

Dependencies

~2.7–5MB
~83K SLoC