iowo/crates/lang/src/parser.rs

103 lines
3.3 KiB
Rust
Raw Normal View History

2024-04-03 00:08:00 +02:00
use std::ops::Range;
use chumsky::{
error::Rich,
input::{Stream, ValueInput},
prelude::*,
primitive::just,
recursive::recursive,
span::SimpleSpan,
2024-04-07 00:55:12 +02:00
IterParser,
2024-04-03 00:08:00 +02:00
};
use indexmap::IndexMap;
use logos::{Logos, Source};
use crate::tokens::Token;
pub mod ast;
2024-04-03 00:30:11 +02:00
#[cfg(test)]
mod tests;
2024-04-07 00:55:12 +02:00
use self::ast::{Expr, Expression, File};
2024-04-03 00:08:00 +02:00
pub type Span = SimpleSpan;
pub type Spanned<T> = (T, Span);
2024-04-03 00:08:00 +02:00
pub fn parse<'src>(src: &'src str) -> ParseResult<File<'_>, Rich<'_, Token<'_>>> {
let toks: Vec<_> = Token::lexer(src)
.spanned()
.into_iter()
.map(|(t, s)| (t.expect("TODO: add lexer error(s)"), Span::from(s)))
.collect();
let tok_stream = Stream::from_iter(toks).spanned((src.len()..src.len()).into());
2024-04-07 00:55:12 +02:00
parser().parse(tok_stream)
2024-04-03 00:08:00 +02:00
}
2024-04-07 00:55:12 +02:00
pub(crate) fn parser<
'tokens,
'src: 'tokens,
I: ValueInput<'tokens, Token = Token<'src>, Span = Span>,
>() -> impl Parser<'tokens, I, File<'src>, extra::Err<Rich<'tokens, Token<'src>, Span>>> {
2024-04-03 00:08:00 +02:00
let word = select! { Token::Word(word) => word };
let expr = recursive(|expr| {
let var = select! {
Token::VarIdent(name) => (Expr::Var as fn(_) -> _, name),
Token::InputIdent(name) => (Expr::InputVar as fn(_) -> _, name)
}
2024-04-07 00:55:12 +02:00
.map_with(|(item_type, name), extra| Expression::new(item_type(name), extra.span()))
.labelled("variable");
2024-04-03 00:08:00 +02:00
let attrset = word
.map_with(|n, e| (n, e.span()))
.labelled("attr name")
2024-04-03 00:08:00 +02:00
.then_ignore(just(Token::Colon))
.then(expr)
.labelled("attr body")
2024-04-03 00:08:00 +02:00
.separated_by(just(Token::Comma))
.collect::<Vec<_>>()
.map(IndexMap::from_iter)
.delimited_by(just(Token::BracketOpen), just(Token::BracketClose))
.map_with(|v, e| (v, e.span()))
.labelled("attrset");
2024-04-03 00:08:00 +02:00
let node = word
.map_with(|v, e| (v, e.span()))
.then(attrset.clone().or_not())
2024-04-07 00:55:12 +02:00
.map_with(|(name, params), extra| {
Expression::new(Expr::Node(name, params), extra.span())
})
2024-04-03 00:08:00 +02:00
.or(var)
2024-04-07 00:55:12 +02:00
.or(attrset
.map_with(|attrset, extra| Expression::new(Expr::AttrSet(attrset), extra.span())))
.labelled("node");
2024-04-03 00:08:00 +02:00
2024-04-07 00:55:12 +02:00
#[allow(clippy::let_and_return)]
2024-04-03 00:08:00 +02:00
let pipeline = node
.clone()
.then(choice((
just(Token::Pipe).to(Expr::SimplePipe as fn(_, _) -> _),
just(Token::MappingPipe).to(Expr::MappingPipe as fn(_, _) -> _),
just(Token::NullPipe).to(Expr::NullPipe as fn(_, _) -> _),
)))
.repeated()
2024-04-07 00:55:12 +02:00
.foldr_with(node, |(curr, pipe), next, extra| {
Expression::new(pipe(Box::new(curr), Box::new(next)), extra.span())
2024-04-03 00:08:00 +02:00
});
pipeline
});
let decl = just(Token::Def).ignore_then(
word.map_with(|n, e| (n, e.span()))
.then_ignore(just(Token::Equals))
2024-04-07 00:55:12 +02:00
.then(expr.clone().map(|expr| expr))
2024-04-03 00:08:00 +02:00
.then_ignore(just(Token::SemiColon)),
);
2024-04-07 00:55:12 +02:00
expr.map(|expr| File {
decls: IndexMap::from_iter([(("main", (0..0).into()), expr)]),
2024-04-03 00:08:00 +02:00
})
.or(decl.repeated().collect::<Vec<_>>().map(|decls| File {
decls: IndexMap::from_iter(decls),
}))
}