Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions core/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "inference-parser"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0 OR MIT"
description = "Custom parser for the Inference language with resilient error recovery"

[dependencies]
inference-ast = { path = "../ast" }
thiserror = "1.0"
winnow = "0.7"
tracing = { version = "0.1", optional = true }
drop_bomb = "0.1"
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dependencies inference-ast, winnow, and drop_bomb are declared but not used in any of the parser implementation files (lexer.rs, parser.rs, error.rs, lib.rs). These unused dependencies should be removed to reduce build times and avoid confusion.

Suggested change
inference-ast = { path = "../ast" }
thiserror = "1.0"
winnow = "0.7"
tracing = { version = "0.1", optional = true }
drop_bomb = "0.1"
thiserror = "1.0"
tracing = { version = "0.1", optional = true }

Copilot uses AI. Check for mistakes.

[dev-dependencies]
expect-test = "1.4"

[features]
default = []
tracing = ["dep:tracing"]

[[test]]
name = "parser_tests"
path = "tests/parser_tests.rs"
76 changes: 76 additions & 0 deletions core/parser/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::fmt;
use thiserror::Error;

/// Parser error types with location information
#[derive(Debug, Clone, Error)]
pub enum ParseError {
#[error("Unexpected token at position {pos}: expected {expected}, found {found}")]
UnexpectedToken {
pos: usize,
expected: String,
found: String,
},

#[error("Unexpected end of file while parsing {context}")]
UnexpectedEof { context: String },

#[error("Invalid syntax at position {pos}: {reason}")]
InvalidSyntax { pos: usize, reason: String },

#[error("Failed to parse {context} at position {pos}")]
FailedToParse { pos: usize, context: String },

#[error("Duplicate definition: {name}")]
DuplicateName { name: String },

#[error("Invalid type annotation: {reason}")]
InvalidTypeAnnotation { reason: String },

#[error("Invalid generic parameters: {reason}")]
InvalidGenerics { reason: String },
}

/// Error recovery mode allows the parser to continue after errors
#[derive(Debug, Clone)]
pub struct ParseErrorWithRecovery {
pub error: ParseError,
pub recovered: bool,
}

impl fmt::Display for ParseErrorWithRecovery {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.recovered {
write!(f, "{} (recovered)", self.error)
} else {
write!(f, "{}", self.error)
}
}
}

/// Collects multiple errors during parsing for batch reporting
#[derive(Debug, Default, Clone)]
pub struct ParseErrorCollector {
errors: Vec<ParseError>,
}

impl ParseErrorCollector {
pub fn new() -> Self {
Self { errors: Vec::new() }
}

pub fn add_error(&mut self, error: ParseError) {
self.errors.push(error);
}

pub fn has_errors(&self) -> bool {
!self.errors.is_empty()
}

pub fn errors(&self) -> &[ParseError] {
&self.errors
}

pub fn take_errors(self) -> Vec<ParseError> {
self.errors
}
}
Loading