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::types::{Severity, Span, SpanDiagnostic};
18
19/// Top-level AST node for a parsed MIB module.
20///
21/// Produced by the parser from a single MIB source file. Contains the
22/// module's [`ImportClause`]s, body [`Definition`]s, and any diagnostics
23/// emitted during parsing.
24#[derive(Debug, PartialEq, Eq)]
25pub struct Module {
26    /// Module name (e.g. `IF-MIB`). `None` if parsing failed before the header.
27    pub name: Option<Ident>,
28    /// `IMPORTS ... ;` clauses.
29    pub imports: Vec<ImportClause>,
30    /// Definitions in the module body, between `BEGIN` and `END`.
31    pub body: Vec<Definition>,
32    /// Span covering the entire module source.
33    pub span: Span,
34    /// Diagnostics collected during parsing.
35    pub diagnostics: Vec<SpanDiagnostic>,
36}
37
38impl Module {
39    /// Creates a new module with the given name and span, and empty imports/body/diagnostics.
40    pub fn new(name: Ident, span: Span) -> Self {
41        Module {
42            name: Some(name),
43            imports: Vec::new(),
44            body: Vec::new(),
45            span,
46            diagnostics: Vec::new(),
47        }
48    }
49
50    /// Reports whether any diagnostic has error severity or worse.
51    pub fn has_errors(&self) -> bool {
52        self.diagnostics
53            .iter()
54            .any(|d| d.severity <= Severity::Error)
55    }
56}
57
58/// Symbols imported from a single source module.
59///
60/// Corresponds to one `symbol1, symbol2 FROM ModuleName` group
61/// inside an `IMPORTS` section.
62#[derive(Debug, PartialEq, Eq)]
63pub struct ImportClause {
64    /// Imported symbol names.
65    pub symbols: Vec<Ident>,
66    /// Source module name (the `FROM` target).
67    pub from_module: Ident,
68    /// Span covering the entire clause.
69    pub span: Span,
70}