cl-parser: Move inline modules out of line

This commit is contained in:
John 2024-02-29 20:44:49 -06:00
parent ab17ebbadc
commit abf00f383c
3 changed files with 1268 additions and 1270 deletions

209
cl-parser/src/error.rs Normal file
View File

@ -0,0 +1,209 @@
use super::*;
use conlang::lexer::error::{Error as LexError, Reason};
use std::fmt::Display;
pub type PResult<T> = Result<T, Error>;
/// Contains information about [Parser] errors
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
pub reason: ErrorKind,
pub while_parsing: Parsing,
pub loc: Loc,
}
impl std::error::Error for Error {}
/// Represents the reason for parse failure
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ErrorKind {
Lexical(LexError),
EndOfInput,
UnmatchedParentheses,
UnmatchedCurlyBraces,
UnmatchedSquareBrackets,
Unexpected(Type),
Expected {
want: Type,
got: Type,
},
/// No rules matched
Nothing,
/// Indicates unfinished code
Todo,
}
impl From<LexError> for ErrorKind {
fn from(value: LexError) -> Self {
match value.reason() {
Reason::EndOfFile => Self::EndOfInput,
_ => Self::Lexical(value),
}
}
}
/// Compactly represents the stage of parsing an [Error] originated in
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Parsing {
File,
Attrs,
Meta,
Item,
Visibility,
Mutability,
ItemKind,
Alias,
Const,
Static,
Module,
ModuleKind,
Function,
Param,
Struct,
StructKind,
StructMember,
Enum,
EnumKind,
Variant,
VariantKind,
Impl,
Ty,
TyKind,
TyTuple,
TyRef,
TyFn,
Stmt,
StmtKind,
Let,
Expr,
ExprKind,
Assign,
AssignKind,
Binary,
BinaryKind,
Unary,
UnaryKind,
Index,
Call,
Member,
PathExpr,
PathPart,
Identifier,
Literal,
Array,
ArrayRep,
AddrOf,
Block,
Group,
Tuple,
While,
If,
For,
Else,
Break,
Return,
Continue,
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { reason, while_parsing, loc } = self;
match reason {
// TODO entries are debug-printed
ErrorKind::Todo => write!(f, "{loc} {reason} {while_parsing:?}"),
// lexical errors print their own higher-resolution loc info
ErrorKind::Lexical(e) => write!(f, "{e} (while parsing {while_parsing})"),
_ => write!(f, "{loc} {reason} while parsing {while_parsing}"),
}
}
}
impl Display for ErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ErrorKind::Lexical(e) => e.fmt(f),
ErrorKind::EndOfInput => write!(f, "End of input"),
ErrorKind::UnmatchedParentheses => write!(f, "Unmatched parentheses"),
ErrorKind::UnmatchedCurlyBraces => write!(f, "Unmatched curly braces"),
ErrorKind::UnmatchedSquareBrackets => write!(f, "Unmatched square brackets"),
ErrorKind::Unexpected(t) => write!(f, "Encountered unexpected token `{t}`"),
ErrorKind::Expected { want: e, got: g } => {
write!(f, "Expected {e}, but got {g}")
}
ErrorKind::Nothing => write!(f, "Nothing found"),
ErrorKind::Todo => write!(f, "TODO:"),
}
}
}
impl Display for Parsing {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Parsing::File => "a file",
Parsing::Attrs => "an attribute-set",
Parsing::Meta => "an attribute",
Parsing::Item => "an item",
Parsing::Visibility => "a visibility qualifier",
Parsing::Mutability => "a mutability qualifier",
Parsing::ItemKind => "an item",
Parsing::Alias => "a type alias",
Parsing::Const => "a const item",
Parsing::Static => "a static variable",
Parsing::Module => "a module",
Parsing::ModuleKind => "a module",
Parsing::Function => "a function",
Parsing::Param => "a function parameter",
Parsing::Struct => "a struct",
Parsing::StructKind => "a struct",
Parsing::StructMember => "a struct member",
Parsing::Enum => "an enum",
Parsing::EnumKind => "an enum",
Parsing::Variant => "an enum variant",
Parsing::VariantKind => "an enum variant",
Parsing::Impl => "an impl block",
Parsing::Ty => "a type",
Parsing::TyKind => "a type",
Parsing::TyTuple => "a tuple of types",
Parsing::TyRef => "a reference type",
Parsing::TyFn => "a function pointer type",
Parsing::Stmt => "a statement",
Parsing::StmtKind => "a statement",
Parsing::Let => "a local variable declaration",
Parsing::Expr => "an expression",
Parsing::ExprKind => "an expression",
Parsing::Assign => "an assignment",
Parsing::AssignKind => "an assignment operator",
Parsing::Binary => "a binary expression",
Parsing::BinaryKind => "a binary operator",
Parsing::Unary => "a unary expression",
Parsing::UnaryKind => "a unary operator",
Parsing::Index => "an indexing expression",
Parsing::Call => "a call expression",
Parsing::Member => "a member access expression",
Parsing::PathExpr => "a path",
Parsing::PathPart => "a path component",
Parsing::Identifier => "an identifier",
Parsing::Literal => "a literal",
Parsing::Array => "an array",
Parsing::ArrayRep => "an array of form [k;N]",
Parsing::AddrOf => "a borrow op",
Parsing::Block => "a block",
Parsing::Group => "a grouped expression",
Parsing::Tuple => "a tuple",
Parsing::While => "a while expression",
Parsing::If => "an if expression",
Parsing::For => "a for expression",
Parsing::Else => "an else block",
Parsing::Break => "a break expression",
Parsing::Return => "a return expression",
Parsing::Continue => "a continue expression",
}
.fmt(f)
}
}

File diff suppressed because it is too large Load Diff

1057
cl-parser/src/parser.rs Normal file

File diff suppressed because it is too large Load Diff