//! The parser takes a stream of [Token]s from the [Lexer], and turns them into [crate::ast] nodes. use crate::{ ast::*, lexer::{LexError, Lexer}, span::Span, token::{Lexeme, TKind, Token}, }; use std::{error::Error, fmt::Display, vec}; pub mod numeric; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ParseError { FromLexer(LexError), Expected(TKind, Span), NotLiteral(TKind, Span), NotPattern(TKind, Span), NotType(TKind, Span), NotPrefix(TKind, Span), NotInfix(TKind, Span), NotPostfix(TKind, Span), } impl Error for ParseError {} impl Display for ParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::FromLexer(e) => e.fmt(f), Self::Expected(tk, loc) => write!(f, "{loc}: Expected {tk:?}."), Self::NotLiteral(tk, loc) => write!(f, "{loc}: {tk:?} is not valid in a literal."), Self::NotPattern(tk, loc) => write!(f, "{loc}: {tk:?} is not valid in a pattern."), Self::NotType(tk, loc) => write!(f, "{loc}: {tk:?} is not valid in a type."), Self::NotPrefix(tk, loc) => write!(f, "{loc}: {tk:?} is not a prefix operator."), Self::NotInfix(tk, loc) => write!(f, "{loc}: {tk:?} is not a infix operator."), Self::NotPostfix(tk, loc) => write!(f, "{loc}: {tk:?} is not a postfix operator."), } } } pub type PResult = Result; #[derive(Debug)] pub struct Parser<'t> { pub lexer: Lexer<'t>, pub next_tok: Option, pub last_loc: Span, pub elide_do: bool, } impl<'t> Parser<'t> { /// Constructs a new Parser pub fn new(lexer: Lexer<'t>) -> Self { Self { lexer, next_tok: None, last_loc: Span::default(), elide_do: false } } /// The identity function. This exists to make production chaining easier. pub fn then(&self, t: T) -> T { t } pub fn span(&self) -> Span { self.last_loc } /// Parses a value that implements the [Parse] trait. pub fn parse>(&mut self, level: T::Prec) -> PResult { Parse::parse(self, level) } /// Peeks the next [Token]. Returns [ParseError::FromLexer] on lexer error. pub fn peek(&mut self) -> PResult<&Token> { let next_tok = match self.next_tok.take() { Some(tok) => tok, None => match self.lexer.scan() { Ok(tok) => tok, Err(e) => Err(ParseError::FromLexer(e))?, }, }; self.last_loc = next_tok.span; self.next_tok = Some(next_tok); Ok(self.next_tok.as_ref().expect("should have token")) } /// Peeks the next token if it matches the `expected` [TKind] pub fn peek_if(&mut self, expected: TKind) -> Option<&Token> { self.peek().into_iter().find(|tok| tok.kind == expected) } /// Consumes and returns the currently-peeked [Token]. pub fn take(&mut self) -> Option { let tok = self.next_tok.take(); self.elide_do = matches!(tok, Some(Token { kind: TKind::RCurly, .. })); tok } /// Consumes the currently-peeked [Token], returning its lexeme without cloning. pub fn take_lexeme(&mut self) -> Option { self.take().map(|tok| tok.lexeme) } #[allow(clippy::should_implement_trait)] pub fn next(&mut self) -> PResult { self.peek()?; Ok(self.take().expect("should have token here")) } /// Consumes and returns the next [Token] if it matches the `expected` [TKind] pub fn next_if(&mut self, expected: TKind) -> PResult { let token = self.peek()?; if token.kind == expected { Ok(self.take().expect("should have token here")) } else { Err(ParseError::Expected(expected, token.span)) } } /// Parses a list of P separated by `sep` tokens, ending in an `end` token. /// ```ignore /// List = (T `sep`)* T? `end` ; /// ``` pub fn list>( &mut self, mut elems: Vec

