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
|
|
|
|
2024-04-03 17:00:20 +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()
|
|
|
|
.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| {
|
2024-04-08 15:43:42 +02:00
|
|
|
let lit = select! {
|
|
|
|
Token::Int(i) = e => Expression::new(Expr::Lit(ast::Lit::Int(i.parse().unwrap())), e.span()),
|
|
|
|
Token::Float(f) = e => Expression::new(Expr::Lit(ast::Lit::Float(f.parse().unwrap())), e.span()),
|
|
|
|
};
|
2024-04-03 00:08:00 +02:00
|
|
|
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()))
|
2024-04-03 17:00:20 +02:00
|
|
|
.labelled("variable");
|
2024-04-03 00:08:00 +02:00
|
|
|
|
|
|
|
let attrset = word
|
|
|
|
.map_with(|n, e| (n, e.span()))
|
2024-04-03 17:00:20 +02:00
|
|
|
.labelled("attr name")
|
2024-04-03 00:08:00 +02:00
|
|
|
.then_ignore(just(Token::Colon))
|
|
|
|
.then(expr)
|
2024-04-03 17:00:20 +02:00
|
|
|
.labelled("attr body")
|
2024-04-03 00:08:00 +02:00
|
|
|
.separated_by(just(Token::Comma))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.map(IndexMap::from_iter)
|
2024-04-08 14:04:52 +02:00
|
|
|
.delimited_by(just(Token::BraceOpen), just(Token::BraceClose))
|
2024-04-03 17:00:20 +02:00
|
|
|
.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())))
|
2024-04-03 17:00:20 +02:00
|
|
|
.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),
|
|
|
|
}))
|
|
|
|
}
|
2024-04-08 14:04:52 +02:00
|
|
|
|
|
|
|
pub mod asg {
|
|
|
|
use petgraph::graph::DiGraph;
|
|
|
|
|
|
|
|
use super::Spanned;
|
|
|
|
|
|
|
|
pub struct Asg<'src> {
|
|
|
|
graph: DiGraph<AsgNode<'src>, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum AsgNode<'src> {
|
|
|
|
Node(Spanned<&'src str>),
|
|
|
|
}
|
|
|
|
}
|