cl-parser: Move precedence parser into its own module

This commit is contained in:
John 2024-07-31 02:48:39 -05:00
parent 1eb0516baf
commit 533436afc1

View File

@ -36,7 +36,7 @@ impl<'t> Parser<'t> {
} }
/// Constructs an [Error] /// Constructs an [Error]
fn error(&self, reason: ErrorKind, while_parsing: Parsing) -> Error { pub fn error(&self, reason: ErrorKind, while_parsing: Parsing) -> Error {
Error { reason, while_parsing, loc: self.loc } Error { reason, while_parsing, loc: self.loc }
} }
@ -191,6 +191,7 @@ macro path_like() {
} }
pub trait Parse<'t>: Sized { pub trait Parse<'t>: Sized {
/// Parses a Self from the provided [Parser]
fn parse(p: &mut Parser<'t>) -> PResult<Self>; fn parse(p: &mut Parser<'t>) -> PResult<Self>;
} }
@ -881,7 +882,7 @@ impl Parse<'_> for StmtKind {
impl Parse<'_> for Expr { impl Parse<'_> for Expr {
/// Parses an [Expr] /// Parses an [Expr]
/// ///
/// See also: [Parser::exprkind] /// See also: [ExprKind::parse]
fn parse(p: &mut Parser) -> PResult<Expr> { fn parse(p: &mut Parser) -> PResult<Expr> {
let start = p.loc(); let start = p.loc();
Ok(Expr { kind: ExprKind::parse(p)?, extents: Span(start, p.loc()) }) Ok(Expr { kind: ExprKind::parse(p)?, extents: Span(start, p.loc()) })
@ -892,242 +893,396 @@ impl Parse<'_> for ExprKind {
/// Parses an [ExprKind] at the lowest precedence level /// Parses an [ExprKind] at the lowest precedence level
// Implementer's note: Do not call this from within [Parser::exprkind] // Implementer's note: Do not call this from within [Parser::exprkind]
fn parse(p: &mut Parser<'_>) -> PResult<ExprKind> { fn parse(p: &mut Parser<'_>) -> PResult<ExprKind> {
exprkind(p, 0) prec::exprkind(p, 0)
} }
} }
/// Parses an [ExprKind] mod prec {
/// //! Parses an [ExprKind] using a modified pratt parser
/// See also: [Expr::parse] //!
// Implementer's note: [ExprKind::parse] is the public API for parsing ExprKinds. //! See also: [Expr::parse], [ExprKind::parse]
// Do not call it from within this function. //!
fn exprkind(p: &mut Parser, power: u8) -> PResult<ExprKind> { //! Implementer's note: [ExprKind::parse] is the public API for parsing [ExprKind]s.
let parsing = Parsing::ExprKind; //! Do not call it from within this function.
// Prefix expressions use super::{Parse, *};
let mut head = match p.peek_kind(Parsing::Unary)? {
literal_like!() => Literal::parse(p)?.into(), /// Parses an [ExprKind]
path_like!() => exprkind_pathlike(p)?, pub fn exprkind(p: &mut Parser, power: u8) -> PResult<ExprKind> {
TokenKind::Amp | TokenKind::AmpAmp => AddrOf::parse(p)?.into(), let parsing = Parsing::ExprKind;
TokenKind::LCurly => Block::parse(p)?.into(),
TokenKind::LBrack => exprkind_arraylike(p)?, // Prefix expressions
TokenKind::LParen => exprkind_tuplelike(p)?, let mut head = match p.peek_kind(Parsing::Unary)? {
TokenKind::Let => Let::parse(p)?.into(), literal_like!() => Literal::parse(p)?.into(),
TokenKind::While => ExprKind::While(While::parse(p)?), path_like!() => exprkind_pathlike(p)?,
TokenKind::If => ExprKind::If(If::parse(p)?), TokenKind::Amp | TokenKind::AmpAmp => AddrOf::parse(p)?.into(),
TokenKind::For => ExprKind::For(For::parse(p)?), TokenKind::LCurly => Block::parse(p)?.into(),
TokenKind::Break => ExprKind::Break(Break::parse(p)?), TokenKind::LBrack => exprkind_arraylike(p)?,
TokenKind::Return => ExprKind::Return(Return::parse(p)?), TokenKind::LParen => exprkind_tuplelike(p)?,
TokenKind::Continue => { TokenKind::Let => Let::parse(p)?.into(),
p.consume_peeked(); TokenKind::While => ExprKind::While(While::parse(p)?),
ExprKind::Continue TokenKind::If => ExprKind::If(If::parse(p)?),
TokenKind::For => ExprKind::For(For::parse(p)?),
TokenKind::Break => ExprKind::Break(Break::parse(p)?),
TokenKind::Return => ExprKind::Return(Return::parse(p)?),
TokenKind::Continue => {
p.consume_peeked();
ExprKind::Continue
}
op => {
let (kind, prec) =
from_prefix(op).ok_or_else(|| p.error(Unexpected(op), parsing))?;
let ((), after) = prec.prefix().expect("should have a precedence");
p.consume_peeked();
Unary { kind, tail: exprkind(p, after)?.into() }.into()
}
};
fn from_postfix(op: TokenKind) -> Option<Precedence> {
Some(match op {
TokenKind::LBrack => Precedence::Index,
TokenKind::LParen => Precedence::Call,
TokenKind::Dot => Precedence::Member,
_ => None?,
})
} }
op => { while let Ok(op) = p.peek_kind(parsing) {
let (kind, prec) = from_prefix(op).ok_or_else(|| p.error(Unexpected(op), parsing))?; // Postfix expressions
let ((), after) = prec.prefix().expect("should have a precedence"); if let Some((before, ())) = from_postfix(op).and_then(Precedence::postfix) {
p.consume_peeked(); if before < power {
Unary { kind, tail: exprkind(p, after)?.into() }.into() break;
} }
}; p.consume_peeked();
fn from_postfix(op: TokenKind) -> Option<Precedence> { head = match op {
Some(match op { TokenKind::LBrack => {
TokenKind::LBrack => Precedence::Index, let indices =
TokenKind::LParen => Precedence::Call, sep(Expr::parse, TokenKind::Comma, TokenKind::RBrack, parsing)(p)?;
TokenKind::Dot => Precedence::Member, p.match_type(TokenKind::RBrack, parsing)?;
_ => None?, ExprKind::Index(Index { head: head.into(), indices })
}
TokenKind::LParen => {
let exprs =
sep(Expr::parse, TokenKind::Comma, TokenKind::RParen, parsing)(p)?;
p.match_type(TokenKind::RParen, parsing)?;
Binary {
kind: BinaryKind::Call,
parts: (head, Tuple { exprs }.into()).into(),
}
.into()
}
TokenKind::Dot => {
let kind = MemberKind::parse(p)?;
Member { head: Box::new(head), kind }.into()
}
_ => Err(p.error(Unexpected(op), parsing))?,
};
continue;
}
// infix expressions
if let Some((kind, prec)) = from_infix(op) {
let (before, after) = prec.infix().expect("should have a precedence");
if before < power {
break;
}
p.consume_peeked();
let tail = exprkind(p, after)?;
head = Binary { kind, parts: (head, tail).into() }.into();
continue;
}
if let Some((kind, prec)) = from_modify(op) {
let (before, after) = prec.infix().expect("should have a precedence");
if before < power {
break;
}
p.consume_peeked();
let tail = exprkind(p, after)?;
head = Modify { kind, parts: (head, tail).into() }.into();
continue;
}
if let TokenKind::Eq = op {
let (before, after) = Precedence::Assign
.infix()
.expect("should have a precedence");
if before < power {
break;
}
p.consume_peeked();
let tail = exprkind(p, after)?;
head = Assign { parts: (head, tail).into() }.into();
continue;
}
if let TokenKind::As = op {
let before = Precedence::Cast.level();
if before < power {
break;
}
p.consume_peeked();
let ty = Ty::parse(p)?;
head = Cast { head: head.into(), ty }.into();
continue;
}
break;
}
Ok(head)
}
/// [Array] = '[' ([Expr] ',')* [Expr]? ']'
///
/// Array and ArrayRef are ambiguous until the second token,
/// so they can't be independent subexpressions
fn exprkind_arraylike(p: &mut Parser) -> PResult<ExprKind> {
const P: Parsing = Parsing::Array;
const START: TokenKind = TokenKind::LBrack;
const END: TokenKind = TokenKind::RBrack;
p.match_type(START, P)?;
let out = match p.peek_kind(P)? {
END => Array { values: vec![] }.into(),
_ => exprkind_array_rep(p)?,
};
p.match_type(END, P)?;
Ok(out)
}
/// [ArrayRep] = `[` [Expr] `;` [Expr] `]`
fn exprkind_array_rep(p: &mut Parser) -> PResult<ExprKind> {
const P: Parsing = Parsing::Array;
const END: TokenKind = TokenKind::RBrack;
let first = Expr::parse(p)?;
Ok(match p.peek_kind(P)? {
TokenKind::Semi => ArrayRep {
value: first.kind.into(),
repeat: {
p.consume_peeked();
Box::new(exprkind(p, 0)?)
},
}
.into(),
TokenKind::RBrack => Array { values: vec![first] }.into(),
TokenKind::Comma => Array {
values: {
p.consume_peeked();
let mut out = vec![first];
out.extend(sep(Expr::parse, TokenKind::Comma, END, P)(p)?);
out
},
}
.into(),
ty => Err(p.error(Unexpected(ty), P))?,
}) })
} }
while let Ok(op) = p.peek_kind(parsing) { /// [Group] = `(`([Empty](ExprKind::Empty)|[Expr]|[Tuple])`)`
// Postfix expressions ///
if let Some((before, ())) = from_postfix(op).and_then(Precedence::postfix) { /// [ExprKind::Empty] and [Group] are special cases of [Tuple]
if before < power { fn exprkind_tuplelike(p: &mut Parser) -> PResult<ExprKind> {
break; p.match_type(TokenKind::LParen, Parsing::Group)?;
} let out = match p.peek_kind(Parsing::Group)? {
p.consume_peeked(); TokenKind::RParen => Ok(ExprKind::Empty),
_ => exprkind_group(p),
head = match op { };
TokenKind::LBrack => { p.match_type(TokenKind::RParen, Parsing::Group)?;
let indices = out
sep(Expr::parse, TokenKind::Comma, TokenKind::RBrack, parsing)(p)?;
p.match_type(TokenKind::RBrack, parsing)?;
ExprKind::Index(Index { head: head.into(), indices })
}
TokenKind::LParen => {
let exprs = sep(Expr::parse, TokenKind::Comma, TokenKind::RParen, parsing)(p)?;
p.match_type(TokenKind::RParen, parsing)?;
Binary { kind: BinaryKind::Call, parts: (head, Tuple { exprs }.into()).into() }
.into()
}
TokenKind::Dot => {
let kind = MemberKind::parse(p)?;
Member { head: Box::new(head), kind }.into()
}
_ => Err(p.error(Unexpected(op), parsing))?,
};
continue;
}
// infix expressions
if let Some((kind, prec)) = from_infix(op) {
let (before, after) = prec.infix().expect("should have a precedence");
if before < power {
break;
}
p.consume_peeked();
let tail = exprkind(p, after)?;
head = Binary { kind, parts: (head, tail).into() }.into();
continue;
}
if let Some((kind, prec)) = from_modify(op) {
let (before, after) = prec.infix().expect("should have a precedence");
if before < power {
break;
}
p.consume_peeked();
let tail = exprkind(p, after)?;
head = Modify { kind, parts: (head, tail).into() }.into();
continue;
}
if let TokenKind::Eq = op {
let (before, after) = Precedence::Assign
.infix()
.expect("should have a precedence");
if before < power {
break;
}
p.consume_peeked();
let tail = exprkind(p, after)?;
head = Assign { parts: (head, tail).into() }.into();
continue;
}
if let TokenKind::As = op {
let before = Precedence::Cast.level();
if before < power {
break;
}
p.consume_peeked();
let ty = Ty::parse(p)?;
head = Cast { head: head.into(), ty }.into();
continue;
}
break;
} }
Ok(head) /// [Group] = `(`([Empty](ExprKind::Empty)|[Expr]|[Tuple])`)`
} fn exprkind_group(p: &mut Parser) -> PResult<ExprKind> {
let first = Expr::parse(p)?;
/// [Array] = '[' ([Expr] ',')* [Expr]? ']' match p.peek_kind(Parsing::Group)? {
/// TokenKind::Comma => {
/// Array and ArrayRef are ambiguous until the second token, let mut exprs = vec![first];
/// so they can't be independent subexpressions
fn exprkind_arraylike(p: &mut Parser) -> PResult<ExprKind> {
const P: Parsing = Parsing::Array;
const START: TokenKind = TokenKind::LBrack;
const END: TokenKind = TokenKind::RBrack;
p.match_type(START, P)?;
let out = match p.peek_kind(P)? {
END => Array { values: vec![] }.into(),
_ => exprkind_array_rep(p)?,
};
p.match_type(END, P)?;
Ok(out)
}
/// [ArrayRep] = `[` [Expr] `;` [Expr] `]`
fn exprkind_array_rep(p: &mut Parser) -> PResult<ExprKind> {
const P: Parsing = Parsing::Array;
const END: TokenKind = TokenKind::RBrack;
let first = Expr::parse(p)?;
Ok(match p.peek_kind(P)? {
TokenKind::Semi => ArrayRep {
value: first.kind.into(),
repeat: {
p.consume_peeked(); p.consume_peeked();
Box::new(exprkind(p, 0)?) while TokenKind::RParen != p.peek_kind(Parsing::Tuple)? {
}, exprs.push(Expr::parse(p)?);
} match p.peek_kind(Parsing::Tuple)? {
.into(), TokenKind::Comma => p.consume_peeked(),
TokenKind::RBrack => Array { values: vec![first] }.into(), _ => break,
TokenKind::Comma => Array { };
values: { }
p.consume_peeked(); Ok(Tuple { exprs }.into())
let mut out = vec![first];
out.extend(sep(Expr::parse, TokenKind::Comma, END, P)(p)?);
out
},
}
.into(),
ty => Err(p.error(Unexpected(ty), P))?,
})
}
/// [Group] = `(`([Empty](ExprKind::Empty)|[Expr]|[Tuple])`)`
///
/// [ExprKind::Empty] and [Group] are special cases of [Tuple]
fn exprkind_tuplelike(p: &mut Parser) -> PResult<ExprKind> {
p.match_type(TokenKind::LParen, Parsing::Group)?;
let out = match p.peek_kind(Parsing::Group)? {
TokenKind::RParen => Ok(ExprKind::Empty),
_ => exprkind_group(p),
};
p.match_type(TokenKind::RParen, Parsing::Group)?;
out
}
/// [Group] = `(`([Empty](ExprKind::Empty)|[Expr]|[Tuple])`)`
fn exprkind_group(p: &mut Parser) -> PResult<ExprKind> {
let first = Expr::parse(p)?;
match p.peek_kind(Parsing::Group)? {
TokenKind::Comma => {
let mut exprs = vec![first];
p.consume_peeked();
while TokenKind::RParen != p.peek_kind(Parsing::Tuple)? {
exprs.push(Expr::parse(p)?);
match p.peek_kind(Parsing::Tuple)? {
TokenKind::Comma => p.consume_peeked(),
_ => break,
};
} }
Ok(Tuple { exprs }.into()) _ => Ok(Group { expr: first.kind.into() }.into()),
} }
_ => Ok(Group { expr: first.kind.into() }.into()),
} }
}
/// Parses an expression beginning with a [Path] (i.e. [Path] or [Structor]) /// Parses an expression beginning with a [Path] (i.e. [Path] or [Structor])
fn exprkind_pathlike(p: &mut Parser) -> PResult<ExprKind> { fn exprkind_pathlike(p: &mut Parser) -> PResult<ExprKind> {
let head = Path::parse(p)?; let head = Path::parse(p)?;
Ok(match p.match_type(TokenKind::Colon, Parsing::Path) { Ok(match p.match_type(TokenKind::Colon, Parsing::Path) {
Ok(_) => ExprKind::Structor(structor_body(p, head)?), Ok(_) => ExprKind::Structor(structor_body(p, head)?),
Err(_) => ExprKind::Path(head), Err(_) => ExprKind::Path(head),
}) })
} }
/// [Structor]Body = `{` ([Fielder] `,`)* [Fielder]? `}` /// [Structor]Body = `{` ([Fielder] `,`)* [Fielder]? `}`
fn structor_body(p: &mut Parser, to: Path) -> PResult<Structor> { fn structor_body(p: &mut Parser, to: Path) -> PResult<Structor> {
let init = delim( let init = delim(
sep( sep(
Fielder::parse, Fielder::parse,
TokenKind::Comma, TokenKind::Comma,
CURLIES.1, CURLIES.1,
Parsing::Structor,
),
CURLIES,
Parsing::Structor, Parsing::Structor,
), )(p)?;
CURLIES,
Parsing::Structor,
)(p)?;
Ok(Structor { to, init }) Ok(Structor { to, init })
}
/// Precedence provides a total ordering among operators
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Precedence {
Assign,
Compare,
Range,
Logic,
Bitwise,
Shift,
Factor,
Term,
Unary,
Index,
Cast,
Member, // left-associative
Call,
}
impl Precedence {
#[inline]
pub const fn level(self) -> u8 {
(self as u8) << 1
}
pub fn prefix(self) -> Option<((), u8)> {
match self {
Self::Assign => Some(((), self.level())),
Self::Unary => Some(((), self.level())),
_ => None,
}
}
pub fn infix(self) -> Option<(u8, u8)> {
let level = self.level();
match self {
Self::Unary => None,
Self::Assign => Some((level + 1, level)),
_ => Some((level, level + 1)),
}
}
pub fn postfix(self) -> Option<(u8, ())> {
match self {
Self::Index | Self::Call | Self::Member => Some((self.level(), ())),
_ => None,
}
}
}
impl From<ModifyKind> for Precedence {
fn from(_value: ModifyKind) -> Self {
Precedence::Assign
}
}
impl From<BinaryKind> for Precedence {
fn from(value: BinaryKind) -> Self {
use BinaryKind as Op;
match value {
Op::Call => Precedence::Call,
Op::Mul | Op::Div | Op::Rem => Precedence::Term,
Op::Add | Op::Sub => Precedence::Factor,
Op::Shl | Op::Shr => Precedence::Shift,
Op::BitAnd | Op::BitOr | Op::BitXor => Precedence::Bitwise,
Op::LogAnd | Op::LogOr | Op::LogXor => Precedence::Logic,
Op::RangeExc | Op::RangeInc => Precedence::Range,
Op::Lt | Op::LtEq | Op::Equal | Op::NotEq | Op::GtEq | Op::Gt => {
Precedence::Compare
}
}
}
}
impl From<UnaryKind> for Precedence {
fn from(value: UnaryKind) -> Self {
use UnaryKind as Op;
match value {
Op::Loop => Precedence::Assign,
Op::Deref | Op::Neg | Op::Not | Op::At | Op::Tilde => Precedence::Unary,
}
}
}
/// Creates helper functions for turning TokenKinds into AST operators
macro operator($($name:ident ($takes:ident => $returns:ident) {$($t:ident => $p:ident),*$(,)?};)*) {$(
pub fn $name (value: $takes) -> Option<($returns, Precedence)> {
match value {
$($takes::$t => Some(($returns::$p, Precedence::from($returns::$p))),)*
_ => None?,
}
})*
}
operator! {
from_prefix (TokenKind => UnaryKind) {
Loop => Loop,
Star => Deref,
Minus => Neg,
Bang => Not,
At => At,
Tilde => Tilde,
};
from_modify(TokenKind => ModifyKind) {
AmpEq => And,
BarEq => Or,
XorEq => Xor,
LtLtEq => Shl,
GtGtEq => Shr,
PlusEq => Add,
MinusEq => Sub,
StarEq => Mul,
SlashEq => Div,
RemEq => Rem,
};
from_infix (TokenKind => BinaryKind) {
Lt => Lt,
LtEq => LtEq,
EqEq => Equal,
BangEq => NotEq,
GtEq => GtEq,
Gt => Gt,
DotDot => RangeExc,
DotDotEq => RangeInc,
AmpAmp => LogAnd,
BarBar => LogOr,
XorXor => LogXor,
Amp => BitAnd,
Bar => BitOr,
Xor => BitXor,
LtLt => Shl,
GtGt => Shr,
Plus => Add,
Minus => Sub,
Star => Mul,
Slash => Div,
Rem => Rem,
};
}
} }
impl Parse<'_> for Let { impl Parse<'_> for Let {
@ -1297,142 +1452,3 @@ fn ret_body(p: &mut Parser, while_parsing: Parsing) -> PResult<Option<Box<Expr>>
_ => Some(Expr::parse(p)?.into()), _ => Some(Expr::parse(p)?.into()),
}) })
} }
/// Precedence provides a total ordering among operators
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Precedence {
Assign,
Compare,
Range,
Logic,
Bitwise,
Shift,
Factor,
Term,
Unary,
Index,
Cast,
Member, // left-associative
Call,
}
impl Precedence {
#[inline]
pub const fn level(self) -> u8 {
(self as u8) << 1
}
pub fn prefix(self) -> Option<((), u8)> {
match self {
Self::Assign => Some(((), self.level())),
Self::Unary => Some(((), self.level())),
_ => None,
}
}
pub fn infix(self) -> Option<(u8, u8)> {
let level = self.level();
match self {
Self::Unary => None,
Self::Assign => Some((level + 1, level)),
_ => Some((level, level + 1)),
}
}
pub fn postfix(self) -> Option<(u8, ())> {
match self {
Self::Index | Self::Call | Self::Member => Some((self.level(), ())),
_ => None,
}
}
}
impl From<ModifyKind> for Precedence {
fn from(_value: ModifyKind) -> Self {
Precedence::Assign
}
}
impl From<BinaryKind> for Precedence {
fn from(value: BinaryKind) -> Self {
use BinaryKind as Op;
match value {
Op::Call => Precedence::Call,
Op::Mul | Op::Div | Op::Rem => Precedence::Term,
Op::Add | Op::Sub => Precedence::Factor,
Op::Shl | Op::Shr => Precedence::Shift,
Op::BitAnd | Op::BitOr | Op::BitXor => Precedence::Bitwise,
Op::LogAnd | Op::LogOr | Op::LogXor => Precedence::Logic,
Op::RangeExc | Op::RangeInc => Precedence::Range,
Op::Lt | Op::LtEq | Op::Equal | Op::NotEq | Op::GtEq | Op::Gt => Precedence::Compare,
}
}
}
impl From<UnaryKind> for Precedence {
fn from(value: UnaryKind) -> Self {
use UnaryKind as Op;
match value {
Op::Loop => Precedence::Assign,
Op::Deref | Op::Neg | Op::Not | Op::At | Op::Tilde => Precedence::Unary,
}
}
}
/// Creates helper functions for turning TokenKinds into AST operators
macro operator($($name:ident ($takes:ident => $returns:ident) {$($t:ident => $p:ident),*$(,)?};)*) {$(
pub fn $name (value: $takes) -> Option<($returns, Precedence)> {
match value {
$($takes::$t => Some(($returns::$p, Precedence::from($returns::$p))),)*
_ => None?,
}
})*
}
operator! {
from_prefix (TokenKind => UnaryKind) {
Loop => Loop,
Star => Deref,
Minus => Neg,
Bang => Not,
At => At,
Tilde => Tilde,
};
from_modify(TokenKind => ModifyKind) {
AmpEq => And,
BarEq => Or,
XorEq => Xor,
LtLtEq => Shl,
GtGtEq => Shr,
PlusEq => Add,
MinusEq => Sub,
StarEq => Mul,
SlashEq => Div,
RemEq => Rem,
};
from_infix (TokenKind => BinaryKind) {
Lt => Lt,
LtEq => LtEq,
EqEq => Equal,
BangEq => NotEq,
GtEq => GtEq,
Gt => Gt,
DotDot => RangeExc,
DotDotEq => RangeInc,
AmpAmp => LogAnd,
BarBar => LogOr,
XorXor => LogXor,
Amp => BitAnd,
Bar => BitOr,
Xor => BitXor,
LtLt => Shl,
GtGt => Shr,
Plus => Add,
Minus => Sub,
Star => Mul,
Slash => Div,
Rem => Rem,
};
}