36 lines
873 B
Rust
36 lines
873 B
Rust
use super::*;
|
|
|
|
pub struct Module {
|
|
items: Vec<Id<Item>>,
|
|
}
|
|
|
|
impl Parsable for Module {
|
|
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
|
|
let mut items = Vec::new();
|
|
if ctx.peek().is_none() {
|
|
return Ok(Self { items });
|
|
}
|
|
items.push(ctx.parse()?);
|
|
while *ctx.expect_peek()? == Token::Semicolon {
|
|
ctx.next();
|
|
items.push(ctx.parse()?);
|
|
}
|
|
Ok(Self { items })
|
|
}
|
|
}
|
|
|
|
impl FmtNode for Module {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter, mut ctx: DisplayCtx) -> std::fmt::Result {
|
|
ctx.indent += 3;
|
|
write!(f, "{{")?;
|
|
if !self.items.is_empty() {
|
|
writeln!(f)?;
|
|
}
|
|
for &i in &self.items {
|
|
writeln!(f, "{}{};", " ".repeat(ctx.indent), i.dsp(ctx))?;
|
|
}
|
|
write!(f, "}}")?;
|
|
Ok(())
|
|
}
|
|
}
|