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 statistic_stmt;
32pub mod subscription_stmt;
33
34use std::collections::HashMap;
35
36use crate::identifier::QualifiedName;
37use crate::ir::catalog::Catalog;
38use crate::parse::error::SourceLocation;
39
40/// Mutable accumulator passed through builders during a single
41/// `parse_directory` pass.
42#[derive(Debug, Default)]
43pub struct Builder {
44    /// The catalog being assembled.
45    pub catalog: Catalog,
46    /// First-seen source location for every object qname, for duplicate diagnostics.
47    pub locations: HashMap<String, SourceLocation>,
48}
49
50impl Builder {
51    /// Construct an empty builder.
52    pub fn new() -> Self {
53        Self::default()
54    }
55
56    /// Record the source location at which `qname` was first defined.
57    /// Returns the prior location if the qname is already known.
58    pub fn record_location(
59        &mut self,
60        qname: &QualifiedName,
61        location: SourceLocation,
62    ) -> Option<SourceLocation> {
63        let key = qname.to_string();
64        if let Some(prior) = self.locations.get(&key) {
65            return Some(prior.clone());
66        }
67        self.locations.insert(key, location);
68        None
69    }
70}