a nom parser combinator that matches a psql statement.

Overview

psql_splitter

a nom parser combinator that matches a psql statement.

Postgres has a dialect of SQL that I'm going to call pgsql. Postgres also has a client, psql, which recognizes meta-commands and inline-copy syntax that normal pgsql does not. This parser recognizes psql meta-commands and pgsql tokens, allowing the statement combinator to know when a statement has ended. The statement combinator also attaches color commentary on the end of a line to the previous statement, so select 1; -- take a number is matched as one statement.

Status

This is a hack with some tests, a README, and a license.

This parser doesn't understand all the tokens in pgsql and psql, which has its pros:

  • psql_splitter::statement is more likely to split statements out of invalid psql
  • a simpler parser is easier to maintain

but also its cons:

  • a simpler parser is more likely to be missing something about the enormous pgsql language

Scope/features

  • the splitter should match invalid psql, always splitting the input into one or more statements
  • the splitter should treat psql meta-commands as statement terminators, e.g select 1 as foo \gset and \d my_table would both be complete statements.
  • the splitter should ignore statement-terminators inside pgsql strings and parentheses and exotic psql such as \if ... \endif and COPY table_name FROM STDIN; ... \.
  • the splitter should somewhat-reliably split psql and pgsql statements as defined by postgres, but not neccessarily other sql-like dialects derived from postgres's psql and pgsql.
  • the splitter should be correct. Speed, a small binary, and memory efficiency aren't goals, but they're nice to have.

Contributing

If you find a bug, please submit an issue with a reproduction case. PRs for tooling would also be welcome.

License

