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