2024-03-01 02:44:49 +00:00
|
|
|
use super::*;
|
|
|
|
use crate::error::{
|
|
|
|
Error,
|
|
|
|
ErrorKind::{self, *},
|
|
|
|
PResult, Parsing,
|
|
|
|
};
|
|
|
|
use cl_ast::*;
|
2024-03-01 02:58:50 +00:00
|
|
|
use cl_lexer::Lexer;
|
2024-03-01 02:44:49 +00:00
|
|
|
|
|
|
|
/// Parses a sequence of [Tokens](Token) into an [AST](cl_ast)
|
|
|
|
pub struct Parser<'t> {
|
|
|
|
/// Lazy tokenizer
|
|
|
|
lexer: Lexer<'t>,
|
|
|
|
/// Look-ahead buffer
|
|
|
|
next: Option<Token>,
|
|
|
|
/// The location of the current token
|
|
|
|
loc: Loc,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Basic parser functionality
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
pub fn new(lexer: Lexer<'t>) -> Self {
|
|
|
|
Self { loc: Loc::from(&lexer), lexer, next: None }
|
|
|
|
}
|
|
|
|
/// Gets the location of the last consumed [Token]
|
|
|
|
pub fn loc(&self) -> Loc {
|
|
|
|
self.loc
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs an [Error]
|
|
|
|
fn error(&self, reason: ErrorKind, while_parsing: Parsing) -> Error {
|
|
|
|
Error { reason, while_parsing, loc: self.loc }
|
|
|
|
}
|
|
|
|
/// Internal impl of peek and consume
|
|
|
|
fn consume_from_lexer(&mut self, while_parsing: Parsing) -> PResult<Token> {
|
|
|
|
loop {
|
|
|
|
match self
|
|
|
|
.lexer
|
|
|
|
.scan()
|
|
|
|
.map_err(|e| self.error(e.into(), while_parsing))?
|
|
|
|
{
|
2024-04-12 19:36:26 +00:00
|
|
|
t if t.ty() == TokenKind::Invalid => continue,
|
|
|
|
t if t.ty() == TokenKind::Comment => continue,
|
2024-03-01 02:44:49 +00:00
|
|
|
t => break Ok(t),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Looks ahead one token
|
|
|
|
///
|
|
|
|
/// Stores the token in an internal lookahead buffer
|
|
|
|
pub fn peek(&mut self, while_parsing: Parsing) -> PResult<&Token> {
|
|
|
|
if self.next.is_none() {
|
|
|
|
self.next = Some(self.consume_from_lexer(while_parsing)?);
|
|
|
|
}
|
|
|
|
self.next.as_ref().ok_or_else(|| unreachable!())
|
|
|
|
}
|
|
|
|
/// Consumes a previously peeked [Token], returning it.
|
|
|
|
/// Returns [None] when there is no peeked token.
|
|
|
|
///
|
|
|
|
/// This avoids the overhead of constructing an [Error]
|
|
|
|
pub fn consume_peeked(&mut self) -> Option<Token> {
|
|
|
|
// location must be updated whenever a token is pulled from the lexer
|
|
|
|
self.loc = Loc::from(&self.lexer);
|
|
|
|
self.next.take()
|
|
|
|
}
|
2024-04-12 19:36:26 +00:00
|
|
|
/// Looks ahead at the next [Token]'s [TokenKind]
|
|
|
|
pub fn peek_kind(&mut self, while_parsing: Parsing) -> PResult<TokenKind> {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.peek(while_parsing).map(|t| t.ty())
|
|
|
|
}
|
|
|
|
/// Consumes one [Token]
|
|
|
|
pub fn consume(&mut self, while_parsing: Parsing) -> PResult<Token> {
|
|
|
|
self.loc = Loc::from(&self.lexer);
|
|
|
|
match self.next.take() {
|
|
|
|
Some(token) => Ok(token),
|
|
|
|
None => self.consume_from_lexer(while_parsing),
|
|
|
|
}
|
|
|
|
}
|
2024-04-12 19:36:26 +00:00
|
|
|
/// Consumes the next [Token] if it matches the pattern [TokenKind]
|
|
|
|
pub fn match_type(&mut self, want: TokenKind, while_parsing: Parsing) -> PResult<Token> {
|
|
|
|
let got = self.peek_kind(while_parsing)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
if got == want {
|
|
|
|
Ok(self.consume_peeked().expect("should not fail after peek"))
|
|
|
|
} else {
|
|
|
|
Err(self.error(Expected { want, got }, while_parsing))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the three matched delimiter pairs
|
|
|
|
/// Square brackets: `[` `]`
|
2024-04-12 19:36:26 +00:00
|
|
|
const BRACKETS: (TokenKind, TokenKind) = (TokenKind::LBrack, TokenKind::RBrack);
|
2024-03-01 02:44:49 +00:00
|
|
|
/// Curly braces: `{` `}`
|
2024-04-12 19:36:26 +00:00
|
|
|
const CURLIES: (TokenKind, TokenKind) = (TokenKind::LCurly, TokenKind::RCurly);
|
2024-03-01 02:44:49 +00:00
|
|
|
/// Parentheses: `(` `)`
|
2024-04-12 19:36:26 +00:00
|
|
|
const PARENS: (TokenKind, TokenKind) = (TokenKind::LParen, TokenKind::RParen);
|
2024-03-01 02:44:49 +00:00
|
|
|
|
|
|
|
/// Parses constructions of the form `delim.0 f delim.1` (i.e. `(` `foobar` `)`)
|
|
|
|
const fn delim<'t, T>(
|
|
|
|
f: impl Fn(&mut Parser<'t>) -> PResult<T>,
|
2024-04-12 19:36:26 +00:00
|
|
|
delim: (TokenKind, TokenKind),
|
2024-03-01 02:44:49 +00:00
|
|
|
while_parsing: Parsing,
|
|
|
|
) -> impl Fn(&mut Parser<'t>) -> PResult<T> {
|
|
|
|
move |parser| {
|
|
|
|
parser.match_type(delim.0, while_parsing)?;
|
|
|
|
let out = f(parser)?;
|
|
|
|
parser.match_type(delim.1, while_parsing)?;
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses constructions of the form `(f sep ~until)*`
|
|
|
|
///
|
|
|
|
/// where `~until` is a negative lookahead assertion
|
|
|
|
const fn sep<'t, T>(
|
|
|
|
f: impl Fn(&mut Parser<'t>) -> PResult<T>,
|
2024-04-12 19:36:26 +00:00
|
|
|
sep: TokenKind,
|
|
|
|
until: TokenKind,
|
2024-03-01 02:44:49 +00:00
|
|
|
while_parsing: Parsing,
|
|
|
|
) -> impl Fn(&mut Parser<'t>) -> PResult<Vec<T>> {
|
|
|
|
move |parser| {
|
|
|
|
let mut args = vec![];
|
2024-04-12 19:36:26 +00:00
|
|
|
while until != parser.peek_kind(while_parsing)? {
|
2024-03-01 02:44:49 +00:00
|
|
|
args.push(f(parser)?);
|
2024-04-12 19:36:26 +00:00
|
|
|
if sep != parser.peek_kind(while_parsing)? {
|
2024-03-01 02:44:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
parser.consume_peeked();
|
|
|
|
}
|
|
|
|
Ok(args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses constructions of the form `(f ~until)*`
|
|
|
|
///
|
|
|
|
/// where `~until` is a negative lookahead assertion
|
|
|
|
#[allow(dead_code)]
|
|
|
|
const fn rep<'t, T>(
|
|
|
|
f: impl Fn(&mut Parser<'t>) -> PResult<T>,
|
2024-04-12 19:36:26 +00:00
|
|
|
until: TokenKind,
|
2024-03-01 02:44:49 +00:00
|
|
|
while_parsing: Parsing,
|
|
|
|
) -> impl Fn(&mut Parser<'t>) -> PResult<Vec<T>> {
|
|
|
|
move |parser| {
|
|
|
|
let mut out = vec![];
|
2024-04-12 19:36:26 +00:00
|
|
|
while until != parser.peek_kind(while_parsing)? {
|
2024-03-01 02:44:49 +00:00
|
|
|
out.push(f(parser)?)
|
|
|
|
}
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 19:36:26 +00:00
|
|
|
/// Expands to a pattern which matches item-like [Token] [TokenKind]s
|
2024-03-01 02:44:49 +00:00
|
|
|
macro item_like() {
|
2024-04-12 19:36:26 +00:00
|
|
|
TokenKind::Hash
|
|
|
|
| TokenKind::Pub
|
|
|
|
| TokenKind::Type
|
|
|
|
| TokenKind::Const
|
|
|
|
| TokenKind::Static
|
|
|
|
| TokenKind::Mod
|
|
|
|
| TokenKind::Fn
|
|
|
|
| TokenKind::Struct
|
|
|
|
| TokenKind::Enum
|
|
|
|
| TokenKind::Impl
|
2024-03-01 02:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Top level parsing
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
/// Parses a [File]
|
|
|
|
pub fn file(&mut self) -> PResult<File> {
|
|
|
|
let mut items = vec![];
|
2024-04-12 19:36:26 +00:00
|
|
|
while match self.peek_kind(Parsing::File) {
|
|
|
|
Ok(TokenKind::RCurly) | Err(Error { reason: EndOfInput, .. }) => false,
|
2024-03-01 02:44:49 +00:00
|
|
|
Ok(_) => true,
|
|
|
|
Err(e) => Err(e)?,
|
|
|
|
} {
|
|
|
|
items.push(self.item()?)
|
|
|
|
}
|
|
|
|
Ok(File { items })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses an [Item]
|
|
|
|
///
|
|
|
|
/// See also: [Parser::itemkind]
|
|
|
|
pub fn item(&mut self) -> PResult<Item> {
|
|
|
|
let start = self.loc();
|
|
|
|
Ok(Item {
|
|
|
|
vis: self.visibility()?,
|
|
|
|
attrs: self.attributes()?,
|
|
|
|
kind: self.itemkind()?,
|
|
|
|
extents: Span(start, self.loc()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a [Ty]
|
|
|
|
///
|
|
|
|
/// See also: [Parser::tykind]
|
|
|
|
pub fn ty(&mut self) -> PResult<Ty> {
|
|
|
|
let start = self.loc();
|
|
|
|
Ok(Ty { kind: self.tykind()?, extents: Span(start, self.loc()) })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a [Path]
|
|
|
|
///
|
|
|
|
/// See also: [Parser::path_part], [Parser::identifier]
|
|
|
|
pub fn path(&mut self) -> PResult<Path> {
|
|
|
|
const PARSING: Parsing = Parsing::PathExpr;
|
2024-04-12 19:36:26 +00:00
|
|
|
let absolute = matches!(self.peek_kind(PARSING)?, TokenKind::ColonColon);
|
2024-03-01 02:44:49 +00:00
|
|
|
if absolute {
|
|
|
|
self.consume_peeked();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut parts = vec![self.path_part()?];
|
2024-04-12 19:36:26 +00:00
|
|
|
while let Ok(TokenKind::ColonColon) = self.peek_kind(PARSING) {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
parts.push(self.path_part()?);
|
|
|
|
}
|
|
|
|
Ok(Path { absolute, parts })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a [Stmt]
|
|
|
|
///
|
|
|
|
/// See also: [Parser::stmtkind]
|
|
|
|
pub fn stmt(&mut self) -> PResult<Stmt> {
|
|
|
|
const PARSING: Parsing = Parsing::Stmt;
|
|
|
|
let start = self.loc();
|
|
|
|
Ok(Stmt {
|
|
|
|
kind: self.stmtkind()?,
|
2024-04-12 19:36:26 +00:00
|
|
|
semi: match self.peek_kind(PARSING) {
|
|
|
|
Ok(TokenKind::Semi) => {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
Semi::Terminated
|
|
|
|
}
|
|
|
|
_ => Semi::Unterminated,
|
|
|
|
},
|
|
|
|
extents: Span(start, self.loc()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses an [Expr]
|
|
|
|
///
|
|
|
|
/// See also: [Parser::exprkind]
|
|
|
|
pub fn expr(&mut self) -> PResult<Expr> {
|
|
|
|
self.expr_from(Self::exprkind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attribute parsing
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
/// Parses an [attribute set](Attrs)
|
|
|
|
pub fn attributes(&mut self) -> PResult<Attrs> {
|
2024-04-12 19:36:26 +00:00
|
|
|
if self.match_type(TokenKind::Hash, Parsing::Attrs).is_err() {
|
2024-03-01 02:44:49 +00:00
|
|
|
return Ok(Attrs { meta: vec![] });
|
|
|
|
}
|
|
|
|
let meta = delim(
|
2024-04-12 19:36:26 +00:00
|
|
|
sep(Self::meta, TokenKind::Comma, BRACKETS.1, Parsing::Attrs),
|
2024-03-01 02:44:49 +00:00
|
|
|
BRACKETS,
|
|
|
|
Parsing::Attrs,
|
|
|
|
);
|
|
|
|
Ok(Attrs { meta: meta(self)? })
|
|
|
|
}
|
|
|
|
pub fn meta(&mut self) -> PResult<Meta> {
|
|
|
|
Ok(Meta { name: self.identifier()?, kind: self.meta_kind()? })
|
|
|
|
}
|
|
|
|
pub fn meta_kind(&mut self) -> PResult<MetaKind> {
|
|
|
|
const PARSING: Parsing = Parsing::Meta;
|
|
|
|
let lit_tuple = delim(
|
2024-04-12 19:36:26 +00:00
|
|
|
sep(Self::literal, TokenKind::Comma, PARENS.1, PARSING),
|
2024-03-01 02:44:49 +00:00
|
|
|
PARENS,
|
|
|
|
PARSING,
|
|
|
|
);
|
2024-04-12 19:36:26 +00:00
|
|
|
Ok(match self.peek_kind(PARSING) {
|
|
|
|
Ok(TokenKind::Eq) => {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
MetaKind::Equals(self.literal()?)
|
|
|
|
}
|
2024-04-12 19:36:26 +00:00
|
|
|
Ok(TokenKind::LParen) => MetaKind::Func(lit_tuple(self)?),
|
2024-03-01 02:44:49 +00:00
|
|
|
_ => MetaKind::Plain,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Item parsing
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
/// Parses an [ItemKind]
|
|
|
|
///
|
|
|
|
/// See also: [Parser::item]
|
|
|
|
pub fn itemkind(&mut self) -> PResult<ItemKind> {
|
2024-04-12 19:36:26 +00:00
|
|
|
Ok(match self.peek_kind(Parsing::Item)? {
|
|
|
|
TokenKind::Type => self.parse_alias()?.into(),
|
|
|
|
TokenKind::Const => self.parse_const()?.into(),
|
|
|
|
TokenKind::Static => self.parse_static()?.into(),
|
|
|
|
TokenKind::Mod => self.parse_module()?.into(),
|
|
|
|
TokenKind::Fn => self.parse_function()?.into(),
|
|
|
|
TokenKind::Struct => self.parse_struct()?.into(),
|
|
|
|
TokenKind::Enum => self.parse_enum()?.into(),
|
|
|
|
TokenKind::Impl => self.parse_impl()?.into(),
|
2024-03-01 02:44:49 +00:00
|
|
|
t => Err(self.error(Unexpected(t), Parsing::Item))?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_alias(&mut self) -> PResult<Alias> {
|
|
|
|
const PARSING: Parsing = Parsing::Alias;
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Type, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
let out = Ok(Alias {
|
2024-04-01 09:20:26 +00:00
|
|
|
to: self.identifier()?,
|
2024-04-12 19:36:26 +00:00
|
|
|
from: if self.match_type(TokenKind::Eq, PARSING).is_ok() {
|
2024-03-01 02:44:49 +00:00
|
|
|
Some(self.ty()?.into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
});
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Semi, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
out
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_const(&mut self) -> PResult<Const> {
|
|
|
|
const PARSING: Parsing = Parsing::Const;
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Const, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
let out = Ok(Const {
|
|
|
|
name: self.identifier()?,
|
|
|
|
ty: {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Colon, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
self.ty()?.into()
|
|
|
|
},
|
|
|
|
init: {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Eq, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
self.expr()?.into()
|
|
|
|
},
|
|
|
|
});
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Semi, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
out
|
|
|
|
}
|
|
|
|
pub fn parse_static(&mut self) -> PResult<Static> {
|
|
|
|
const PARSING: Parsing = Parsing::Static;
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Static, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
let out = Ok(Static {
|
|
|
|
mutable: self.mutability()?,
|
|
|
|
name: self.identifier()?,
|
|
|
|
ty: {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Colon, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
self.ty()?.into()
|
|
|
|
},
|
|
|
|
init: {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Eq, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
self.expr()?.into()
|
|
|
|
},
|
|
|
|
});
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Semi, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
out
|
|
|
|
}
|
|
|
|
pub fn parse_module(&mut self) -> PResult<Module> {
|
|
|
|
const PARSING: Parsing = Parsing::Module;
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Mod, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
Ok(Module { name: self.identifier()?, kind: self.modulekind()? })
|
|
|
|
}
|
|
|
|
pub fn modulekind(&mut self) -> PResult<ModuleKind> {
|
|
|
|
const PARSING: Parsing = Parsing::ModuleKind;
|
|
|
|
let inline = delim(Self::file, CURLIES, PARSING);
|
|
|
|
|
2024-04-12 19:36:26 +00:00
|
|
|
match self.peek_kind(PARSING)? {
|
|
|
|
TokenKind::LCurly => Ok(ModuleKind::Inline(inline(self)?)),
|
|
|
|
TokenKind::Semi => {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
Ok(ModuleKind::Outline)
|
|
|
|
}
|
2024-04-12 19:36:26 +00:00
|
|
|
got => Err(self.error(Expected { want: TokenKind::Semi, got }, PARSING)),
|
2024-03-01 02:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn parse_function(&mut self) -> PResult<Function> {
|
|
|
|
const PARSING: Parsing = Parsing::Function;
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Fn, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
Ok(Function {
|
|
|
|
name: self.identifier()?,
|
|
|
|
args: self.parse_params()?,
|
2024-04-12 19:36:26 +00:00
|
|
|
rety: match self.peek_kind(PARSING)? {
|
|
|
|
TokenKind::LCurly | TokenKind::Semi => None,
|
|
|
|
TokenKind::Arrow => {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
Some(self.ty()?.into())
|
|
|
|
}
|
2024-04-12 19:36:26 +00:00
|
|
|
got => Err(self.error(Expected { want: TokenKind::Arrow, got }, PARSING))?,
|
2024-03-01 02:44:49 +00:00
|
|
|
},
|
2024-04-12 19:36:26 +00:00
|
|
|
body: match self.peek_kind(PARSING)? {
|
|
|
|
TokenKind::LCurly => Some(self.block()?),
|
|
|
|
TokenKind::Semi => {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
None
|
|
|
|
}
|
|
|
|
t => Err(self.error(Unexpected(t), PARSING))?,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pub fn parse_params(&mut self) -> PResult<Vec<Param>> {
|
|
|
|
const PARSING: Parsing = Parsing::Function;
|
|
|
|
delim(
|
2024-04-12 19:36:26 +00:00
|
|
|
sep(Self::parse_param, TokenKind::Comma, PARENS.1, PARSING),
|
2024-03-01 02:44:49 +00:00
|
|
|
PARENS,
|
|
|
|
PARSING,
|
|
|
|
)(self)
|
|
|
|
}
|
|
|
|
pub fn parse_param(&mut self) -> PResult<Param> {
|
|
|
|
Ok(Param {
|
|
|
|
mutability: self.mutability()?,
|
|
|
|
name: self.identifier()?,
|
|
|
|
ty: {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Colon, Parsing::Param)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
self.ty()?.into()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pub fn parse_struct(&mut self) -> PResult<Struct> {
|
|
|
|
const PARSING: Parsing = Parsing::Struct;
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Struct, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
Ok(Struct {
|
|
|
|
name: self.identifier()?,
|
2024-04-12 19:36:26 +00:00
|
|
|
kind: match self.peek_kind(PARSING)? {
|
|
|
|
TokenKind::LParen => self.structkind_tuple()?,
|
|
|
|
TokenKind::LCurly => self.structkind_struct()?,
|
|
|
|
TokenKind::Semi => {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
StructKind::Empty
|
|
|
|
}
|
2024-04-12 19:36:26 +00:00
|
|
|
got => Err(self.error(Expected { want: TokenKind::Semi, got }, PARSING))?,
|
2024-03-01 02:44:49 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pub fn structkind_tuple(&mut self) -> PResult<StructKind> {
|
|
|
|
const PARSING: Parsing = Parsing::StructKind;
|
|
|
|
|
|
|
|
Ok(StructKind::Tuple(delim(
|
2024-04-12 19:36:26 +00:00
|
|
|
sep(Self::ty, TokenKind::Comma, PARENS.1, PARSING),
|
2024-03-01 02:44:49 +00:00
|
|
|
PARENS,
|
|
|
|
PARSING,
|
|
|
|
)(self)?))
|
|
|
|
}
|
|
|
|
pub fn structkind_struct(&mut self) -> PResult<StructKind> {
|
|
|
|
const PARSING: Parsing = Parsing::StructKind;
|
|
|
|
Ok(StructKind::Struct(delim(
|
2024-04-12 19:36:26 +00:00
|
|
|
sep(Self::struct_member, TokenKind::Comma, CURLIES.1, PARSING),
|
2024-03-01 02:44:49 +00:00
|
|
|
CURLIES,
|
|
|
|
PARSING,
|
|
|
|
)(self)?))
|
|
|
|
}
|
|
|
|
pub fn struct_member(&mut self) -> PResult<StructMember> {
|
|
|
|
const PARSING: Parsing = Parsing::StructMember;
|
|
|
|
Ok(StructMember {
|
|
|
|
vis: self.visibility()?,
|
|
|
|
name: self.identifier()?,
|
|
|
|
ty: {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Colon, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
self.ty()?
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pub fn parse_enum(&mut self) -> PResult<Enum> {
|
2024-04-01 09:28:30 +00:00
|
|
|
// Enum = "enum" Identifier '{' (Variant ',')* Variant? '}' ;
|
2024-03-01 02:44:49 +00:00
|
|
|
const PARSING: Parsing = Parsing::Enum;
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Enum, PARSING)?;
|
2024-04-01 09:28:30 +00:00
|
|
|
Ok(Enum {
|
|
|
|
name: self.identifier()?,
|
2024-04-12 19:36:26 +00:00
|
|
|
kind: match self.peek_kind(PARSING)? {
|
|
|
|
TokenKind::LCurly => EnumKind::Variants(delim(
|
|
|
|
sep(
|
|
|
|
Self::enum_variant,
|
|
|
|
TokenKind::Comma,
|
|
|
|
TokenKind::RCurly,
|
|
|
|
PARSING,
|
|
|
|
),
|
2024-04-01 09:28:30 +00:00
|
|
|
CURLIES,
|
|
|
|
PARSING,
|
|
|
|
)(self)?),
|
2024-04-12 19:36:26 +00:00
|
|
|
TokenKind::Semi => {
|
2024-04-01 09:28:30 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
EnumKind::NoVariants
|
|
|
|
}
|
|
|
|
t => Err(self.error(Unexpected(t), PARSING))?,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enum_variant(&mut self) -> PResult<Variant> {
|
|
|
|
const PARSING: Parsing = Parsing::Variant;
|
|
|
|
Ok(Variant {
|
|
|
|
name: self.identifier()?,
|
2024-04-12 19:36:26 +00:00
|
|
|
kind: match self.peek_kind(PARSING)? {
|
|
|
|
TokenKind::Eq => self.variantkind_clike()?,
|
|
|
|
TokenKind::LCurly => self.variantkind_struct()?,
|
|
|
|
TokenKind::LParen => self.variantkind_tuple()?,
|
2024-04-01 09:28:30 +00:00
|
|
|
_ => VariantKind::Plain,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pub fn variantkind_clike(&mut self) -> PResult<VariantKind> {
|
|
|
|
const PARSING: Parsing = Parsing::VariantKind;
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Eq, PARSING)?;
|
|
|
|
let tok = self.match_type(TokenKind::Integer, PARSING)?;
|
2024-04-01 09:28:30 +00:00
|
|
|
Ok(VariantKind::CLike(match tok.data() {
|
2024-04-12 19:36:26 +00:00
|
|
|
TokenData::Integer(i) => *i,
|
2024-04-01 09:28:30 +00:00
|
|
|
_ => panic!("Expected token data for {tok:?} while parsing {PARSING}"),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
pub fn variantkind_struct(&mut self) -> PResult<VariantKind> {
|
|
|
|
const PARSING: Parsing = Parsing::VariantKind;
|
|
|
|
Ok(VariantKind::Struct(delim(
|
2024-04-12 19:36:26 +00:00
|
|
|
sep(
|
|
|
|
Self::struct_member,
|
|
|
|
TokenKind::Comma,
|
|
|
|
TokenKind::RCurly,
|
|
|
|
PARSING,
|
|
|
|
),
|
2024-04-01 09:28:30 +00:00
|
|
|
CURLIES,
|
|
|
|
PARSING,
|
|
|
|
)(self)?))
|
|
|
|
}
|
|
|
|
pub fn variantkind_tuple(&mut self) -> PResult<VariantKind> {
|
|
|
|
const PARSING: Parsing = Parsing::VariantKind;
|
|
|
|
Ok(VariantKind::Tuple(delim(
|
2024-04-12 19:36:26 +00:00
|
|
|
sep(Self::ty, TokenKind::Comma, TokenKind::RParen, PARSING),
|
2024-04-01 09:28:30 +00:00
|
|
|
PARENS,
|
|
|
|
PARSING,
|
|
|
|
)(self)?))
|
2024-03-01 02:44:49 +00:00
|
|
|
}
|
2024-04-01 09:28:30 +00:00
|
|
|
|
2024-03-01 02:44:49 +00:00
|
|
|
pub fn parse_impl(&mut self) -> PResult<Impl> {
|
|
|
|
const PARSING: Parsing = Parsing::Impl;
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Impl, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
Err(self.error(Todo, PARSING))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn visibility(&mut self) -> PResult<Visibility> {
|
2024-04-12 19:36:26 +00:00
|
|
|
if let TokenKind::Pub = self.peek_kind(Parsing::Visibility)? {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
return Ok(Visibility::Public);
|
|
|
|
};
|
|
|
|
Ok(Visibility::Private)
|
|
|
|
}
|
|
|
|
pub fn mutability(&mut self) -> PResult<Mutability> {
|
2024-04-12 19:36:26 +00:00
|
|
|
if let TokenKind::Mut = self.peek_kind(Parsing::Mutability)? {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
return Ok(Mutability::Mut);
|
|
|
|
};
|
|
|
|
Ok(Mutability::Not)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// # Type parsing
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
/// Parses a [TyKind]
|
|
|
|
///
|
|
|
|
/// See also: [Parser::ty]
|
|
|
|
pub fn tykind(&mut self) -> PResult<TyKind> {
|
|
|
|
const PARSING: Parsing = Parsing::TyKind;
|
2024-04-12 19:36:26 +00:00
|
|
|
let out = match self.peek_kind(PARSING)? {
|
|
|
|
TokenKind::Bang => {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
TyKind::Never
|
|
|
|
}
|
2024-04-12 19:36:26 +00:00
|
|
|
TokenKind::SelfTy => {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
TyKind::SelfTy
|
|
|
|
}
|
2024-04-12 19:36:26 +00:00
|
|
|
TokenKind::Amp | TokenKind::AmpAmp => self.tyref()?.into(),
|
|
|
|
TokenKind::LParen => self.tytuple()?.into(),
|
|
|
|
TokenKind::Fn => self.tyfn()?.into(),
|
2024-03-01 02:44:49 +00:00
|
|
|
path_like!() => self.path()?.into(),
|
|
|
|
t => Err(self.error(Unexpected(t), PARSING))?,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
/// [TyTuple] = `(` ([Ty] `,`)* [Ty]? `)`
|
|
|
|
pub fn tytuple(&mut self) -> PResult<TyTuple> {
|
|
|
|
const PARSING: Parsing = Parsing::TyTuple;
|
|
|
|
Ok(TyTuple {
|
|
|
|
types: delim(
|
2024-04-12 19:36:26 +00:00
|
|
|
sep(Self::ty, TokenKind::Comma, PARENS.1, PARSING),
|
2024-03-01 02:44:49 +00:00
|
|
|
PARENS,
|
|
|
|
PARSING,
|
|
|
|
)(self)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/// [TyRef] = (`&`|`&&`)* [Path]
|
|
|
|
pub fn tyref(&mut self) -> PResult<TyRef> {
|
|
|
|
const PARSING: Parsing = Parsing::TyRef;
|
|
|
|
let mut count = 0;
|
|
|
|
loop {
|
2024-04-12 19:36:26 +00:00
|
|
|
match self.peek_kind(PARSING)? {
|
|
|
|
TokenKind::Amp => count += 1,
|
|
|
|
TokenKind::AmpAmp => count += 2,
|
2024-03-01 02:44:49 +00:00
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
self.consume_peeked();
|
|
|
|
}
|
|
|
|
Ok(TyRef { count, to: self.path()? })
|
|
|
|
}
|
|
|
|
/// [TyFn] = `fn` [TyTuple] (-> [Ty])?
|
|
|
|
pub fn tyfn(&mut self) -> PResult<TyFn> {
|
|
|
|
const PARSING: Parsing = Parsing::TyFn;
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Fn, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
Ok(TyFn {
|
|
|
|
args: self.tytuple()?,
|
|
|
|
rety: {
|
2024-04-12 19:36:26 +00:00
|
|
|
match self.peek_kind(PARSING)? {
|
|
|
|
TokenKind::Arrow => {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
Some(self.ty()?.into())
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 19:36:26 +00:00
|
|
|
/// Expands to a pattern which matches literal-like [TokenKind]s
|
2024-03-01 02:44:49 +00:00
|
|
|
macro literal_like() {
|
2024-04-12 19:36:26 +00:00
|
|
|
TokenKind::True | TokenKind::False | TokenKind::String | TokenKind::Character | TokenKind::Integer | TokenKind::Float
|
2024-03-01 02:44:49 +00:00
|
|
|
}
|
|
|
|
/// Expands to a pattern which matches path-like [token Types](Type)
|
|
|
|
macro path_like() {
|
2024-04-12 19:36:26 +00:00
|
|
|
TokenKind::Super | TokenKind::SelfKw | TokenKind::Identifier | TokenKind::ColonColon
|
2024-03-01 02:44:49 +00:00
|
|
|
}
|
|
|
|
/// # Path parsing
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
/// [PathPart] = `super` | `self` | [Identifier]
|
|
|
|
pub fn path_part(&mut self) -> PResult<PathPart> {
|
|
|
|
const PARSING: Parsing = Parsing::PathPart;
|
2024-04-12 19:36:26 +00:00
|
|
|
let out = match self.peek_kind(PARSING)? {
|
|
|
|
TokenKind::Super => PathPart::SuperKw,
|
|
|
|
TokenKind::SelfKw => PathPart::SelfKw,
|
|
|
|
TokenKind::Identifier => PathPart::Ident(self.identifier()?),
|
2024-03-01 02:44:49 +00:00
|
|
|
t => return Err(self.error(Unexpected(t), PARSING)),
|
|
|
|
};
|
|
|
|
self.consume_peeked();
|
|
|
|
Ok(out)
|
|
|
|
}
|
2024-04-12 19:36:26 +00:00
|
|
|
/// [Identifier] = [`Identifier`](TokenKind::Identifier)
|
2024-03-01 02:44:49 +00:00
|
|
|
pub fn identifier(&mut self) -> PResult<Identifier> {
|
2024-04-12 19:36:26 +00:00
|
|
|
let tok = self.match_type(TokenKind::Identifier, Parsing::Identifier)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
match tok.data() {
|
2024-04-12 19:36:26 +00:00
|
|
|
TokenData::Identifier(ident) => Ok(ident.into()),
|
2024-03-01 02:44:49 +00:00
|
|
|
_ => panic!("Expected token data for {tok:?}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// # Statement parsing
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
/// Parses a [StmtKind]
|
|
|
|
///
|
|
|
|
/// See also: [Parser::stmt]
|
|
|
|
pub fn stmtkind(&mut self) -> PResult<StmtKind> {
|
2024-04-12 19:36:26 +00:00
|
|
|
Ok(match self.peek_kind(Parsing::StmtKind)? {
|
|
|
|
TokenKind::Semi => StmtKind::Empty,
|
|
|
|
TokenKind::Let => self.parse_let()?.into(),
|
2024-03-01 02:44:49 +00:00
|
|
|
item_like!() => self.item()?.into(),
|
|
|
|
_ => self.expr()?.into(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_let(&mut self) -> PResult<Let> {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Let, Parsing::Let)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
Ok(Let {
|
|
|
|
mutable: self.mutability()?,
|
|
|
|
name: self.identifier()?,
|
2024-04-12 19:36:26 +00:00
|
|
|
ty: if Ok(TokenKind::Colon) == self.peek_kind(Parsing::Let) {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
Some(self.ty()?.into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
2024-04-12 19:36:26 +00:00
|
|
|
init: if Ok(TokenKind::Eq) == self.peek_kind(Parsing::Let) {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
Some(self.expr()?.into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro binary($($name:ident {$lower:ident, $op:ident})*) {
|
|
|
|
$(pub fn $name(&mut self) -> PResult<ExprKind> {
|
|
|
|
let head = self.expr_from(Self::$lower)?;
|
|
|
|
let mut tail = vec![];
|
|
|
|
loop {
|
|
|
|
match self.$op() {
|
|
|
|
Ok(op) => tail.push((op, self.expr_from(Self::$lower)?)),
|
|
|
|
Err(Error { reason: Unexpected(_) | EndOfInput, ..}) => break,
|
|
|
|
Err(e) => Err(e)?,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tail.is_empty() {
|
|
|
|
return Ok(head.kind);
|
|
|
|
}
|
|
|
|
Ok(Binary { head: head.into(), tail }.into())
|
|
|
|
})*
|
|
|
|
}
|
|
|
|
/// # Expression parsing
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
/// Parses an [ExprKind]
|
|
|
|
///
|
|
|
|
/// See also: [Parser::expr], [Parser::exprkind_primary]
|
|
|
|
pub fn exprkind(&mut self) -> PResult<ExprKind> {
|
|
|
|
self.exprkind_assign()
|
|
|
|
}
|
|
|
|
/// 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()) })
|
|
|
|
}
|
|
|
|
pub fn optional_expr(&mut self) -> PResult<Option<Expr>> {
|
|
|
|
match self.expr() {
|
|
|
|
Ok(v) => Ok(Some(v)),
|
|
|
|
Err(Error { reason: Nothing, .. }) => Ok(None),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// [Assign] = [Path] ([AssignKind] [Assign]) | [Compare](Binary)
|
|
|
|
pub fn exprkind_assign(&mut self) -> PResult<ExprKind> {
|
|
|
|
let head = self.expr_from(Self::exprkind_compare)?;
|
|
|
|
// TODO: Formalize the concept of a "place expression"
|
|
|
|
if !matches!(
|
|
|
|
head.kind,
|
|
|
|
ExprKind::Path(_) | ExprKind::Call(_) | ExprKind::Member(_) | ExprKind::Index(_)
|
|
|
|
) {
|
|
|
|
return Ok(head.kind);
|
|
|
|
}
|
|
|
|
let Ok(op) = self.assign_op() else {
|
|
|
|
return Ok(head.kind);
|
|
|
|
};
|
2024-03-28 21:34:24 +00:00
|
|
|
Ok(
|
|
|
|
Assign {
|
|
|
|
head: Box::new(head),
|
|
|
|
op,
|
|
|
|
tail: self.expr_from(Self::exprkind_assign)?.into(),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
)
|
2024-03-01 02:44:49 +00:00
|
|
|
}
|
|
|
|
// TODO: use a pratt parser for binary expressions, to simplify this
|
|
|
|
binary! {
|
|
|
|
exprkind_compare {exprkind_range, compare_op}
|
|
|
|
exprkind_range {exprkind_logic, range_op}
|
|
|
|
exprkind_logic {exprkind_bitwise, logic_op}
|
|
|
|
exprkind_bitwise {exprkind_shift, bitwise_op}
|
|
|
|
exprkind_shift {exprkind_factor, shift_op}
|
|
|
|
exprkind_factor {exprkind_term, factor_op}
|
|
|
|
exprkind_term {exprkind_unary, term_op}
|
|
|
|
}
|
|
|
|
/// [Unary] = [UnaryKind]* [Member]
|
|
|
|
pub fn exprkind_unary(&mut self) -> PResult<ExprKind> {
|
|
|
|
let mut ops = vec![];
|
|
|
|
loop {
|
|
|
|
match self.unary_op() {
|
|
|
|
Ok(v) => ops.push(v),
|
|
|
|
Err(Error { reason: Unexpected(_), .. }) => break,
|
|
|
|
Err(e) => Err(e)?,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let tail = self.expr_from(Self::exprkind_member)?;
|
|
|
|
if ops.is_empty() {
|
|
|
|
return Ok(tail.kind);
|
|
|
|
}
|
|
|
|
Ok(Unary { ops, tail: Box::new(tail) }.into())
|
|
|
|
}
|
|
|
|
/// [Member] = [Call] `.` [Call]
|
|
|
|
pub fn exprkind_member(&mut self) -> PResult<ExprKind> {
|
|
|
|
let head = self.expr_from(Self::exprkind_call)?;
|
|
|
|
let mut tail = vec![];
|
|
|
|
while self.member_op().is_ok() {
|
|
|
|
tail.push(self.expr_from(Self::exprkind_call)?)
|
|
|
|
}
|
|
|
|
if tail.is_empty() {
|
|
|
|
Ok(head.kind)
|
|
|
|
} else {
|
|
|
|
Ok(Member { head: head.into(), tail }.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Call = [Index] (`(` [Tuple]? `)`)*
|
|
|
|
pub fn exprkind_call(&mut self) -> PResult<ExprKind> {
|
|
|
|
const PARSING: Parsing = Parsing::Call;
|
|
|
|
let callee = self.expr_from(Self::exprkind_index)?;
|
|
|
|
let mut args = vec![];
|
2024-04-12 19:36:26 +00:00
|
|
|
while Ok(TokenKind::LParen) == self.peek_kind(PARSING) {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
args.push(self.tuple()?);
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::RParen, PARSING)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
}
|
|
|
|
if args.is_empty() {
|
|
|
|
Ok(callee.kind)
|
|
|
|
} else {
|
|
|
|
Ok(Call { callee: callee.into(), args }.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// [Index] = [Primary](Parser::exprkind_primary) (`[` [Indices] `]`)*
|
|
|
|
pub fn exprkind_index(&mut self) -> PResult<ExprKind> {
|
|
|
|
const PARSING: Parsing = Parsing::Index;
|
|
|
|
let head = self.expr_from(Self::exprkind_primary)?;
|
2024-04-12 19:36:26 +00:00
|
|
|
if Ok(TokenKind::LBrack) != self.peek_kind(PARSING) {
|
2024-03-01 02:44:49 +00:00
|
|
|
return Ok(head.kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut indices = vec![];
|
2024-04-12 19:36:26 +00:00
|
|
|
while Ok(TokenKind::LBrack) == self.peek_kind(PARSING) {
|
2024-03-01 02:44:49 +00:00
|
|
|
indices.push(delim(Self::tuple, BRACKETS, PARSING)(self)?.into());
|
|
|
|
}
|
|
|
|
Ok(Index { head: head.into(), indices }.into())
|
|
|
|
}
|
|
|
|
/// Delegates to the set of highest-priority rules based on unambiguous pattern matching
|
|
|
|
pub fn exprkind_primary(&mut self) -> PResult<ExprKind> {
|
2024-04-12 19:36:26 +00:00
|
|
|
match self.peek_kind(Parsing::Expr)? {
|
|
|
|
TokenKind::Amp | TokenKind::AmpAmp => self.exprkind_addrof(),
|
|
|
|
TokenKind::LCurly => self.exprkind_block(),
|
|
|
|
TokenKind::LBrack => self.exprkind_array(),
|
|
|
|
TokenKind::LParen => self.exprkind_empty_group_or_tuple(),
|
2024-03-01 02:44:49 +00:00
|
|
|
literal_like!() => Ok(self.literal()?.into()),
|
|
|
|
path_like!() => Ok(self.path()?.into()),
|
2024-04-12 19:36:26 +00:00
|
|
|
TokenKind::If => Ok(self.parse_if()?.into()),
|
|
|
|
TokenKind::For => Ok(self.parse_for()?.into()),
|
|
|
|
TokenKind::While => Ok(self.parse_while()?.into()),
|
|
|
|
TokenKind::Break => Ok(self.parse_break()?.into()),
|
|
|
|
TokenKind::Return => Ok(self.parse_return()?.into()),
|
|
|
|
TokenKind::Continue => Ok(self.parse_continue()?.into()),
|
2024-03-01 02:44:49 +00:00
|
|
|
_ => Err(self.error(Nothing, Parsing::Expr)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// [Array] = '[' ([Expr] ',')* [Expr]? ']'
|
|
|
|
///
|
|
|
|
/// Array and ArrayRef are ambiguous until the second token,
|
|
|
|
/// so they can't be independent subexpressions
|
|
|
|
pub fn exprkind_array(&mut self) -> PResult<ExprKind> {
|
|
|
|
const PARSING: Parsing = Parsing::Array;
|
2024-04-12 19:36:26 +00:00
|
|
|
const START: TokenKind = TokenKind::LBrack;
|
|
|
|
const END: TokenKind = TokenKind::RBrack;
|
2024-03-01 02:44:49 +00:00
|
|
|
self.match_type(START, PARSING)?;
|
2024-04-12 19:36:26 +00:00
|
|
|
match self.peek_kind(PARSING)? {
|
2024-03-01 02:44:49 +00:00
|
|
|
END => {
|
|
|
|
self.consume_peeked();
|
|
|
|
Ok(Array { values: vec![] }.into())
|
|
|
|
}
|
|
|
|
_ => self.exprkind_array_rep(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// [ArrayRep] = `[` [Expr] `;` [Expr] `]`
|
|
|
|
pub fn exprkind_array_rep(&mut self) -> PResult<ExprKind> {
|
|
|
|
const PARSING: Parsing = Parsing::Array;
|
2024-04-12 19:36:26 +00:00
|
|
|
const END: TokenKind = TokenKind::RBrack;
|
2024-03-01 02:44:49 +00:00
|
|
|
let first = self.expr()?;
|
2024-04-12 19:36:26 +00:00
|
|
|
let out: ExprKind = match self.peek_kind(PARSING)? {
|
|
|
|
TokenKind::Semi => ArrayRep {
|
2024-03-01 02:44:49 +00:00
|
|
|
value: first.into(),
|
|
|
|
repeat: {
|
|
|
|
self.consume_peeked();
|
|
|
|
Box::new(self.expr()?)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
.into(),
|
2024-04-12 19:36:26 +00:00
|
|
|
TokenKind::RBrack => Array { values: vec![first] }.into(),
|
|
|
|
TokenKind::Comma => Array {
|
2024-03-01 02:44:49 +00:00
|
|
|
values: {
|
|
|
|
self.consume_peeked();
|
|
|
|
let mut out = vec![first];
|
2024-04-12 19:36:26 +00:00
|
|
|
out.extend(sep(Self::expr, TokenKind::Comma, END, PARSING)(self)?);
|
2024-03-01 02:44:49 +00:00
|
|
|
out
|
|
|
|
},
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
ty => Err(self.error(Unexpected(ty), PARSING))?,
|
|
|
|
};
|
|
|
|
self.match_type(END, PARSING)?;
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// [AddrOf] = (`&`|`&&`)* [Expr]
|
|
|
|
pub fn exprkind_addrof(&mut self) -> PResult<ExprKind> {
|
|
|
|
const PARSING: Parsing = Parsing::AddrOf;
|
|
|
|
let mut count = 0;
|
|
|
|
loop {
|
2024-04-12 19:36:26 +00:00
|
|
|
match self.peek_kind(PARSING)? {
|
|
|
|
TokenKind::Amp => count += 1,
|
|
|
|
TokenKind::AmpAmp => count += 2,
|
2024-03-01 02:44:49 +00:00
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
self.consume_peeked();
|
|
|
|
}
|
|
|
|
Ok(AddrOf { count, mutable: self.mutability()?, expr: self.expr()?.into() }.into())
|
|
|
|
}
|
|
|
|
/// [Block] = `{` [Stmt]* `}`
|
|
|
|
pub fn exprkind_block(&mut self) -> PResult<ExprKind> {
|
|
|
|
self.block().map(Into::into)
|
|
|
|
}
|
|
|
|
/// [Group] = `(`([Empty](ExprKind::Empty)|[Expr]|[Tuple])`)`
|
|
|
|
///
|
|
|
|
/// [ExprKind::Empty] and [Group] are special cases of [Tuple]
|
|
|
|
pub fn exprkind_empty_group_or_tuple(&mut self) -> PResult<ExprKind> {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::LParen, Parsing::Group)?;
|
|
|
|
let out = match self.peek_kind(Parsing::Group)? {
|
|
|
|
TokenKind::RParen => Ok(ExprKind::Empty),
|
2024-03-01 02:44:49 +00:00
|
|
|
_ => self.exprkind_group(),
|
|
|
|
};
|
2024-04-12 19:36:26 +00:00
|
|
|
match self.peek_kind(Parsing::Group) {
|
|
|
|
Ok(TokenKind::RParen) => self.consume_peeked(),
|
2024-03-01 02:44:49 +00:00
|
|
|
_ => Err(self.error(UnmatchedParentheses, Parsing::Group))?,
|
|
|
|
};
|
|
|
|
out
|
|
|
|
}
|
|
|
|
/// [Group] = `(`([Empty](ExprKind::Empty)|[Expr]|[Tuple])`)`
|
|
|
|
pub fn exprkind_group(&mut self) -> PResult<ExprKind> {
|
|
|
|
let first = self.expr()?;
|
2024-04-12 19:36:26 +00:00
|
|
|
match self.peek_kind(Parsing::Group)? {
|
|
|
|
TokenKind::Comma => {
|
2024-03-01 02:44:49 +00:00
|
|
|
let mut exprs = vec![first];
|
|
|
|
self.consume_peeked();
|
2024-04-12 19:36:26 +00:00
|
|
|
while TokenKind::RParen != self.peek_kind(Parsing::Tuple)? {
|
2024-03-01 02:44:49 +00:00
|
|
|
exprs.push(self.expr()?);
|
2024-04-12 19:36:26 +00:00
|
|
|
match self.peek_kind(Parsing::Tuple)? {
|
|
|
|
TokenKind::Comma => self.consume_peeked(),
|
2024-03-01 02:44:49 +00:00
|
|
|
_ => break,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(Tuple { exprs }.into())
|
|
|
|
}
|
|
|
|
_ => Ok(Group { expr: first.into() }.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ## Subexpressions
|
|
|
|
impl<'t> Parser<'t> {
|
2024-04-12 19:36:26 +00:00
|
|
|
/// [Literal] = [String](TokenKind::String) | [Character](TokenKind::Character)
|
|
|
|
/// | [Float](TokenKind::Float) (TODO) | [Integer](TokenKind::Integer) | `true` | `false`
|
2024-03-01 02:44:49 +00:00
|
|
|
pub fn literal(&mut self) -> PResult<Literal> {
|
|
|
|
let tok = self.consume(Parsing::Literal)?;
|
|
|
|
// keyword literals true and false
|
|
|
|
match tok.ty() {
|
2024-04-12 19:36:26 +00:00
|
|
|
TokenKind::True => return Ok(Literal::Bool(true)),
|
|
|
|
TokenKind::False => return Ok(Literal::Bool(false)),
|
|
|
|
TokenKind::String | TokenKind::Character | TokenKind::Integer | TokenKind::Float => (),
|
2024-03-01 02:44:49 +00:00
|
|
|
t => return Err(self.error(Unexpected(t), Parsing::Literal)),
|
|
|
|
}
|
|
|
|
Ok(match tok.data() {
|
2024-04-12 19:36:26 +00:00
|
|
|
TokenData::String(v) => Literal::from(v.as_str()),
|
|
|
|
TokenData::Character(v) => Literal::from(*v),
|
|
|
|
TokenData::Integer(v) => Literal::from(*v),
|
|
|
|
TokenData::Float(v) => todo!("Literal::Float({v})"),
|
2024-03-01 02:44:49 +00:00
|
|
|
_ => panic!("Expected token data for {tok:?}"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/// [Tuple] = ([Expr] `,`)* [Expr]?
|
|
|
|
pub fn tuple(&mut self) -> PResult<Tuple> {
|
|
|
|
let mut exprs = vec![];
|
|
|
|
while let Some(expr) = match self.expr() {
|
|
|
|
Ok(v) => Some(v),
|
|
|
|
Err(Error { reason: Nothing, .. }) => None,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
} {
|
|
|
|
exprs.push(expr);
|
2024-04-12 19:36:26 +00:00
|
|
|
match self.peek_kind(Parsing::Tuple)? {
|
|
|
|
TokenKind::Comma => self.consume_peeked(),
|
2024-03-01 02:44:49 +00:00
|
|
|
_ => break,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(Tuple { exprs })
|
|
|
|
}
|
|
|
|
/// [Block] = `{` [Stmt]* `}`
|
|
|
|
pub fn block(&mut self) -> PResult<Block> {
|
|
|
|
const PARSING: Parsing = Parsing::Block;
|
2024-03-01 02:58:50 +00:00
|
|
|
Ok(Block { stmts: delim(rep(Self::stmt, CURLIES.1, PARSING), CURLIES, PARSING)(self)? })
|
2024-03-01 02:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/// ## Control flow subexpressions
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
/// [Break] = `break` [Expr]?
|
|
|
|
pub fn parse_break(&mut self) -> PResult<Break> {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Break, Parsing::Break)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
Ok(Break { body: self.optional_expr()?.map(Into::into) })
|
|
|
|
}
|
|
|
|
/// [Return] = `return` [Expr]?
|
|
|
|
pub fn parse_return(&mut self) -> PResult<Return> {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Return, Parsing::Return)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
Ok(Return { body: self.optional_expr()?.map(Into::into) })
|
|
|
|
}
|
|
|
|
/// [Continue] = `continue`
|
|
|
|
pub fn parse_continue(&mut self) -> PResult<Continue> {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::Continue, Parsing::Continue)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
Ok(Continue)
|
|
|
|
}
|
|
|
|
/// [While] = `while` [Expr] [Block] [Else]?
|
|
|
|
pub fn parse_while(&mut self) -> PResult<While> {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::While, Parsing::While)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
Ok(While {
|
|
|
|
cond: self.expr()?.into(),
|
|
|
|
pass: self.block()?.into(),
|
|
|
|
fail: self.parse_else()?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/// [If] = <code>`if` [Expr] [Block] [Else]?</code>
|
|
|
|
#[rustfmt::skip] // second line is barely not long enough
|
|
|
|
pub fn parse_if(&mut self) -> PResult<If> {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::If, Parsing::If)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
Ok(If {
|
|
|
|
cond: self.expr()?.into(),
|
|
|
|
pass: self.block()?.into(),
|
|
|
|
fail: self.parse_else()?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/// [For]: `for` Pattern (TODO) `in` [Expr] [Block] [Else]?
|
|
|
|
pub fn parse_for(&mut self) -> PResult<For> {
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::For, Parsing::For)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
let bind = self.identifier()?;
|
2024-04-12 19:36:26 +00:00
|
|
|
self.match_type(TokenKind::In, Parsing::For)?;
|
2024-03-01 02:44:49 +00:00
|
|
|
Ok(For {
|
|
|
|
bind,
|
|
|
|
cond: self.expr()?.into(),
|
|
|
|
pass: self.block()?.into(),
|
|
|
|
fail: self.parse_else()?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/// [Else]: (`else` [Block])?
|
|
|
|
pub fn parse_else(&mut self) -> PResult<Else> {
|
2024-04-12 19:36:26 +00:00
|
|
|
match self.peek_kind(Parsing::Else) {
|
|
|
|
Ok(TokenKind::Else) => {
|
2024-03-01 02:44:49 +00:00
|
|
|
self.consume_peeked();
|
|
|
|
Ok(self.expr()?.into())
|
|
|
|
}
|
|
|
|
Ok(_) | Err(Error { reason: EndOfInput, .. }) => Ok(None.into()),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro operator($($name:ident ($returns:ident) {$($t:ident => $p:ident),*$(,)?};)*) {$(
|
|
|
|
pub fn $name (&mut self) -> PResult<$returns> {
|
|
|
|
const PARSING: Parsing = Parsing::$returns;
|
2024-04-12 19:36:26 +00:00
|
|
|
let out = Ok(match self.peek_kind(PARSING) {
|
|
|
|
$(Ok(TokenKind::$t) => $returns::$p,)*
|
2024-03-01 02:44:49 +00:00
|
|
|
Err(e) => Err(e)?,
|
|
|
|
Ok(t) => Err(self.error(Unexpected(t), PARSING))?,
|
|
|
|
});
|
|
|
|
self.consume_peeked();
|
|
|
|
out
|
|
|
|
}
|
|
|
|
)*}
|
|
|
|
|
|
|
|
/// ## Operator Kinds
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
operator! {
|
|
|
|
assign_op (AssignKind) {
|
|
|
|
Eq => Plain, // =
|
|
|
|
AmpEq => And, // &=
|
|
|
|
BarEq => Or, // |=
|
|
|
|
XorEq => Xor, // ^=
|
|
|
|
LtLtEq => Shl, // <<=
|
|
|
|
GtGtEq => Shr, // >>=
|
|
|
|
PlusEq => Add, // +=
|
|
|
|
MinusEq => Sub, // -=
|
|
|
|
StarEq => Mul, // *=
|
|
|
|
SlashEq => Div, // /=
|
|
|
|
RemEq => Rem, // %=
|
|
|
|
};
|
|
|
|
compare_op (BinaryKind) {
|
|
|
|
Lt => Lt, // <
|
|
|
|
LtEq => LtEq, // <=
|
|
|
|
EqEq => Equal, // ==
|
|
|
|
BangEq => NotEq,// !=
|
|
|
|
GtEq => GtEq, // >=
|
|
|
|
Gt => Gt, // >
|
|
|
|
};
|
|
|
|
range_op (BinaryKind) {
|
|
|
|
DotDot => RangeExc, // ..
|
|
|
|
DotDotEq => RangeInc,// ..=
|
|
|
|
};
|
|
|
|
logic_op (BinaryKind) {
|
|
|
|
AmpAmp => LogAnd, // &&
|
|
|
|
BarBar => LogOr, // ||
|
|
|
|
XorXor => LogXor, // ^^
|
|
|
|
};
|
|
|
|
bitwise_op (BinaryKind) {
|
|
|
|
Amp => BitAnd, // &
|
|
|
|
Bar => BitOr, // |
|
|
|
|
Xor => BitXor, // ^
|
|
|
|
};
|
|
|
|
shift_op (BinaryKind) {
|
|
|
|
LtLt => Shl, // <<
|
|
|
|
GtGt => Shr, // >>
|
|
|
|
};
|
|
|
|
factor_op (BinaryKind) {
|
|
|
|
Plus => Add, // +
|
|
|
|
Minus => Sub, // -
|
|
|
|
};
|
|
|
|
term_op (BinaryKind) {
|
|
|
|
Star => Mul, // *
|
|
|
|
Slash => Div, // /
|
|
|
|
Rem => Rem, // %
|
|
|
|
};
|
|
|
|
unary_op (UnaryKind) {
|
|
|
|
Star => Deref, // *
|
|
|
|
Minus => Neg, // -
|
|
|
|
Bang => Not, // !
|
|
|
|
At => At, // @
|
|
|
|
Tilde => Tilde, // ~
|
|
|
|
};
|
|
|
|
}
|
|
|
|
pub fn member_op(&mut self) -> PResult<()> {
|
|
|
|
const PARSING: Parsing = Parsing::Member;
|
|
|
|
match self.peek(PARSING)?.ty() {
|
2024-04-12 19:36:26 +00:00
|
|
|
TokenKind::Dot => {}
|
2024-03-01 02:44:49 +00:00
|
|
|
t => Err(self.error(Unexpected(t), PARSING))?,
|
|
|
|
}
|
|
|
|
self.consume_peeked();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|