d1ac752de6
This is a big commit, but it breaks into two conceptual pieces. The first is to "parse without copying". We replace a stream of an owned collection of edn::ValueAndSpan and instead have a stream of a borrowed collection of &edn::ValueAndSpan references. (Generally, this is represented as an iterator over a slice, but it can be over other things too.) Cloning such iterators is constant time, which improves on cloning an owned collection of edn::ValueAndSpan, which is linear time in the length of the collection and additional time depending on the complexity of the EDN values. The second conceptual piece is to parse keyword maps using a special parser and a macro to build the parser implementations. Before, we created a new edn::ValueAndSpan::Map to represent a keyword map in vector form; since we're working with &edn::ValueAndSpan references now, we can't create an &edn::ValueAndSpan reference with an appropriate lifetime. Therefore we generalize the concept of iteration slightly and turn keyword maps in map form into linear iterators by flattening the value maps. This is a potentially obscuring transformation, so we have to take care to protect against some failure cases. (See the comments and the tests in the code.) After these changes, parsing using `combine` is linear time (and reasonably fast).
77 lines
2.6 KiB
Rust
77 lines
2.6 KiB
Rust
// Copyright 2016 Mozilla
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
// this file except in compliance with the License. You may obtain a copy of the
|
|
// License at http://www.apache.org/licenses/LICENSE-2.0
|
|
// Unless required by applicable law or agreed to in writing, software distributed
|
|
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
// specific language governing permissions and limitations under the License.
|
|
|
|
extern crate combine;
|
|
extern crate edn;
|
|
extern crate itertools;
|
|
|
|
/// A `ValueParseError` is a `combine::primitives::ParseError`-alike that implements the `Debug`,
|
|
/// `Display`, and `std::error::Error` traits. In addition, it doesn't capture references, making
|
|
/// it possible to store `ValueParseError` instances in local links with the `error-chain` crate.
|
|
///
|
|
/// This is achieved by wrapping slices of type `&'a [edn::Value]` in an owning type that implements
|
|
/// `Display`; rather than introducing a newtype like `DisplayVec`, we re-use `edn::Value::Vector`.
|
|
#[derive(PartialEq)]
|
|
pub struct ValueParseError {
|
|
pub position: edn::Span,
|
|
// Think of this as `Vec<Error<edn::Value, DisplayVec<edn::Value>>>`; see above.
|
|
pub errors: Vec<combine::primitives::Error<edn::ValueAndSpan, edn::ValueAndSpan>>,
|
|
}
|
|
|
|
#[macro_use]
|
|
pub mod macros;
|
|
|
|
pub use macros::{
|
|
KeywordMapParser,
|
|
ResultParser,
|
|
};
|
|
|
|
pub mod log;
|
|
pub mod value_and_span;
|
|
pub use value_and_span::{
|
|
Stream,
|
|
};
|
|
|
|
pub use log::{
|
|
LogParsing,
|
|
};
|
|
|
|
impl std::fmt::Debug for ValueParseError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
write!(f,
|
|
"ParseError {{ position: {:?}, errors: {:?} }}",
|
|
self.position,
|
|
self.errors)
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for ValueParseError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
try!(writeln!(f, "Parse error at {:?}", self.position));
|
|
combine::primitives::Error::fmt_errors(&self.errors, f)
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for ValueParseError {
|
|
fn description(&self) -> &str {
|
|
"parse error parsing EDN values"
|
|
}
|
|
}
|
|
|
|
impl<'a> From<combine::primitives::ParseError<Stream<'a>>> for ValueParseError {
|
|
fn from(e: combine::primitives::ParseError<Stream<'a>>) -> ValueParseError {
|
|
ValueParseError {
|
|
position: e.position.0,
|
|
errors: e.errors.into_iter()
|
|
.map(|e| e.map_token(|t| t.clone()).map_range(|r| r.clone()))
|
|
.collect(),
|
|
}
|
|
}
|
|
}
|