ADR-0024 Phase B: DDL commands without value literals
Migrate the five DDL commands at four entry words: drop (drop
table / drop column / drop relationship), add (add column /
add 1:n relationship), rename (rename column), change (change
column). The walker route now owns these end-to-end; chumsky
declarations remain unreachable for these inputs but stay
until Phase F.
Walker extensions:
- New node kinds: NumberLit (with optional content validator)
and Literal(&str) (verbatim byte sequence with word-boundary
lookahead — used for the `1` in `add 1:n …` so it surfaces
as `\`1\`` in the expected-set, matching the existing
parse_error_pedagogy contract).
- Flag (--name) terminal — Phase A stubbed; now wired to the
walker driver with consume_flag() in lex_helpers.
- Repeated combinator with optional separator and `min` floor.
Used by referential clauses (0..2 `on <delete|update>` runs)
and change-column flags (0..N --force-conversion /
--dont-convert; AST builder enforces mutual exclusion).
- Optional now propagates its inner's expectations as a
`skipped` field on the Matched result. Seq accumulates these
across children so the next failure's expected-set surfaces
the full union — closes the keyword-completion regression
(`add column ` must offer `to`, `table`, plus the table-name
identifier slot).
- Expectation::Ident gained a `source: IdentSource` field; the
parser-side bridge maps Tables/Columns/Relationships/Types
to the IdentSlot::expected_label strings ("table name",
"column name", …) so the existing completion engine's
schema-cache lookup still resolves.
- Walker error wording now includes "after `<consumed>`,
expected …" framing — matches the chumsky-side test
contract for structural errors mid-shape.
- AST-builder validation errors now propagate as
WalkOutcome::ValidationFailed (not the generic "AST builder
failed" fallback), so `change column … --force-conversion
--dont-convert` and repeated `on delete` clauses surface
their friendly catalog wording verbatim.
Grammar additions:
- src/dsl/grammar/shared.rs: type-name validator (TYPE_VALIDATOR
uses Type::from_str via parse.custom.unknown_type catalog),
qualified_column sub-grammar, referential action keyword
(`cascade`/`restrict`/`set null`/`no action`), repeated
on-clauses.
- src/dsl/grammar/ddl.rs: drop/add/rename/change CommandNodes
with inline shapes (per-use-site `role` annotations let the
AST builder discriminate parent vs child columns, etc.).
The four entry words each have one CommandNode whose `shape`
is a Choice across sub-forms.
Tests:
- 14 new walker-specific tests covering all DDL forms (bare
drop table, drop column with optional connectives, drop
relationship by name and by endpoints, add column with type
validator, rename column, change column with each flag form
+ mutual-exclusion check, add 1:n relationship minimal /
full, repeated-clause-twice rejection).
- Total: 819 passed, 0 failed, 1 ignored (was 805 / 1).
- cargo clippy --all-targets -- -D warnings clean.
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
//! Shared sub-grammars for the DDL/DML migrations
|
||||
//! (ADR-0024 §architecture, §sub-grammars).
|
||||
//!
|
||||
//! Phase B uses these for relationship endpoints and referential
|
||||
//! actions; Phase D extends with `where_clause`,
|
||||
//! `column_value_list`, and the typed value slots.
|
||||
|
||||
use crate::dsl::grammar::{IdentSource, IdentValidator, Node, ValidationError, Word};
|
||||
use crate::dsl::types::Type;
|
||||
use std::str::FromStr;
|
||||
|
||||
// --- Type-name validator ------------------------------------------
|
||||
|
||||
/// Reject any identifier that isn't a known user-facing type name.
|
||||
///
|
||||
/// Mirrors the chumsky-side `Type::from_str` + `UnknownType`
|
||||
/// flow — surfaces the same `parse.custom.unknown_type` catalog
|
||||
/// wording with `{found}` and `{expected}` args.
|
||||
pub fn validate_type_name(value: &str) -> Result<(), ValidationError> {
|
||||
if Type::from_str(value).is_ok() {
|
||||
Ok(())
|
||||
} else {
|
||||
let expected = Type::all()
|
||||
.iter()
|
||||
.map(|t| t.keyword())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
Err(ValidationError {
|
||||
message_key: "parse.custom.unknown_type",
|
||||
args: vec![
|
||||
("found", value.to_string()),
|
||||
("expected", expected),
|
||||
],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub const TYPE_VALIDATOR: IdentValidator = validate_type_name;
|
||||
|
||||
// --- Type-slot leaf -----------------------------------------------
|
||||
|
||||
/// `Ident` slot for a column type. Validation runs after a
|
||||
/// successful identifier-shape match.
|
||||
pub const TYPE_SLOT: Node = Node::Ident {
|
||||
source: IdentSource::Types,
|
||||
role: "type",
|
||||
validator: Some(TYPE_VALIDATOR),
|
||||
highlight_override: None,
|
||||
};
|
||||
|
||||
// --- Qualified column reference (`<Table>.<Column>`) --------------
|
||||
|
||||
const QUALIFIED_COLUMN_NODES: &[Node] = &[
|
||||
Node::Ident {
|
||||
source: IdentSource::Tables,
|
||||
role: "table_name",
|
||||
validator: None,
|
||||
highlight_override: None,
|
||||
},
|
||||
Node::Punct('.'),
|
||||
Node::Ident {
|
||||
source: IdentSource::Columns,
|
||||
role: "column_name",
|
||||
validator: None,
|
||||
highlight_override: None,
|
||||
},
|
||||
];
|
||||
pub const QUALIFIED_COLUMN: Node = Node::Seq(QUALIFIED_COLUMN_NODES);
|
||||
|
||||
// --- Relationship-endpoint clauses (`from <T>.<c> to <T>.<c>`) ----
|
||||
|
||||
const RELATIONSHIP_ENDPOINTS_NODES: &[Node] = &[
|
||||
Node::Word(Word::keyword("from")),
|
||||
QUALIFIED_COLUMN,
|
||||
Node::Word(Word::keyword("to")),
|
||||
QUALIFIED_COLUMN,
|
||||
];
|
||||
pub const RELATIONSHIP_ENDPOINTS: Node = Node::Seq(RELATIONSHIP_ENDPOINTS_NODES);
|
||||
|
||||
// --- Referential action (`cascade`, `restrict`, `set null`,
|
||||
// `no action`) -----------------------------------------------
|
||||
|
||||
const ACTION_SET_NULL: &[Node] = &[
|
||||
Node::Word(Word::keyword("set")),
|
||||
Node::Word(Word::keyword("null")),
|
||||
];
|
||||
const ACTION_NO_ACTION: &[Node] = &[
|
||||
Node::Word(Word::keyword("no")),
|
||||
Node::Word(Word::keyword("action")),
|
||||
];
|
||||
const ACTION_CHOICES: &[Node] = &[
|
||||
Node::Word(Word::keyword("cascade")),
|
||||
Node::Word(Word::keyword("restrict")),
|
||||
Node::Seq(ACTION_SET_NULL),
|
||||
Node::Seq(ACTION_NO_ACTION),
|
||||
];
|
||||
pub const ACTION_KEYWORD: Node = Node::Choice(ACTION_CHOICES);
|
||||
|
||||
// --- A single `on <delete|update> <action>` clause ----------------
|
||||
|
||||
const ON_TARGET_CHOICES: &[Node] = &[
|
||||
Node::Word(Word::keyword("delete")),
|
||||
Node::Word(Word::keyword("update")),
|
||||
];
|
||||
|
||||
const ON_CLAUSE_NODES: &[Node] = &[
|
||||
Node::Word(Word::keyword("on")),
|
||||
Node::Choice(ON_TARGET_CHOICES),
|
||||
ACTION_KEYWORD,
|
||||
];
|
||||
pub const ON_CLAUSE: Node = Node::Seq(ON_CLAUSE_NODES);
|
||||
|
||||
/// Repeated `on <target> <action>` clauses (0..2 occurrences).
|
||||
/// Validation of "specified twice" + max=2 lives in the
|
||||
/// command's AST builder.
|
||||
pub const REFERENTIAL_CLAUSES: Node = Node::Repeated {
|
||||
inner: &ON_CLAUSE,
|
||||
separator: None,
|
||||
min: 0,
|
||||
};
|
||||
Reference in New Issue
Block a user