Skip to main content

pgevolve_core/parse/builder/
mod.rs

1//! AST → IR builders.
2//!
3//! Each submodule consumes one classified [`crate::parse::Statement`] variant
4//! and produces zero-or-more IR objects, optionally appended to a partial
5//! [`crate::ir::catalog::Catalog`] via [`Builder`].
6
7pub mod alter_table_attach_partition;
8pub mod alter_table_stmt;
9pub mod comment_stmt;
10pub mod create_composite_type_stmt;
11pub mod create_domain_stmt;
12pub mod create_enum_stmt;
13pub mod create_extension_stmt;
14pub mod create_function_stmt;
15pub mod create_materialized_view_stmt;
16pub mod create_schema_stmt;
17pub mod create_seq_stmt;
18pub mod create_stmt;
19pub mod create_trigger_stmt;
20pub mod create_view_stmt;
21pub mod default_privileges;
22pub mod desugar_serial;
23pub mod grants;
24pub mod index_stmt;
25pub mod owner_stmt;
26pub mod plpgsql;
27pub mod policy_stmt;
28pub mod publication_stmt;
29pub mod reloptions;
30pub mod shared;
31pub mod subscription_stmt;
32
33use std::collections::HashMap;
34
35use crate::identifier::QualifiedName;
36use crate::ir::catalog::Catalog;
37use crate::parse::error::SourceLocation;
38
39/// Mutable accumulator passed through builders during a single
40/// `parse_directory` pass.
41#[derive(Debug, Default)]
42pub struct Builder {
43    /// The catalog being assembled.
44    pub catalog: Catalog,
45    /// First-seen source location for every object qname, for duplicate diagnostics.
46    pub locations: HashMap<String, SourceLocation>,
47}
48
49impl Builder {
50    /// Construct an empty builder.
51    pub fn new() -> Self {
52        Self::default()
53    }
54
55    /// Record the source location at which `qname` was first defined.
56    /// Returns the prior location if the qname is already known.
57    pub fn record_location(
58        &mut self,
59        qname: &QualifiedName,
60        location: SourceLocation,
61    ) -> Option<SourceLocation> {
62        let key = qname.to_string();
63        if let Some(prior) = self.locations.get(&key) {
64            return Some(prior.clone());
65        }
66        self.locations.insert(key, location);
67        None
68    }
69}