Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions core/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[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]
thiserror = "1.0"
tracing = { version = "0.1", optional = true }

[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
}
}
67 changes: 67 additions & 0 deletions core/parser/src/grammar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/// Grammar module - Parsing rules for Inference language constructs
///
/// This module provides the grammar parsing functions called by parse_module().
/// Each function parses a specific construct and advances the parser position.

use crate::parser::Parser;
use crate::syntax_kind::SyntaxKind;

pub mod items;
pub mod expressions;
pub mod types;

pub use items::*;
pub use expressions::*;
pub use types::*;

/// Parse a top-level item (function, struct, enum, etc.)
pub fn parse_item(p: &mut Parser) {
// Check for pub visibility modifier
if p.at(SyntaxKind::PUB) {
p.bump();
}

match p.current() {
SyntaxKind::FN => items::parse_function(p),
SyntaxKind::STRUCT => items::parse_struct(p),
SyntaxKind::ENUM => items::parse_enum(p),
SyntaxKind::TRAIT => items::parse_trait(p),
SyntaxKind::IMPL => items::parse_impl(p),
SyntaxKind::TYPE => items::parse_type_alias(p),
SyntaxKind::CONST => items::parse_const(p),
SyntaxKind::IMPORT => items::parse_import(p),
SyntaxKind::MOD => items::parse_module(p),
SyntaxKind::LET => items::parse_let_binding(p),
_ => {
// Unknown item - skip it
if !p.at_eof() {
p.bump();
}
}
}
}

/// Parse a statement inside a block
pub fn parse_statement(p: &mut Parser) {
match p.current() {
SyntaxKind::LET => items::parse_let_binding(p),
SyntaxKind::IF => expressions::parse_if_expr(p),
SyntaxKind::WHILE => expressions::parse_while_expr(p),
SyntaxKind::FOR => expressions::parse_for_expr(p),
SyntaxKind::LOOP => expressions::parse_loop_expr(p),
SyntaxKind::RETURN => expressions::parse_return_expr(p),
SyntaxKind::BREAK => {
p.bump();
}
SyntaxKind::CONTINUE => {
p.bump();
}
_ => {
// Try to parse as expression
expressions::parse_expression(p);
if p.at(SyntaxKind::SEMICOLON) {
p.bump();
}
}
}
}
Loading