2023-10-17 18:36:21 +00:00
|
|
|
//! Parses [tokens](super::token) into an [AST](super::ast)
|
2023-10-19 19:40:03 +00:00
|
|
|
use std::vec;
|
|
|
|
|
2023-10-17 18:36:21 +00:00
|
|
|
use super::{
|
|
|
|
ast::preamble::*,
|
|
|
|
lexer::Lexer,
|
|
|
|
token::{Keyword, Token, Type},
|
|
|
|
};
|
2023-10-19 19:40:03 +00:00
|
|
|
use error::{Error, Reason::*, *};
|
2023-10-17 18:36:21 +00:00
|
|
|
|
|
|
|
mod error {
|
|
|
|
use super::{Token, Type};
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
|
|
|
pub enum Reason {
|
|
|
|
Expected(Type),
|
|
|
|
NotIdentifier,
|
|
|
|
NotLiteral,
|
|
|
|
NotString,
|
2023-10-19 19:40:03 +00:00
|
|
|
NotChar,
|
2023-10-17 18:36:21 +00:00
|
|
|
NotBool,
|
|
|
|
NotFloat,
|
|
|
|
FloatExponentOverflow,
|
|
|
|
FloatMantissaOverflow,
|
|
|
|
NotInt,
|
|
|
|
IntOverflow,
|
|
|
|
NotControlFlow,
|
|
|
|
NotBranch,
|
|
|
|
EndOfFile,
|
|
|
|
#[default]
|
|
|
|
Unspecified,
|
|
|
|
}
|
|
|
|
use Reason::*;
|
|
|
|
/// [Parser] [Result]
|
|
|
|
pub type PResult<T> = Result<T, Error>;
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
|
|
|
pub struct Error {
|
|
|
|
reason: Reason,
|
|
|
|
start: Option<Token>,
|
|
|
|
}
|
|
|
|
macro error_impl($($fn:ident$(($($p:ident: $t:ty),*))?: $reason:expr),*$(,)?) {$(
|
|
|
|
/// Creates an [Error] with this [Reason]:
|
|
|
|
#[doc = concat!("[`", stringify!($reason), "`]")]
|
2023-10-19 19:40:03 +00:00
|
|
|
pub fn $fn($($($p : $t),*)?) -> Self {
|
|
|
|
Self { reason: $reason$(($($p)*))?, start: None }
|
|
|
|
}
|
|
|
|
)*}
|
2023-10-17 18:36:21 +00:00
|
|
|
impl Error {
|
|
|
|
pub fn token(self, start: Token) -> Self {
|
|
|
|
Self { start: Some(start), ..self }
|
|
|
|
}
|
2023-10-19 19:40:03 +00:00
|
|
|
pub fn maybe_token(self, start: Option<Token>) -> Self {
|
|
|
|
Self { start, ..self }
|
|
|
|
}
|
2023-10-17 18:36:21 +00:00
|
|
|
pub fn start(&self) -> Option<Token> {
|
|
|
|
self.start
|
|
|
|
}
|
|
|
|
pub fn reason(self, reason: Reason) -> Self {
|
|
|
|
Self { reason, ..self }
|
|
|
|
}
|
|
|
|
error_impl! {
|
|
|
|
expected(e: Type): Expected,
|
|
|
|
not_identifier: NotIdentifier,
|
|
|
|
not_literal: NotLiteral,
|
|
|
|
not_string: NotString,
|
2023-10-19 19:40:03 +00:00
|
|
|
not_char: NotChar,
|
2023-10-17 18:36:21 +00:00
|
|
|
not_bool: NotBool,
|
|
|
|
not_float: NotFloat,
|
|
|
|
float_exponent_overflow: FloatExponentOverflow,
|
|
|
|
float_mantissa_overflow: FloatMantissaOverflow,
|
|
|
|
not_int: NotInt,
|
|
|
|
int_overflow: IntOverflow,
|
|
|
|
not_control_flow: NotControlFlow,
|
|
|
|
not_branch: NotBranch,
|
|
|
|
end_of_file: EndOfFile,
|
|
|
|
unspecified: Unspecified,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The Parser performs recursive descent on the AST's grammar
|
|
|
|
/// using a provided [Lexer].
|
|
|
|
pub struct Parser<'t> {
|
|
|
|
tokens: Vec<Token>,
|
|
|
|
panic_stack: Vec<usize>,
|
|
|
|
text: &'t str,
|
|
|
|
curr: usize,
|
|
|
|
}
|
|
|
|
impl<'t> From<Lexer<'t>> for Parser<'t> {
|
|
|
|
fn from(value: Lexer<'t>) -> Self {
|
|
|
|
let (tokens, text) = value.consume();
|
|
|
|
Self::new(tokens, text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
/// Create a new [Parser] from a list of [Tokens][1]
|
|
|
|
/// and the [text](str) used to generate that list
|
|
|
|
/// (as [Tokens][1] do not store their strings)
|
|
|
|
///
|
|
|
|
/// [1]: Token
|
|
|
|
pub fn new(tokens: Vec<Token>, text: &'t str) -> Self {
|
|
|
|
Self { tokens, text, panic_stack: vec![], curr: 0 }
|
|
|
|
}
|
|
|
|
/// Consumes any consecutive comments
|
|
|
|
fn consume_comments(&mut self) -> &mut Self {
|
|
|
|
while let Some(Type::Comment) = self.peek().map(|t| t.ty()) {
|
|
|
|
self.curr += 1;
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
/// Consume the current token
|
|
|
|
#[inline]
|
|
|
|
pub fn consume(&mut self) -> &mut Self {
|
|
|
|
self.curr += 1;
|
|
|
|
self.consume_comments();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
/// Peek at the current token
|
|
|
|
pub fn peek(&self) -> Option<&Token> {
|
|
|
|
self.tokens.get(self.curr)
|
|
|
|
}
|
|
|
|
/// Records the current position on the panic stack
|
|
|
|
pub fn mark(&mut self) -> &mut Self {
|
|
|
|
self.panic_stack.push(self.curr);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
/// Erases a recorded position from the panic stack
|
|
|
|
pub fn unmark(&mut self) -> &mut Self {
|
|
|
|
self.panic_stack.pop();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
/// Unwinds the panic stack one step
|
|
|
|
pub fn unwind(&mut self) -> Option<usize> {
|
|
|
|
let out = self.panic_stack.pop();
|
|
|
|
if let Some(v) = out {
|
|
|
|
self.curr = v;
|
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
|
|
|
/// Parse the [start of an AST](Start)
|
|
|
|
pub fn parse(&mut self) -> PResult<Start> {
|
|
|
|
self.consume_comments();
|
|
|
|
Ok(Start(self.expr()?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Helpers
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
fn consume_type(&mut self, t: Type) -> PResult<&mut Self> {
|
|
|
|
self.matches(t)?;
|
|
|
|
Ok(self.consume())
|
|
|
|
}
|
|
|
|
fn check_eof(&mut self) -> PResult<&mut Self> {
|
|
|
|
if self.curr < self.tokens.len() {
|
|
|
|
Ok(self)
|
|
|
|
} else {
|
|
|
|
Err(Error::end_of_file())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn todo_error(&mut self, l: u32, c: u32, s: &str) -> Error {
|
|
|
|
eprintln!("TODO: {s}:{l}:{c}");
|
|
|
|
Error::unspecified().token(*self.peek().unwrap())
|
|
|
|
}
|
|
|
|
fn matches(&mut self, e: Type) -> PResult<&Token> {
|
|
|
|
let t = self.check_eof()?.peek().expect("self should not be eof");
|
|
|
|
if t.ty() != e {
|
|
|
|
Err(Error::expected(e).token(*t))?
|
|
|
|
}
|
|
|
|
Ok(t)
|
|
|
|
}
|
|
|
|
fn keyword(&mut self, keyword: Keyword) -> PResult<&mut Self> {
|
|
|
|
self.consume_type(Type::Keyword(keyword))
|
|
|
|
}
|
|
|
|
fn delimited<F, R>(&mut self, lhs: Type, mid: F, rhs: Type) -> PResult<R>
|
|
|
|
where F: Fn(&mut Self) -> PResult<R> {
|
|
|
|
self.consume_type(lhs)?;
|
2023-10-19 19:40:03 +00:00
|
|
|
let out = mid(self)?;
|
2023-10-17 18:36:21 +00:00
|
|
|
self.consume_type(rhs)?;
|
2023-10-19 19:40:03 +00:00
|
|
|
Ok(out)
|
2023-10-17 18:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
macro ptodo_err($self:expr $(, $t:expr)*) {
|
|
|
|
$($t;)*
|
|
|
|
$self.todo_error(line!(), column!(), file!())
|
|
|
|
}
|
|
|
|
macro ptodo($self:expr $(, $t:expr)*) {
|
|
|
|
$($t;)*
|
|
|
|
Err(ptodo_err!($self))
|
|
|
|
}
|
|
|
|
fn check_eof(t: Option<&Token>) -> PResult<&Token> {
|
|
|
|
t.ok_or(Error::end_of_file())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// # Terminals and Pseudo-Terminals
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
pub fn identifier(&mut self) -> PResult<Identifier> {
|
2023-10-19 19:40:03 +00:00
|
|
|
let range = self
|
|
|
|
.matches(Type::Identifier)
|
|
|
|
.map_err(|e| Error::not_identifier().maybe_token(e.start()))?
|
|
|
|
.range();
|
2023-10-17 18:36:21 +00:00
|
|
|
Ok(Identifier(self.consume().text[range].into()))
|
|
|
|
}
|
|
|
|
pub fn literal(&mut self) -> PResult<literal::Literal> {
|
|
|
|
use literal::Literal::*;
|
|
|
|
use Keyword::{False, True};
|
|
|
|
let tok = check_eof(self.peek())?;
|
|
|
|
match tok.ty() {
|
|
|
|
Type::Float => self.float().map(Float),
|
2023-10-19 19:40:03 +00:00
|
|
|
Type::Integer => self.int::<10>().map(Int),
|
2023-10-17 18:36:21 +00:00
|
|
|
Type::String => self.string().map(String),
|
|
|
|
Type::Character => self.char().map(Char),
|
|
|
|
Type::Keyword(True | False) => self.bool().map(Bool),
|
|
|
|
_ => Err(Error::not_literal().token(*tok)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn float(&mut self) -> PResult<literal::Float> {
|
|
|
|
ptodo!(self)
|
|
|
|
}
|
|
|
|
pub fn int(&mut self) -> PResult<u128> {
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
eprintln!("/* TODO: parse integer literals from other bases */");
|
|
|
|
let token = *self.matches(Type::Integer)?;
|
|
|
|
self.consume().text[token.range()]
|
|
|
|
.parse()
|
|
|
|
.map_err(|_| Error::not_int().token(token))
|
|
|
|
}
|
|
|
|
pub fn string(&mut self) -> PResult<String> {
|
|
|
|
let range = self.matches(Type::String)?.range();
|
|
|
|
Ok(self.consume().text[range].into())
|
|
|
|
}
|
|
|
|
pub fn char(&mut self) -> PResult<char> {
|
2023-10-19 19:40:03 +00:00
|
|
|
let token = *self.matches(Type::Character)?;
|
|
|
|
self.consume().text[&token]
|
|
|
|
.chars()
|
|
|
|
.next()
|
|
|
|
.ok_or(Error::not_char().token(token))
|
2023-10-17 18:36:21 +00:00
|
|
|
}
|
|
|
|
pub fn bool(&mut self) -> PResult<bool> {
|
|
|
|
use Keyword::{False, True};
|
|
|
|
let token = check_eof(self.peek())?;
|
|
|
|
let out = match token.ty() {
|
|
|
|
Type::Keyword(False) => false,
|
|
|
|
Type::Keyword(True) => true,
|
|
|
|
_ => Err(Error::not_bool().token(*token))?,
|
|
|
|
};
|
|
|
|
self.consume();
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Expressions
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
pub fn expr(&mut self) -> PResult<expression::Expr> {
|
|
|
|
use expression::Expr;
|
2023-10-19 19:40:03 +00:00
|
|
|
self.ignore().map(Expr::Ignore)
|
|
|
|
}
|
|
|
|
pub fn if_not_expr(&mut self, matches: Type) -> PResult<Option<expression::Expr>> {
|
|
|
|
if check_eof(self.peek())?.ty() == matches {
|
|
|
|
Ok(None)
|
|
|
|
} else {
|
|
|
|
Some(self.expr()).transpose()
|
|
|
|
}
|
2023-10-17 18:36:21 +00:00
|
|
|
}
|
|
|
|
pub fn block(&mut self) -> PResult<expression::Block> {
|
2023-10-19 19:40:03 +00:00
|
|
|
self.delimited(Type::LCurly, |p| p.if_not_expr(Type::RCurly), Type::RCurly)
|
|
|
|
.map(|e| expression::Block { expr: e.map(Box::new) })
|
2023-10-17 18:36:21 +00:00
|
|
|
}
|
|
|
|
pub fn group(&mut self) -> PResult<expression::Group> {
|
2023-10-19 19:40:03 +00:00
|
|
|
let t = check_eof(self.consume_type(Type::LParen)?.peek())?;
|
|
|
|
match t.ty() {
|
|
|
|
Type::RParen => {
|
|
|
|
self.consume();
|
|
|
|
Ok(expression::Group { expr: None })
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let out = self.expr().map(|expr| expression::Group {expr: Some(expr.into())});
|
|
|
|
self.consume_type(Type::RParen)?;
|
|
|
|
out
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 18:36:21 +00:00
|
|
|
}
|
2023-10-19 19:40:03 +00:00
|
|
|
pub fn primary(&mut self) -> PResult<expression::Primary> {
|
|
|
|
use expression::Primary;
|
2023-10-17 18:36:21 +00:00
|
|
|
self.identifier()
|
2023-10-19 19:40:03 +00:00
|
|
|
.map(Primary::Identifier)
|
|
|
|
.or_else(|_| self.literal().map(Primary::Literal))
|
|
|
|
.or_else(|_| self.block().map(Primary::Block))
|
|
|
|
.or_else(|_| self.group().map(Primary::Group))
|
|
|
|
.or_else(|_| self.flow().map(Primary::Branch))
|
2023-10-17 18:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper macro for math parsing subexpressions with production
|
|
|
|
/// ```ebnf
|
|
|
|
/// Ret = a (b a)*
|
|
|
|
/// ```
|
|
|
|
/// # Examples
|
|
|
|
/// ```rust,ignore
|
2023-10-19 19:40:03 +00:00
|
|
|
/// binary!{
|
2023-10-17 18:36:21 +00:00
|
|
|
/// function_name: ret::Value = parse_operands, parse_operators;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// becomes
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// pub fn function_name(&mut self) -> PResult<ret::Value> { ... }
|
|
|
|
/// ```
|
2023-10-19 19:40:03 +00:00
|
|
|
macro binary ($($f:ident: $Ret:ty = $a:ident, $b:ident);*$(;)?) {$(
|
2023-10-17 18:36:21 +00:00
|
|
|
pub fn $f (&mut self) -> PResult<$Ret> {
|
|
|
|
let (first, mut others) = (self.$a()?, vec![]);
|
|
|
|
while let Some(op) = self.$b() {
|
|
|
|
others.push((op, self.$a()?));
|
|
|
|
}
|
2023-10-19 19:40:03 +00:00
|
|
|
Ok(<$Ret>::new(first, others))
|
2023-10-17 18:36:21 +00:00
|
|
|
}
|
|
|
|
)*}
|
|
|
|
/// # [Arithmetic and Logical Subexpressions](math)
|
|
|
|
impl<'t> Parser<'t> {
|
2023-10-19 19:40:03 +00:00
|
|
|
binary! {
|
2023-10-17 18:36:21 +00:00
|
|
|
//name returns operands operators
|
|
|
|
ignore: math::Ignore = assign, ignore_op;
|
|
|
|
assign: math::Assign = compare, assign_op;
|
|
|
|
compare: math::Compare = logic, compare_op;
|
|
|
|
logic: math::Logic = bitwise, logic_op;
|
|
|
|
bitwise: math::Bitwise = shift, bitwise_op;
|
|
|
|
shift: math::Shift = term, shift_op;
|
|
|
|
term: math::Term = factor, term_op;
|
|
|
|
factor: math::Factor = unary, factor_op;
|
|
|
|
}
|
|
|
|
pub fn unary(&mut self) -> PResult<math::Unary> {
|
|
|
|
let mut ops = vec![];
|
|
|
|
while let Some(op) = self.unary_op() {
|
|
|
|
ops.push(op)
|
|
|
|
}
|
2023-10-19 19:40:03 +00:00
|
|
|
Ok(math::Unary(ops, self.primary()?))
|
2023-10-17 18:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
macro operator_impl($($(#[$m:meta])*$f:ident: $Ret:ty),*$(,)*) {$(
|
|
|
|
$(#[$m])* pub fn $f(&mut self) -> Option<$Ret> {
|
|
|
|
let out: Option<$Ret> = self.peek()?.ty().into();
|
|
|
|
if out.is_some() { self.consume(); }
|
|
|
|
out
|
|
|
|
}
|
|
|
|
)*}
|
|
|
|
/// # [Operators](operator)
|
|
|
|
impl<'t> Parser<'t> {
|
|
|
|
operator_impl! {
|
|
|
|
ignore_op: operator::Ignore,
|
|
|
|
compare_op: operator::Compare,
|
|
|
|
assign_op: operator::Assign,
|
|
|
|
logic_op: operator::Logic,
|
|
|
|
bitwise_op: operator::Bitwise,
|
|
|
|
shift_op: operator::Shift,
|
|
|
|
term_op: operator::Term,
|
|
|
|
factor_op: operator::Factor,
|
|
|
|
unary_op: operator::Unary,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// # [Control Flow](control)
|
|
|
|
impl<'t> Parser<'t> {
|
2023-10-19 19:40:03 +00:00
|
|
|
pub fn flow(&mut self) -> PResult<control::Flow> {
|
|
|
|
use control::Flow;
|
|
|
|
use Keyword::{Break, Continue, For, If, Return, While};
|
2023-10-17 18:36:21 +00:00
|
|
|
let token = check_eof(self.peek())?;
|
|
|
|
match token.ty() {
|
2023-10-19 19:40:03 +00:00
|
|
|
Type::Keyword(While) => self.parse_while().map(Flow::While),
|
|
|
|
Type::Keyword(For) => self.parse_for().map(Flow::For),
|
|
|
|
Type::Keyword(If) => self.parse_if().map(Flow::If),
|
|
|
|
Type::Keyword(Break) => self.parse_break().map(Flow::Break),
|
|
|
|
Type::Keyword(Return) => self.parse_return().map(Flow::Return),
|
|
|
|
Type::Keyword(Continue) => self.parse_continue().map(Flow::Continue),
|
2023-10-17 18:36:21 +00:00
|
|
|
_ => Err(Error::not_branch().token(*token)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn parse_if(&mut self) -> PResult<control::If> {
|
2023-10-19 19:40:03 +00:00
|
|
|
self.keyword(Keyword::If)?;
|
2023-10-17 18:36:21 +00:00
|
|
|
Ok(control::If {
|
|
|
|
cond: self.expr()?.into(),
|
|
|
|
body: self.block()?,
|
|
|
|
else_: self.parse_else()?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pub fn parse_while(&mut self) -> PResult<control::While> {
|
2023-10-19 19:40:03 +00:00
|
|
|
self.keyword(Keyword::While)?;
|
2023-10-17 18:36:21 +00:00
|
|
|
Ok(control::While {
|
|
|
|
cond: self.expr()?.into(),
|
|
|
|
body: self.block()?,
|
|
|
|
else_: self.parse_else()?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pub fn parse_for(&mut self) -> PResult<control::For> {
|
|
|
|
self.keyword(Keyword::For)?;
|
|
|
|
Ok(control::For {
|
|
|
|
var: self.identifier()?,
|
|
|
|
iter: { self.keyword(Keyword::In)?.expr()?.into() },
|
|
|
|
body: self.block()?,
|
|
|
|
else_: self.parse_else()?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pub fn parse_else(&mut self) -> PResult<Option<control::Else>> {
|
|
|
|
// it's fine for `else` to be missing entirely
|
2023-10-19 19:40:03 +00:00
|
|
|
self.keyword(Keyword::Else)
|
|
|
|
.ok()
|
|
|
|
.map(|p| Ok(control::Else { block: p.block()? }))
|
|
|
|
.transpose()
|
2023-10-17 18:36:21 +00:00
|
|
|
}
|
|
|
|
pub fn parse_break(&mut self) -> PResult<control::Break> {
|
|
|
|
Ok(control::Break { expr: self.keyword(Keyword::Break)?.expr()?.into() })
|
|
|
|
}
|
|
|
|
pub fn parse_return(&mut self) -> PResult<control::Return> {
|
|
|
|
Ok(control::Return { expr: self.keyword(Keyword::Return)?.expr()?.into() })
|
|
|
|
}
|
|
|
|
pub fn parse_continue(&mut self) -> PResult<control::Continue> {
|
|
|
|
ptodo!(self)
|
|
|
|
}
|
|
|
|
}
|