Diffstat (limited to 'src/parser.rs')
-rw-r--r--src/parser.rs77
1 files changed, 21 insertions, 56 deletions
diff --git a/src/parser.rs b/src/parser.rs
index e90f11e..b1cf87d 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -1,10 +1,6 @@
pub mod types;
use crate::lexer::{Lexer, Token};
-use chumsky::{
- input::{SpannedInput, Stream},
- prelude::*,
- Parser,
-};
+use chumsky::{input::Stream, prelude::*, Parser};
pub mod fun;
pub mod util;
use types::*;
@@ -15,6 +11,7 @@ use self::fun::Function;
impl<'s> Value<'s> {
pub fn parse() -> parser![Self] {
select! {
+ Token::Char(x) => Value::Int(x as _),
Token::Int(x) => Value::Int(x),
Token::Float(x) => Value::Float(x),
Token::String(s) => Value::String(s),
@@ -26,80 +23,48 @@ impl<'s> Value<'s> {
impl<'s> Expr<'s> {
pub fn parse() -> parser![Self] {
recursive::<_, Expr, _, _, _>(|expr| {
- let inline_expr = recursive(|inline_expr| {
- let val = select! {
- Token::Int(x) => Expr::Value(Value::Int(x)),
- Token::Float(x) => Expr::Value(Value::Float(x)),
- Token::String(s) => Expr::Value(Value::String(s)),
- }
- .labelled("value");
-
- choice((t![ident].map(Expr::Ident), val)).boxed()
- });
+ let inline_expr = Value::parse().map(Expr::Value);
let λ = Λ::parse(expr.clone());
- let decl = t![ident]
- .then_ignore(t![<-])
- .then(inline_expr.clone().or(λ.clone().map(Expr::Lambda)))
- .map(|(name, body)| Expr::Let {
- name,
- rhs: Box::new(body),
- })
- .labelled("declare")
- .boxed();
-
- let r#if = t![if]
- .ignore_then(
- expr.clone()
- .then(t![else].or_not().ignore_then(expr.clone().or_not()))
- .delimited_by(t!['('], t![')']),
- )
- .map(|(a, b)| Expr::If {
- then: Box::new(a),
- or: Box::new(b.unwrap_or_else(|| Expr::Value(Value::Unit))),
- })
- .labelled("🐋")
- .boxed();
choice((
- decl,
- r#if,
inline_expr,
- λ.clone().map(Expr::Lambda),
- Function::parse(λ).map(Expr::Function),
+ Function::parse(λ.clone()).map(Expr::Function),
+ λ.map(Expr::Lambda),
))
.labelled("expr")
})
}
}
+impl<'s> Ast<'s> {
+ pub fn parse() -> parser![Self] {
+ Expr::parse().repeated().collect().map(Ast::Module)
+ }
+}
#[test]
fn parse_expr() {
- parse_s("a ← λ ( +- 🍴 )", Expr::parse());
- // dbg!(Expr::parse().parse(code("a ← λ ( +- 🍴 )")).unwrap());
+ // parse_s("a ← λ ( +-🍴 )", Expr::parse());
+ let src = r#"⏫⏫⏫
+ "#;
+ println!(
+ "{:?}",
+ crate::lexer::lex(src).map(|x| x.0).collect::<Vec<_>>()
+ );
+ parse_s(src, Ast::parse());
}
-pub fn stream(lexer: Lexer<'_>, len: usize) -> SpannedInput<Token<'_>, Span, Stream<Lexer<'_>>> {
- Stream::from_iter(lexer).spanned((len..len).into())
+pub fn stream(lexer: Lexer<'_>, len: usize) -> types::Input<'_> {
+ Stream::from_iter(lexer).map(SimpleSpan::new((), len..len), |x| x)
}
-#[cfg(test)]
-pub fn code<'s>(x: &'s str) -> SpannedInput<Token<'s>, Span, Stream<Lexer<'s>>> {
+pub fn code<'s>(x: &'s str) -> types::Input<'s> {
stream(crate::lexer::lex(x), x.len())
}
-#[cfg(test)]
pub fn parse_s<'s, T: std::fmt::Debug>(x: &'s str, p: parser![T]) -> T {
match crate::ui::display(p.parse(code(x)).into_result(), x) {
Ok(x) => dbg!(x),
Err(()) => panic!(),
}
}
-
-pub fn parse(tokens: Lexer<'_>, len: usize) -> Result<Ast<'_>, Vec<Error<'_>>> {
- parser().parse(stream(tokens, len)).into_result()
-}
-
-fn parser<'s>() -> parser![Ast<'s>] {
- Expr::parse().repeated().collect().map(Ast::Module)
-}