Skip to content

Commit dcefa3a

Browse files
committed
syntax: share native parsers' Gap and Parsed; checker picks the local parser per dialect
Gap and Parsed move from sqlquack-syntax-postgres into sqlquack-syntax so a second native dialect parser returns the same shape; the Postgres crate re-exports them unchanged. The receipt checker maps each LocalParser to its verdict through local_verdict, and parse-worker takes --parser instead of --postgres, so a new dialect parser is one enum variant and one match arm.
1 parent f2d93ee commit dcefa3a

13 files changed

Lines changed: 64 additions & 51 deletions

File tree

‎crates/sqlquack-syntax-coverage/src/cli/_helpers/commands.rs‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ use std::path::{Path, PathBuf};
77
use std::process::{Command, ExitCode};
88
use std::time::Duration;
99

10+
use clap::ValueEnum;
1011
use serde_json::{Value, json};
1112

1213
use crate::check::main::check_coverage::check_coverage;
1314
use crate::check::models::{
1415
CheckResult, FormResult, LocalFailure, LocalVerdict, ObligationResult, Reason,
1516
};
16-
use crate::cli::_helpers::local::{
17-
frozen_catalogue, parser_tree, postgres_verdict, snowflake_verdict,
18-
};
17+
use crate::cli::_helpers::local::{frozen_catalogue, local_verdict, parser_tree};
1918
use crate::cli::_helpers::output::{print, say};
2019
use crate::cli::_helpers::worker::Worker;
2120
use crate::cli::models::{Common, TranslationArguments};
@@ -53,10 +52,12 @@ const PARSE_MEMORY_KIB: u64 = 4 * 1024 * 1024;
5352

