Add comments

This commit is contained in:
Timothy Warren 2020-02-07 21:27:46 -05:00
parent 7588f29cc9
commit 7e49c40e98

View File

@ -1,51 +1,102 @@
//! # Naive JSON Parser //! # 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::collections::HashMap;
use std::iter::FromIterator; use std::iter::FromIterator;
/// The type of JSON value
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum JSONValue { pub enum JSONValue {
/// Object Literal
Object(HashMap<String, JSONValue>), Object(HashMap<String, JSONValue>),
/// Array Literal
Array(Vec<JSONValue>), Array(Vec<JSONValue>),
/// String Literal
String(String), String(String),
/// Number Literal
Number(f64), Number(f64),
/// True Literal
True, True,
/// False Literal
False, False,
/// Null Literal
Null, Null,
} }
/// The type of error returned by the parser
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum ParseError { pub enum ParseError {
/// The input looks like JSON, but seems to end
UnexpectedEndOfInput(String), UnexpectedEndOfInput(String),
/// Looks like JSON, but seems to have characters after it should
ExpectedEndOfInput(String), ExpectedEndOfInput(String),
/// Wasn't this supposed to be an object literal?
ExpectedObjectKey(String), ExpectedObjectKey(String),
/// Hey, wasn't there supposed to be...?
ExpectedToken(String), ExpectedToken(String),
/// What's this character?
UnexpectedToken(String), UnexpectedToken(String),
/// Shouldn't this be a numeral?
ExpectedDigit(String), ExpectedDigit(String),
/// There's a backslash...were you going somewhere with that?
ExpectedEscapeChar(String), ExpectedEscapeChar(String),
/// Should be a unicode escape character...missing a few digits?
ExpectedUnicodeEscape(String), ExpectedUnicodeEscape(String),
} }
/// This struct holds a little state for parsing
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct JSON { pub struct JSON {
/// The input JSON String as a character array
chars: Vec<char>, 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 { impl JSON {
/// Private constructor
fn new(json: &str) -> Self { fn new(json: &str) -> Self {
JSON { JSON {
chars: json.chars().collect(), chars: json.chars().collect(),
i: 0 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(); 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_number()?.into_iter())
.chain(self.parse_object()?.into_iter()) .chain(self.parse_object()?.into_iter())
.chain(self.parse_array()?.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("false", JSONValue::False)?.into_iter())
.chain(self.parse_keyword("null", JSONValue::Null)?.into_iter()); .chain(self.parse_keyword("null", JSONValue::Null)?.into_iter());
match types.next() { match value.next() {
Some(val) => Ok(val), 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] != '{' { if self.chars[self.i] != '{' {
return Ok(None); return Ok(None);
} }
@ -85,7 +139,7 @@ impl JSON {
JSONValue::String(s) => s, JSONValue::String(s) => s,
_ => panic!("parse_string returned non-string value"), _ => panic!("parse_string returned non-string value"),
}, },
None => String::new() None => String::new(),
}; };
self.skip_whitespace(); self.skip_whitespace();
@ -103,7 +157,8 @@ impl JSON {
Ok(Some(JSONValue::Object(result))) 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] != '[' { if self.chars[self.i] != '[' {
return Ok(None); return Ok(None);
} }
@ -129,15 +184,35 @@ impl JSON {
Ok(Some(JSONValue::Array(result))) Ok(Some(JSONValue::Array(result)))
} }
fn parse_string(&mut self) -> PartialResult { /// See if there's a `JSONValue::String` next in the JSON
Ok(None) 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 { /// See if there's a `JSONValue::Number` next in the JSON
Ok(None) 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 start = self.i;
let end = if self.i + search.len() > self.chars.len() { let end = if self.i + search.len() > self.chars.len() {
self.chars.len() self.chars.len()
@ -147,7 +222,7 @@ impl JSON {
let slice = &String::from_iter(&self.chars[start..end]); let slice = &String::from_iter(&self.chars[start..end]);
if slice == search { if slice == search {
self.i += search.len(); self.i += search.len();
return Ok(Some(value)); return Ok(Some(value));
} }
@ -155,12 +230,18 @@ impl JSON {
Ok(None) Ok(None)
} }
/// Increment the internal index until the next character is not a whitespace character
fn skip_whitespace(&mut self) { fn skip_whitespace(&mut self) {
while self.chars[self.i].is_ascii_whitespace() { while self.chars[self.i].is_ascii_whitespace() {
self.i += 1; 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> { fn eat(&mut self, ch: char) -> Result<(), ParseError> {
if self.chars[self.i] != ch { if self.chars[self.i] != ch {
let msg = format!(r#"Expected "{}"."#, ch); let msg = format!(r#"Expected "{}"."#, ch);
@ -173,13 +254,11 @@ impl JSON {
} }
/// Convert a `&str` containing JSON into a `Result<JSONValue, ParseError>` /// 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() JSON::new(json).parse_value()
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -213,6 +292,13 @@ mod tests {
fn can_parse_array_of_keywords() { fn can_parse_array_of_keywords() {
let result = JSON::parse("[true,false,null]"); 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
]))
);
} }
} }