Add comments
This commit is contained in:
parent
7588f29cc9
commit
7e49c40e98
136
src/lib.rs
136
src/lib.rs
@ -1,51 +1,102 @@
|
||||
//! # Naive JSON Parser
|
||||
//!
|
||||
//! Based on [JSON Parser with JavaScript](https://lihautan.com/json-parser-with-javascript/)
|
||||
#![forbid(unsafe_code)]
|
||||
use std::collections::HashMap;
|
||||
use std::iter::FromIterator;
|
||||
|
||||
/// The type of JSON value
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum JSONValue {
|
||||
/// Object Literal
|
||||
Object(HashMap<String, JSONValue>),
|
||||
|
||||
/// Array Literal
|
||||
Array(Vec<JSONValue>),
|
||||
|
||||
/// String Literal
|
||||
String(String),
|
||||
|
||||
/// Number Literal
|
||||
Number(f64),
|
||||
|
||||
/// True Literal
|
||||
True,
|
||||
|
||||
/// False Literal
|
||||
False,
|
||||
|
||||
/// Null Literal
|
||||
Null,
|
||||
}
|
||||
|
||||
/// The type of error returned by the parser
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum ParseError {
|
||||
/// The input looks like JSON, but seems to end
|
||||
UnexpectedEndOfInput(String),
|
||||
|
||||
/// Looks like JSON, but seems to have characters after it should
|
||||
ExpectedEndOfInput(String),
|
||||
|
||||
/// Wasn't this supposed to be an object literal?
|
||||
ExpectedObjectKey(String),
|
||||
|
||||
/// Hey, wasn't there supposed to be...?
|
||||
ExpectedToken(String),
|
||||
|
||||
/// What's this character?
|
||||
UnexpectedToken(String),
|
||||
|
||||
/// Shouldn't this be a numeral?
|
||||
ExpectedDigit(String),
|
||||
|
||||
/// There's a backslash...were you going somewhere with that?
|
||||
ExpectedEscapeChar(String),
|
||||
|
||||
/// Should be a unicode escape character...missing a few digits?
|
||||
ExpectedUnicodeEscape(String),
|
||||
}
|
||||
|
||||
/// This struct holds a little state for parsing
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct JSON {
|
||||
/// The input JSON String as a character array
|
||||
chars: Vec<char>,
|
||||
i: usize
|
||||
|
||||
/// The internal parsing index
|
||||
i: usize,
|
||||
}
|
||||
|
||||
pub type JSONResult = Result<JSONValue, ParseError>;
|
||||
type PartialResult = Result<Option<JSONValue>, ParseError>;
|
||||
|
||||
impl JSON {
|
||||
/// Private constructor
|
||||
fn new(json: &str) -> Self {
|
||||
JSON {
|
||||
chars: json.chars().collect(),
|
||||
i: 0
|
||||
}
|
||||
JSON {
|
||||
chars: json.chars().collect(),
|
||||
i: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_value(&mut self) -> JSONResult {
|
||||
/// Parse a `JSONValue` from the current JSON string
|
||||
///
|
||||
/// This is probably an abuse of iterators...but it's still much better than the alternative
|
||||
/// of nested matches.
|
||||
///
|
||||
/// In order to determine the type of JSON value, each parse method is tried, until one
|
||||
/// matches, or a parse error happens.
|
||||
///
|
||||
/// * `Option`s implement IntoIterator, which returns an iterator of -1 or 1 items: the
|
||||
/// transferred (not borrowed) Some() value.
|
||||
/// * The `chain` method of iterators allows you to link iterators together,
|
||||
/// to act as one iterator
|
||||
/// * The first result from the iterator is the first parse method with a non-empty value,
|
||||
/// and should be the value wanted
|
||||
fn parse_value(&mut self) -> Result<JSONValue, ParseError> {
|
||||
self.skip_whitespace();
|
||||
|
||||
let mut types = self.parse_string()?.into_iter()
|
||||
let mut value = self
|
||||
.parse_string()?
|
||||
.into_iter()
|
||||
.chain(self.parse_number()?.into_iter())
|
||||
.chain(self.parse_object()?.into_iter())
|
||||
.chain(self.parse_array()?.into_iter())
|
||||
@ -53,13 +104,16 @@ impl JSON {
|
||||
.chain(self.parse_keyword("false", JSONValue::False)?.into_iter())
|
||||
.chain(self.parse_keyword("null", JSONValue::Null)?.into_iter());
|
||||
|
||||
match types.next() {
|
||||
match value.next() {
|
||||
Some(val) => Ok(val),
|
||||
None => Err(ParseError::UnexpectedEndOfInput(String::new()))
|
||||
None => Err(ParseError::UnexpectedEndOfInput(String::from(
|
||||
"Doesn't seem to be valid JSON",
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_object(&mut self) -> PartialResult {
|
||||
/// See if there's a `JSONValue::Object` next in the JSON
|
||||
fn parse_object(&mut self) -> Result<Option<JSONValue>, ParseError> {
|
||||
if self.chars[self.i] != '{' {
|
||||
return Ok(None);
|
||||
}
|
||||
@ -85,7 +139,7 @@ impl JSON {
|
||||
JSONValue::String(s) => s,
|
||||
_ => panic!("parse_string returned non-string value"),
|
||||
},
|
||||
None => String::new()
|
||||
None => String::new(),
|
||||
};
|
||||
|
||||
self.skip_whitespace();
|
||||
@ -103,7 +157,8 @@ impl JSON {
|
||||
Ok(Some(JSONValue::Object(result)))
|
||||
}
|
||||
|
||||
fn parse_array(&mut self) -> PartialResult {
|
||||
/// See if there's a `JSONValue::Array` next in the JSON
|
||||
fn parse_array(&mut self) -> Result<Option<JSONValue>, ParseError> {
|
||||
if self.chars[self.i] != '[' {
|
||||
return Ok(None);
|
||||
}
|
||||
@ -129,15 +184,35 @@ impl JSON {
|
||||
Ok(Some(JSONValue::Array(result)))
|
||||
}
|
||||
|
||||
fn parse_string(&mut self) -> PartialResult {
|
||||
Ok(None)
|
||||
/// See if there's a `JSONValue::String` next in the JSON
|
||||
fn parse_string(&mut self) -> Result<Option<JSONValue>, ParseError> {
|
||||
if self.chars[self.i] != '"' {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
self.i += 1;
|
||||
let mut result = String::new();
|
||||
|
||||
while self.chars[self.i] != '"' {
|
||||
if self.chars[i] == '\\' {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn parse_number(&mut self) -> PartialResult {
|
||||
Ok(None)
|
||||
/// See if there's a `JSONValue::Number` next in the JSON
|
||||
fn parse_number(&mut self) -> Result<Option<JSONValue>, ParseError> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn parse_keyword(&mut self, search: &str, value: JSONValue) -> PartialResult {
|
||||
/// See if there's a `JSONValue::True`, `JSONValue::False`, or a `JSONValue::Null` next in the JSON
|
||||
fn parse_keyword(
|
||||
&mut self,
|
||||
search: &str,
|
||||
value: JSONValue,
|
||||
) -> Result<Option<JSONValue>, ParseError> {
|
||||
let start = self.i;
|
||||
let end = if self.i + search.len() > self.chars.len() {
|
||||
self.chars.len()
|
||||
@ -147,7 +222,7 @@ impl JSON {
|
||||
|
||||
let slice = &String::from_iter(&self.chars[start..end]);
|
||||
if slice == search {
|
||||
self.i += search.len();
|
||||
self.i += search.len();
|
||||
|
||||
return Ok(Some(value));
|
||||
}
|
||||
@ -155,12 +230,18 @@ impl JSON {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Increment the internal index until the next character is not a whitespace character
|
||||
fn skip_whitespace(&mut self) {
|
||||
while self.chars[self.i].is_ascii_whitespace() {
|
||||
self.i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// 'Eat' the specified character
|
||||
///
|
||||
/// * If the next `char` matches the one passed, the internal index is incremented
|
||||
/// * If the next `char` does not match the one passed, a `ParseError::ExpectedToken`
|
||||
/// error is returned
|
||||
fn eat(&mut self, ch: char) -> Result<(), ParseError> {
|
||||
if self.chars[self.i] != ch {
|
||||
let msg = format!(r#"Expected "{}"."#, ch);
|
||||
@ -173,13 +254,11 @@ impl JSON {
|
||||
}
|
||||
|
||||
/// Convert a `&str` containing JSON into a `Result<JSONValue, ParseError>`
|
||||
pub fn parse(json: &str) -> JSONResult {
|
||||
pub fn parse(json: &str) -> Result<JSONValue, ParseError> {
|
||||
JSON::new(json).parse_value()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@ -213,6 +292,13 @@ mod tests {
|
||||
fn can_parse_array_of_keywords() {
|
||||
let result = JSON::parse("[true,false,null]");
|
||||
|
||||
assert_eq!(result, Ok(JSONValue::Array(vec![JSONValue::True, JSONValue::False, JSONValue::Null])));
|
||||
assert_eq!(
|
||||
result,
|
||||
Ok(JSONValue::Array(vec![
|
||||
JSONValue::True,
|
||||
JSONValue::False,
|
||||
JSONValue::Null
|
||||
]))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user