rudus/src/parser.rs

906 lines
28 KiB
Rust
Raw Normal View History

2024-10-31 20:59:26 +00:00
use crate::lexer::*;
use crate::spans::*;
use chumsky::{input::ValueInput, prelude::*, recursive::Recursive};
use std::fmt;
2024-11-21 23:50:13 +00:00
use struct_scalpel::Dissectible;
2024-10-31 20:59:26 +00:00
#[derive(Clone, Debug, PartialEq)]
pub struct WhenClause<'src> {
pub cond: Spanned<Ast<'src>>,
pub body: Spanned<Ast<'src>>,
}
2024-11-09 19:10:08 +00:00
impl<'src> fmt::Display for WhenClause<'src> {
fn fmt(self: &WhenClause<'src>, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "cond: {}, body: {}", self.cond.0, self.body.0)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct MatchClause<'src> {
pub patt: Spanned<Pattern<'src>>,
pub guard: Option<Spanned<Ast<'src>>>,
pub body: Spanned<Ast<'src>>,
}
impl<'src> fmt::Display for MatchClause<'src> {
fn fmt(self: &MatchClause<'src>, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"pattern: {}, guard: {:?} body: {}",
self.patt.0, self.guard, self.body.0
)
}
}
2024-12-04 20:03:09 +00:00
#[derive(Clone, Debug, PartialEq, Eq)]
2024-12-04 23:30:03 +00:00
pub enum StringPart {
Data(String),
Word(String),
Inline(String),
2024-12-04 20:03:09 +00:00
}
2024-12-04 23:30:03 +00:00
impl fmt::Display for StringPart {
fn fmt(self: &StringPart, f: &mut fmt::Formatter) -> fmt::Result {
2024-12-04 20:03:09 +00:00
let rep = match self {
2024-12-04 23:30:03 +00:00
StringPart::Word(s) => format!("{{{s}}}"),
StringPart::Data(s) => s.to_string(),
StringPart::Inline(s) => s.to_string(),
2024-12-04 20:03:09 +00:00
};
write!(f, "{}", rep)
}
}
2024-11-21 23:50:13 +00:00
#[derive(Clone, Debug, PartialEq, Dissectible)]
2024-10-31 20:59:26 +00:00
pub enum Ast<'src> {
Error,
Placeholder,
Nil,
Boolean(bool),
Number(f64),
Keyword(&'src str),
2024-10-31 20:59:26 +00:00
Word(&'src str),
String(&'src str),
2024-12-04 23:30:03 +00:00
Interpolated(Vec<Spanned<StringPart>>),
2024-10-31 20:59:26 +00:00
Block(Vec<Spanned<Self>>),
If(Box<Spanned<Self>>, Box<Spanned<Self>>, Box<Spanned<Self>>),
Tuple(Vec<Spanned<Self>>),
Arguments(Vec<Spanned<Self>>),
List(Vec<Spanned<Self>>),
2024-11-21 21:41:46 +00:00
Dict(Vec<Spanned<Self>>),
2024-10-31 20:59:26 +00:00
Let(Box<Spanned<Pattern<'src>>>, Box<Spanned<Self>>),
Box(&'src str, Box<Spanned<Self>>),
Synthetic(Box<Spanned<Self>>, Box<Spanned<Self>>, Vec<Spanned<Self>>),
When(Vec<Spanned<WhenClause<'src>>>),
2024-11-11 01:12:19 +00:00
Match(Box<Spanned<Self>>, Vec<MatchClause<'src>>),
2024-11-22 03:36:57 +00:00
Fn(&'src str, Vec<MatchClause<'src>>, Option<&'src str>),
2024-10-31 20:59:26 +00:00
FnDeclaration(&'src str),
Panic(Box<Spanned<Self>>),
Do(Vec<Spanned<Self>>),
Repeat(Box<Spanned<Self>>, Box<Spanned<Self>>),
2024-11-21 21:41:46 +00:00
Splat(&'src str),
Pair(&'src str, Box<Spanned<Self>>),
Loop(Box<Spanned<Self>>, Vec<MatchClause<'src>>),
Recur(Vec<Spanned<Self>>),
2024-10-31 20:59:26 +00:00
}
2024-11-22 00:54:50 +00:00
impl fmt::Display for Ast<'_> {
2024-10-31 20:59:26 +00:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Ast::Error => write!(f, "Error"),
Ast::Nil => write!(f, "nil"),
Ast::String(s) => write!(f, "String: \"{}\"", s),
2024-12-04 20:03:09 +00:00
Ast::Interpolated(strs) => {
write!(
f,
2024-12-04 23:30:03 +00:00
"Interpolated: \"{}\"",
2024-12-04 20:03:09 +00:00
strs.iter()
.map(|(s, _)| s.to_string())
.collect::<Vec<_>>()
.join("")
)
}
Ast::Boolean(b) => write!(f, "Boolean: {}", b),
Ast::Number(n) => write!(f, "Number: {}", n),
Ast::Keyword(k) => write!(f, "Keyword: :{}", k),
2024-10-31 20:59:26 +00:00
Ast::Word(w) => write!(f, "Word: {}", w),
Ast::Block(b) => write!(
f,
"Block: <{}>",
b.iter()
.map(|(line, _)| line.to_string())
.collect::<Vec<_>>()
.join("\n")
),
Ast::If(cond, then_branch, else_branch) => write!(
f,
"If: {} Then: {} Else: {}",
cond.0, then_branch.0, else_branch.0
),
Ast::Let(pattern, expression) => {
write!(f, "Let: {} = {}", pattern.0, expression.0)
}
Ast::Dict(entries) => write!(
f,
"#{{{}}}",
entries
.iter()
2024-11-22 00:54:50 +00:00
.map(|pair| pair.0.to_string())
2024-10-31 20:59:26 +00:00
.collect::<Vec<_>>()
.join(", ")
),
Ast::List(l) => write!(
f,
"List: [{}]",
l.iter()
.map(|(line, _)| line.to_string())
.collect::<Vec<_>>()
.join("\n")
),
Ast::Tuple(t) | Ast::Arguments(t) => write!(
f,
"Tuple: ({})",
t.iter()
.map(|(line, _)| line.to_string())
.collect::<Vec<_>>()
.join("\n")
),
Ast::Synthetic(root, first, rest) => write!(
f,
"Synth: [{}, {}, {}]",
root.0,
first.0,
rest.iter()
.map(|(term, _)| term.to_string())
.collect::<Vec<_>>()
.join("\n")
),
2024-11-09 19:10:08 +00:00
Ast::When(clauses) => write!(
f,
"When: [{}]",
clauses
.iter()
.map(|clause| clause.0.to_string())
.collect::<Vec<_>>()
.join("\n")
),
2024-11-11 01:12:19 +00:00
Ast::Placeholder => todo!(),
Ast::Box(_name, _rhs) => todo!(),
Ast::Match(value, clauses) => {
write!(
f,
"match: {} with {}",
&value.0.to_string(),
clauses
.iter()
.map(|clause| clause.to_string())
.collect::<Vec<_>>()
.join("\n")
)
}
2024-11-22 03:36:57 +00:00
Ast::Fn(name, clauses, _) => {
2024-11-11 01:12:19 +00:00
write!(
f,
"fn: {}\n{}",
name,
clauses
.iter()
.map(|clause| clause.to_string())
.collect::<Vec<_>>()
.join("\n")
)
}
Ast::FnDeclaration(_name) => todo!(),
Ast::Panic(_expr) => todo!(),
Ast::Do(terms) => {
write!(
f,
"do: {}",
terms
.iter()
.map(|(term, _)| term.to_string())
.collect::<Vec<_>>()
.join(" > ")
)
}
2024-11-11 01:12:19 +00:00
Ast::Repeat(_times, _body) => todo!(),
2024-11-21 21:41:46 +00:00
Ast::Splat(word) => {
write!(f, "splat: {}", word)
}
Ast::Pair(k, v) => {
2024-11-22 00:54:50 +00:00
write!(f, "pair: {} {}", k, v.0)
2024-11-21 21:41:46 +00:00
}
Ast::Loop(init, body) => {
write!(
f,
"loop: {} with {}",
2024-11-22 00:54:50 +00:00
init.0,
2024-11-21 21:41:46 +00:00
body.iter()
.map(|clause| clause.to_string())
.collect::<Vec<_>>()
.join("\n")
)
}
Ast::Recur(args) => {
write!(
f,
"recur: {}",
args.iter()
.map(|(arg, _)| arg.to_string())
.collect::<Vec<_>>()
.join(", ")
)
}
2024-10-31 20:59:26 +00:00
}
}
}
2024-11-11 01:12:19 +00:00
#[derive(Clone, Debug, PartialEq)]
pub struct PairPattern<'src> {
pub key: &'src str,
pub patt: Spanned<Pattern<'src>>,
}
impl<'src> fmt::Display for PairPattern<'src> {
fn fmt(self: &PairPattern<'src>, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "pair pattern: {}: {}", self.key, self.patt.0)
}
}
2024-10-31 20:59:26 +00:00
#[derive(Clone, Debug, PartialEq)]
pub enum Pattern<'src> {
Nil,
Boolean(bool),
Number(f64),
String(&'src str),
2024-12-04 23:30:03 +00:00
Interpolated(Vec<StringPart>),
Keyword(&'src str),
2024-10-31 20:59:26 +00:00
Word(&'src str),
2024-11-09 19:10:08 +00:00
As(&'src str, &'src str),
2024-11-18 18:25:54 +00:00
Splattern(Box<Spanned<Self>>),
2024-10-31 20:59:26 +00:00
Placeholder,
Tuple(Vec<Spanned<Self>>),
List(Vec<Spanned<Self>>),
2024-11-21 01:10:17 +00:00
Pair(&'src str, Box<Spanned<Self>>),
Dict(Vec<Spanned<Self>>),
2024-10-31 20:59:26 +00:00
}
2024-11-22 00:54:50 +00:00
impl fmt::Display for Pattern<'_> {
2024-10-31 20:59:26 +00:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Pattern::Nil => write!(f, "nil"),
Pattern::Boolean(b) => write!(f, "{}", b),
Pattern::Number(n) => write!(f, "{}", n),
Pattern::String(s) => write!(f, "{}", s),
Pattern::Keyword(k) => write!(f, ":{}", k),
2024-10-31 20:59:26 +00:00
Pattern::Word(w) => write!(f, "{}", w),
2024-11-09 19:10:08 +00:00
Pattern::As(w, t) => write!(f, "{} as {}", w, t),
2024-11-22 00:54:50 +00:00
Pattern::Splattern(p) => write!(f, "...{}", p.0),
2024-10-31 20:59:26 +00:00
Pattern::Placeholder => write!(f, "_"),
Pattern::Tuple(t) => write!(
f,
"({})",
t.iter()
.map(|x| x.0.to_string())
.collect::<Vec<_>>()
.join(", ")
),
Pattern::List(l) => write!(
f,
"({})",
l.iter()
.map(|x| x.0.to_string())
.collect::<Vec<_>>()
.join(", ")
),
Pattern::Dict(entries) => write!(
f,
"#{{{}}}",
entries
.iter()
.map(|(pair, _)| pair.to_string())
.collect::<Vec<_>>()
.join(", ")
),
2024-11-21 01:10:17 +00:00
Pattern::Pair(key, value) => write!(f, ":{} {}", key, value.0),
2024-12-04 23:30:03 +00:00
Pattern::Interpolated(strprts) => write!(
f,
"\"{}\"",
strprts
.iter()
.map(|part| part.to_string())
.collect::<Vec<_>>()
.join("")
),
2024-10-31 20:59:26 +00:00
}
}
}
2024-12-04 23:30:03 +00:00
fn is_word_char(c: char) -> bool {
if c.is_ascii_alphanumeric() {
return true;
};
matches!(c, '_' | '/' | '?' | '!')
}
2024-12-04 20:03:09 +00:00
// TODO: write this
// 1. we need an enum for a return type
// either a string part or a word part
// 2. our string types, both patterns and values, now contains a vec of nodes
// 3. this should loop through the string and allow for escaping braces
// consider using Rust-style escapes: {{}}, rather than \{\}
// {{{foo}}}
2024-12-04 23:30:03 +00:00
fn parse_string(s: &str, span: SimpleSpan) -> Result<Vec<Spanned<StringPart>>, String> {
let mut parts = vec![];
let mut current_part = String::new();
let mut start = span.start;
let mut is_word = false;
let mut chars = s.char_indices();
while let Some((i, char)) = chars.next() {
match char {
'{' => {
if is_word {
return Err("interpolations must only contain words".to_string());
};
match chars.next() {
None => return Err("unclosed brace".to_string()),
Some((_, '{')) => current_part.push('{'),
Some((i, c)) => {
if !current_part.is_empty() {
parts.push((
StringPart::Data(current_part),
SimpleSpan::new(start, start + i),
));
};
current_part = String::new();
start = i;
is_word = true;
if c.is_ascii_lowercase() {
current_part.push(c);
} else {
return Err("interpolations must only contain words".to_string());
}
}
}
}
'}' => {
if is_word {
parts.push((
StringPart::Word(current_part),
SimpleSpan::new(start, start + i),
));
current_part = String::new();
start = i;
is_word = false;
} else {
match chars.next() {
None => return Err("unclosed brace".to_string()),
Some((_, c)) => current_part.push(c),
}
}
}
_ => {
if is_word {
if is_word_char(char) {
current_part.push(char)
} else {
return Err("interpolations must only contain words".to_string());
}
} else {
current_part.push(char)
}
}
}
}
parts.push((
if is_word {
StringPart::Word(current_part)
2024-12-04 23:31:13 +00:00
} else if current_part == s {
2024-12-04 23:30:03 +00:00
StringPart::Inline(current_part)
} else {
StringPart::Data(current_part)
},
SimpleSpan::new(start, span.end),
));
Ok(parts)
2024-12-04 20:03:09 +00:00
}
2024-10-31 20:59:26 +00:00
pub fn parser<'src, I>(
) -> impl Parser<'src, I, Spanned<Ast<'src>>, extra::Err<Rich<'src, Token<'src>, Span>>> + Clone
where
I: ValueInput<'src, Token = Token<'src>, Span = Span>,
{
let mut expr = Recursive::declare();
let mut pattern = Recursive::declare();
let mut simple = Recursive::declare();
let mut nonbinding = Recursive::declare();
let separators = recursive(|separators| {
just(Token::Punctuation(","))
.or(just(Token::Punctuation("\n")))
.then(separators.clone().repeated())
});
let terminators = recursive(|terminators| {
just(Token::Punctuation(";"))
.or(just(Token::Punctuation("\n")))
.then(terminators.clone().repeated())
});
let placeholder_pattern =
select! {Token::Punctuation("_") => Pattern::Placeholder}.map_with(|p, e| (p, e.span()));
let word_pattern =
select! { Token::Word(w) => Pattern::Word(w) }.map_with(|w, e| (w, e.span()));
let atom_pattern = select! {
Token::Nil => Pattern::Nil,
Token::Boolean(b) => Pattern::Boolean(b),
Token::Number(n) => Pattern::Number(n),
Token::Keyword(k) => Pattern::Keyword(k),
2024-11-07 23:57:01 +00:00
// todo: actual string patterns
Token::String(s) => Pattern::String(s)
2024-10-31 20:59:26 +00:00
}
.map_with(|a, e| (a, e.span()));
2024-11-18 18:25:54 +00:00
let bare_splat = just(Token::Punctuation("...")).map_with(|_, e| {
(
Pattern::Splattern(Box::new((Pattern::Placeholder, e.span()))),
e.span(),
)
});
2024-11-22 00:54:50 +00:00
let splattable = word_pattern.or(placeholder_pattern);
2024-11-18 18:25:54 +00:00
let patt_splat = just(Token::Punctuation("..."))
.ignore_then(splattable)
.map_with(|x, e| (Pattern::Splattern(Box::new(x)), e.span()));
let splattern = patt_splat.or(bare_splat);
2024-10-31 20:59:26 +00:00
let tuple_pattern = pattern
.clone()
2024-11-18 18:25:54 +00:00
.or(splattern.clone())
2024-10-31 20:59:26 +00:00
.separated_by(separators.clone())
.allow_leading()
.allow_trailing()
.collect()
.delimited_by(just(Token::Punctuation("(")), just(Token::Punctuation(")")))
.map_with(|tuple, e| (Pattern::Tuple(tuple), e.span()))
.labelled("tuple pattern");
let list_pattern = pattern
.clone()
2024-11-18 18:25:54 +00:00
.or(splattern.clone())
2024-10-31 20:59:26 +00:00
.separated_by(separators.clone())
.allow_leading()
.allow_trailing()
.collect()
.delimited_by(just(Token::Punctuation("[")), just(Token::Punctuation("]")))
.map_with(|list, e| (Pattern::List(list), e.span()));
let pair_pattern = select! {Token::Keyword(k) => k}
2024-10-31 20:59:26 +00:00
.then(pattern.clone())
2024-11-21 01:10:17 +00:00
.map_with(|(key, patt), e| (Pattern::Pair(key, Box::new(patt)), e.span()));
2024-10-31 20:59:26 +00:00
let shorthand_pattern = select! {Token::Word(w) => w}.map_with(|w, e| {
(
2024-11-21 01:10:17 +00:00
Pattern::Pair(w, Box::new((Pattern::Word(w), e.span()))),
2024-10-31 20:59:26 +00:00
e.span(),
)
});
let dict_pattern = pair_pattern
.or(shorthand_pattern)
2024-11-21 01:10:17 +00:00
.or(splattern.clone())
2024-10-31 20:59:26 +00:00
.separated_by(separators.clone())
.allow_leading()
.allow_trailing()
.collect()
.delimited_by(
just(Token::Punctuation("#{")),
just(Token::Punctuation("}")),
)
.map_with(|dict, e| (Pattern::Dict(dict), e.span()));
2024-11-09 19:10:08 +00:00
let keyword = select! {Token::Keyword(k) => Ast::Keyword(k),}.map_with(|k, e| (k, e.span()));
let as_pattern = select! {Token::Word(w) => w}
.then_ignore(just(Token::Reserved("as")))
.then(select! {Token::Keyword(k) => k})
.map_with(|(w, t), e| (Pattern::As(w, t), e.span()));
2024-10-31 20:59:26 +00:00
pattern.define(
atom_pattern
2024-11-09 19:10:08 +00:00
.or(as_pattern)
2024-10-31 20:59:26 +00:00
.or(word_pattern)
.or(placeholder_pattern)
.or(tuple_pattern.clone())
.or(list_pattern)
.or(dict_pattern)
.labelled("pattern"),
);
let placeholder =
select! {Token::Punctuation("_") => Ast::Placeholder}.map_with(|p, e| (p, e.span()));
let word = select! { Token::Word(w) => Ast::Word(w) }
.map_with(|w, e| (w, e.span()))
.labelled("word");
let value = select! {
Token::Nil => Ast::Nil,
Token::Boolean(b) => Ast::Boolean(b),
Token::Number(n) => Ast::Number(n),
2024-10-31 20:59:26 +00:00
}
.map_with(|v, e| (v, e.span()));
2024-12-04 23:30:03 +00:00
let string = select! {Token::String(s) => s}.try_map_with(|s, e| {
let parsed = parse_string(s, e.span());
match parsed {
Ok(parts) => match parts[0] {
(StringPart::Inline(_), _) => Ok((Ast::String(s), e.span())),
_ => Ok((Ast::Interpolated(parts), e.span())),
},
Err(msg) => Err(Rich::custom(e.span(), msg)),
}
});
2024-11-22 03:36:57 +00:00
2024-10-31 20:59:26 +00:00
let tuple = simple
.clone()
.separated_by(separators.clone())
.allow_leading()
.allow_trailing()
.collect()
.delimited_by(just(Token::Punctuation("(")), just(Token::Punctuation(")")))
.map_with(|tuple, e| (Ast::Tuple(tuple), e.span()));
let args = simple
.clone()
.or(placeholder)
.separated_by(separators.clone())
.allow_leading()
.allow_trailing()
.collect()
.delimited_by(just(Token::Punctuation("(")), just(Token::Punctuation(")")))
.map_with(|args, e| (Ast::Arguments(args), e.span()));
2024-11-22 00:54:50 +00:00
let synth_root = word.or(keyword);
2024-10-31 20:59:26 +00:00
2024-11-22 00:54:50 +00:00
let synth_term = keyword.or(args);
2024-10-31 20:59:26 +00:00
let synthetic = synth_root
.then(synth_term.clone())
.then(synth_term.clone().repeated().collect())
.map_with(|((root, first), rest), e| {
(
Ast::Synthetic(Box::new(root), Box::new(first), rest),
e.span(),
)
});
2024-11-21 21:41:46 +00:00
let splat = just(Token::Punctuation("..."))
2024-11-22 00:54:50 +00:00
.ignore_then(word)
2024-11-21 21:41:46 +00:00
.map_with(|(w, _), e| {
(
Ast::Splat(if let Ast::Word(w) = w {
w
} else {
unreachable!()
}),
e.span(),
)
});
2024-10-31 20:59:26 +00:00
let list = simple
.clone()
2024-11-21 21:41:46 +00:00
.or(splat.clone())
2024-10-31 20:59:26 +00:00
.separated_by(separators.clone())
.allow_leading()
.allow_trailing()
.collect()
.delimited_by(just(Token::Punctuation("[")), just(Token::Punctuation("]")))
.map_with(|list, e| (Ast::List(list), e.span()));
let pair = select! {Token::Keyword(k) => k}
2024-10-31 20:59:26 +00:00
.then(simple.clone())
2024-11-21 21:41:46 +00:00
.map_with(|(key, value), e| (Ast::Pair(key, Box::new(value)), e.span()));
2024-10-31 20:59:26 +00:00
2024-11-21 21:41:46 +00:00
let shorthand = select! {Token::Word(w) => w}
.map_with(|w, e| (Ast::Pair(w, Box::new((Ast::Word(w), e.span()))), e.span()));
2024-10-31 20:59:26 +00:00
let dict = pair
.or(shorthand)
2024-11-21 21:41:46 +00:00
.or(splat.clone())
2024-10-31 20:59:26 +00:00
.separated_by(separators.clone())
.allow_leading()
.allow_trailing()
.collect()
.delimited_by(
just(Token::Punctuation("#{")),
just(Token::Punctuation("}")),
)
.map_with(|dict, e| (Ast::Dict(dict), e.span()));
2024-11-21 21:41:46 +00:00
let recur = just(Token::Reserved("recur"))
.ignore_then(tuple.clone())
.map_with(|args, e| {
let (Ast::Tuple(args), _) = args else {
unreachable!()
};
(Ast::Recur(args), e.span())
});
2024-10-31 20:59:26 +00:00
simple.define(
synthetic
2024-11-21 21:41:46 +00:00
.or(recur)
2024-10-31 20:59:26 +00:00
.or(word)
.or(keyword)
.or(value)
2024-11-21 21:41:46 +00:00
.or(tuple.clone())
2024-10-31 20:59:26 +00:00
.or(list)
.or(dict)
2024-11-22 03:36:57 +00:00
.or(string)
2024-10-31 20:59:26 +00:00
.labelled("simple expression"),
);
let block = expr
.clone()
.separated_by(terminators.clone())
.allow_leading()
.allow_trailing()
.collect()
.delimited_by(just(Token::Punctuation("{")), just(Token::Punctuation("}")))
.map_with(|block, e| (Ast::Block(block), e.span()))
.recover_with(via_parser(nested_delimiters(
Token::Punctuation("{"),
Token::Punctuation("}"),
[
(Token::Punctuation("("), Token::Punctuation(")")),
(Token::Punctuation("["), Token::Punctuation("]")),
],
|span| (Ast::Error, span),
)));
let if_ = just(Token::Reserved("if"))
.ignore_then(simple.clone())
.then_ignore(just(Token::Reserved("then")))
.then(expr.clone())
.then_ignore(just(Token::Reserved("else")))
.then(expr.clone())
.map_with(|((condition, then_branch), else_branch), e| {
(
Ast::If(
Box::new(condition),
Box::new(then_branch),
Box::new(else_branch),
),
e.span(),
)
});
let when_clause = simple
.clone()
.then_ignore(just(Token::Punctuation("->")))
.then(expr.clone())
.map_with(|(cond, body), e| (WhenClause { cond, body }, e.span()));
2024-10-31 20:59:26 +00:00
let when = just(Token::Reserved("when"))
.ignore_then(
when_clause
.separated_by(terminators.clone())
.allow_trailing()
.allow_leading()
.collect()
.delimited_by(just(Token::Punctuation("{")), just(Token::Punctuation("}"))),
)
.map_with(|clauses, e| (Ast::When(clauses), e.span()));
2024-11-21 01:10:17 +00:00
let guarded_clause = pattern
.clone()
.then_ignore(just(Token::Reserved("if")))
.then(simple.clone())
.then_ignore(just(Token::Punctuation("->")))
.then(expr.clone())
.map_with(|((patt, guard), body), _| MatchClause {
patt,
guard: Some(guard),
body,
});
2024-10-31 20:59:26 +00:00
let match_clause = pattern
.clone()
.then_ignore(just(Token::Punctuation("->")))
.then(expr.clone())
2024-11-11 01:12:19 +00:00
.map_with(|(patt, body), _| MatchClause {
patt,
guard: None,
body,
2024-11-09 19:10:08 +00:00
});
2024-10-31 20:59:26 +00:00
let match_ = just(Token::Reserved("match"))
.ignore_then(simple.clone())
.then_ignore(just(Token::Reserved("with")))
.then(
match_clause
.clone()
2024-11-21 01:10:17 +00:00
.or(guarded_clause)
2024-10-31 20:59:26 +00:00
.separated_by(terminators.clone())
.allow_leading()
.allow_trailing()
.collect()
.delimited_by(just(Token::Punctuation("{")), just(Token::Punctuation("}"))),
)
.map_with(|(expr, clauses), e| (Ast::Match(Box::new(expr), clauses), e.span()));
let conditional = when.or(if_).or(match_);
let panic = just(Token::Reserved("panic!"))
.ignore_then(nonbinding.clone())
.map_with(|expr, e| (Ast::Panic(Box::new(expr)), e.span()));
let do_ = just(Token::Reserved("do"))
.ignore_then(
nonbinding
.clone()
.separated_by(
just(Token::Punctuation(">")).then(just(Token::Punctuation("\n")).repeated()),
)
.collect(),
)
.map_with(|exprs, e| (Ast::Do(exprs), e.span()));
let repeat = just(Token::Reserved("repeat"))
.ignore_then(simple.clone())
.then(block.clone())
.map_with(|(count, body), e| (Ast::Repeat(Box::new(count), Box::new(body)), e.span()));
2024-11-21 01:10:17 +00:00
let fn_guarded = tuple_pattern
.clone()
.then_ignore(just(Token::Reserved("if")))
.then(simple.clone())
.then_ignore(just(Token::Punctuation("->")))
.then(nonbinding.clone())
.map_with(|((patt, guard), body), _| MatchClause {
patt,
body,
guard: Some(guard),
})
.labelled("function clause");
2024-11-21 21:41:46 +00:00
let fn_unguarded = tuple_pattern
2024-10-31 20:59:26 +00:00
.clone()
.then_ignore(just(Token::Punctuation("->")))
.then(nonbinding.clone())
2024-11-11 01:12:19 +00:00
.map_with(|(patt, body), _| MatchClause {
patt,
body,
guard: None,
2024-11-09 19:10:08 +00:00
})
2024-10-31 20:59:26 +00:00
.labelled("function clause");
2024-11-21 21:41:46 +00:00
let fn_clause = fn_guarded.clone().or(fn_unguarded.clone());
2024-10-31 20:59:26 +00:00
let lambda = just(Token::Reserved("fn"))
2024-11-21 21:41:46 +00:00
.ignore_then(fn_unguarded.clone())
2024-11-22 03:36:57 +00:00
.map_with(|clause, e| (Ast::Fn("anonymous", vec![clause], None), e.span()));
2024-10-31 20:59:26 +00:00
2024-11-22 03:36:57 +00:00
let fn_clauses = fn_clause
2024-11-21 21:41:46 +00:00
.clone()
.separated_by(terminators.clone())
.allow_leading()
.allow_trailing()
2024-11-22 03:36:57 +00:00
.collect();
let loop_multiclause = fn_clauses
.clone()
2024-11-21 21:41:46 +00:00
.delimited_by(just(Token::Punctuation("{")), just(Token::Punctuation("}")));
2024-11-21 22:10:50 +00:00
let fn_single_clause = fn_clause.clone().map_with(|c, _| vec![c]);
2024-11-21 21:41:46 +00:00
let r#loop = just(Token::Reserved("loop"))
.ignore_then(tuple.clone())
.then_ignore(just(Token::Reserved("with")))
2024-11-22 03:36:57 +00:00
.then(loop_multiclause.clone().or(fn_single_clause.clone()))
2024-11-21 21:41:46 +00:00
.map_with(|(init, body), e| (Ast::Loop(Box::new(init), body), e.span()));
2024-10-31 20:59:26 +00:00
nonbinding.define(
simple
.clone()
.or(conditional)
.or(block)
.or(lambda)
.or(panic)
.or(do_)
.or(repeat)
2024-11-21 21:41:46 +00:00
.or(r#loop)
2024-10-31 20:59:26 +00:00
.labelled("nonbinding expression"),
);
let let_ = just(Token::Reserved("let"))
.ignore_then(pattern.clone())
.then_ignore(just(Token::Punctuation("=")))
.then(nonbinding.clone())
.map_with(|(pattern, expression), e| {
(Ast::Let(Box::new(pattern), Box::new(expression)), e.span())
});
let box_ = just(Token::Reserved("box"))
2024-11-22 00:54:50 +00:00
.ignore_then(word)
2024-10-31 20:59:26 +00:00
.then_ignore(just(Token::Punctuation("=")))
.then(nonbinding.clone())
.map_with(|(word, expr), e| {
let name = if let Ast::Word(w) = word.0 {
w
} else {
unreachable!()
};
(Ast::Box(name, Box::new(expr)), e.span())
});
let fn_decl = just(Token::Reserved("fn"))
2024-11-22 00:54:50 +00:00
.ignore_then(word)
2024-10-31 20:59:26 +00:00
.map_with(|(word, _), e| {
let name = if let Ast::Word(w) = word {
w
} else {
unreachable!()
};
(Ast::FnDeclaration(name), e.span())
});
let fn_named = just(Token::Reserved("fn"))
2024-11-22 00:54:50 +00:00
.ignore_then(word)
2024-11-21 21:41:46 +00:00
.then(fn_unguarded.clone())
2024-10-31 20:59:26 +00:00
.map_with(|(word, clause), e| {
let name = if let Ast::Word(word) = word.0 {
word
} else {
unreachable!()
};
2024-11-22 03:36:57 +00:00
(Ast::Fn(name, vec![clause], None), e.span())
2024-10-31 20:59:26 +00:00
});
2024-11-22 03:36:57 +00:00
let docstr = select! {Token::String(s) => s};
let fn_multiclause = separators
.clone()
.or_not()
.ignore_then(docstr.or_not())
.then(fn_clauses.clone())
.delimited_by(just(Token::Punctuation("{")), just(Token::Punctuation("}")))
.map_with(|(docstr, clauses), e| (docstr, clauses, e.span()));
2024-10-31 20:59:26 +00:00
let fn_compound = just(Token::Reserved("fn"))
2024-11-22 00:54:50 +00:00
.ignore_then(word)
2024-11-22 03:36:57 +00:00
.then(fn_multiclause)
.map_with(|(word, (docstr, clauses, _)), e| {
2024-10-31 20:59:26 +00:00
let name = if let Ast::Word(word) = word.0 {
word
} else {
unreachable!()
};
2024-11-22 03:36:57 +00:00
(Ast::Fn(name, clauses, docstr), e.span())
2024-10-31 20:59:26 +00:00
});
let fn_ = fn_named.or(fn_compound).or(fn_decl);
let binding = let_.or(box_).or(fn_);
expr.define(binding.or(nonbinding));
let script = expr
.separated_by(terminators.clone())
.allow_trailing()
.allow_leading()
.collect()
.map_with(|exprs, e| (Ast::Block(exprs), e.span()));
script
}