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