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