, level: P::Prec, sep: TKind, end: TKind, ) -> PResult> { // TODO: This loses lexer errors while self.peek_if(end).is_none() { elems.push(self.parse(level.clone())?); if self.next_if(sep).is_err() { break; } } self.next_if(end)?; Ok(elems) } /// Parses a list of one or more P at level `level`, separated by `sep` tokens /// ```ignore /// UnterminatedList

= P (`sep` P)* /// ``` pub fn list_bare>( &mut self, mut elems: Vec

, level: P::Prec, sep: TKind, ) -> PResult> { loop { elems.push(self.parse(level.clone())?); if self.next_if(sep).is_err() { break Ok(elems); } } } /// Parses into an [`Option

`] if the next token is `next` pub fn opt_if>(&mut self, level: P::Prec, next: TKind) -> PResult> { Ok(match self.next_if(next) { Ok(_) => Some(self.parse(level)?), Err(_) => None, }) } /// Parses a P unless the next token is `end` pub fn opt>(&mut self, level: P::Prec, end: TKind) -> PResult> { let out = match self.peek_if(end) { None => Some(self.parse(level)?), Some(_) => None, }; self.next_if(end)?; Ok(out) } pub fn consume_if(&mut self, next: TKind) -> PResult<&mut Self> { self.next_if(next)?; Ok(self) } /// Consumes the currently peeked token without returning it. pub fn consume(&mut self) -> &mut Self { self.next_tok = None; self } } pub trait Parse<'t> { type Prec: Clone; fn parse(p: &mut Parser<'t>, _level: Self::Prec) -> PResult where Self: Sized; } impl<'t> Parse<'t> for FqPath { // ugly hack: provide a partial path to parse() type Prec = (); fn parse(p: &mut Parser<'t>, _level: Self::Prec) -> PResult { let mut parts = vec![]; if p.next_if(TKind::ColonColon).is_ok() { parts.push("".into()); // the "root" } loop { parts.push( p.next_if(TKind::Identifier)? .lexeme .string() .expect("Identifier should have String"), ); if p.next_if(TKind::ColonColon).is_err() { break; } } Ok(FqPath { parts }) } } impl<'t> Parse<'t> for Literal { type Prec = (); fn parse(p: &mut Parser<'t>, _level: ()) -> PResult { let tok = p.peek()?; Ok(match tok.kind { TKind::True => p.consume().then(Literal::Bool(true)), TKind::False => p.consume().then(Literal::Bool(false)), TKind::Character => Literal::Char( p.take_lexeme() .expect("should have Token") .char() .expect("should have one char in char literal"), ), TKind::Integer => { let Token { lexeme, span, .. } = p.take().expect("should have Token"); let Lexeme::Integer(int, _) = lexeme else { Err(ParseError::Expected(TKind::Integer, span))? }; Literal::Int(int) } TKind::String => Literal::Str({ let Token { lexeme, span, .. } = p.take().expect("should have Token"); lexeme .string() .ok_or(ParseError::Expected(TKind::String, span))? }), _ => Err(ParseError::Expected(TKind::Integer, tok.span))?, }) } } #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] pub enum PPrec { Min, Alt, Tuple, Typed, Range, Max, } impl PPrec { fn next(self) -> Self { match self { Self::Min => Self::Alt, Self::Alt => Self::Tuple, Self::Tuple => Self::Typed, Self::Typed => Self::Range, Self::Range => Self::Max, Self::Max => Self::Max, } } } enum PatPs { Typed, Op(PatOp), } fn pat_from_infix(token: &Token) -> Option<(PatPs, PPrec)> { Some(match token.kind { TKind::DotDot => (PatPs::Op(PatOp::RangeEx), PPrec::Range), TKind::Colon => (PatPs::Typed, PPrec::Typed), TKind::Comma => (PatPs::Op(PatOp::Tuple), PPrec::Tuple), TKind::Bar => (PatPs::Op(PatOp::Alt), PPrec::Alt), _ => None?, }) } impl<'t> Parse<'t> for Pat { type Prec = PPrec; fn parse(p: &mut Parser<'t>, level: PPrec) -> PResult { while p.next_if(TKind::Comment).is_ok() {} let tok = p.peek()?; // Prefix let mut head = match tok.kind { TKind::True | TKind::False | TKind::Character | TKind::Integer | TKind::String => { Pat::Lit(p.parse(())?) } TKind::Bar => p.consume().parse(level)?, TKind::Identifier => match tok.lexeme.str() { Some("_") => p.consume().then(Pat::Ignore), _ => { let mut path: FqPath = p.parse(())?; // TODO: make these postfix. match p.peek().map(|t| t.kind) { Ok(TKind::LParen) => Pat::TupStruct(path, p.parse(PPrec::Typed)?), Ok(TKind::LCurly) => Pat::Struct( path, p.consume() .opt(PPrec::Alt, TKind::RCurly)? .unwrap_or_else(|| Box::new(Pat::Op(PatOp::Tuple, vec![]))), ), Ok(_) | Err(ParseError::FromLexer(LexError { pos: _, res: "EOF" })) => { match path.parts.len() { 1 => Self::Name(path.parts.pop().expect("name has 1 part")), _ => Self::Path(path), } } Err(e) => Err(e)?, } } }, TKind::Grave => Pat::MetId(p.consume().next()?.lexeme.to_string()), TKind::DotDot => Pat::Op( PatOp::Rest, // Identifier in Rest position always becomes binder match p.consume().peek()?.kind { TKind::Identifier => vec![Pat::Name( p.take_lexeme() .expect("should have lexeme") .string() .expect("should be string"), )], TKind::Grave | TKind::Integer | TKind::Character => vec![p.parse(level)?], _ => vec![], }, ), TKind::LParen => Pat::Op( PatOp::Tuple, p.consume() .list(vec![], PPrec::Typed, TKind::Comma, TKind::RParen)?, ), TKind::LBrack => Pat::Op( PatOp::Slice, p.consume() .list(vec![], PPrec::Typed, TKind::Comma, TKind::RBrack)?, ), _ => Err(ParseError::NotPattern(tok.kind, tok.span))?, }; while let Ok(tok) = p.peek() && let Some((op, prec)) = pat_from_infix(tok) && level <= prec { let kind = tok.kind; head = match op { PatPs::Typed => Pat::Typed(head.into(), p.consume().parse(())?), PatPs::Op(op @ PatOp::RangeEx) => Pat::Op( op, match p.consume().peek().map(|t| t.kind) { Ok(TKind::Integer | TKind::Character | TKind::Identifier) => { vec![head, p.parse(prec.next())?] } _ => vec![head], }, ), PatPs::Op(op) => Pat::Op(op, p.consume().list_bare(vec![head], prec.next(), kind)?), } } Ok(head) } } impl<'t> Parse<'t> for Ty { type Prec = (); fn parse(p: &mut Parser<'t>, level: Self::Prec) -> PResult where Self: Sized { let tok = p.peek()?; let head = match tok.kind { TKind::Identifier => match tok.lexeme.str() { Some("_") => p.consume().then(Ty::Infer), _ => Ty::Named(p.parse(())?), }, TKind::LBrack => { let ty = p.consume().parse(level)?; match p.next()? { Token { kind: TKind::Semi, .. } => { let ty = Ty::Array(ty, p.parse(Prec::Binary.next())?); p.next_if(TKind::RBrack)?; ty } Token { kind: TKind::RBrack, .. } => Ty::Slice(ty), tok => Err(ParseError::NotType(tok.kind, tok.span))?, } } TKind::Fn => { p.consume().consume_if(TKind::LParen)?; let mut tys = p.list(vec![], (), TKind::Comma, TKind::RParen)?; tys.push(match p.next_if(TKind::Arrow) { Ok(_) => p.parse(())?, _ => Ty::Tuple(vec![]), }); Ty::Fn(tys) } TKind::LParen => { let mut tys = p.consume().list(vec![], (), TKind::Comma, TKind::RParen)?; match p.next_if(TKind::Arrow) { Ok(_) => { tys.push(p.parse(())?); Ty::Fn(tys) } _ => Ty::Tuple(tys), } } _ => Err(ParseError::NotType(tok.kind, tok.span))?, }; Ok(match p.next_if(TKind::Arrow) { Ok(_) => Ty::Fn(vec![head, p.parse(())?]), _ => head, }) } } /// Organizes the precedence hierarchy for syntactic elements #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] pub enum Prec { Min, /// The Semicolon Operator gets its own precedence level Do, /// An assignment Assign, /// Constructor for a tuple Tuple, /// The body of a function, conditional, etc. Body, /// Constructor for a struct Make, /// The conditional of an `if` or `while` (which is really an `if`) Logical, /// The short-circuiting "boolean or" operator LogOr, /// The short-circuiting "boolean and" operator LogAnd, /// Value comparison operators Compare, /// Constructor for a Range Range, /// Binary/bitwise operators Binary, /// Bit-shifting operators Shift, /// Addition and Subtraction operators Factor, /// Multiplication, Division, and Remainder operators Term, /// Negation, (De)reference, Try Unary, /// Place-projection operators Project, /// Array/Call subscripting and reference Extend, Max, } impl Prec { pub const MIN: usize = Prec::Min.value(); pub const fn value(self) -> usize { self as usize * 2 } pub const fn prev(self) -> usize { match self { Self::Assign => self.value() + 1, _ => self.value(), } } pub const fn next(self) -> usize { match self { Self::Assign => self.value(), _ => self.value() + 1, } } } /// PseudoOperator: fake operators used to give certain tokens special behavior. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Ps { Id, // Identifier Mid, // MetaIdentifier Lit, // Literal Let, // let Pat = Expr Const, // const Pat = Expr Struct, // struct { Pat } | struct ( Pat ) For, // for Pat in Expr Expr else Expr Fn, // fn ( Pat,* ) Expr Lambda0, // || Expr Lambda, // | Pat,* | Expr DoubleRef, // && Expr Make, // Expr{ Expr,* } Match, // match Expr { MatchArm,* } Mod, // mod Ty Expr ImplicitDo, // An implicit semicolon ExplicitDo, // An explicit leading semicolon End, // Produces an empty value. Op(Op), // A normal [ast::Op] } fn from_prefix(token: &Token) -> PResult<(Ps, Prec)> { Ok(match token.kind { TKind::Do => (Ps::Op(Op::Do), Prec::Do), TKind::Semi => (Ps::ExplicitDo, Prec::Do), TKind::Identifier | TKind::ColonColon => (Ps::Id, Prec::Max), TKind::Grave => (Ps::Mid, Prec::Max), TKind::True | TKind::False | TKind::Character | TKind::Integer | TKind::String => { (Ps::Lit, Prec::Max) } TKind::Public => (Ps::Op(Op::Pub), Prec::Body), TKind::For => (Ps::For, Prec::Body), TKind::Fn => (Ps::Fn, Prec::Body), TKind::Match => (Ps::Match, Prec::Body), TKind::Macro => (Ps::Op(Op::Macro), Prec::Assign), TKind::Module => (Ps::Mod, Prec::Body), TKind::Let => (Ps::Let, Prec::Tuple), TKind::Const => (Ps::Const, Prec::Body), TKind::Struct => (Ps::Struct, Prec::Body), TKind::Loop => (Ps::Op(Op::Loop), Prec::Body), TKind::If => (Ps::Op(Op::If), Prec::Body), TKind::While => (Ps::Op(Op::While), Prec::Body), TKind::Break => (Ps::Op(Op::Break), Prec::Body), TKind::Return => (Ps::Op(Op::Return), Prec::Body), TKind::LCurly => (Ps::Op(Op::Block), Prec::Min), TKind::RCurly => (Ps::End, Prec::Do), TKind::LBrack => (Ps::Op(Op::Array), Prec::Tuple), TKind::RBrack => (Ps::End, Prec::Tuple), TKind::LParen => (Ps::Op(Op::Group), Prec::Min), TKind::RParen => (Ps::End, Prec::Tuple), TKind::Amp => (Ps::Op(Op::Refer), Prec::Extend), TKind::AmpAmp => (Ps::DoubleRef, Prec::Extend), TKind::Bang => (Ps::Op(Op::Not), Prec::Unary), TKind::BangBang => (Ps::Op(Op::Identity), Prec::Unary), TKind::Bar => (Ps::Lambda, Prec::Body), TKind::BarBar => (Ps::Lambda0, Prec::Body), TKind::DotDot => (Ps::Op(Op::RangeEx), Prec::Range), TKind::DotDotEq => (Ps::Op(Op::RangeIn), Prec::Range), TKind::Minus => (Ps::Op(Op::Neg), Prec::Unary), TKind::Plus => (Ps::Op(Op::Identity), Prec::Unary), TKind::Star => (Ps::Op(Op::Deref), Prec::Unary), kind => Err(ParseError::NotPrefix(kind, token.span))?, }) } fn from_infix(token: &Token) -> PResult<(Ps, Prec)> { Ok(match token.kind { TKind::Semi => (Ps::Op(Op::Do), Prec::Do), // the inspiration TKind::As => (Ps::Op(Op::As), Prec::Body), TKind::Comma => (Ps::Op(Op::Tuple), Prec::Tuple), TKind::Dot => (Ps::Op(Op::Dot), Prec::Project), TKind::AmpAmp => (Ps::Op(Op::LogAnd), Prec::LogAnd), TKind::BarBar => (Ps::Op(Op::LogOr), Prec::LogOr), TKind::Question => (Ps::Op(Op::Try), Prec::Unary), TKind::LParen => (Ps::Op(Op::Call), Prec::Extend), TKind::LBrack => (Ps::Op(Op::Index), Prec::Extend), TKind::LCurly => (Ps::Make, Prec::Make), TKind::RParen | TKind::RBrack | TKind::RCurly => (Ps::End, Prec::Max), TKind::Eq => (Ps::Op(Op::Set), Prec::Assign), TKind::XorXor => (Ps::Op(Op::LogXor), Prec::Logical), TKind::Lt => (Ps::Op(Op::Lt), Prec::Compare), TKind::LtEq => (Ps::Op(Op::Leq), Prec::Compare), TKind::EqEq => (Ps::Op(Op::Eq), Prec::Compare), TKind::BangEq => (Ps::Op(Op::Neq), Prec::Compare), TKind::GtEq => (Ps::Op(Op::Geq), Prec::Compare), TKind::Gt => (Ps::Op(Op::Gt), Prec::Compare), TKind::DotDot => (Ps::Op(Op::RangeEx), Prec::Range), TKind::DotDotEq => (Ps::Op(Op::RangeIn), Prec::Range), TKind::Amp => (Ps::Op(Op::And), Prec::Binary), TKind::Xor => (Ps::Op(Op::Xor), Prec::Binary), TKind::Bar => (Ps::Op(Op::Or), Prec::Binary), TKind::LtLt => (Ps::Op(Op::Shl), Prec::Shift), TKind::GtGt => (Ps::Op(Op::Shr), Prec::Shift), TKind::Plus => (Ps::Op(Op::Add), Prec::Factor), TKind::Minus => (Ps::Op(Op::Sub), Prec::Factor), TKind::Star => (Ps::Op(Op::Mul), Prec::Term), TKind::Slash => (Ps::Op(Op::Div), Prec::Term), TKind::Rem => (Ps::Op(Op::Rem), Prec::Term), _ => (Ps::ImplicitDo, Prec::Do), }) } impl<'t> Parse<'t> for Const { type Prec = (); fn parse(p: &mut Parser<'t>, _level: Self::Prec) -> PResult { Ok(Self( p.consume().parse(PPrec::Tuple)?, p.consume_if(TKind::Eq)?.parse(Prec::Tuple.value())?, )) } } impl<'t> Parse<'t> for Struct { type Prec = (); fn parse(p: &mut Parser<'t>, _level: Self::Prec) -> PResult { let value = p.consume().parse(PPrec::Min)?; Ok(Self(value)) } } impl<'t> Parse<'t> for Fn { type Prec = (); fn parse(p: &mut Parser<'t>, _level: Self::Prec) -> PResult { match p.consume().next_if(TKind::Identifier) { Ok(Token { lexeme, .. }) => Ok(Self( lexeme.string(), p.parse(PPrec::Tuple)?, p.opt_if((), TKind::Arrow)?.unwrap_or(Ty::Tuple(vec![])), p.parse(Prec::Body.next())?, )), _ => Ok(Self( None, Pat::Op( PatOp::Tuple, p.consume_if(TKind::LParen)?.list( vec![], PPrec::Tuple, TKind::Comma, TKind::RParen, )?, ), p.opt_if((), TKind::Arrow)?.unwrap_or(Ty::Tuple(vec![])), p.parse(Prec::Body.next())?, )), } } } impl<'t> Parse<'t> for Let { type Prec = (); fn parse(p: &mut Parser<'t>, _level: Self::Prec) -> PResult { let pat = p.consume().parse(PPrec::Tuple)?; if p.next_if(TKind::Eq).is_err() { return Ok(Self(pat, vec![])); } let body = p.parse(Prec::Tuple.value())?; if p.next_if(TKind::Else).is_err() { return Ok(Self(pat, vec![body])); } Ok(Self(pat, vec![body, p.parse(Prec::Body.next())?])) } } impl<'t> Parse<'t> for Match { type Prec = (); fn parse(p: &mut Parser<'t>, _level: Self::Prec) -> PResult { Ok(Self(p.consume().parse(Prec::Logical.value())?, { p.next_if(TKind::LCurly)?; p.list(vec![], Prec::Body.next(), TKind::Comma, TKind::RCurly)? })) } } impl<'t> Parse<'t> for MatchArm { type Prec = usize; fn parse(p: &mut Parser<'t>, level: usize) -> PResult { p.next_if(TKind::Bar).ok(); Ok(MatchArm( p.parse(PPrec::Min)?, p.consume_if(TKind::FatArrow)?.parse(level)?, )) } } impl<'t> Parse<'t> for MakeArm { type Prec = (); fn parse(p: &mut Parser<'t>, _level: ()) -> PResult { Ok(MakeArm( p.next_if(TKind::Identifier)? .lexeme .string() .expect("Identifier should have String"), { p.next_if(TKind::Colon) .ok() .map(|_| p.parse(Prec::Body.value())) .transpose()? }, )) } } impl<'t> Parse<'t> for Mod { type Prec = (); fn parse(p: &mut Parser<'t>, _level: Self::Prec) -> PResult { let ty = p.consume().parse(())?; let body = p.parse(Prec::Body.value())?; Ok(Mod(ty, body)) } } fn parse_for<'t>(p: &mut Parser<'t>, _level: ()) -> PResult { // for Pat let pat = p.consume().parse(PPrec::Tuple)?; // in Expr let iter: Anno = p.consume_if(TKind::In)?.parse(Prec::Logical.next())?; let cspan = iter.1; // Expr let pass: Anno = p.parse(Prec::Body.next())?; let pspan = pass.1; // else Expr? let fail = match p.next_if(TKind::Else) { Ok(_) => p.parse(Prec::Body.next())?, _ => Expr::Op(Op::Tuple, vec![]).anno(pspan), }; let fspan = fail.1; /* for `pat in `iter `pass else `fail ==> match (`iter).into_iter() { #iter => loop match #iter.next() { None => break `fail, Some(`pat) => `pass, }, } */ // let mut tmp_p = Parser::new(Lexer::new( // "match `iter.into_iter() { // `iterator => loop match `iterator.next() { // None => break `fail, // Some(`pat) => `pass, // }, // }", // )); // let mut template: Expr = tmp_p.parse(Prec::MIN)?; // let mut subst = Subst:: { exp: Default::default(), pat: Default::default() }; // subst.exp.extend([ // ("iterator".into(), Expr::Id("#iter".into())), // ("iter".into(), iter.0), // ("fail".into(), fail.0), // ("pass".into(), pass.0), // ]); // subst.pat.extend([ // ("iterator".into(), Pat::Name("#iter".into())), // ("pat".into(), pat), // ]); // template.apply(&subst); // Ok(template) Ok(Expr::Match(Box::new(Match( Expr::Op( Op::Dot, vec![ iter, Expr::Op(Op::Call, vec![Expr::Id("into_iter".into()).anno(cspan)]).anno(cspan), ], ) .anno(cspan), vec![MatchArm( Pat::Name("#iter".into()), Expr::Op( Op::Loop, vec![ Expr::Match(Box::new(Match( Expr::Op( Op::Dot, vec![ Expr::Id("#iter".into()).anno(cspan), Expr::Op(Op::Call, vec![Expr::Id("next".into()).anno(cspan)]) .anno(cspan), ], ) .anno(cspan), vec![ MatchArm( Pat::Name("None".into()), Expr::Op(Op::Break, vec![fail]).anno(fspan), ), MatchArm( Pat::TupStruct( "Some".into(), Box::new(Pat::Op(PatOp::Tuple, vec![pat])), ), pass, ), ], ))) .anno(pspan), ], ) .anno(pspan), )], )))) } impl<'t> Parse<'t> for Expr { type Prec = usize; /// Parses an [Expr]ession. /// /// The `level` parameter indicates the operator binding level of the expression. fn parse(p: &mut Parser<'t>, level: usize) -> PResult { const MIN: usize = Prec::MIN; while p.next_if(TKind::Comment).is_ok() {} // Prefix let tok = p.peek()?; let ((op, prec), span) = (from_prefix(tok)?, tok.span); let mut head = match op { // Empty is returned when a block finisher is an expr prefix. // It's the only expr that doesn't consume. Ps::End if level == prec.next() => Expr::Op(Op::Tuple, vec![]), Ps::End => Err(ParseError::NotPrefix(tok.kind, span))?, Ps::ExplicitDo => { p.consume(); Expr::Op(Op::Tuple, vec![]) } Ps::Id => Expr::Id(p.parse(())?), Ps::Mid => Expr::MetId(p.consume().next()?.lexeme.to_string()), Ps::Lit => Expr::Lit(p.parse(())?), Ps::Let => Expr::Let(p.parse(())?), Ps::For => parse_for(p, ())?, Ps::Const => Expr::Const(p.parse(())?), Ps::Struct => Expr::Struct(p.parse(())?), Ps::Match => Expr::Match(p.parse(())?), Ps::Mod => Expr::Mod(p.parse(())?), Ps::Op(Op::Block) => Expr::Op( Op::Block, p.consume().opt(MIN, TKind::RCurly)?.into_iter().collect(), ), Ps::Op(Op::Array) => parse_array(p)?, Ps::Op(Op::Group) => match p.consume().opt(MIN, TKind::RParen)? { Some(value) => Expr::Op(Op::Group, vec![value]), None => Expr::Op(Op::Tuple, vec![]), }, Ps::Op(op @ (Op::If | Op::While)) => { p.consume(); let exprs = vec![ // conditional restricted to Logical operators or above p.parse(Prec::Logical.value())?, p.parse(prec.next())?, match p.peek() { Ok(Token { kind: TKind::Else, .. }) => p.consume().parse(prec.next())?, _ => Expr::Op(Op::Tuple, vec![]).anno(span.merge(p.span())), }, ]; Expr::Op(op, exprs) } Ps::Fn => Expr::Fn(p.parse(())?), Ps::Lambda => Expr::Fn(Box::new(Fn( None, p.consume() .opt(PPrec::Tuple, TKind::Bar)? .unwrap_or(Pat::Op(PatOp::Tuple, vec![])), p.opt_if((), TKind::Arrow)?.unwrap_or(Ty::Infer), p.parse(Prec::Body.next())?, ))), Ps::Lambda0 => Expr::Fn(Box::new(Fn( None, Pat::Op(PatOp::Tuple, vec![]), p.consume().opt_if((), TKind::Arrow)?.unwrap_or(Ty::Infer), p.parse(Prec::Body.next())?, ))), Ps::DoubleRef => p.consume().parse(prec.next()).map(|Anno(expr, span)| { Expr::Op( Op::Refer, vec![Anno(Expr::Op(Op::Refer, vec![Anno(expr, span)]), span)], ) })?, Ps::Op(op) => Expr::Op(op, vec![p.consume().parse(prec.next())?]), _ => unimplemented!("prefix {op:?}"), }; // Infix and Postfix while let Ok(tok) = p.peek() && let Ok((op, prec)) = from_infix(tok) && level <= prec.prev() && op != Ps::End { let kind = tok.kind; let span = span.merge(p.span()); head = match op { // Make (structor expressions) are context-sensitive Ps::Make => match &head { Expr::Id(_) | Expr::MetId(_) => Expr::Make(Box::new(Make( head.anno(span), p.consume().list(vec![], (), TKind::Comma, TKind::RCurly)?, ))), _ => break, }, // As is ImplicitDo (semicolon elision) Ps::ImplicitDo if p.elide_do => head.and_do(span, p.parse(prec.next())?), Ps::ImplicitDo => break, Ps::Op(Op::Do) => head.and_do(span, p.consume().parse(prec.next())?), Ps::Op(Op::Index) => Expr::Op( Op::Index, p.consume() .list(vec![head.anno(span)], 0, TKind::Comma, TKind::RBrack)?, ), Ps::Op(Op::Call) => Expr::Op( Op::Call, p.consume() .list(vec![head.anno(span)], 0, TKind::Comma, TKind::RParen)?, ), Ps::Op(op @ (Op::Tuple | Op::Dot | Op::LogAnd | Op::LogOr)) => Expr::Op( op, p.consume() .list_bare(vec![head.anno(span)], prec.next(), kind)?, ), Ps::Op(op @ Op::Try) => { p.consume(); Expr::Op(op, vec![head.anno(span)]) } Ps::Op(op) => Expr::Op(op, vec![head.anno(span), p.consume().parse(prec.next())?]), _ => Err(ParseError::NotInfix(kind, span))?, } } Ok(head) } } /// Parses an array with 0 or more elements, or an array-repetition fn parse_array<'t>(p: &mut Parser<'t>) -> PResult { if p.consume().peek()?.kind == TKind::RBrack { p.consume(); return Ok(Expr::Op(Op::Array, vec![])); } let prec = Prec::Tuple; let item = p.parse(prec.value())?; let repeat = p.opt_if(prec.next(), TKind::Semi)?; p.next_if(TKind::RBrack)?; Ok(match (repeat, item) { (Some(repeat), item) => Expr::Op(Op::ArRep, vec![item, repeat]), (None, Anno(Expr::Op(Op::Tuple, items), _)) => Expr::Op(Op::Array, items), (None, item) => Expr::Op(Op::Array, vec![item]), }) } impl<'t, P: Parse<'t> + Annotation> Parse<'t> for Anno

{ type Prec = P::Prec; fn parse(p: &mut Parser<'t>, level: P::Prec) -> PResult where Self: Sized { let start = p.span(); let anno = Anno(p.parse(level)?, start.merge(p.span())); Ok(anno) } } impl<'t, P: Parse<'t>> Parse<'t> for Box

{ type Prec = P::Prec; fn parse(p: &mut Parser<'t>, level: P::Prec) -> PResult where Self: Sized { Ok(Box::new(p.parse(level)?)) } }