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