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