use crate::ast::{ItemExtras, Span, Unit};
use microcad_lang_base::CompactString;
use std::num::{ParseFloatError, ParseIntError};
use thiserror::Error;
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct Literal {
pub span: Span,
pub extras: ItemExtras,
pub literal: LiteralKind,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub enum LiteralKind {
Error(LiteralError),
String(StringLiteral),
Bool(BoolLiteral),
Integer(IntegerLiteral),
Float(FloatLiteral),
Quantity(QuantityLiteral),
}
impl LiteralKind {
pub fn span(&self) -> Span {
match self {
LiteralKind::Error(lit) => lit.span.clone(),
LiteralKind::String(lit) => lit.span.clone(),
LiteralKind::Bool(lit) => lit.span.clone(),
LiteralKind::Integer(lit) => lit.span.clone(),
LiteralKind::Float(lit) => lit.span.clone(),
LiteralKind::Quantity(lit) => lit.span.clone(),
}
}
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct StringLiteral {
pub span: Span,
pub content: String,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct BoolLiteral {
pub span: Span,
pub value: bool,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct IntegerLiteral {
pub span: Span,
pub value: i64,
pub raw: CompactString,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct FloatLiteral {
pub span: Span,
pub value: f64,
pub raw: CompactString,
}
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub struct QuantityLiteral {
pub span: Span,
pub value: f64,
pub raw: CompactString,
pub unit: Unit,
}
#[derive(Debug, PartialEq, Clone)]
#[allow(missing_docs)]
pub struct LiteralError {
pub span: Span,
pub kind: LiteralErrorKind,
}
#[derive(Debug, Error, PartialEq, Clone)]
#[allow(missing_docs)]
pub enum LiteralErrorKind {
#[error(transparent)]
Float(#[from] ParseFloatError),
#[error(transparent)]
Int(#[from] ParseIntError),
#[error("unclosed string literal")]
UnclosedString,
#[error("only numeric literals can be typed")]
Untypable,
}