cl-parser: Rearrange to match cl-ast
Also reorder `Let` in the AST
This commit is contained in:
parent
97808fd855
commit
1eb0516baf
@ -258,7 +258,6 @@ pub enum TyKind {
|
|||||||
Tuple(TyTuple),
|
Tuple(TyTuple),
|
||||||
Ref(TyRef),
|
Ref(TyRef),
|
||||||
Fn(TyFn),
|
Fn(TyFn),
|
||||||
// TODO: slice, array types
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An array of [`T`](Ty)
|
/// An array of [`T`](Ty)
|
||||||
@ -334,16 +333,6 @@ pub enum Semi {
|
|||||||
Unterminated,
|
Unterminated,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A local variable declaration [Stmt]
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
||||||
pub struct Let {
|
|
||||||
pub mutable: Mutability,
|
|
||||||
pub name: Sym,
|
|
||||||
pub ty: Option<Box<Ty>>,
|
|
||||||
pub init: Option<Box<Expr>>,
|
|
||||||
pub tail: Option<Box<Expr>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An expression, the beating heart of the language
|
/// An expression, the beating heart of the language
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct Expr {
|
pub struct Expr {
|
||||||
@ -406,6 +395,16 @@ pub enum ExprKind {
|
|||||||
Continue,
|
Continue,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A local variable declaration [Stmt]
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
|
pub struct Let {
|
||||||
|
pub mutable: Mutability,
|
||||||
|
pub name: Sym,
|
||||||
|
pub ty: Option<Box<Ty>>,
|
||||||
|
pub init: Option<Box<Expr>>,
|
||||||
|
pub tail: Option<Box<Expr>>,
|
||||||
|
}
|
||||||
|
|
||||||
/// An [Assign]ment expression: [`Expr`] ([`ModifyKind`] [`Expr`])\+
|
/// An [Assign]ment expression: [`Expr`] ([`ModifyKind`] [`Expr`])\+
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct Assign {
|
pub struct Assign {
|
||||||
|
@ -404,23 +404,6 @@ mod display {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Let {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
let Self { mutable, name, ty, init, tail } = self;
|
|
||||||
write!(f, "let {mutable}{name}")?;
|
|
||||||
if let Some(value) = ty {
|
|
||||||
write!(f, ": {value}")?;
|
|
||||||
}
|
|
||||||
if let Some(value) = init {
|
|
||||||
write!(f, " = {value}")?;
|
|
||||||
}
|
|
||||||
if let Some(value) = tail {
|
|
||||||
write!(f, ";\n{value}")?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Expr {
|
impl Display for Expr {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
self.kind.fmt(f)
|
self.kind.fmt(f)
|
||||||
@ -458,6 +441,23 @@ mod display {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for Let {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let Self { mutable, name, ty, init, tail } = self;
|
||||||
|
write!(f, "let {mutable}{name}")?;
|
||||||
|
if let Some(value) = ty {
|
||||||
|
write!(f, ": {value}")?;
|
||||||
|
}
|
||||||
|
if let Some(value) = init {
|
||||||
|
write!(f, " = {value}")?;
|
||||||
|
}
|
||||||
|
if let Some(value) = tail {
|
||||||
|
write!(f, ";\n{value}")?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Display for Assign {
|
impl Display for Assign {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let Self { parts } = self;
|
let Self { parts } = self;
|
||||||
|
@ -161,7 +161,7 @@ const fn rep<'t, T>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expands to a pattern which matches item-like [Token] [TokenKind]s
|
/// Expands to a pattern which matches item-like [TokenKind]s
|
||||||
macro item_like() {
|
macro item_like() {
|
||||||
TokenKind::Hash
|
TokenKind::Hash
|
||||||
| TokenKind::Pub
|
| TokenKind::Pub
|
||||||
@ -176,10 +176,77 @@ macro item_like() {
|
|||||||
| TokenKind::Use
|
| TokenKind::Use
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Expands to a pattern which matches literal-like [TokenKind]s
|
||||||
|
macro literal_like() {
|
||||||
|
TokenKind::True | TokenKind::False | TokenKind::Literal
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Expands to a pattern which matches path-like [TokenKinds](TokenKind)
|
||||||
|
macro path_like() {
|
||||||
|
TokenKind::Super
|
||||||
|
| TokenKind::SelfKw
|
||||||
|
| TokenKind::SelfTy
|
||||||
|
| TokenKind::Identifier
|
||||||
|
| TokenKind::ColonColon
|
||||||
|
}
|
||||||
|
|
||||||
pub trait Parse<'t>: Sized {
|
pub trait Parse<'t>: Sized {
|
||||||
fn parse(p: &mut Parser<'t>) -> PResult<Self>;
|
fn parse(p: &mut Parser<'t>) -> PResult<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Parse<'_> for Sym {
|
||||||
|
/// [Sym] = [`Identifier`](TokenKind::Identifier)
|
||||||
|
fn parse(p: &mut Parser) -> PResult<Sym> {
|
||||||
|
let tok = p.match_type(TokenKind::Identifier, Parsing::Identifier)?;
|
||||||
|
match tok.data() {
|
||||||
|
TokenData::String(ident) => Ok(ident.into()),
|
||||||
|
_ => panic!("Expected token data for {tok:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parse<'_> for Mutability {
|
||||||
|
/// [Mutability] = `mut`?
|
||||||
|
#[inline]
|
||||||
|
fn parse(p: &mut Parser) -> PResult<Mutability> {
|
||||||
|
Ok(match p.match_type(TokenKind::Mut, Parsing::Mutability) {
|
||||||
|
Ok(_) => Mutability::Mut,
|
||||||
|
Err(_) => Mutability::Not,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parse<'_> for Visibility {
|
||||||
|
/// [Visibility] = `pub`?
|
||||||
|
#[inline]
|
||||||
|
fn parse(p: &mut Parser) -> PResult<Self> {
|
||||||
|
Ok(match p.match_type(TokenKind::Pub, Parsing::Visibility) {
|
||||||
|
Ok(_) => Visibility::Public,
|
||||||
|
Err(_) => Visibility::Private,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parse<'_> for Literal {
|
||||||
|
/// [Literal] = [LITERAL](TokenKind::Literal) | `true` | `false`
|
||||||
|
fn parse(p: &mut Parser) -> PResult<Literal> {
|
||||||
|
let Token { ty, data, .. } = p.consume(Parsing::Literal)?;
|
||||||
|
match ty {
|
||||||
|
TokenKind::True => return Ok(Literal::Bool(true)),
|
||||||
|
TokenKind::False => return Ok(Literal::Bool(false)),
|
||||||
|
TokenKind::Literal => (),
|
||||||
|
t => return Err(p.error(Unexpected(t), Parsing::Literal)),
|
||||||
|
}
|
||||||
|
Ok(match data {
|
||||||
|
TokenData::String(v) => Literal::String(v),
|
||||||
|
TokenData::Character(v) => Literal::Char(v),
|
||||||
|
TokenData::Integer(v) => Literal::Int(v),
|
||||||
|
TokenData::Float(v) => todo!("Literal::Float({v})"),
|
||||||
|
_ => panic!("Expected token data for {ty:?}"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Parse<'_> for File {
|
impl Parse<'_> for File {
|
||||||
/// Parses a [File]
|
/// Parses a [File]
|
||||||
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
|
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
|
||||||
@ -195,86 +262,6 @@ impl Parse<'_> for File {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse<'_> for Item {
|
|
||||||
/// Parses an [Item]
|
|
||||||
///
|
|
||||||
/// See also: [ItemKind::parse]
|
|
||||||
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
|
|
||||||
let start = p.loc();
|
|
||||||
Ok(Item {
|
|
||||||
attrs: Attrs::parse(p)?,
|
|
||||||
vis: Visibility::parse(p)?,
|
|
||||||
kind: ItemKind::parse(p)?,
|
|
||||||
extents: Span(start, p.loc()),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse<'_> for Ty {
|
|
||||||
/// Parses a [Ty]
|
|
||||||
///
|
|
||||||
/// See also: [TyKind::parse]
|
|
||||||
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
|
|
||||||
let start = p.loc();
|
|
||||||
Ok(Ty { kind: TyKind::parse(p)?, extents: Span(start, p.loc()) })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse<'_> for Path {
|
|
||||||
/// Parses a [Path]
|
|
||||||
///
|
|
||||||
/// See also: [PathPart::parse], [Sym::parse]
|
|
||||||
///
|
|
||||||
/// [Path] = `::` *RelativePath*? | *RelativePath* \
|
|
||||||
/// *RelativePath* = [PathPart] (`::` [PathPart])*
|
|
||||||
fn parse(p: &mut Parser) -> PResult<Path> {
|
|
||||||
const P: Parsing = Parsing::Path;
|
|
||||||
let absolute = p.match_type(TokenKind::ColonColon, P).is_ok();
|
|
||||||
let mut parts = vec![];
|
|
||||||
|
|
||||||
if absolute {
|
|
||||||
match PathPart::parse(p) {
|
|
||||||
Ok(part) => parts.push(part),
|
|
||||||
Err(_) => return Ok(Path { absolute, parts }),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
parts.push(PathPart::parse(p)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
while p.match_type(TokenKind::ColonColon, Parsing::Path).is_ok() {
|
|
||||||
parts.push(PathPart::parse(p)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Path { absolute, parts })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse<'_> for Stmt {
|
|
||||||
/// Parses a [Stmt]
|
|
||||||
///
|
|
||||||
/// See also: [StmtKind::parse]
|
|
||||||
fn parse(p: &mut Parser) -> PResult<Stmt> {
|
|
||||||
let start = p.loc();
|
|
||||||
Ok(Stmt {
|
|
||||||
kind: StmtKind::parse(p)?,
|
|
||||||
semi: match p.match_type(TokenKind::Semi, Parsing::Stmt) {
|
|
||||||
Ok(_) => Semi::Terminated,
|
|
||||||
_ => Semi::Unterminated,
|
|
||||||
},
|
|
||||||
extents: Span(start, p.loc()),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse<'_> for Expr {
|
|
||||||
/// Parses an [Expr]
|
|
||||||
///
|
|
||||||
/// See also: [Parser::exprkind]
|
|
||||||
fn parse(p: &mut Parser) -> PResult<Expr> {
|
|
||||||
p.expr_from(|this| this.exprkind(0))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse<'_> for Attrs {
|
impl Parse<'_> for Attrs {
|
||||||
/// Parses an [attribute set](Attrs)
|
/// Parses an [attribute set](Attrs)
|
||||||
fn parse(p: &mut Parser) -> PResult<Attrs> {
|
fn parse(p: &mut Parser) -> PResult<Attrs> {
|
||||||
@ -317,6 +304,23 @@ impl Parse<'_> for MetaKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Items ---
|
||||||
|
|
||||||
|
impl Parse<'_> for Item {
|
||||||
|
/// Parses an [Item]
|
||||||
|
///
|
||||||
|
/// See also: [ItemKind::parse]
|
||||||
|
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
|
||||||
|
let start = p.loc();
|
||||||
|
Ok(Item {
|
||||||
|
attrs: Attrs::parse(p)?,
|
||||||
|
vis: Visibility::parse(p)?,
|
||||||
|
kind: ItemKind::parse(p)?,
|
||||||
|
extents: Span(start, p.loc()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Parse<'_> for ItemKind {
|
impl Parse<'_> for ItemKind {
|
||||||
/// Parses an [ItemKind]
|
/// Parses an [ItemKind]
|
||||||
///
|
///
|
||||||
@ -546,12 +550,17 @@ impl Parse<'_> for StructMember {
|
|||||||
impl Parse<'_> for Enum {
|
impl Parse<'_> for Enum {
|
||||||
/// Parses an [`enum`](Enum) definition
|
/// Parses an [`enum`](Enum) definition
|
||||||
fn parse(p: &mut Parser) -> PResult<Enum> {
|
fn parse(p: &mut Parser) -> PResult<Enum> {
|
||||||
const P: Parsing = Parsing::Enum;
|
p.match_type(TokenKind::Enum, Parsing::Enum)?;
|
||||||
p.match_type(TokenKind::Enum, P)?;
|
|
||||||
|
|
||||||
Ok(Enum {
|
Ok(Enum { name: Sym::parse(p)?, kind: EnumKind::parse(p)? })
|
||||||
name: Sym::parse(p)?,
|
}
|
||||||
kind: match p.peek_kind(P)? {
|
}
|
||||||
|
|
||||||
|
impl Parse<'_> for EnumKind {
|
||||||
|
/// Parses the various [kinds of Enum](EnumKind)
|
||||||
|
fn parse(p: &mut Parser<'_>) -> PResult<EnumKind> {
|
||||||
|
const P: Parsing = Parsing::EnumKind;
|
||||||
|
Ok(match p.peek_kind(P)? {
|
||||||
TokenKind::LCurly => EnumKind::Variants(delim(
|
TokenKind::LCurly => EnumKind::Variants(delim(
|
||||||
sep(Variant::parse, TokenKind::Comma, TokenKind::RCurly, P),
|
sep(Variant::parse, TokenKind::Comma, TokenKind::RCurly, P),
|
||||||
CURLIES,
|
CURLIES,
|
||||||
@ -562,7 +571,6 @@ impl Parse<'_> for Enum {
|
|||||||
EnumKind::NoVariants
|
EnumKind::NoVariants
|
||||||
}
|
}
|
||||||
t => Err(p.error(Unexpected(t), P))?,
|
t => Err(p.error(Unexpected(t), P))?,
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -677,25 +685,15 @@ impl Parse<'_> for UseTree {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse<'_> for Visibility {
|
// --- Types ---
|
||||||
/// [Visibility] = `pub`?
|
|
||||||
#[inline]
|
|
||||||
fn parse(p: &mut Parser) -> PResult<Self> {
|
|
||||||
Ok(match p.match_type(TokenKind::Pub, Parsing::Visibility) {
|
|
||||||
Ok(_) => Visibility::Public,
|
|
||||||
Err(_) => Visibility::Private,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse<'_> for Mutability {
|
impl Parse<'_> for Ty {
|
||||||
/// [Mutability] = `mut`?
|
/// Parses a [Ty]
|
||||||
#[inline]
|
///
|
||||||
fn parse(p: &mut Parser) -> PResult<Mutability> {
|
/// See also: [TyKind::parse]
|
||||||
Ok(match p.match_type(TokenKind::Mut, Parsing::Mutability) {
|
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
|
||||||
Ok(_) => Mutability::Mut,
|
let start = p.loc();
|
||||||
Err(_) => Mutability::Not,
|
Ok(Ty { kind: TyKind::parse(p)?, extents: Span(start, p.loc()) })
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -798,17 +796,35 @@ impl Parse<'_> for TyFn {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expands to a pattern which matches literal-like [TokenKind]s
|
// --- Paths ---
|
||||||
macro literal_like() {
|
|
||||||
TokenKind::True | TokenKind::False | TokenKind::Literal
|
impl Parse<'_> for Path {
|
||||||
}
|
/// Parses a [Path]
|
||||||
/// Expands to a pattern which matches path-like [token Types](Type)
|
///
|
||||||
macro path_like() {
|
/// See also: [PathPart::parse], [Sym::parse]
|
||||||
TokenKind::Super
|
///
|
||||||
| TokenKind::SelfKw
|
/// [Path] = `::` *RelativePath*? | *RelativePath* \
|
||||||
| TokenKind::SelfTy
|
/// *RelativePath* = [PathPart] (`::` [PathPart])*
|
||||||
| TokenKind::Identifier
|
fn parse(p: &mut Parser) -> PResult<Path> {
|
||||||
| TokenKind::ColonColon
|
const P: Parsing = Parsing::Path;
|
||||||
|
let absolute = p.match_type(TokenKind::ColonColon, P).is_ok();
|
||||||
|
let mut parts = vec![];
|
||||||
|
|
||||||
|
if absolute {
|
||||||
|
match PathPart::parse(p) {
|
||||||
|
Ok(part) => parts.push(part),
|
||||||
|
Err(_) => return Ok(Path { absolute, parts }),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
parts.push(PathPart::parse(p)?)
|
||||||
|
};
|
||||||
|
|
||||||
|
while p.match_type(TokenKind::ColonColon, Parsing::Path).is_ok() {
|
||||||
|
parts.push(PathPart::parse(p)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Path { absolute, parts })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse<'_> for PathPart {
|
impl Parse<'_> for PathPart {
|
||||||
@ -828,14 +844,22 @@ impl Parse<'_> for PathPart {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse<'_> for Sym {
|
// --- Statements ---
|
||||||
/// [Sym] = [`Identifier`](TokenKind::Identifier)
|
|
||||||
fn parse(p: &mut Parser) -> PResult<Sym> {
|
impl Parse<'_> for Stmt {
|
||||||
let tok = p.match_type(TokenKind::Identifier, Parsing::Identifier)?;
|
/// Parses a [Stmt]
|
||||||
match tok.data() {
|
///
|
||||||
TokenData::String(ident) => Ok(ident.into()),
|
/// See also: [StmtKind::parse]
|
||||||
_ => panic!("Expected token data for {tok:?}"),
|
fn parse(p: &mut Parser) -> PResult<Stmt> {
|
||||||
}
|
let start = p.loc();
|
||||||
|
Ok(Stmt {
|
||||||
|
kind: StmtKind::parse(p)?,
|
||||||
|
semi: match p.match_type(TokenKind::Semi, Parsing::Stmt) {
|
||||||
|
Ok(_) => Semi::Terminated,
|
||||||
|
_ => Semi::Unterminated,
|
||||||
|
},
|
||||||
|
extents: Span(start, p.loc()),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -852,44 +876,58 @@ impl Parse<'_> for StmtKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Expression parsing
|
// --- Expressions ---
|
||||||
impl<'t> Parser<'t> {
|
|
||||||
/// Creates an [Expr] with the given [ExprKind]-parser
|
|
||||||
pub fn expr_from(&mut self, f: impl Fn(&mut Self) -> PResult<ExprKind>) -> PResult<Expr> {
|
|
||||||
let start = self.loc();
|
|
||||||
Ok(Expr { kind: f(self)?, extents: Span(start, self.loc()) })
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parses an [ExprKind]
|
impl Parse<'_> for Expr {
|
||||||
|
/// Parses an [Expr]
|
||||||
///
|
///
|
||||||
/// See also: [Expr::parse]
|
/// See also: [Parser::exprkind]
|
||||||
pub fn exprkind(&mut self, power: u8) -> PResult<ExprKind> {
|
fn parse(p: &mut Parser) -> PResult<Expr> {
|
||||||
|
let start = p.loc();
|
||||||
|
Ok(Expr { kind: ExprKind::parse(p)?, extents: Span(start, p.loc()) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parse<'_> for ExprKind {
|
||||||
|
/// Parses an [ExprKind] at the lowest precedence level
|
||||||
|
// Implementer's note: Do not call this from within [Parser::exprkind]
|
||||||
|
fn parse(p: &mut Parser<'_>) -> PResult<ExprKind> {
|
||||||
|
exprkind(p, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parses an [ExprKind]
|
||||||
|
///
|
||||||
|
/// See also: [Expr::parse]
|
||||||
|
// Implementer's note: [ExprKind::parse] is the public API for parsing ExprKinds.
|
||||||
|
// Do not call it from within this function.
|
||||||
|
fn exprkind(p: &mut Parser, power: u8) -> PResult<ExprKind> {
|
||||||
let parsing = Parsing::ExprKind;
|
let parsing = Parsing::ExprKind;
|
||||||
|
|
||||||
// Prefix expressions
|
// Prefix expressions
|
||||||
let mut head = match self.peek_kind(Parsing::Unary)? {
|
let mut head = match p.peek_kind(Parsing::Unary)? {
|
||||||
literal_like!() => Literal::parse(self)?.into(),
|
literal_like!() => Literal::parse(p)?.into(),
|
||||||
path_like!() => self.exprkind_pathlike()?,
|
path_like!() => exprkind_pathlike(p)?,
|
||||||
TokenKind::Amp | TokenKind::AmpAmp => AddrOf::parse(self)?.into(),
|
TokenKind::Amp | TokenKind::AmpAmp => AddrOf::parse(p)?.into(),
|
||||||
TokenKind::LCurly => Block::parse(self)?.into(),
|
TokenKind::LCurly => Block::parse(p)?.into(),
|
||||||
TokenKind::LBrack => self.exprkind_arraylike()?,
|
TokenKind::LBrack => exprkind_arraylike(p)?,
|
||||||
TokenKind::LParen => self.exprkind_tuplelike()?,
|
TokenKind::LParen => exprkind_tuplelike(p)?,
|
||||||
TokenKind::Let => Let::parse(self)?.into(),
|
TokenKind::Let => Let::parse(p)?.into(),
|
||||||
TokenKind::While => ExprKind::While(While::parse(self)?),
|
TokenKind::While => ExprKind::While(While::parse(p)?),
|
||||||
TokenKind::If => ExprKind::If(If::parse(self)?),
|
TokenKind::If => ExprKind::If(If::parse(p)?),
|
||||||
TokenKind::For => ExprKind::For(For::parse(self)?),
|
TokenKind::For => ExprKind::For(For::parse(p)?),
|
||||||
TokenKind::Break => ExprKind::Break(Break::parse(self)?),
|
TokenKind::Break => ExprKind::Break(Break::parse(p)?),
|
||||||
TokenKind::Return => ExprKind::Return(Return::parse(self)?),
|
TokenKind::Return => ExprKind::Return(Return::parse(p)?),
|
||||||
TokenKind::Continue => {
|
TokenKind::Continue => {
|
||||||
self.consume_peeked();
|
p.consume_peeked();
|
||||||
ExprKind::Continue
|
ExprKind::Continue
|
||||||
}
|
}
|
||||||
|
|
||||||
op => {
|
op => {
|
||||||
let (kind, prec) =
|
let (kind, prec) = from_prefix(op).ok_or_else(|| p.error(Unexpected(op), parsing))?;
|
||||||
from_prefix(op).ok_or_else(|| self.error(Unexpected(op), parsing))?;
|
|
||||||
let ((), after) = prec.prefix().expect("should have a precedence");
|
let ((), after) = prec.prefix().expect("should have a precedence");
|
||||||
self.consume_peeked();
|
p.consume_peeked();
|
||||||
Unary { kind, tail: self.exprkind(after)?.into() }.into()
|
Unary { kind, tail: exprkind(p, after)?.into() }.into()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -902,36 +940,32 @@ impl<'t> Parser<'t> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
while let Ok(op) = self.peek_kind(parsing) {
|
while let Ok(op) = p.peek_kind(parsing) {
|
||||||
// Postfix expressions
|
// Postfix expressions
|
||||||
if let Some((before, ())) = from_postfix(op).and_then(Precedence::postfix) {
|
if let Some((before, ())) = from_postfix(op).and_then(Precedence::postfix) {
|
||||||
if before < power {
|
if before < power {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
self.consume_peeked();
|
p.consume_peeked();
|
||||||
|
|
||||||
head = match op {
|
head = match op {
|
||||||
TokenKind::LBrack => {
|
TokenKind::LBrack => {
|
||||||
let indices =
|
let indices =
|
||||||
sep(Expr::parse, TokenKind::Comma, TokenKind::RBrack, parsing)(self)?;
|
sep(Expr::parse, TokenKind::Comma, TokenKind::RBrack, parsing)(p)?;
|
||||||
self.match_type(TokenKind::RBrack, parsing)?;
|
p.match_type(TokenKind::RBrack, parsing)?;
|
||||||
ExprKind::Index(Index { head: head.into(), indices })
|
ExprKind::Index(Index { head: head.into(), indices })
|
||||||
}
|
}
|
||||||
TokenKind::LParen => {
|
TokenKind::LParen => {
|
||||||
let exprs =
|
let exprs = sep(Expr::parse, TokenKind::Comma, TokenKind::RParen, parsing)(p)?;
|
||||||
sep(Expr::parse, TokenKind::Comma, TokenKind::RParen, parsing)(self)?;
|
p.match_type(TokenKind::RParen, parsing)?;
|
||||||
self.match_type(TokenKind::RParen, parsing)?;
|
Binary { kind: BinaryKind::Call, parts: (head, Tuple { exprs }.into()).into() }
|
||||||
Binary {
|
|
||||||
kind: BinaryKind::Call,
|
|
||||||
parts: (head, Tuple { exprs }.into()).into(),
|
|
||||||
}
|
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
TokenKind::Dot => {
|
TokenKind::Dot => {
|
||||||
let kind = MemberKind::parse(self)?;
|
let kind = MemberKind::parse(p)?;
|
||||||
Member { head: Box::new(head), kind }.into()
|
Member { head: Box::new(head), kind }.into()
|
||||||
}
|
}
|
||||||
_ => Err(self.error(Unexpected(op), parsing))?,
|
_ => Err(p.error(Unexpected(op), parsing))?,
|
||||||
};
|
};
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -941,9 +975,9 @@ impl<'t> Parser<'t> {
|
|||||||
if before < power {
|
if before < power {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
self.consume_peeked();
|
p.consume_peeked();
|
||||||
|
|
||||||
let tail = self.exprkind(after)?;
|
let tail = exprkind(p, after)?;
|
||||||
head = Binary { kind, parts: (head, tail).into() }.into();
|
head = Binary { kind, parts: (head, tail).into() }.into();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -953,24 +987,13 @@ impl<'t> Parser<'t> {
|
|||||||
if before < power {
|
if before < power {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
self.consume_peeked();
|
p.consume_peeked();
|
||||||
|
|
||||||
let tail = self.exprkind(after)?;
|
let tail = exprkind(p, after)?;
|
||||||
head = Modify { kind, parts: (head, tail).into() }.into();
|
head = Modify { kind, parts: (head, tail).into() }.into();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let TokenKind::As = op {
|
|
||||||
let before = Precedence::Cast.level();
|
|
||||||
if before < power {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
self.consume_peeked();
|
|
||||||
let ty = Ty::parse(self)?;
|
|
||||||
head = Cast { head: head.into(), ty }.into();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let TokenKind::Eq = op {
|
if let TokenKind::Eq = op {
|
||||||
let (before, after) = Precedence::Assign
|
let (before, after) = Precedence::Assign
|
||||||
.infix()
|
.infix()
|
||||||
@ -978,87 +1001,101 @@ impl<'t> Parser<'t> {
|
|||||||
if before < power {
|
if before < power {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
self.consume_peeked();
|
p.consume_peeked();
|
||||||
let tail = self.exprkind(after)?;
|
|
||||||
|
let tail = exprkind(p, after)?;
|
||||||
head = Assign { parts: (head, tail).into() }.into();
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(head)
|
Ok(head)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [Array] = '[' ([Expr] ',')* [Expr]? ']'
|
/// [Array] = '[' ([Expr] ',')* [Expr]? ']'
|
||||||
///
|
///
|
||||||
/// Array and ArrayRef are ambiguous until the second token,
|
/// Array and ArrayRef are ambiguous until the second token,
|
||||||
/// so they can't be independent subexpressions
|
/// so they can't be independent subexpressions
|
||||||
pub fn exprkind_arraylike(&mut self) -> PResult<ExprKind> {
|
fn exprkind_arraylike(p: &mut Parser) -> PResult<ExprKind> {
|
||||||
const P: Parsing = Parsing::Array;
|
const P: Parsing = Parsing::Array;
|
||||||
const START: TokenKind = TokenKind::LBrack;
|
const START: TokenKind = TokenKind::LBrack;
|
||||||
const END: TokenKind = TokenKind::RBrack;
|
const END: TokenKind = TokenKind::RBrack;
|
||||||
|
|
||||||
self.match_type(START, P)?;
|
p.match_type(START, P)?;
|
||||||
let out = match self.peek_kind(P)? {
|
let out = match p.peek_kind(P)? {
|
||||||
END => Array { values: vec![] }.into(),
|
END => Array { values: vec![] }.into(),
|
||||||
_ => self.exprkind_array_rep()?,
|
_ => exprkind_array_rep(p)?,
|
||||||
};
|
};
|
||||||
self.match_type(END, P)?;
|
p.match_type(END, P)?;
|
||||||
Ok(out)
|
Ok(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [ArrayRep] = `[` [Expr] `;` [Expr] `]`
|
/// [ArrayRep] = `[` [Expr] `;` [Expr] `]`
|
||||||
pub fn exprkind_array_rep(&mut self) -> PResult<ExprKind> {
|
fn exprkind_array_rep(p: &mut Parser) -> PResult<ExprKind> {
|
||||||
const P: Parsing = Parsing::Array;
|
const P: Parsing = Parsing::Array;
|
||||||
const END: TokenKind = TokenKind::RBrack;
|
const END: TokenKind = TokenKind::RBrack;
|
||||||
|
|
||||||
let first = Expr::parse(self)?;
|
let first = Expr::parse(p)?;
|
||||||
Ok(match self.peek_kind(P)? {
|
Ok(match p.peek_kind(P)? {
|
||||||
TokenKind::Semi => ArrayRep {
|
TokenKind::Semi => ArrayRep {
|
||||||
value: first.kind.into(),
|
value: first.kind.into(),
|
||||||
repeat: {
|
repeat: {
|
||||||
self.consume_peeked();
|
p.consume_peeked();
|
||||||
Box::new(self.exprkind(0)?)
|
Box::new(exprkind(p, 0)?)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
TokenKind::RBrack => Array { values: vec![first] }.into(),
|
TokenKind::RBrack => Array { values: vec![first] }.into(),
|
||||||
TokenKind::Comma => Array {
|
TokenKind::Comma => Array {
|
||||||
values: {
|
values: {
|
||||||
self.consume_peeked();
|
p.consume_peeked();
|
||||||
let mut out = vec![first];
|
let mut out = vec![first];
|
||||||
out.extend(sep(Expr::parse, TokenKind::Comma, END, P)(self)?);
|
out.extend(sep(Expr::parse, TokenKind::Comma, END, P)(p)?);
|
||||||
out
|
out
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
ty => Err(self.error(Unexpected(ty), P))?,
|
ty => Err(p.error(Unexpected(ty), P))?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [Group] = `(`([Empty](ExprKind::Empty)|[Expr]|[Tuple])`)`
|
/// [Group] = `(`([Empty](ExprKind::Empty)|[Expr]|[Tuple])`)`
|
||||||
///
|
///
|
||||||
/// [ExprKind::Empty] and [Group] are special cases of [Tuple]
|
/// [ExprKind::Empty] and [Group] are special cases of [Tuple]
|
||||||
pub fn exprkind_tuplelike(&mut self) -> PResult<ExprKind> {
|
fn exprkind_tuplelike(p: &mut Parser) -> PResult<ExprKind> {
|
||||||
self.match_type(TokenKind::LParen, Parsing::Group)?;
|
p.match_type(TokenKind::LParen, Parsing::Group)?;
|
||||||
let out = match self.peek_kind(Parsing::Group)? {
|
let out = match p.peek_kind(Parsing::Group)? {
|
||||||
TokenKind::RParen => Ok(ExprKind::Empty),
|
TokenKind::RParen => Ok(ExprKind::Empty),
|
||||||
_ => self.exprkind_group(),
|
_ => exprkind_group(p),
|
||||||
};
|
};
|
||||||
self.match_type(TokenKind::RParen, Parsing::Group)?;
|
p.match_type(TokenKind::RParen, Parsing::Group)?;
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [Group] = `(`([Empty](ExprKind::Empty)|[Expr]|[Tuple])`)`
|
/// [Group] = `(`([Empty](ExprKind::Empty)|[Expr]|[Tuple])`)`
|
||||||
pub fn exprkind_group(&mut self) -> PResult<ExprKind> {
|
fn exprkind_group(p: &mut Parser) -> PResult<ExprKind> {
|
||||||
let first = Expr::parse(self)?;
|
let first = Expr::parse(p)?;
|
||||||
match self.peek_kind(Parsing::Group)? {
|
match p.peek_kind(Parsing::Group)? {
|
||||||
TokenKind::Comma => {
|
TokenKind::Comma => {
|
||||||
let mut exprs = vec![first];
|
let mut exprs = vec![first];
|
||||||
self.consume_peeked();
|
p.consume_peeked();
|
||||||
while TokenKind::RParen != self.peek_kind(Parsing::Tuple)? {
|
while TokenKind::RParen != p.peek_kind(Parsing::Tuple)? {
|
||||||
exprs.push(Expr::parse(self)?);
|
exprs.push(Expr::parse(p)?);
|
||||||
match self.peek_kind(Parsing::Tuple)? {
|
match p.peek_kind(Parsing::Tuple)? {
|
||||||
TokenKind::Comma => self.consume_peeked(),
|
TokenKind::Comma => p.consume_peeked(),
|
||||||
_ => break,
|
_ => break,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -1066,19 +1103,19 @@ impl<'t> Parser<'t> {
|
|||||||
}
|
}
|
||||||
_ => 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])
|
||||||
pub fn exprkind_pathlike(&mut self) -> PResult<ExprKind> {
|
fn exprkind_pathlike(p: &mut Parser) -> PResult<ExprKind> {
|
||||||
let head = Path::parse(self)?;
|
let head = Path::parse(p)?;
|
||||||
Ok(match self.match_type(TokenKind::Colon, Parsing::Path) {
|
Ok(match p.match_type(TokenKind::Colon, Parsing::Path) {
|
||||||
Ok(_) => ExprKind::Structor(self.structor_body(head)?),
|
Ok(_) => ExprKind::Structor(structor_body(p, head)?),
|
||||||
Err(_) => ExprKind::Path(head),
|
Err(_) => ExprKind::Path(head),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [Structor]Body = `{` ([Fielder] `,`)* [Fielder]? `}`
|
/// [Structor]Body = `{` ([Fielder] `,`)* [Fielder]? `}`
|
||||||
pub fn structor_body(&mut self, 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,
|
||||||
@ -1088,9 +1125,33 @@ impl<'t> Parser<'t> {
|
|||||||
),
|
),
|
||||||
CURLIES,
|
CURLIES,
|
||||||
Parsing::Structor,
|
Parsing::Structor,
|
||||||
)(self)?;
|
)(p)?;
|
||||||
|
|
||||||
Ok(Structor { to, init })
|
Ok(Structor { to, init })
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parse<'_> for Let {
|
||||||
|
fn parse(p: &mut Parser) -> PResult<Let> {
|
||||||
|
p.consume_peeked();
|
||||||
|
Ok(Let {
|
||||||
|
mutable: Mutability::parse(p)?,
|
||||||
|
name: Sym::parse(p)?,
|
||||||
|
ty: if p.match_type(TokenKind::Colon, Parsing::Let).is_ok() {
|
||||||
|
Some(Ty::parse(p)?.into())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
init: if p.match_type(TokenKind::Eq, Parsing::Let).is_ok() {
|
||||||
|
Some(Expr::parse(p)?.into())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
tail: if p.match_type(TokenKind::Semi, Parsing::Let).is_ok() {
|
||||||
|
Some(Expr::parse(p)?.into())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1145,27 +1206,7 @@ impl Parse<'_> for AddrOf {
|
|||||||
};
|
};
|
||||||
p.consume_peeked();
|
p.consume_peeked();
|
||||||
}
|
}
|
||||||
Ok(AddrOf { count, mutable: Mutability::parse(p)?, expr: p.exprkind(0)?.into() })
|
Ok(AddrOf { count, mutable: Mutability::parse(p)?, expr: ExprKind::parse(p)?.into() })
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse<'_> for Literal {
|
|
||||||
/// [Literal] = [LITERAL](TokenKind::Literal) | `true` | `false`
|
|
||||||
fn parse(p: &mut Parser) -> PResult<Literal> {
|
|
||||||
let Token { ty, data, .. } = p.consume(Parsing::Literal)?;
|
|
||||||
match ty {
|
|
||||||
TokenKind::True => return Ok(Literal::Bool(true)),
|
|
||||||
TokenKind::False => return Ok(Literal::Bool(false)),
|
|
||||||
TokenKind::Literal => (),
|
|
||||||
t => return Err(p.error(Unexpected(t), Parsing::Literal)),
|
|
||||||
}
|
|
||||||
Ok(match data {
|
|
||||||
TokenData::String(v) => Literal::String(v),
|
|
||||||
TokenData::Character(v) => Literal::Char(v),
|
|
||||||
TokenData::Integer(v) => Literal::Int(v),
|
|
||||||
TokenData::Float(v) => todo!("Literal::Float({v})"),
|
|
||||||
_ => panic!("Expected token data for {ty:?}"),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1177,55 +1218,6 @@ impl Parse<'_> for Block {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse<'_> for Let {
|
|
||||||
fn parse(p: &mut Parser) -> PResult<Let> {
|
|
||||||
p.consume_peeked();
|
|
||||||
Ok(Let {
|
|
||||||
mutable: Mutability::parse(p)?,
|
|
||||||
name: Sym::parse(p)?,
|
|
||||||
ty: if p.match_type(TokenKind::Colon, Parsing::Let).is_ok() {
|
|
||||||
Some(Ty::parse(p)?.into())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
},
|
|
||||||
init: if p.match_type(TokenKind::Eq, Parsing::Let).is_ok() {
|
|
||||||
Some(Expr::parse(p)?.into())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
},
|
|
||||||
tail: if p.match_type(TokenKind::Semi, Parsing::Let).is_ok() {
|
|
||||||
Some(Expr::parse(p)?.into())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse<'_> for Break {
|
|
||||||
/// [Break] = `break` (*unconsumed* `;` | [Expr])
|
|
||||||
fn parse(p: &mut Parser) -> PResult<Break> {
|
|
||||||
p.match_type(TokenKind::Break, Parsing::Break)?;
|
|
||||||
Ok(Break { body: ret_body(p, Parsing::Break)? })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse<'_> for Return {
|
|
||||||
/// [Return] = `return` (*unconsumed* `;` | [Expr])
|
|
||||||
fn parse(p: &mut Parser) -> PResult<Return> {
|
|
||||||
p.match_type(TokenKind::Return, Parsing::Return)?;
|
|
||||||
Ok(Return { body: ret_body(p, Parsing::Return)? })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// ret_body = (*unconsumed* `;` | [Expr])
|
|
||||||
fn ret_body(p: &mut Parser, while_parsing: Parsing) -> PResult<Option<Box<Expr>>> {
|
|
||||||
Ok(match p.peek_kind(while_parsing)? {
|
|
||||||
TokenKind::Semi => None,
|
|
||||||
_ => Some(Expr::parse(p)?.into()),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse<'_> for While {
|
impl Parse<'_> for While {
|
||||||
/// [While] = `while` [Expr] [Block] [Else]?
|
/// [While] = `while` [Expr] [Block] [Else]?
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
@ -1282,6 +1274,30 @@ impl Parse<'_> for Else {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Parse<'_> for Break {
|
||||||
|
/// [Break] = `break` (*unconsumed* `;` | [Expr])
|
||||||
|
fn parse(p: &mut Parser) -> PResult<Break> {
|
||||||
|
p.match_type(TokenKind::Break, Parsing::Break)?;
|
||||||
|
Ok(Break { body: ret_body(p, Parsing::Break)? })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parse<'_> for Return {
|
||||||
|
/// [Return] = `return` (*unconsumed* `;` | [Expr])
|
||||||
|
fn parse(p: &mut Parser) -> PResult<Return> {
|
||||||
|
p.match_type(TokenKind::Return, Parsing::Return)?;
|
||||||
|
Ok(Return { body: ret_body(p, Parsing::Return)? })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ret_body = (*unconsumed* `;` | [Expr])
|
||||||
|
fn ret_body(p: &mut Parser, while_parsing: Parsing) -> PResult<Option<Box<Expr>>> {
|
||||||
|
Ok(match p.peek_kind(while_parsing)? {
|
||||||
|
TokenKind::Semi => None,
|
||||||
|
_ => Some(Expr::parse(p)?.into()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Precedence provides a total ordering among operators
|
/// Precedence provides a total ordering among operators
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
pub enum Precedence {
|
pub enum Precedence {
|
||||||
@ -1305,6 +1321,7 @@ impl Precedence {
|
|||||||
pub const fn level(self) -> u8 {
|
pub const fn level(self) -> u8 {
|
||||||
(self as u8) << 1
|
(self as u8) << 1
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prefix(self) -> Option<((), u8)> {
|
pub fn prefix(self) -> Option<((), u8)> {
|
||||||
match self {
|
match self {
|
||||||
Self::Assign => Some(((), self.level())),
|
Self::Assign => Some(((), self.level())),
|
||||||
@ -1312,6 +1329,7 @@ impl Precedence {
|
|||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn infix(self) -> Option<(u8, u8)> {
|
pub fn infix(self) -> Option<(u8, u8)> {
|
||||||
let level = self.level();
|
let level = self.level();
|
||||||
match self {
|
match self {
|
||||||
@ -1320,6 +1338,7 @@ impl Precedence {
|
|||||||
_ => Some((level, level + 1)),
|
_ => Some((level, level + 1)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn postfix(self) -> Option<(u8, ())> {
|
pub fn postfix(self) -> Option<(u8, ())> {
|
||||||
match self {
|
match self {
|
||||||
Self::Index | Self::Call | Self::Member => Some((self.level(), ())),
|
Self::Index | Self::Call | Self::Member => Some((self.level(), ())),
|
||||||
@ -1327,6 +1346,7 @@ impl Precedence {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ModifyKind> for Precedence {
|
impl From<ModifyKind> for Precedence {
|
||||||
fn from(_value: ModifyKind) -> Self {
|
fn from(_value: ModifyKind) -> Self {
|
||||||
Precedence::Assign
|
Precedence::Assign
|
||||||
@ -1348,6 +1368,7 @@ impl From<BinaryKind> for Precedence {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<UnaryKind> for Precedence {
|
impl From<UnaryKind> for Precedence {
|
||||||
fn from(value: UnaryKind) -> Self {
|
fn from(value: UnaryKind) -> Self {
|
||||||
use UnaryKind as Op;
|
use UnaryKind as Op;
|
||||||
@ -1358,7 +1379,7 @@ impl From<UnaryKind> for Precedence {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates helper functions for
|
/// Creates helper functions for turning TokenKinds into AST operators
|
||||||
macro operator($($name:ident ($takes:ident => $returns:ident) {$($t:ident => $p:ident),*$(,)?};)*) {$(
|
macro operator($($name:ident ($takes:ident => $returns:ident) {$($t:ident => $p:ident),*$(,)?};)*) {$(
|
||||||
pub fn $name (value: $takes) -> Option<($returns, Precedence)> {
|
pub fn $name (value: $takes) -> Option<($returns, Precedence)> {
|
||||||
match value {
|
match value {
|
||||||
@ -1377,6 +1398,7 @@ operator! {
|
|||||||
At => At,
|
At => At,
|
||||||
Tilde => Tilde,
|
Tilde => Tilde,
|
||||||
};
|
};
|
||||||
|
|
||||||
from_modify(TokenKind => ModifyKind) {
|
from_modify(TokenKind => ModifyKind) {
|
||||||
AmpEq => And,
|
AmpEq => And,
|
||||||
BarEq => Or,
|
BarEq => Or,
|
||||||
@ -1389,8 +1411,8 @@ operator! {
|
|||||||
SlashEq => Div,
|
SlashEq => Div,
|
||||||
RemEq => Rem,
|
RemEq => Rem,
|
||||||
};
|
};
|
||||||
from_infix (TokenKind => BinaryKind) {
|
|
||||||
|
|
||||||
|
from_infix (TokenKind => BinaryKind) {
|
||||||
Lt => Lt,
|
Lt => Lt,
|
||||||
LtEq => LtEq,
|
LtEq => LtEq,
|
||||||
EqEq => Equal,
|
EqEq => Equal,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user