use super::{Attribute, Expr, Item, NodeId, Pattern, Type};
use crate::lexer::Span;
#[derive(Debug, Clone, PartialEq)]
pub struct Stmt {
pub kind: StmtKind,
pub span: Span,
pub id: NodeId,
}
impl Stmt {
pub fn new(kind: StmtKind, span: Span) -> Self {
Self {
kind,
span,
id: NodeId::DUMMY,
}
}
pub fn local(local: Local) -> Self {
let span = local.span;
Self::new(StmtKind::Local(Box::new(local)), span)
}
pub fn expr(expr: Expr) -> Self {
let span = expr.span;
Self::new(StmtKind::Expr(Box::new(expr)), span)
}
pub fn semi(expr: Expr) -> Self {
let span = expr.span;
Self::new(StmtKind::Semi(Box::new(expr)), span)
}
pub fn item(item: Item) -> Self {
let span = item.span;
Self::new(StmtKind::Item(Box::new(item)), span)
}
pub fn empty(span: Span) -> Self {
Self::new(StmtKind::Empty, span)
}
pub fn has_semi(&self) -> bool {
matches!(
self.kind,
StmtKind::Local(_) | StmtKind::Semi(_) | StmtKind::Empty
)
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum StmtKind {
Local(Box<Local>),
Expr(Box<Expr>),
Semi(Box<Expr>),
Item(Box<Item>),
Empty,
Macro {
path: super::Path,
tokens: Vec<super::TokenTree>,
is_semi: bool,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct Local {
pub attrs: Vec<Attribute>,
pub pattern: Pattern,
pub ty: Option<Box<Type>>,
pub init: Option<LocalInit>,
pub span: Span,
pub id: NodeId,
}
impl Local {
pub fn new(
pattern: Pattern,
ty: Option<Box<Type>>,
init: Option<LocalInit>,
span: Span,
) -> Self {
Self {
attrs: Vec::new(),
pattern,
ty,
init,
span,
id: NodeId::DUMMY,
}
}
pub fn with_attrs(
attrs: Vec<Attribute>,
pattern: Pattern,
ty: Option<Box<Type>>,
init: Option<LocalInit>,
span: Span,
) -> Self {
Self {
attrs,
pattern,
ty,
init,
span,
id: NodeId::DUMMY,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct LocalInit {
pub expr: Box<Expr>,
pub diverge: Option<Box<Expr>>,
}
impl LocalInit {
pub fn simple(expr: Expr) -> Self {
Self {
expr: Box::new(expr),
diverge: None,
}
}
pub fn with_else(expr: Expr, diverge: Expr) -> Self {
Self {
expr: Box::new(expr),
diverge: Some(Box::new(diverge)),
}
}
}