Switch TablConstr to BoundedSeparated

This commit is contained in:
Joscha 2022-11-21 23:54:40 +01:00
parent e3fa3500d4
commit 0e9cfd67c2
6 changed files with 82 additions and 102 deletions

View file

@ -126,8 +126,6 @@ impl HasSpan for TableLitElem {
}
/// `'{ a, foo: b }`
///
/// Structure: `'{ s0 elems s1 }`
#[derive(Debug, Clone)]
pub struct TableLit(pub BoundedSeparated<TableLitElem>);

View file

@ -1,6 +1,6 @@
use crate::span::{HasSpan, Span};
use super::{Expr, Separated, Space, TableLitElem};
use super::{BoundedSeparated, Expr, Space, TableLitElem};
#[derive(Debug, Clone)]
pub enum TableConstrElem {
@ -31,18 +31,11 @@ impl HasSpan for TableConstrElem {
}
/// `{ a, b, foo: c, [d]: e }`
///
/// Structure: `{ s0 elems s1 }`
#[derive(Debug, Clone)]
pub struct TableConstr {
pub s0: Space,
pub elems: Separated<TableConstrElem, (Space, Space), Space>,
pub s1: Space,
pub span: Span,
}
pub struct TableConstr(pub BoundedSeparated<TableConstrElem>);
impl HasSpan for TableConstr {
fn span(&self) -> Span {
self.span
self.0.span()
}
}

View file

@ -1,5 +1,5 @@
use crate::ast::{
Call, Expr, Field, Line, Lit, Separated, Space, StringLit, StringLitElem, TableConstr,
BoundedSeparated, Call, Expr, Field, Line, Lit, Space, StringLit, StringLitElem, TableConstr,
TableConstrElem, TableLitElem,
};
use crate::builtin::Builtin;
@ -17,25 +17,26 @@ impl Field {
} => {
// ` expr s0 [ s1 index s2 ]`
// -> `'get s0 { expr, s1 index s2 }`
let elems = Separated::NonEmpty {
first_elem: TableConstrElem::Lit(TableLitElem::Positional(expr)),
last_elems: vec![(
(Space::empty(span), s1),
let elems = vec![
(
Space::empty(span),
TableConstrElem::Lit(TableLitElem::Positional(expr)),
Space::empty(span),
),
(
s1,
TableConstrElem::Lit(TableLitElem::Positional(index)),
)],
trailing: None,
span,
};
let constr = TableConstr {
s0: Space::empty(span),
elems,
s1: s2,
span,
};
s2,
),
];
let new = Expr::Call(Call::Constr {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Get, span))),
s0,
constr,
constr: TableConstr(BoundedSeparated {
elems,
trailing: None,
span,
}),
span,
});
(new, true)
@ -54,31 +55,31 @@ impl Field {
} => {
// `expr s0 [ s1 index s2 ] s3 = s4 value`
// -> `'set s0 { expr, s1 index s2, s3 s4 value }`
let elems = Separated::NonEmpty {
first_elem: TableConstrElem::Lit(TableLitElem::Positional(expr)),
last_elems: vec![
let elems = vec![
(
(Space::empty(span), s1),
Space::empty(span),
TableConstrElem::Lit(TableLitElem::Positional(expr)),
Space::empty(span),
),
(
s1,
TableConstrElem::Lit(TableLitElem::Positional(index)),
s2,
),
(
(s2, s3.then_line(Line::Empty).then(s4)),
s3.then_line(Line::Empty).then(s4),
TableConstrElem::Lit(TableLitElem::Positional(value)),
Space::empty(span),
),
],
trailing: None,
span,
};
let constr = TableConstr {
s0: Space::empty(span),
elems,
s1: Space::empty(span),
span,
};
];
let new = Expr::Call(Call::Constr {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Set, span))),
s0,
constr,
constr: TableConstr(BoundedSeparated {
elems,
trailing: None,
span,
}),
span,
});
(new, true)

View file

@ -1,5 +1,5 @@
use crate::ast::{
Call, Expr, Field, Line, Lit, Separated, Space, StringLit, StringLitElem, TableConstr,
BoundedSeparated, Call, Expr, Field, Line, Lit, Space, StringLit, StringLitElem, TableConstr,
TableConstrElem, TableLitElem, Var,
};
use crate::builtin::Builtin;
@ -83,31 +83,31 @@ impl Var {
s1: Space::empty(span),
span,
});
let elems = Separated::NonEmpty {
first_elem: TableConstrElem::Lit(TableLitElem::Positional(Box::new(scope))),
last_elems: vec![
let elems = vec![
(
(Space::empty(span), local.then_line(Line::Empty).then(s0)),
Space::empty(span),
TableConstrElem::Lit(TableLitElem::Positional(Box::new(scope))),
Space::empty(span),
),
(
local.then_line(Line::Empty).then(s0),
TableConstrElem::Lit(TableLitElem::Positional(index)),
s1,
),
(
(s1, s2.then_line(Line::Empty).then(s3)),
s2.then_line(Line::Empty).then(s3),
TableConstrElem::Lit(TableLitElem::Positional(value)),
Space::empty(span),
),
],
trailing: None,
span,
};
let constr = TableConstr {
s0: Space::empty(span),
elems,
s1: Space::empty(span),
span,
};
];
let new = Expr::Call(Call::Constr {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::SetRaw, span))),
s0: Space::empty(span),
constr,
constr: TableConstr(BoundedSeparated {
elems,
trailing: None,
span,
}),
span,
});
(new, true)

View file

@ -4,7 +4,7 @@ use chumsky::prelude::*;
use crate::ast::{Expr, Space, TableConstr, TableConstrElem, TableLitElem};
use super::basic::{separated_by, EParser, Error};
use super::basic::{bounded_separated, EParser, Error};
fn table_constr_elem(
space: EParser<Space>,
@ -43,19 +43,13 @@ pub fn table_constr(
expr: EParser<Expr>,
) -> EParser<TableConstr> {
let elem = table_constr_elem(space.clone(), table_lit_elem, expr);
let separator = space.clone().then_ignore(just(',')).then(space.clone());
let trailing_separator = space.clone().then_ignore(just(','));
space
.clone()
.then(separated_by(elem, separator, trailing_separator))
.then(space)
.delimited_by(just('{'), just('}'))
.map_with_span(|((s0, elems), s1), span| TableConstr {
s0,
elems,
s1,
span,
})
bounded_separated(
space,
just('{').to(()),
just('}').to(()),
just(',').to(()),
elem,
)
.map(TableConstr)
.boxed()
}

View file

@ -2,8 +2,6 @@ use pretty::{DocAllocator, DocBuilder, Pretty};
use crate::ast::{TableConstr, TableConstrElem};
use super::NEST_DEPTH;
impl<'a, D> Pretty<'a, D> for TableConstrElem
where
D: DocAllocator<'a>,
@ -35,16 +33,12 @@ where
D::Doc: Clone,
{
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> {
self.elems
.pretty(
self.0.pretty(
allocator,
|e| allocator.line().append(e.pretty(allocator)),
|(s0, s1)| allocator.text(","),
|s| allocator.text(","),
allocator.text("{"),
allocator.text("}"),
allocator.text(","),
|e| e.pretty(allocator),
)
.nest(NEST_DEPTH)
.append(allocator.line())
.braces()
.group()
}
}