Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffered parse #11

Closed
wants to merge 12 commits into from
24 changes: 17 additions & 7 deletions examples/json.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, ops::Deref};
use yap::{IntoTokens, TokenLocation, Tokens};

/// An example JSON parser. We don't handle every case (ie proper float
Expand Down Expand Up @@ -111,7 +111,9 @@ impl ErrorKind {
///
/// Try parsing each of the different types of value we know about,
/// and return the first error that we encounter, or a valid `Value`.
fn value(toks: &mut impl Tokens<Item = char>) -> Result<Value, Error> {
fn value(
toks: &mut impl Tokens<Item = char, Buffer = impl Deref<Target = str>>,
) -> Result<Value, Error> {
// Return the first thing we parse successfully from our token stream,
// mapping values into their `Value` container.
let value = yap::one_of!(ts from toks;
Expand All @@ -137,7 +139,9 @@ fn value(toks: &mut impl Tokens<Item = char>) -> Result<Value, Error> {
/// - `Some(Ok(values))` means we successfully parsed 0 or more array values.
/// - `Some(Err(e))` means that we hit an error parsing the array.
/// - `None` means that this wasn't an array and so nothing was parsed.
fn array(toks: &mut impl Tokens<Item = char>) -> Option<Result<Vec<Value>, Error>> {
fn array(
toks: &mut impl Tokens<Item = char, Buffer = impl Deref<Target = str>>,
) -> Option<Result<Vec<Value>, Error>> {
// Note the location of the start of the array.
let start = toks.location();

Expand Down Expand Up @@ -167,7 +171,9 @@ fn array(toks: &mut impl Tokens<Item = char>) -> Option<Result<Vec<Value>, Error
/// - `Some(Ok(values))` means we successfully parsed 0 or more object values.
/// - `Some(Err(e))` means that we hit an error parsing the object.
/// - `None` means that this wasn't an object and so nothing was parsed.
fn object(toks: &mut impl Tokens<Item = char>) -> Option<Result<HashMap<String, Value>, Error>> {
fn object(
toks: &mut impl Tokens<Item = char, Buffer = impl Deref<Target = str>>,
) -> Option<Result<HashMap<String, Value>, Error>> {
// Note the location of the start of the object.
let start = toks.location();

Expand Down Expand Up @@ -201,7 +207,9 @@ fn object(toks: &mut impl Tokens<Item = char>) -> Option<Result<HashMap<String,
/// - `Some(Ok((key, val)))` means we parsed a keyval field pair.
/// - `Some(Err(e))` means we hit some unrecoverable error.
/// - `None` means we parsed nothing and hit the end of the object.
fn object_field(toks: &mut impl Tokens<Item = char>) -> Option<Result<(String, Value), Error>> {
fn object_field(
toks: &mut impl Tokens<Item = char, Buffer = impl Deref<Target = str>>,
) -> Option<Result<(String, Value), Error>> {
if toks.peek() == Some('}') {
return None;
}
Expand Down Expand Up @@ -302,7 +310,9 @@ fn null(toks: &mut impl Tokens<Item = char>) -> bool {
/// over at once and parse them into a number.
///
/// A better parser could return specific errors depending on where we failed in our parsing.
fn number(toks: &mut impl Tokens<Item = char>) -> Option<Result<f64, Error>> {
fn number(
toks: &mut impl Tokens<Item = char, Buffer = impl Deref<Target = str>>,
) -> Option<Result<f64, Error>> {
let start = toks.location();

// Look for the start of a number. return None if
Expand Down Expand Up @@ -344,7 +354,7 @@ fn number(toks: &mut impl Tokens<Item = char>) -> Option<Result<f64, Error>> {

// If we get this far, we saw a valid number. Just let Rust parse it for us..
let end = toks.location();
let n_str: String = toks.slice(start, end).as_iter().collect();
let n_str = toks.get_buffer(start, end);
Some(Ok(n_str.parse().expect("valid number expected here")))
}

Expand Down
Loading
Loading