5453
/// The worker command: this binary, its memory bounded by the shell before it starts.
5554
fn worker_command(binary: PathBuf, local_parser: LocalParser) -> Box<dyn Fn() -> Command> {
56-
let worker: &str = match local_parser {
57-
LocalParser::Postgres => "parse-worker --postgres",
58-
LocalParser::Snowflake | LocalParser::None => "parse-worker",
59-
};
55+
let worker: String = format!(
56+
"parse-worker --parser {}",
57+
local_parser
58+
.to_possible_value()
59+
.map_or_else(String::new, |value| value.get_name().to_owned())
60+
);
6061
Box::new(move || {
6162
let mut command = Command::new("sh");
6263
command
@@ -101,9 +102,10 @@ fn checked(coverage: &SyntaxCoverage) -> Result<CheckResult, ExitCode> {
101102
worker_command(binary, local_parser),
102103
PARSE_BOUND,
103104
));
104-
let check: CheckResult = check_coverage(coverage, tree.as_deref(), &|sql| match local_parser {
105-
LocalParser::Snowflake | LocalParser::Postgres => worker.borrow_mut().verdict(sql),
106-
LocalParser::None => LocalVerdict::Failed {
105+
let parses: bool = local_verdict(local_parser).is_some();
106+
let check: CheckResult = check_coverage(coverage, tree.as_deref(), &|sql| match parses {
107+
true => worker.borrow_mut().verdict(sql),
108+
false => LocalVerdict::Failed {
107109
failure: LocalFailure::NoParser,
108110
},
109111
});
@@ -521,11 +523,8 @@ pub(crate) fn diff(
521523
}
522524

523525
/// Reads one JSON string of SQL per line and prints the local verdict on each, flushed.
524-
pub(crate) fn parse_worker(postgres: bool) -> Result<ExitCode, ExitCode> {
525-
let verdict_of: fn(&str) -> LocalVerdict = match postgres {
526-
true => postgres_verdict,
527-
false => snowflake_verdict,
528-
};
526+
pub(crate) fn parse_worker(parser: LocalParser) -> Result<ExitCode, ExitCode> {
527+
let verdict_of: fn(&str) -> LocalVerdict = local_verdict(parser).ok_or(ExitCode::FAILURE)?;
529528
let stdout = std::io::stdout();
530529
for line in std::io::stdin().lock().lines() {
531530
let line: String = line.map_err(|_| ExitCode::FAILURE)?;

‎crates/sqlquack-syntax-coverage/src/cli/_helpers/local.rs‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
33
use std::process::Command;
44

5-
use sqlquack_syntax::{CallMode, Dialect, Expression, ExpressionWalk, ParseOptions};
5+
use sqlquack_syntax::{CallMode, Dialect, Expression, ExpressionWalk, ParseOptions, Parsed};
66

77
use crate::check::models::{LocalFailure, LocalVerdict};
88
use crate::formats::constants::SNOWFLAKE_DIALECT;
99
use crate::formats::main::content_sha256::content_sha256;
1010
use crate::formats::main::current_dialect::current_dialect;
11+
use crate::formats::models::LocalParser;
1112

1213
/// The code prefix of every complexity-guard error, which is never a syntax verdict.
1314
const GUARD_PREFIX: &str = "E_GUARD_";
@@ -28,7 +29,7 @@ fn accepted(statements: &[Expression], gapped: bool) -> LocalVerdict {
2829
}
2930

3031
/// SQLQuack's Snowflake verdict; a Command or Raw node anywhere makes the parse opaque.
31-
pub(crate) fn snowflake_verdict(sql: &str) -> LocalVerdict {
32+
fn snowflake_verdict(sql: &str) -> LocalVerdict {
3233
match sqlquack_syntax::parse_statements_with(
3334
sql,
3435
Dialect::Snowflake,
@@ -45,14 +46,28 @@ pub(crate) fn snowflake_verdict(sql: &str) -> LocalVerdict {
4546
}
4647
}
4748

48-
/// SQLQuack's strict PostgreSQL verdict: a gap makes it opaque, and any refusal rejects it.
49-
pub(crate) fn postgres_verdict(sql: &str) -> LocalVerdict {
50-
match sqlquack_syntax_postgres::parse_strict(sql) {
49+
/// A native dialect parser's strict verdict: a gap makes it opaque, and any refusal rejects it.
50+
fn native_verdict<E>(parsed: Result<Parsed, E>) -> LocalVerdict {
51+
match parsed {
5152
Ok(parsed) => accepted(&parsed.statements, !parsed.gaps.is_empty()),
5253
Err(_) => LocalVerdict::Rejected,
5354
}
5455
}
5556

57+
/// SQLQuack's strict PostgreSQL verdict.
58+
pub(crate) fn postgres_verdict(sql: &str) -> LocalVerdict {
59+
native_verdict(sqlquack_syntax_postgres::parse_strict(sql))
60+
}
61+
62+
/// The verdict of each dialect's local parser, or none when the dialect has no parser yet.
63+
pub(crate) fn local_verdict(parser: LocalParser) -> Option<fn(&str) -> LocalVerdict> {
64+
match parser {
65+
LocalParser::Snowflake => Some(snowflake_verdict),
66+
LocalParser::Postgres => Some(postgres_verdict),
67+
LocalParser::None => None,
68+
}
69+
}
70+
5671
/// Runs git; its exit code, and its trimmed stdout, or why it could not run.
5772
pub(crate) fn git(arguments: &[&str]) -> Result<(i32, String), String> {
5873
match Command::new("git").args(arguments).output() {

‎crates/sqlquack-syntax-coverage/src/cli/main/run_cli.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn run_cli() -> ExitCode {
4141
&common,
4242
),
4343
Command::Diff { base, head, common } => diff(&base, head.as_deref(), &common),
44-
Command::ParseWorker { postgres } => parse_worker(postgres),
44+
Command::ParseWorker { parser } => parse_worker(parser),
4545
};
4646
result.unwrap_or_else(|code| code)
4747
}

‎crates/sqlquack-syntax-coverage/src/cli/models.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::path::PathBuf;
55
use clap::{Args, Parser, Subcommand};
66

77
use crate::formats::constants::COVERAGE_ROOT;
8+
use crate::formats::models::LocalParser;
89

910
#[derive(Debug, Parser)]
1011
#[command(
@@ -103,9 +104,9 @@ pub enum Command {
103104
/// Internal: one verdict line per SQL line on stdin, for the isolated local parse.
104105
#[command(hide = true)]
105106
ParseWorker {
106-
/// Parse with the PostgreSQL parser instead of the Snowflake one.
107-
#[arg(long)]
108-
postgres: bool,
107+
/// The dialect parser that gives the verdicts.
108+
#[arg(long, value_enum)]
109+
parser: LocalParser,
109110
},
110111
/// What conversion makes of every case Snowflake parsed, by form and family (decision D107).
111112
Translation {

‎crates/sqlquack-syntax-coverage/src/formats/models.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,8 @@ pub struct Dialect {
634634
pub syntax_codes: Vec<String>,
635635
}
636636

637-
/// The parser the checker compares receipts with.
638-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
637+
/// The parser the checker compares receipts with; `local_verdict` maps each to its verdict.
638+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize, clap::ValueEnum)]
639639
#[serde(rename_all = "snake_case")]
640640
pub enum LocalParser {
641641
/// `sqlquack_syntax` in the Snowflake dialect, through the isolated worker.

‎crates/sqlquack-syntax-postgres/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ pub use crate::parse::main::format_type::format_type;
1111
pub use crate::parse::main::parse::parse;
1212
pub use crate::parse::main::parse_strict::parse_strict;
1313
pub use crate::parse::main::parse_type::parse_type;
14-
pub use crate::parse::models::{Gap, Parsed};
14+
pub use sqlquack_syntax::{Gap, Parsed};

‎crates/sqlquack-syntax-postgres/src/parse/_helpers/tree/cursor.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use sqlquack_syntax::expressions::names::models::Identifier;
55
use crate::keywords::models::KeywordCategory;
66
use crate::parse::constants::{EXPRESSION_KEYWORDS, QUERY_STARTS};
77
use crate::parse::errors::ParseError;
8-
use crate::parse::models::Gap;
98
use crate::scan::models::{Token, TokenKind};
9+
use sqlquack_syntax::Gap;
1010

1111
pub(crate) type Parsing<T> = Result<T, ParseError>;
1212

‎crates/sqlquack-syntax-postgres/src/parse/main/parse.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use sqlquack_syntax::Expression;
55
use crate::parse::_helpers::tree::cursor::Parser;
66
use crate::parse::_helpers::tree::nodes::command;
77
use crate::parse::errors::ParseError;
8-
use crate::parse::models::Parsed;
98
use crate::scan::main::scan::scan;
9+
use sqlquack_syntax::Parsed;
1010

1111
/// Every statement of `sql`; a statement holding a construct the shared tree lacks is a `Command`.
1212
pub fn parse(sql: &str) -> Result<Parsed, ParseError> {

‎crates/sqlquack-syntax-postgres/src/parse/main/parse_strict.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use crate::parse::errors::StrictFailure;
44
use crate::parse::main::parse::parse;
5-
use crate::parse::models::Parsed;
65
use crate::scan::main::uncovered::uncovered;
6+
use sqlquack_syntax::Parsed;
77

88
/// `parse`'s statements, refused on any parse error, whatever its SQLSTATE, or uncovered byte.
99
pub fn parse_strict(sql: &str) -> Result<Parsed, StrictFailure> {

‎crates/sqlquack-syntax-postgres/src/parse/mod.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pub(crate) mod _helpers;
44
pub(crate) mod constants;
55
pub mod errors;
66
pub mod main;
7-
pub mod models;
87

98
#[cfg(test)]
109
mod tests;

0 commit comments

Comments
 (0)