Switch TablConstr to BoundedSeparated
This commit is contained in:
parent
e3fa3500d4
commit
0e9cfd67c2
6 changed files with 82 additions and 102 deletions
|
|
@ -126,8 +126,6 @@ impl HasSpan for TableLitElem {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `'{ a, foo: b }`
|
/// `'{ a, foo: b }`
|
||||||
///
|
|
||||||
/// Structure: `'{ s0 elems s1 }`
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct TableLit(pub BoundedSeparated<TableLitElem>);
|
pub struct TableLit(pub BoundedSeparated<TableLitElem>);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::span::{HasSpan, Span};
|
use crate::span::{HasSpan, Span};
|
||||||
|
|
||||||
use super::{Expr, Separated, Space, TableLitElem};
|
use super::{BoundedSeparated, Expr, Space, TableLitElem};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum TableConstrElem {
|
pub enum TableConstrElem {
|
||||||
|
|
@ -31,18 +31,11 @@ impl HasSpan for TableConstrElem {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `{ a, b, foo: c, [d]: e }`
|
/// `{ a, b, foo: c, [d]: e }`
|
||||||
///
|
|
||||||
/// Structure: `{ s0 elems s1 }`
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct TableConstr {
|
pub struct TableConstr(pub BoundedSeparated<TableConstrElem>);
|
||||||
pub s0: Space,
|
|
||||||
pub elems: Separated<TableConstrElem, (Space, Space), Space>,
|
|
||||||
pub s1: Space,
|
|
||||||
pub span: Span,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasSpan for TableConstr {
|
impl HasSpan for TableConstr {
|
||||||
fn span(&self) -> Span {
|
fn span(&self) -> Span {
|
||||||
self.span
|
self.0.span()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::ast::{
|
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,
|
TableConstrElem, TableLitElem,
|
||||||
};
|
};
|
||||||
use crate::builtin::Builtin;
|
use crate::builtin::Builtin;
|
||||||
|
|
@ -17,25 +17,26 @@ impl Field {
|
||||||
} => {
|
} => {
|
||||||
// ` expr s0 [ s1 index s2 ]`
|
// ` expr s0 [ s1 index s2 ]`
|
||||||
// -> `'get s0 { expr, s1 index s2 }`
|
// -> `'get s0 { expr, s1 index s2 }`
|
||||||
let elems = Separated::NonEmpty {
|
let elems = vec![
|
||||||
first_elem: TableConstrElem::Lit(TableLitElem::Positional(expr)),
|
(
|
||||||
last_elems: vec![(
|
Space::empty(span),
|
||||||
(Space::empty(span), s1),
|
TableConstrElem::Lit(TableLitElem::Positional(expr)),
|
||||||
|
Space::empty(span),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
s1,
|
||||||
TableConstrElem::Lit(TableLitElem::Positional(index)),
|
TableConstrElem::Lit(TableLitElem::Positional(index)),
|
||||||
)],
|
s2,
|
||||||
trailing: None,
|
),
|
||||||
span,
|
];
|
||||||
};
|
|
||||||
let constr = TableConstr {
|
|
||||||
s0: Space::empty(span),
|
|
||||||
elems,
|
|
||||||
s1: s2,
|
|
||||||
span,
|
|
||||||
};
|
|
||||||
let new = Expr::Call(Call::Constr {
|
let new = Expr::Call(Call::Constr {
|
||||||
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Get, span))),
|
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Get, span))),
|
||||||
s0,
|
s0,
|
||||||
constr,
|
constr: TableConstr(BoundedSeparated {
|
||||||
|
elems,
|
||||||
|
trailing: None,
|
||||||
|
span,
|
||||||
|
}),
|
||||||
span,
|
span,
|
||||||
});
|
});
|
||||||
(new, true)
|
(new, true)
|
||||||
|
|
@ -54,31 +55,31 @@ impl Field {
|
||||||
} => {
|
} => {
|
||||||
// `expr s0 [ s1 index s2 ] s3 = s4 value`
|
// `expr s0 [ s1 index s2 ] s3 = s4 value`
|
||||||
// -> `'set s0 { expr, s1 index s2, s3 s4 value }`
|
// -> `'set s0 { expr, s1 index s2, s3 s4 value }`
|
||||||
let elems = Separated::NonEmpty {
|
let elems = vec![
|
||||||
first_elem: TableConstrElem::Lit(TableLitElem::Positional(expr)),
|
(
|
||||||
last_elems: vec![
|
Space::empty(span),
|
||||||
(
|
TableConstrElem::Lit(TableLitElem::Positional(expr)),
|
||||||
(Space::empty(span), s1),
|
Space::empty(span),
|
||||||
TableConstrElem::Lit(TableLitElem::Positional(index)),
|
),
|
||||||
),
|
(
|
||||||
(
|
s1,
|
||||||
(s2, s3.then_line(Line::Empty).then(s4)),
|
TableConstrElem::Lit(TableLitElem::Positional(index)),
|
||||||
TableConstrElem::Lit(TableLitElem::Positional(value)),
|
s2,
|
||||||
),
|
),
|
||||||
],
|
(
|
||||||
trailing: None,
|
s3.then_line(Line::Empty).then(s4),
|
||||||
span,
|
TableConstrElem::Lit(TableLitElem::Positional(value)),
|
||||||
};
|
Space::empty(span),
|
||||||
let constr = TableConstr {
|
),
|
||||||
s0: Space::empty(span),
|
];
|
||||||
elems,
|
|
||||||
s1: Space::empty(span),
|
|
||||||
span,
|
|
||||||
};
|
|
||||||
let new = Expr::Call(Call::Constr {
|
let new = Expr::Call(Call::Constr {
|
||||||
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Set, span))),
|
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Set, span))),
|
||||||
s0,
|
s0,
|
||||||
constr,
|
constr: TableConstr(BoundedSeparated {
|
||||||
|
elems,
|
||||||
|
trailing: None,
|
||||||
|
span,
|
||||||
|
}),
|
||||||
span,
|
span,
|
||||||
});
|
});
|
||||||
(new, true)
|
(new, true)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::ast::{
|
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,
|
TableConstrElem, TableLitElem, Var,
|
||||||
};
|
};
|
||||||
use crate::builtin::Builtin;
|
use crate::builtin::Builtin;
|
||||||
|
|
@ -83,31 +83,31 @@ impl Var {
|
||||||
s1: Space::empty(span),
|
s1: Space::empty(span),
|
||||||
span,
|
span,
|
||||||
});
|
});
|
||||||
let elems = Separated::NonEmpty {
|
let elems = vec![
|
||||||
first_elem: TableConstrElem::Lit(TableLitElem::Positional(Box::new(scope))),
|
(
|
||||||
last_elems: vec![
|
Space::empty(span),
|
||||||
(
|
TableConstrElem::Lit(TableLitElem::Positional(Box::new(scope))),
|
||||||
(Space::empty(span), local.then_line(Line::Empty).then(s0)),
|
Space::empty(span),
|
||||||
TableConstrElem::Lit(TableLitElem::Positional(index)),
|
),
|
||||||
),
|
(
|
||||||
(
|
local.then_line(Line::Empty).then(s0),
|
||||||
(s1, s2.then_line(Line::Empty).then(s3)),
|
TableConstrElem::Lit(TableLitElem::Positional(index)),
|
||||||
TableConstrElem::Lit(TableLitElem::Positional(value)),
|
s1,
|
||||||
),
|
),
|
||||||
],
|
(
|
||||||
trailing: None,
|
s2.then_line(Line::Empty).then(s3),
|
||||||
span,
|
TableConstrElem::Lit(TableLitElem::Positional(value)),
|
||||||
};
|
Space::empty(span),
|
||||||
let constr = TableConstr {
|
),
|
||||||
s0: Space::empty(span),
|
];
|
||||||
elems,
|
|
||||||
s1: Space::empty(span),
|
|
||||||
span,
|
|
||||||
};
|
|
||||||
let new = Expr::Call(Call::Constr {
|
let new = Expr::Call(Call::Constr {
|
||||||
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::SetRaw, span))),
|
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::SetRaw, span))),
|
||||||
s0: Space::empty(span),
|
s0: Space::empty(span),
|
||||||
constr,
|
constr: TableConstr(BoundedSeparated {
|
||||||
|
elems,
|
||||||
|
trailing: None,
|
||||||
|
span,
|
||||||
|
}),
|
||||||
span,
|
span,
|
||||||
});
|
});
|
||||||
(new, true)
|
(new, true)
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use chumsky::prelude::*;
|
||||||
|
|
||||||
use crate::ast::{Expr, Space, TableConstr, TableConstrElem, TableLitElem};
|
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(
|
fn table_constr_elem(
|
||||||
space: EParser<Space>,
|
space: EParser<Space>,
|
||||||
|
|
@ -43,19 +43,13 @@ pub fn table_constr(
|
||||||
expr: EParser<Expr>,
|
expr: EParser<Expr>,
|
||||||
) -> EParser<TableConstr> {
|
) -> EParser<TableConstr> {
|
||||||
let elem = table_constr_elem(space.clone(), table_lit_elem, expr);
|
let elem = table_constr_elem(space.clone(), table_lit_elem, expr);
|
||||||
let separator = space.clone().then_ignore(just(',')).then(space.clone());
|
bounded_separated(
|
||||||
let trailing_separator = space.clone().then_ignore(just(','));
|
space,
|
||||||
|
just('{').to(()),
|
||||||
space
|
just('}').to(()),
|
||||||
.clone()
|
just(',').to(()),
|
||||||
.then(separated_by(elem, separator, trailing_separator))
|
elem,
|
||||||
.then(space)
|
)
|
||||||
.delimited_by(just('{'), just('}'))
|
.map(TableConstr)
|
||||||
.map_with_span(|((s0, elems), s1), span| TableConstr {
|
.boxed()
|
||||||
s0,
|
|
||||||
elems,
|
|
||||||
s1,
|
|
||||||
span,
|
|
||||||
})
|
|
||||||
.boxed()
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ use pretty::{DocAllocator, DocBuilder, Pretty};
|
||||||
|
|
||||||
use crate::ast::{TableConstr, TableConstrElem};
|
use crate::ast::{TableConstr, TableConstrElem};
|
||||||
|
|
||||||
use super::NEST_DEPTH;
|
|
||||||
|
|
||||||
impl<'a, D> Pretty<'a, D> for TableConstrElem
|
impl<'a, D> Pretty<'a, D> for TableConstrElem
|
||||||
where
|
where
|
||||||
D: DocAllocator<'a>,
|
D: DocAllocator<'a>,
|
||||||
|
|
@ -35,16 +33,12 @@ where
|
||||||
D::Doc: Clone,
|
D::Doc: Clone,
|
||||||
{
|
{
|
||||||
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> {
|
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> {
|
||||||
self.elems
|
self.0.pretty(
|
||||||
.pretty(
|
allocator,
|
||||||
allocator,
|
allocator.text("{"),
|
||||||
|e| allocator.line().append(e.pretty(allocator)),
|
allocator.text("}"),
|
||||||
|(s0, s1)| allocator.text(","),
|
allocator.text(","),
|
||||||
|s| allocator.text(","),
|
|e| e.pretty(allocator),
|
||||||
)
|
)
|
||||||
.nest(NEST_DEPTH)
|
|
||||||
.append(allocator.line())
|
|
||||||
.braces()
|
|
||||||
.group()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue