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