Parse table destructuring
This commit is contained in:
parent
a82b625631
commit
73e32252c4
3 changed files with 73 additions and 3 deletions
67
src/parser/table_destr.rs
Normal file
67
src/parser/table_destr.rs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
//! Corresponds to `ast::table_constr`.
|
||||
|
||||
use chumsky::prelude::*;
|
||||
|
||||
use crate::ast::{Expr, TableDestr, TablePattern, TablePatternElem};
|
||||
|
||||
use super::basic::{ident, space, Error};
|
||||
|
||||
pub fn table_pattern_elem() -> impl Parser<char, TablePatternElem, Error = Error> {
|
||||
let positional = ident().map(TablePatternElem::Positional);
|
||||
|
||||
let named = ident()
|
||||
.then(space())
|
||||
.then_ignore(just(':'))
|
||||
.then(space())
|
||||
.then(ident())
|
||||
.map_with_span(|(((name, s0), s1), ident), span| TablePatternElem::Named {
|
||||
name,
|
||||
s0,
|
||||
s1,
|
||||
ident,
|
||||
span,
|
||||
});
|
||||
|
||||
named.or(positional)
|
||||
}
|
||||
|
||||
pub fn table_pattern() -> impl Parser<char, TablePattern, Error = Error> {
|
||||
let elem = space()
|
||||
.then(table_pattern_elem())
|
||||
.then(space())
|
||||
.map(|((s0, elem), s1)| (s0, elem, s1));
|
||||
|
||||
let trailing_comma = just(",").ignore_then(space()).or_not();
|
||||
|
||||
let elems = elem.separated_by(just(",")).then(trailing_comma);
|
||||
|
||||
just("{")
|
||||
.ignore_then(elems)
|
||||
.then_ignore(just("}"))
|
||||
.map_with_span(|(elems, trailing_comma), span| TablePattern {
|
||||
elems,
|
||||
trailing_comma,
|
||||
span,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn table_destr(
|
||||
expr: impl Parser<char, Expr, Error = Error>,
|
||||
) -> impl Parser<char, TableDestr, Error = Error> {
|
||||
let local = text::keyword("local").ignore_then(space()).or_not();
|
||||
|
||||
local
|
||||
.then(table_pattern())
|
||||
.then(space())
|
||||
.then_ignore(just('='))
|
||||
.then(space())
|
||||
.then(expr)
|
||||
.map_with_span(|((((local, pattern), s0), s1), value), span| TableDestr {
|
||||
local,
|
||||
pattern,
|
||||
s0,
|
||||
s1,
|
||||
value: Box::new(value),
|
||||
span,
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue