Track space, comments and spans in ast

This commit is contained in:
Joscha 2022-11-18 12:05:38 +01:00
parent 34a08d82ee
commit 8bf446b9c3

View file

@ -1,11 +1,65 @@
use std::fmt; use std::fmt;
use crate::span::{HasSpan, Span};
#[derive(Clone)] #[derive(Clone)]
pub struct Ident(pub String); pub struct Space {
pub comment: Vec<(String, Span)>,
pub span: Span,
}
impl fmt::Debug for Space {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Space").finish()
}
}
impl HasSpan for Space {
fn span(&self) -> Span {
self.span
}
}
#[derive(Clone)]
pub struct Ident {
pub name: String,
pub span: Span,
}
impl fmt::Debug for Ident { impl fmt::Debug for Ident {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "i#{}", self.0) write!(f, "i#{}", self.name)
}
}
impl HasSpan for Ident {
fn span(&self) -> Span {
self.span
}
}
#[derive(Clone)]
pub enum NumLitStr {
/// - `0b_0001_1011`
/// - `0b10`
Bin(String),
/// - `12_345`
/// - `7`
Dec(String),
/// - `0x_c0_f3`
/// - `0xB`
Hex(String),
}
impl fmt::Debug for NumLitStr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Bin(str) => write!(f, "0b{str}"),
Self::Dec(str) => write!(f, "{str}"),
Self::Hex(str) => write!(f, "0x{str}"),
}
} }
} }
@ -14,26 +68,44 @@ impl fmt::Debug for Ident {
/// Possible bases are binary, decimal, hexadecimal. Underscores can be inserted /// Possible bases are binary, decimal, hexadecimal. Underscores can be inserted
/// before and after any digit. /// before and after any digit.
#[derive(Clone)] #[derive(Clone)]
pub enum NumLit { pub struct NumLit {
/// - `0b_0001_1011` value: i64,
/// - `0b10` str: NumLitStr,
Bin(i64, String), span: Span,
/// - `12_345`
/// - `7`
Dec(i64, String),
/// - `0x_c0_f3`
/// - `0xB`
Hex(i64, String),
} }
impl fmt::Debug for NumLit { impl fmt::Debug for NumLit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.str.fmt(f)
}
}
impl HasSpan for NumLit {
fn span(&self) -> Span {
self.span
}
}
#[derive(Debug, Clone)]
pub enum StringLit {
/// - `"Hello world\n"`
/// - `""`
Inline(String, Span),
/// ```text
/// """
/// Hello,
/// world!
/// """
/// ```
Multiline(String, Span),
}
impl HasSpan for StringLit {
fn span(&self) -> Span {
match self { match self {
Self::Bin(_, str) => write!(f, "0b{str}"), StringLit::Inline(_, span) => *span,
Self::Dec(_, str) => write!(f, "{str}"), StringLit::Multiline(_, span) => *span,
Self::Hex(_, str) => write!(f, "0x{str}"),
} }
} }
} }
@ -44,32 +116,54 @@ pub enum TableLitElem {
Positional(Box<Expr>), Positional(Box<Expr>),
/// `foo: a` /// `foo: a`
Named(Ident, Box<Expr>), ///
/// Structure: `name s0 : s1 value`
Named {
name: Ident,
s0: Space,
s1: Space,
value: Box<Expr>,
},
}
impl HasSpan for TableLitElem {
fn span(&self) -> Span {
match self {
TableLitElem::Positional(value) => value.span(),
TableLitElem::Named { name, value, .. } => name.span().join(value.span()),
}
}
} }
/// `'{ a, foo: b }` /// `'{ a, foo: b }`
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TableLit { pub struct TableLit {
pub elems: Vec<TableLitElem>, pub elems: Vec<(Space, TableLitElem, Space)>,
pub trailing_comma: bool, /// `Some` if there is a trailing comma, `None` otherwise.
pub trailing_comma: Option<Space>,
pub span: Span,
}
impl HasSpan for TableLit {
fn span(&self) -> Span {
self.span
}
} }
#[derive(Clone)] #[derive(Clone)]
pub enum Lit { pub enum Lit {
/// `nil` /// `nil`
Nil, Nil(Span),
/// - `true` /// - `true`
/// - `false` /// - `false`
Bool(bool), Bool(bool, Span),
/// See [`NumLit`]. /// See [`NumLit`].
Num(NumLit), Num(NumLit),
/// - `"foo"` /// See [`StringLit`]
/// - `"Hello world!\n"` String(StringLit),
/// - `""`
String(String),
/// See [`TableLit`]. /// See [`TableLit`].
Table(TableLit), Table(TableLit),
@ -78,8 +172,8 @@ pub enum Lit {
impl fmt::Debug for Lit { impl fmt::Debug for Lit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Self::Nil => write!(f, "l#nil"), Self::Nil(_) => write!(f, "l#nil"),
Self::Bool(b) => write!(f, "l#{b:?}"), Self::Bool(b, _) => write!(f, "l#{b:?}"),
Self::Num(n) => write!(f, "l#{n:?}"), Self::Num(n) => write!(f, "l#{n:?}"),
Self::String(s) => write!(f, "l#{s:?}"), Self::String(s) => write!(f, "l#{s:?}"),
Self::Table(t) => { Self::Table(t) => {
@ -90,20 +184,59 @@ impl fmt::Debug for Lit {
} }
} }
impl HasSpan for Lit {
fn span(&self) -> Span {
match self {
Lit::Nil(span) => *span,
Lit::Bool(_, span) => *span,
Lit::Num(n) => n.span(),
Lit::String(s) => s.span(),
Lit::Table(t) => t.span(),
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum TableConstrElem { pub enum TableConstrElem {
/// See [`TableLitElem`]. /// See [`TableLitElem`].
Lit(TableLitElem), Lit(TableLitElem),
/// `[a]: b` /// `[a]: b`
Indexed(Box<Expr>, Box<Expr>), ///
/// Structure: `[ s0 index s1 ] s2 : s3 value`
Indexed {
s0: Space,
index: Box<Expr>,
s1: Space,
s2: Space,
s3: Space,
value: Box<Expr>,
span: Span,
},
}
impl HasSpan for TableConstrElem {
fn span(&self) -> Span {
match self {
TableConstrElem::Lit(lit) => lit.span(),
TableConstrElem::Indexed { span, .. } => *span,
}
}
} }
/// `{ a, b, foo: c, [d]: e }` /// `{ a, b, foo: c, [d]: e }`
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TableConstr { pub struct TableConstr {
pub elems: Vec<TableConstrElem>, pub elems: Vec<(Space, TableConstrElem, Space)>,
pub trailing_comma: bool, /// `Some` if there is a trailing comma, `None` otherwise.
pub trailing_comma: Option<Space>,
pub span: Span,
}
impl HasSpan for TableConstr {
fn span(&self) -> Span {
self.span
}
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -136,42 +269,152 @@ pub enum Expr {
TableConstr(TableConstr), TableConstr(TableConstr),
/// `[a]` /// `[a]`
Var(Box<Expr>), ///
/// Structure: `[ s0 index s1 ]`
Var {
s0: Space,
index: Box<Expr>,
s1: Space,
span: Span,
},
/// `foo` /// `foo`
VarIdent(Ident), VarIdent(Ident),
/// `[a] = b` /// `[a] = b`
VarAssign(Box<Expr>, Box<Expr>), ///
/// Structure: `[ s0 index s1 ] s2 = s3 value`
VarAssign {
s0: Space,
index: Box<Expr>,
s1: Space,
s2: Space,
s3: Space,
value: Box<Expr>,
span: Span,
},
/// `foo = a` /// `foo = a`
VarIdentAssign(Ident, Box<Expr>), ///
/// Structure: `name s0 = s1 value`
VarIdentAssign {
name: Ident,
s0: Space,
s1: Space,
value: Box<Expr>,
},
/// `-a` /// `-a`
Neg(Box<Expr>), ///
/// Structure: `- s0 expr`
Neg {
minus: Span,
s0: Space,
expr: Box<Expr>,
},
/// `not a` /// `not a`
Not(Box<Expr>), ///
/// Structure: `'not' s0 expr`
Not {
not: Span,
s0: Space,
expr: Box<Expr>,
},
/// `a[b]` /// `a[b]`
Field(Box<Expr>, Box<Expr>), ///
/// Structure: `expr s0 [ s1 index s2 ]`
Field {
expr: Box<Expr>,
s0: Space,
s1: Space,
index: Box<Expr>,
s2: Space,
span: Span,
},
/// `a.foo` /// `a.foo`
FieldIdent(Box<Expr>, Ident), ///
/// Structure: `expr s0 . s1 ident`
FieldIdent {
expr: Box<Expr>,
s0: Space,
s1: Space,
ident: Ident,
},
/// `a[b] = c` /// `a[b] = c`
FieldAssign(Box<Expr>, Box<Expr>, Box<Expr>), ///
/// Structure: `expr s0 [ s1 index s2 ] s3 = s4 value`
FieldAssign {
expr: Box<Expr>,
s0: Space,
s1: Space,
index: Box<Expr>,
s2: Space,
s3: Space,
s4: Space,
value: Box<Expr>,
},
/// `a.foo = b` /// `a.foo = b`
FieldIdentAssign(Box<Expr>, Ident, Box<Expr>), ///
/// Structure: `expr s0 . s1 ident s2 = s3 value`
FieldIdentAssign {
expr: Box<Expr>,
s0: Space,
s1: Space,
ident: Ident,
s2: Space,
s3: Space,
value: Box<Expr>,
},
/// - `a + b` /// - `a + b`
/// - `a == b` /// - `a == b`
/// - `a and b` /// - `a and b`
BinOp(Box<Expr>, BinOp, Box<Expr>), ///
/// Structure: `left s0 op s1 right`
BinOp {
left: Box<Expr>,
s0: Space,
op: BinOp,
s1: Space,
right: Box<Expr>,
},
}
impl HasSpan for Expr {
fn span(&self) -> Span {
match self {
Expr::Lit(lit) => lit.span(),
Expr::TableConstr(tcr) => tcr.span(),
Expr::Var { span, .. } => *span,
Expr::VarIdent(_) => todo!(),
Expr::VarAssign { span, .. } => *span,
Expr::VarIdentAssign { name, value, .. } => name.span().join(value.span()),
Expr::Neg { minus, expr, .. } => minus.join(expr.span()),
Expr::Not { not, expr, .. } => not.join(expr.span()),
Expr::Field { span, .. } => *span,
Expr::FieldIdent { expr, ident, .. } => expr.span().join(ident.span()),
Expr::FieldAssign { expr, value, .. } => expr.span().join(value.span()),
Expr::FieldIdentAssign { expr, value, .. } => expr.span().join(value.span()),
Expr::BinOp { left, right, .. } => left.span().join(right.span()),
}
}
} }
/// The contents of a program file are just a table literal without the
/// surrounding `'{` and `}`.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Program(pub TableLit); pub struct Program {
pub elems: Vec<(Space, TableLitElem, Space)>,
/// `Some` if there is a trailing comma, `None` otherwise.
pub trailing_comma: Option<Space>,
pub span: Span,
}
impl HasSpan for Program {
fn span(&self) -> Span {
self.span
}
}