Skip to main content

Parser

Struct Parser 

Source
pub struct Parser(/* private fields */);
Expand description

High-level entry point for parsing SQLite SQL into typed AST statements.

Use this in most applications.

  • Hides grammar setup and returns SQLite SQL-native result types.
  • Reusable across many SQL inputs.
  • Supports batch/script parsing via parse.
  • Supports editor-style token feeds via incremental_parse.

Advanced generic APIs exist in crate::typed and crate::any.

Implementations§

Source§

impl Parser

Source

pub fn new() -> Self

Create a parser for the SQLite grammar with default configuration.

Source

pub fn with_config(config: &ParserConfig) -> Self

Create a parser for the SQLite grammar with custom configuration.

Source

pub fn register_macro(&mut self, name: &str, params: &[&str], body: &str)

Register a template macro with the parser.

The macro body uses $param placeholders (e.g. "$x + 1"). All strings are copied; the caller may free them after this call returns.

Source

pub fn deregister_macro(&mut self, name: &str) -> bool

Deregister a macro by name. Returns true if it was found and removed.

Source

pub fn parse(&self, source: &str) -> ParseSession

Parse a SQL script and return a statement-by-statement session.

§Examples
use syntaqlite_syntax::{ParseErrorKind, Parser};

let parser = Parser::new();
let mut session = parser.parse("SELECT 1; SELECT FROM;");
let mut ok_count = 0;

loop {
    match session.next() {
        syntaqlite_syntax::ParseOutcome::Ok(stmt) => {
            ok_count += 1;
            let _ = stmt.root();
        }
        syntaqlite_syntax::ParseOutcome::Err(err) => {
            assert!(!err.message().is_empty());
            if err.kind() == ParseErrorKind::Fatal {
                break;
            }
        }
        syntaqlite_syntax::ParseOutcome::Done => break,
    }
}

assert!(ok_count >= 1);
§Panics

Panics if another session from this parser is still active. Drop the previous session before starting a new one.

Source

pub fn incremental_parse(&self, source: &str) -> IncrementalParseSession

Start an incremental parse session for token-by-token input.

This mode is intended for IDEs, completion engines, and other workflows where SQL is consumed progressively.

§Examples
use syntaqlite_syntax::{Parser, TokenType};

let parser = Parser::new();
let mut session = parser.incremental_parse("SELECT 1");

assert!(session.feed_token(TokenType::Select, 0..6).is_none());
assert!(session.feed_token(TokenType::Integer, 7..8).is_none());

let stmt = session.finish().and_then(Result::ok).unwrap();
let _ = stmt.root();
§Panics

Panics if another session from this parser is still active. Drop the previous session before starting a new one.