Skip to content

Commit 2083d70

Browse files
committed
refactor: ♻️ reorganize Cargo.toml dependencies, enhance Context struct with clone_source and call path management
1 parent 74ee02d commit 2083d70

5 files changed

Lines changed: 31 additions & 17 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7-
env_logger = "0.11.8"
87
log = "0.4.29"
98
rustc-hash = "2.1.1"
9+
10+
[dev-dependencies]
11+
env_logger = "0.11.8"

examples/baab_baab.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
use packrust::*;
66

77
fn main() {
8+
env_logger::init();
9+
810
// S -> A '-' A
911
// A -> B 'b' / 'b'
1012
// B -> B 'a' / A 'a'
11-
12-
let parser = {
13+
let s = {
1314
let a = lazy("A", |a| {
1415
let b = {
1516
let a = a.clone();
@@ -25,6 +26,6 @@ fn main() {
2526
.end()
2627
.map(|_| "parse success");
2728

28-
let res = parser.run("baab-baab");
29+
let res = s.run("baab-baab");
2930
println!("{:?}", res);
3031
}

src/combinators.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ where
3131
let (pos, val) = (self.raw_parser)(pos, ctx)?;
3232
let Some(val) = f(val) else {
3333
return Err(ParseError {
34-
source: ctx.source.iter().collect(),
34+
source: ctx.clone_source(),
3535
pos,
3636
reason: String::from("try map failed: got None"),
3737
});
@@ -110,7 +110,7 @@ where
110110
let (pos, val) = self.parse(pos, ctx)?;
111111
match ctx.source.get(pos) {
112112
Some(c) => Err(ParseError {
113-
source: ctx.source.iter().collect(),
113+
source: ctx.clone_source(),
114114
pos,
115115
reason: format!("expected EOF found {}", c),
116116
}),
@@ -129,12 +129,12 @@ pub fn satisfy(name: impl Into<String>, f: impl Fn(char) -> bool + 'static) -> P
129129
Rc::new(move |pos, ctx: &mut Context| match ctx.source.get(pos) {
130130
Some(&c) if f(c) => Ok((pos + 1, c)),
131131
Some(c) => Err(ParseError {
132-
source: ctx.source.iter().collect(),
132+
source: ctx.clone_source(),
133133
pos,
134134
reason: format!("expected {} got {}", name, c),
135135
}),
136136
None => Err(ParseError {
137-
source: ctx.source.iter().collect(),
137+
source: ctx.clone_source(),
138138
pos,
139139
reason: format!("expected {} got EOF", name),
140140
}),
@@ -166,7 +166,7 @@ pub fn keyword(keyword: impl Into<String>) -> Parser<String> {
166166
Ok((pos + keyword.len(), name.clone()))
167167
} else {
168168
Err(ParseError {
169-
source: ctx.source.iter().collect(),
169+
source: ctx.clone_source(),
170170
pos,
171171
reason: String::from(""),
172172
})

src/context.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pub struct Context {
77
pub cache: FxHashMap<CacheKey, Box<dyn Any>>,
88
pub source: Vec<char>,
99
pub lr_stack: Vec<CacheKey>,
10-
pub call_path: Vec<CacheKey>,
11-
pub pending_evictions: FxHashMap<CacheKey, Vec<CacheKey>>,
10+
call_path: Vec<CacheKey>,
11+
pending_evictions: FxHashMap<CacheKey, Vec<CacheKey>>,
1212
}
1313

1414
impl Context {
@@ -22,6 +22,19 @@ impl Context {
2222
}
2323
}
2424

25+
pub(crate) fn clone_source(&self) -> String {
26+
self.source.iter().collect()
27+
}
28+
29+
pub(crate) fn push_call_path(&mut self, key: CacheKey) {
30+
self.call_path.push(key);
31+
}
32+
33+
pub(crate) fn pop_call_path(&mut self, key: CacheKey) {
34+
debug_assert_eq!(self.call_path.last(), Some(&key));
35+
self.call_path.pop();
36+
}
37+
2538
pub(crate) fn schedule_cache_eviction(&mut self, key: CacheKey) {
2639
let dependents = self.pending_evictions.entry(key).or_default();
2740

src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ where
7373
ctx.schedule_cache_eviction(key);
7474

7575
return Err(ParseError {
76-
source: ctx.source.iter().collect(),
76+
source: ctx.clone_source(),
7777
pos,
7878
reason: String::from("failed to resolve left recursion"),
7979
});
@@ -86,7 +86,7 @@ where
8686

8787
ctx.cache
8888
.insert(key, Box::new(CacheEntry::<T>::LeftRecursion));
89-
ctx.call_path.push(key);
89+
ctx.push_call_path(key);
9090

9191
let mut result = (self.raw_parser)(pos, ctx);
9292

@@ -101,8 +101,7 @@ where
101101
let mut best_res @ Ok((mut best_pos, _)) = result else {
102102
let popped = ctx.lr_stack.pop();
103103
debug_assert_eq!(popped, Some(key));
104-
let popped = ctx.call_path.pop();
105-
debug_assert_eq!(popped, Some(key));
104+
ctx.pop_call_path(key);
106105
return result;
107106
};
108107

@@ -130,8 +129,7 @@ where
130129
result = best_res
131130
}
132131

133-
let popped = ctx.call_path.pop();
134-
debug_assert_eq!(popped, Some(key));
132+
ctx.pop_call_path(key);
135133
result
136134
}
137135

0 commit comments

Comments
 (0)