Skip to main content

mib_rs/ast/
mod.rs

1//! Abstract syntax tree types produced by the parser.
2//!
3//! These types directly reflect the surface syntax of SMI MIB modules.
4//! After parsing, the AST is consumed by the [lowering pass](crate::lower)
5//! which normalizes it into a language-independent [`ir::Module`](crate::ir::Module).
6
7pub mod common;
8pub mod definition;
9pub mod oid;
10pub mod syntax;
11
12pub use common::{Ident, NamedNumber, QuotedString};
13pub use definition::*;
14pub use oid::{OidAssignment, OidComponent};
15pub use syntax::*;
16
17use crate::source::SourceRange;
18use crate::types::{Diagnostic, Severity};
19
20/// Top-level AST node for a parsed MIB module.
21///
22/// Produced by the parser from a single MIB source file. Contains the
23/// module's [`ImportClause`]s, body [`Definition`]s, and any diagnostics
24/// emitted during parsing.
25#[derive(Debug, PartialEq, Eq)]
26pub struct Module {
27    /// Module name (e.g. `IF-MIB`). `None` if parsing failed before the header.
28    pub name: Option<Ident>,
29    /// `IMPORTS ... ;` clauses.
30    pub imports: Vec<ImportClause>,
31    /// Definitions in the module body, between `BEGIN` and `END`.
32    pub body: Vec<Definition>,
33    /// Source range covering the entire module.
34    pub span: SourceRange,
35    /// Diagnostics collected during parsing.
36    pub diagnostics: Vec<Diagnostic>,
37}
38
39impl Module {
40    /// Creates a new module with the given name and span, and empty imports/body/diagnostics.
41    pub fn new(name: Ident, span: SourceRange) -> Self {
42        Module {
43            name: Some(name),
44            imports: Vec::new(),
45            body: Vec::new(),
46            span,
47            diagnostics: Vec::new(),
48        }
49    }
50
51    /// Reports whether any diagnostic has error severity or worse.
52    pub fn has_errors(&self) -> bool {
53        self.diagnostics
54            .iter()
55            .any(|d| d.severity <= Severity::Error)
56    }
57}
58
59/// Symbols imported from a single source module.
60///
61/// Corresponds to one `symbol1, symbol2 FROM ModuleName` group
62/// inside an `IMPORTS` section.
63#[derive(Debug, PartialEq, Eq)]
64pub struct ImportClause {
65    /// Imported symbol names.
66    pub symbols: Vec<Ident>,
67    /// Source module name (the `FROM` target).
68    pub from_module: Ident,
69    /// Source range covering the entire clause.
70    pub span: SourceRange,
71}