Skip to content

Commit ab0f430

Browse files
committed
better errors and interaction and lex parse
1 parent a2da124 commit ab0f430

File tree

7 files changed

+60
-12
lines changed

7 files changed

+60
-12
lines changed

CHANGELOG.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
11
# Changelog
22

3+
## [0.2.3] - 2025-09-05
4+
5+
### ✨ Added
6+
7+
- **Enhanced Error Messages**: Added colored, user-friendly error reporting with contextual information
8+
- Pretty-printed syntax errors show original formula, successful lexemes (green), and failed lexemes (red)
9+
- Clear "Expected Token:" labels with clean token names
10+
- Contextual display of parsing progress with colored highlighting using `owo-colors`
11+
- **Improved Parser Error Context**: Parse errors now include the exact position and context where parsing failed
12+
13+
### 🔧 Improved
14+
15+
- **Error Message Quality**: Cleaned up error messages to show "Function or ColumnName" instead of "Function token or ColumnName"
16+
- **Code Style**: Eliminated clippy warnings throughout the codebase
17+
- Removed unnecessary `to_string()` calls in format strings
18+
- Converted instance methods to static methods where appropriate
19+
- Removed unnecessary return statements
20+
- Fixed redundant pattern matching in examples
21+
22+
### 🐛 Fixed
23+
24+
- **Error Display**: Fixed duplicate and malformed error message formatting
25+
- **Documentation**: Removed duplicate doc comments and improved consistency
26+
27+
### 📚 Documentation
28+
29+
- **Error Handling**: Updated parser documentation to reflect new pretty error functionality
30+
- **Examples**: Added comprehensive error testing examples demonstrating colored output
31+
32+
### 🧹 Code Quality
33+
34+
- **Dead Code**: Eliminated all dead code warnings in main library
35+
- **Clippy Compliance**: Achieved zero clippy warnings for main library code
36+
- **Performance**: Optimized error formatting to avoid unnecessary string allocations
37+
38+
### 🔄 Internal Changes
39+
40+
- **Error Processing**: Streamlined error handling pipeline to preserve original `ParseError::Unexpected` for better formatting
41+
- **String Processing**: Improved error message generation to be more efficient and readable
42+
43+
#### Example Output Format:
44+
```
45+
Syntax error- Unexpected Token
46+
Formula: y ~ x +
47+
Show: y ~ x + <eoi>
48+
Expected Token: Function or ColumnName
49+
```
50+
51+
These changes maintain backward compatibility while significantly improving the developer experience when working with formula parsing errors.
52+
353
## [0.2.2] - 2025-09-05
454

555
added `lex_formula` for users to inspect raw lexer output

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
description = "High-performance modern Wilkinson's formula parsing for statistical models. Parses R-style formulas into structured JSON metadata supporting linear models, mixed effects, and complex statistical specifications."
33
name = "fiasto"
4-
version = "0.2.2"
4+
version = "0.2.3"
55
edition = "2021"
66
authors = ["Alex Hallam <alexhallam6.28@gmail.com>"]
77
license = "MIT"

examples/test_pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use fiasto::parse_formula;
22

33
fn main() {
44
// Test different error scenarios
5-
let test_cases = vec![
5+
let test_cases = [
66
"y ~ x +", // trailing plus
77
"y ~ + x", // leading plus
88
"~ x", // missing response
@@ -13,7 +13,7 @@ fn main() {
1313
for (i, formula) in test_cases.iter().enumerate() {
1414
println!("\n=== Test case {} ===", i + 1);
1515
println!("Formula: {}", formula);
16-
if let Err(_) = parse_formula(formula) {
16+
if parse_formula(formula).is_err() {
1717
// Error is already printed by parse_formula via eprintln!
1818
}
1919
}

src/internal/meta_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ impl MetaBuilder {
324324
right: &crate::internal::ast::Term,
325325
) {
326326
// Extract variable names from the interaction terms
327-
let left_name = self.extract_variable_name(left);
328-
let right_name = self.extract_variable_name(right);
327+
let left_name = Self::extract_variable_name(left);
328+
let right_name = Self::extract_variable_name(right);
329329

330330
if let (Some(left_var), Some(right_var)) = (left_name, right_name) {
331331
// Ensure both variables exist
@@ -366,7 +366,7 @@ impl MetaBuilder {
366366
}
367367

368368
/// Extracts variable name from a term
369-
fn extract_variable_name(&self, term: &crate::internal::ast::Term) -> Option<String> {
369+
fn extract_variable_name(term: &crate::internal::ast::Term) -> Option<String> {
370370
match term {
371371
crate::internal::ast::Term::Column(name) => Some(name.clone()),
372372
crate::internal::ast::Term::Function { name: _name, args } => {
@@ -381,7 +381,7 @@ impl MetaBuilder {
381381
right: _right,
382382
} => {
383383
// For nested interactions, we'll use the left side for now
384-
self.extract_variable_name(left)
384+
Self::extract_variable_name(left)
385385
}
386386
crate::internal::ast::Term::RandomEffect(_) => None,
387387
}

src/internal/parse_arg.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use crate::internal::{ast::Argument, errors::ParseError, lexer::Token};
22

3-
/// Parses a single argument within a function call.
4-
53
/// Parses a single argument within a function call.
64
///
75
/// This function handles individual arguments that can appear in function calls.

src/internal/parse_random_effect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ fn parse_correlation_type<'a>(
225225
Some(id_slice.to_string()),
226226
))
227227
} else {
228-
return Err(ParseError::Syntax(
228+
Err(ParseError::Syntax(
229229
"expected second '|' after correlation ID".into(),
230-
));
230+
))
231231
}
232232
} else {
233233
Ok((CorrelationType::Correlated, None))

src/internal/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl<'a> Parser<'a> {
146146
out.push_str(&format!("{}\n", failed.red()));
147147

148148
// Expected Token: list expected tokens
149-
out.push_str(&format!("Expected Token: {}\n", expected.to_string()));
149+
out.push_str(&format!("Expected Token: {}\n", expected));
150150

151151
out
152152
}

0 commit comments

Comments
 (0)