Skip to content

Commit

Permalink
chore: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
DiscreteTom committed Dec 29, 2024
1 parent d4e175a commit c30a76c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 45 deletions.
11 changes: 6 additions & 5 deletions examples/json_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use whitehole::{
action::Action,
combinator::{eat, next},
parser::{Builder, Parser},
range::WithRange,
};

pub fn build_lexer(s: &str) -> Parser<impl Action> {
pub fn build_lexer(s: &str) -> Parser<impl Action<Value = WithRange<()>>> {
// Use `* (1..)` to repeat for one or more times.
let whitespaces = next(in_str!(" \t\r\n")) * (1..);

Expand Down Expand Up @@ -36,7 +37,7 @@ pub fn build_lexer(s: &str) -> Parser<impl Action> {
let boundary = next(in_str!("[]{}:,"));

Builder::new()
.entry(whitespaces | boundary | number | string | "true" | "false" | "null")
.entry((whitespaces | boundary | number | string | "true" | "false" | "null").range())
.build(s)
}

Expand Down Expand Up @@ -68,9 +69,9 @@ mod tests {
if let Some(node) = output {
println!(
"{}..{}: {:?}",
node.range.start,
node.range.end,
&s[node.range.clone()]
node.value.range.start,
node.value.range.end,
&s[node.value.range.clone()]
);
} else {
break;
Expand Down
11 changes: 6 additions & 5 deletions examples/json_parser_with_inter_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use whitehole::{
action::Action,
combinator::{eat, next, wrap},
parser::{Builder, Parser},
range::WithRange,
};

pub fn build_parser_with_inter_mut(s: &str) -> Parser<impl Action> {
pub fn build_parser_with_inter_mut(s: &str) -> Parser<impl Action<Value = WithRange<()>>> {
// To re-use a combinator for multiple times, instead of wrapping the combinator in an Rc,
// use a closure to generate the combinator for better runtime performance (via inlining).
let ws = || next(in_str!(" \t\r\n")) * (1..);
Expand Down Expand Up @@ -61,7 +62,7 @@ pub fn build_parser_with_inter_mut(s: &str) -> Parser<impl Action> {
})))
.ok();

Builder::new().entry(ws() | value()).build(s)
Builder::new().entry((ws() | value()).range()).build(s)
}

fn main() {}
Expand Down Expand Up @@ -92,9 +93,9 @@ mod tests {
if let Some(node) = output {
println!(
"{}..{}: {:?}",
node.range.start,
node.range.end,
&s[node.range.clone()]
node.value.range.start,
node.value.range.end,
&s[node.value.range.clone()]
);
} else {
break;
Expand Down
11 changes: 6 additions & 5 deletions examples/json_parser_with_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use whitehole::{
action::Action,
combinator::{eat, next, wrap},
parser::{Builder, Parser},
range::WithRange,
C,
};

pub fn build_parser_with_static(s: &str) -> Parser<impl Action> {
pub fn build_parser_with_static(s: &str) -> Parser<impl Action<Value = WithRange<()>>> {
// To re-use a combinator for multiple times, instead of wrapping the combinator in an Rc,
// use a function to generate the combinator for better runtime performance (via inlining).
fn ws() -> C!() {
Expand Down Expand Up @@ -59,7 +60,7 @@ pub fn build_parser_with_static(s: &str) -> Parser<impl Action> {
wrap(|input| VALUE.exec(input))
}

Builder::new().entry(ws() | value()).build(s)
Builder::new().entry((ws() | value()).range()).build(s)
}

fn main() {}
Expand Down Expand Up @@ -90,9 +91,9 @@ mod tests {
if let Some(node) = output {
println!(
"{}..{}: {:?}",
node.range.start,
node.range.end,
&s[node.range.clone()]
node.value.range.start,
node.value.range.end,
&s[node.value.range.clone()]
);
} else {
break;
Expand Down
48 changes: 24 additions & 24 deletions examples/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,64 +33,64 @@ mod tests {
fn test_whitespaces() {
assert_eq!(
whitespaces()
.parse(&mut Input::new(" \t\r\n", 0, &mut (), &mut ()).unwrap())
.exec(Input::new(" \t\r\n", 0, &mut (), &mut ()).unwrap())
.unwrap()
.rest,
""
.digested,
4
);
assert_eq!(
whitespaces()
.parse(&mut Input::new(" \t\r\n123", 0, &mut (), &mut ()).unwrap())
.exec(Input::new(" \t\r\n123", 0, &mut (), &mut ()).unwrap())
.unwrap()
.rest,
"123"
.digested,
4
);
}

#[test]
fn test_comments() {
assert_eq!(
singleline_comment()
.parse(&mut Input::new("//123", 0, &mut (), &mut ()).unwrap())
.exec(Input::new("//123", 0, &mut (), &mut ()).unwrap())
.unwrap()
.rest,
""
.digested,
5
);
assert_eq!(
singleline_comment()
.parse(&mut Input::new("//123\n", 0, &mut (), &mut ()).unwrap())
.exec(Input::new("//123\n", 0, &mut (), &mut ()).unwrap())
.unwrap()
.rest,
""
.digested,
6
);
assert_eq!(
singleline_comment()
.parse(&mut Input::new("//123\n456", 0, &mut (), &mut ()).unwrap())
.exec(Input::new("//123\n456", 0, &mut (), &mut ()).unwrap())
.unwrap()
.rest,
"456"
.digested,
6
);

assert_eq!(
multiline_comment()
.parse(&mut Input::new("/*123", 0, &mut (), &mut ()).unwrap())
.exec(Input::new("/*123", 0, &mut (), &mut ()).unwrap())
.unwrap()
.rest,
""
.digested,
5
);
assert_eq!(
multiline_comment()
.parse(&mut Input::new("/*123\n*/", 0, &mut (), &mut ()).unwrap())
.exec(Input::new("/*123\n*/", 0, &mut (), &mut ()).unwrap())
.unwrap()
.rest,
""
.digested,
8
);
assert_eq!(
multiline_comment()
.parse(&mut Input::new("/*123\n*/456", 0, &mut (), &mut ()).unwrap())
.exec(Input::new("/*123\n*/456", 0, &mut (), &mut ()).unwrap())
.unwrap()
.rest,
"456"
.digested,
8
);
}
}
12 changes: 6 additions & 6 deletions examples/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ mod tests {
fn test_regex() {
assert_eq!(
regex(r"\d+")
.parse(&mut Input::new("123", 0, &mut (), &mut ()).unwrap())
.exec(Input::new("123", 0, &mut (), &mut ()).unwrap())
.unwrap()
.rest,
""
.digested,
3
);
assert_eq!(
regex(r"\d+")
.parse(&mut Input::new("123abc", 0, &mut (), &mut ()).unwrap())
.exec(Input::new("123abc", 0, &mut (), &mut ()).unwrap())
.unwrap()
.rest,
"abc"
.digested,
3
);
}
}

0 comments on commit c30a76c

Please sign in to comment.