While this library is offered under the attached BSD 3-clause at ./LICENSE, ./src/*.sql are excerpts from the postgres codebase, and so are covered by the postgres license.

Prior art

libpg_query's split_with_scanner codeschool/sqlite-parser's test-suite-extraction tools

You might also like...
Lua bytecode parser written in Rust using nom, part of metaworm's lua decompiler

luac-parser (中文) lua字节码解析器, 目前支持 lua51, lua53, lua54 这是目前效果最好的lua反编译器 metaworm's luadec 的一部分 可以基于此代码定制你所需的lua字节码解析器,编译成WASM,让metaworm's luadec加载使用,来反编

A SQL query parser written using nom.

sqlparser-nom A SQL query parser written using nom. Query Select From Where Order by Limit CTE Group by Having Aggregate Window Pratt Parsing Friendly

Utility to quickly setup Starcraft Broodwar matches between 2 or more bots

BWAIShotgun Utility to quickly setup Starcraft Broodwar matches between 2 or more bots Be aware that all bots will be executed directly, without any l

SHA256 sentence: discover a SHA256 checksum that matches a sentence's description of hex digit words.

SHA256 sentence "The SHA256 for this sentence begins with: one, eight, two, a, seven, c and nine." Inspired by @lauriewired post Inspired by @humbleha

Reef: A zkSNARK system for proving that a committed document matches a regex

Reef This is an implementation of Reef, a system for generating zero-knowledge proofs that a committed document matches or does not match a regular ex

Rust experiments involving Haskell-esque do notation, state, failure and Nom parsers!

Introduction As a long time Haskell developer recently delving into Rust, something I've really missed is monads and do notation. One thing monadic do

Tutorial for parsing with nom 5.

Nom Tutorial Nom is a wonderful parser combinators library written in Rust. It can handle binary and text files. Consider it where you would otherwise

A procedural macro for defining nom combinators in simple DSL

A procedural macro for defining nom combinators in simple DSL

Example of using nom parsers from a proc macro

Example of using nom parsers from a proc macro This project is organised as 3 crates: nom_macro is the main project, exposing the proc macro and the g

Command-line DNS client using bitvec, nom and RFC 1035

Dingo Domain INformation Gatherer, Obviously. Installation Install cargo, see instructions on the Rust website Run ./install.sh (it just does cargo bu

nombytes is a library that provides a wrapper for the bytes::Bytes byte container for use with nom.

NomBytes nombytes is a library that provides a wrapper for the bytes::Bytes byte container for use with nom. I originally made this so that I could ha

A repo which serves as a learning ground for the Nom crate.

Fun with Nom This repo is created as a learning resource for the Nom crate for Rust. Nom is a parser combinators library. It can be used to parse just

Website for Microformats Rust parser (using 'microformats-parser'/'mf2')

Website for Microformats Rust parser (using 'microformats-parser'/'mf2')

A language parser tool to build recursive descent top down parser.

lang-pt A language parser tool to generate recursive descent top down parser. Overview Parsers written for the languages like Javascript are often cus

Djotters is a Djot parser and translater written via parser combinators, in rust.

🎭 Djotters Turning your Djot (markdown) into lovely HTML! Djotters is here to let you draft up a document and watch it render in real time. If you wa

Rust DLT (Diagnostic Log and Trace) packet parser

dlt_parse A zero allocation rust library for basic parsing & writing DLT (Diagnostic Log and Trace) packets. Currently only the parsing and writing of

Rust dbc parser

can-dbc A CAN-dbc format parser written with Rust's nom parser combinator library. 1. Example Read dbc file and generate Rust structs based on the mes

A full featured, fast Command Line Argument Parser for Rust

clap Command Line Argument Parser for Rust It is a simple-to-use, efficient, and full-featured library for parsing command line arguments and subcomma

Docopt for Rust (command line argument parser).

THIS CRATE IS UNMAINTAINED This crate is unlikely to see significant future evolution. The primary reason to choose this crate for a new project is if

Owner
Steven Kalt
I'm a full-stack engineer interested in data science, GIS, network analysis, and renewable energy. He/him.
Steven Kalt
Implements the packet parser for Gran Turismo 7 telemetry data, allowing a developer to retrieve data from a running game.

gran-turismo-query Implements the packet parser for Gran Turismo 7 telemetry data, allowing a developer to retrieve data from a running game. Features

Carlos Menezes 3 Dec 11, 2023
Yet Another Parser library for Rust. A lightweight, dependency free, parser combinator inspired set of utility methods to help with parsing strings and slices.

Yap: Yet another (rust) parsing library A lightweight, dependency free, parser combinator inspired set of utility methods to help with parsing input.

James Wilson 117 Dec 14, 2022
Rust parser combinator framework

nom, eating data byte by byte nom is a parser combinators library written in Rust. Its goal is to provide tools to build safe parsers without compromi

Geoffroy Couprie 7.6k Jan 7, 2023
A fast monadic-style parser combinator designed to work on stable Rust.

Chomp Chomp is a fast monadic-style parser combinator library designed to work on stable Rust. It was written as the culmination of the experiments de

Martin Wernstål 228 Oct 31, 2022
A parser combinator library for Rust

combine An implementation of parser combinators for Rust, inspired by the Haskell library Parsec. As in Parsec the parsers are LL(1) by default but th

Markus Westerlind 1.1k Dec 28, 2022
A friendly parser combinator crate

Chumsky A friendly parser combinator crate that makes writing LL-1 parsers with error recovery easy. Example Here follows a Brainfuck parser. See exam

Joshua Barretto 2.4k Jan 8, 2023
A parser combinator for parsing &[Token].

PickTok A parser combinator like nom but specialized in parsing &[Token]. It has similar combinators as nom, but also provides convenient parser gener

Mikuto Matsuo 6 Feb 24, 2023
A parser combinator that is fully statically dispatched and supports both sync/async.

XParse A parser combinator that is fully statically dispatched and supports both sync & async parsing. How to use An example of a very simple JSON par

Alsein 4 Nov 27, 2023
Interactive interpreter for a statement-based proof-of-concept language.

nhotyp-lang Nhotyp is a conceptual language designed for ease of implementation during my tutoring in an introductive algorithmic course at Harbin Ins

Geoffrey Tang 5 Jun 26, 2022
Rust macro to use a match-like syntax as a elegant alternative to nesting if-else statement

cond Rust macro to use a match-like syntax as an elegant alternative to many if-else statements. I got the idea from empty Go switch statements. I tho

CheckM4te 3 Nov 23, 2023