Use mentat-parser-utils in tx-parser. Fixes #235; r=rnewman,victorporof
Move macros query-parser/…/parser_utils.rs → parser-utils/…/query.rs Signed-off-by: Joe Walker <jwalker@mozilla.com>
This commit is contained in:
parent
1deed24f42
commit
f591c90738
6 changed files with 182 additions and 238 deletions
|
@ -5,3 +5,6 @@ authors = ["Victor Porof <vporof@mozilla.com>", "Richard Newman <rnewman@mozilla
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
combine = "2.1.1"
|
combine = "2.1.1"
|
||||||
|
|
||||||
|
[dependencies.edn]
|
||||||
|
path = "../edn"
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
// specific language governing permissions and limitations under the License.
|
// specific language governing permissions and limitations under the License.
|
||||||
|
|
||||||
extern crate combine;
|
extern crate combine;
|
||||||
|
extern crate edn;
|
||||||
|
|
||||||
use combine::ParseResult;
|
use combine::ParseResult;
|
||||||
use combine::combinator::{Expected, FnParser};
|
use combine::combinator::{Expected, FnParser};
|
||||||
|
@ -39,3 +40,68 @@ macro_rules! satisfy_unwrap {
|
||||||
satisfy_map(|x: edn::Value| if let $cas($var) = x $body else { None })
|
satisfy_map(|x: edn::Value| if let $cas($var) = x $body else { None })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generate a `satisfy_map` expression that matches a `PlainSymbol`
|
||||||
|
/// value with the given name.
|
||||||
|
///
|
||||||
|
/// We do this rather than using `combine::token` so that we don't
|
||||||
|
/// need to allocate a new `String` inside a `PlainSymbol` inside a `Value`
|
||||||
|
/// just to match input.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! matches_plain_symbol {
|
||||||
|
($name: expr, $input: ident) => {
|
||||||
|
satisfy_map(|x: edn::Value| {
|
||||||
|
if let edn::Value::PlainSymbol(ref s) = x {
|
||||||
|
if s.0.as_str() == $name {
|
||||||
|
return Some(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return None;
|
||||||
|
}).parse_stream($input)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Define an `impl` body for the `$parser` type. The body will contain a parser
|
||||||
|
/// function called `$name`, consuming a stream of `$item_type`s. The parser's
|
||||||
|
/// result type will be `$result_type`.
|
||||||
|
///
|
||||||
|
/// The provided `$body` will be evaluated with `$input` bound to the input stream.
|
||||||
|
///
|
||||||
|
/// `$body`, when run, should return a `ParseResult` of the appropriate result type.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! def_parser_fn {
|
||||||
|
( $parser: ident, $name: ident, $item_type: ty, $result_type: ty, $input: ident, $body: block ) => {
|
||||||
|
impl<I> $parser<I> where I: Stream<Item = $item_type> {
|
||||||
|
fn $name() -> ResultParser<$result_type, I> {
|
||||||
|
fn inner<I: Stream<Item = $item_type>>($input: I) -> ParseResult<$result_type, I> {
|
||||||
|
$body
|
||||||
|
}
|
||||||
|
parser(inner as fn(I) -> ParseResult<$result_type, I>).expected("$name")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `def_value_parser_fn` is a short-cut to `def_parser_fn` with the input type
|
||||||
|
/// being `edn::Value`.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! def_value_parser_fn {
|
||||||
|
( $parser: ident, $name: ident, $result_type: ty, $input: ident, $body: block ) => {
|
||||||
|
def_parser_fn!($parser, $name, edn::Value, $result_type, $input, $body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `def_value_satisfy_parser_fn` is a short-cut to `def_parser_fn` with the input type
|
||||||
|
/// being `edn::Value` and the body being a call to `satisfy_map` with the given transformer.
|
||||||
|
///
|
||||||
|
/// In practice this allows you to simply pass a function that accepts an `&edn::Value` and
|
||||||
|
/// returns an `Option<$result_type>`: if a suitable value is at the front of the stream,
|
||||||
|
/// it will be converted and returned by the parser; otherwise, the parse will fail.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! def_value_satisfy_parser_fn {
|
||||||
|
( $parser: ident, $name: ident, $result_type: ty, $transformer: path ) => {
|
||||||
|
def_value_parser_fn!($parser, $name, $result_type, input, {
|
||||||
|
satisfy_map(|x: edn::Value| $transformer(&x)).parse_stream(input)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -14,8 +14,6 @@ extern crate matches;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate mentat_parser_utils;
|
extern crate mentat_parser_utils;
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
mod parser_util;
|
|
||||||
mod util;
|
mod util;
|
||||||
mod parse;
|
mod parse;
|
||||||
pub mod find;
|
pub mod find;
|
||||||
|
|
|
@ -1,76 +0,0 @@
|
||||||
// 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 mentat_parser_utils;
|
|
||||||
extern crate mentat_query;
|
|
||||||
|
|
||||||
/// Generate a `satisfy_map` expression that matches a `PlainSymbol`
|
|
||||||
/// value with the given name.
|
|
||||||
///
|
|
||||||
/// We do this rather than using `combine::token` so that we don't
|
|
||||||
/// need to allocate a new `String` inside a `PlainSymbol` inside a `Value`
|
|
||||||
/// just to match input.
|
|
||||||
macro_rules! matches_plain_symbol {
|
|
||||||
($name: expr, $input: ident) => {
|
|
||||||
satisfy_map(|x: edn::Value| {
|
|
||||||
if let edn::Value::PlainSymbol(ref s) = x {
|
|
||||||
if s.0.as_str() == $name {
|
|
||||||
return Some(());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return None;
|
|
||||||
}).parse_stream($input)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Define an `impl` body for the `$parser` type. The body will contain a parser
|
|
||||||
/// function called `$name`, consuming a stream of `$item_type`s. The parser's
|
|
||||||
/// result type will be `$result_type`.
|
|
||||||
///
|
|
||||||
/// The provided `$body` will be evaluated with `$input` bound to the input stream.
|
|
||||||
///
|
|
||||||
/// `$body`, when run, should return a `ParseResult` of the appropriate result type.
|
|
||||||
macro_rules! def_parser_fn {
|
|
||||||
( $parser: ident, $name: ident, $item_type: ty, $result_type: ty, $input: ident, $body: block ) => {
|
|
||||||
impl<I> $parser<I> where I: Stream<Item = $item_type> {
|
|
||||||
fn $name() -> ResultParser<$result_type, I> {
|
|
||||||
fn inner<I: Stream<Item = $item_type>>($input: I) -> ParseResult<$result_type, I> {
|
|
||||||
$body
|
|
||||||
}
|
|
||||||
parser(inner as fn(I) -> ParseResult<$result_type, I>).expected("$name")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `def_value_parser_fn` is a short-cut to `def_parser_fn` with the input type
|
|
||||||
/// being `edn::Value`.
|
|
||||||
macro_rules! def_value_parser_fn {
|
|
||||||
( $parser: ident, $name: ident, $result_type: ty, $input: ident, $body: block ) => {
|
|
||||||
def_parser_fn!($parser, $name, edn::Value, $result_type, $input, $body);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `def_value_satisfy_parser_fn` is a short-cut to `def_parser_fn` with the input type
|
|
||||||
/// being `edn::Value` and the body being a call to `satisfy_map` with the given transformer.
|
|
||||||
///
|
|
||||||
/// In practice this allows you to simply pass a function that accepts an `&edn::Value` and
|
|
||||||
/// returns an `Option<$result_type>`: if a suitable value is at the front of the stream,
|
|
||||||
/// it will be converted and returned by the parser; otherwise, the parse will fail.
|
|
||||||
macro_rules! def_value_satisfy_parser_fn {
|
|
||||||
( $parser: ident, $name: ident, $result_type: ty, $transformer: path ) => {
|
|
||||||
def_value_parser_fn!($parser, $name, $result_type, input, {
|
|
||||||
satisfy_map(|x: edn::Value| $transformer(&x)).parse_stream(input)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -10,3 +10,6 @@ combine = "2.1.1"
|
||||||
|
|
||||||
[dependencies.mentat_tx]
|
[dependencies.mentat_tx]
|
||||||
path = "../tx"
|
path = "../tx"
|
||||||
|
|
||||||
|
[dependencies.mentat_parser_utils]
|
||||||
|
path = "../parser-utils"
|
||||||
|
|
|
@ -14,11 +14,15 @@ extern crate edn;
|
||||||
extern crate combine;
|
extern crate combine;
|
||||||
extern crate mentat_tx;
|
extern crate mentat_tx;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate mentat_parser_utils;
|
||||||
|
|
||||||
use combine::{any, eof, many, parser, satisfy_map, token, Parser, ParseResult, Stream};
|
use combine::{any, eof, many, parser, satisfy_map, token, Parser, ParseResult, Stream};
|
||||||
use combine::combinator::{Expected, FnParser};
|
use combine::combinator::{Expected, FnParser};
|
||||||
use edn::symbols::NamespacedKeyword;
|
use edn::symbols::NamespacedKeyword;
|
||||||
use edn::types::Value;
|
use edn::types::Value;
|
||||||
use mentat_tx::entities::{Entid, EntidOrLookupRef, Entity, LookupRef, OpType, ValueOrLookupRef};
|
use mentat_tx::entities::{Entid, EntidOrLookupRef, Entity, LookupRef, OpType, ValueOrLookupRef};
|
||||||
|
use mentat_parser_utils::ResultParser;
|
||||||
|
|
||||||
pub struct Tx<I>(::std::marker::PhantomData<fn(I) -> I>);
|
pub struct Tx<I>(::std::marker::PhantomData<fn(I) -> I>);
|
||||||
|
|
||||||
|
@ -30,54 +34,23 @@ fn fn_parser<O, I>(f: fn(I) -> ParseResult<O, I>, err: &'static str) -> TxParser
|
||||||
parser(f).expected(err)
|
parser(f).expected(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I> Tx<I>
|
def_value_satisfy_parser_fn!(Tx, integer, i64, Value::as_integer);
|
||||||
where I: Stream<Item = Value>
|
|
||||||
{
|
|
||||||
fn integer() -> TxParser<i64, I> {
|
|
||||||
fn_parser(Tx::<I>::integer_, "integer")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn integer_(input: I) -> ParseResult<i64, I> {
|
fn value_to_namespaced_keyword(val: &Value) -> Option<NamespacedKeyword> {
|
||||||
return satisfy_map(|x: Value| if let Value::Integer(y) = x {
|
val.as_namespaced_keyword().map(|x| x.clone())
|
||||||
Some(y)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
})
|
|
||||||
.parse_stream(input);
|
|
||||||
}
|
}
|
||||||
|
def_value_satisfy_parser_fn!(Tx, keyword, NamespacedKeyword, value_to_namespaced_keyword);
|
||||||
|
|
||||||
fn keyword() -> TxParser<NamespacedKeyword, I> {
|
def_parser_fn!(Tx, entid, Value, Entid, input, {
|
||||||
fn_parser(Tx::<I>::keyword_, "keyword")
|
Tx::<I>::integer()
|
||||||
}
|
|
||||||
|
|
||||||
fn keyword_(input: I) -> ParseResult<NamespacedKeyword, I> {
|
|
||||||
return satisfy_map(|x: Value| if let Value::NamespacedKeyword(y) = x {
|
|
||||||
Some(y)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
})
|
|
||||||
.parse_stream(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn entid() -> TxParser<Entid, I> {
|
|
||||||
fn_parser(Tx::<I>::entid_, "entid")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn entid_(input: I) -> ParseResult<Entid, I> {
|
|
||||||
let p = Tx::<I>::integer()
|
|
||||||
.map(|x| Entid::Entid(x))
|
.map(|x| Entid::Entid(x))
|
||||||
.or(Tx::<I>::keyword().map(|x| Entid::Ident(x)))
|
.or(Tx::<I>::keyword().map(|x| Entid::Ident(x)))
|
||||||
.parse_lazy(input)
|
.parse_lazy(input)
|
||||||
.into();
|
.into()
|
||||||
return p;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
fn lookup_ref() -> TxParser<LookupRef, I> {
|
def_parser_fn!(Tx, lookup_ref, Value, LookupRef, input, {
|
||||||
fn_parser(Tx::<I>::lookup_ref_, "lookup-ref")
|
satisfy_map(|x: Value| if let Value::Vector(y) = x {
|
||||||
}
|
|
||||||
|
|
||||||
fn lookup_ref_(input: I) -> ParseResult<LookupRef, I> {
|
|
||||||
return satisfy_map(|x: Value| if let Value::Vector(y) = x {
|
|
||||||
let mut p = (Tx::<&[Value]>::entid(), any(), eof())
|
let mut p = (Tx::<&[Value]>::entid(), any(), eof())
|
||||||
.map(|(a, v, _)| LookupRef { a: a, v: v });
|
.map(|(a, v, _)| LookupRef { a: a, v: v });
|
||||||
let r = p.parse_lazy(&y[..]).into();
|
let r = p.parse_lazy(&y[..]).into();
|
||||||
|
@ -88,28 +61,22 @@ impl<I> Tx<I>
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
.parse_stream(input);
|
.parse_stream(input)
|
||||||
}
|
});
|
||||||
|
|
||||||
fn entid_or_lookup_ref() -> TxParser<EntidOrLookupRef, I> {
|
def_parser_fn!(Tx, entid_or_lookup_ref, Value, EntidOrLookupRef, input, {
|
||||||
fn_parser(Tx::<I>::entid_or_lookup_ref_, "entid|lookup-ref")
|
Tx::<I>::entid()
|
||||||
}
|
|
||||||
|
|
||||||
fn entid_or_lookup_ref_(input: I) -> ParseResult<EntidOrLookupRef, I> {
|
|
||||||
let p = Tx::<I>::entid()
|
|
||||||
.map(|x| EntidOrLookupRef::Entid(x))
|
.map(|x| EntidOrLookupRef::Entid(x))
|
||||||
.or(Tx::<I>::lookup_ref().map(|x| EntidOrLookupRef::LookupRef(x)))
|
.or(Tx::<I>::lookup_ref().map(|x| EntidOrLookupRef::LookupRef(x)))
|
||||||
.parse_lazy(input)
|
.parse_lazy(input)
|
||||||
.into();
|
.into()
|
||||||
return p;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: abstract the "match Vector, parse internal stream" pattern to remove this boilerplate.
|
// TODO: abstract the "match Vector, parse internal stream" pattern to remove this boilerplate.
|
||||||
fn add_(input: I) -> ParseResult<Entity, I> {
|
def_parser_fn!(Tx, add, Value, Entity, input, {
|
||||||
return satisfy_map(|x: Value| -> Option<Entity> {
|
satisfy_map(|x: Value| -> Option<Entity> {
|
||||||
if let Value::Vector(y) = x {
|
if let Value::Vector(y) = x {
|
||||||
let mut p = (token(Value::NamespacedKeyword(NamespacedKeyword::new("db",
|
let mut p = (token(Value::NamespacedKeyword(NamespacedKeyword::new("db", "add"))),
|
||||||
"add"))),
|
|
||||||
Tx::<&[Value]>::entid_or_lookup_ref(),
|
Tx::<&[Value]>::entid_or_lookup_ref(),
|
||||||
Tx::<&[Value]>::entid(),
|
Tx::<&[Value]>::entid(),
|
||||||
// TODO: handle lookup-ref.
|
// TODO: handle lookup-ref.
|
||||||
|
@ -132,18 +99,13 @@ impl<I> Tx<I>
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.parse_stream(input);
|
.parse_stream(input)
|
||||||
}
|
});
|
||||||
|
|
||||||
fn add() -> TxParser<Entity, I> {
|
def_parser_fn!(Tx, retract, Value, Entity, input, {
|
||||||
fn_parser(Tx::<I>::add_, "[:db/add e a v]")
|
satisfy_map(|x: Value| -> Option<Entity> {
|
||||||
}
|
|
||||||
|
|
||||||
fn retract_(input: I) -> ParseResult<Entity, I> {
|
|
||||||
return satisfy_map(|x: Value| -> Option<Entity> {
|
|
||||||
if let Value::Vector(y) = x {
|
if let Value::Vector(y) = x {
|
||||||
let mut p = (token(Value::NamespacedKeyword(NamespacedKeyword::new("db",
|
let mut p = (token(Value::NamespacedKeyword(NamespacedKeyword::new("db", "retract"))),
|
||||||
"retract"))),
|
|
||||||
Tx::<&[Value]>::entid_or_lookup_ref(),
|
Tx::<&[Value]>::entid_or_lookup_ref(),
|
||||||
Tx::<&[Value]>::entid(),
|
Tx::<&[Value]>::entid(),
|
||||||
// TODO: handle lookup-ref.
|
// TODO: handle lookup-ref.
|
||||||
|
@ -166,26 +128,17 @@ impl<I> Tx<I>
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.parse_stream(input);
|
.parse_stream(input)
|
||||||
}
|
});
|
||||||
|
|
||||||
fn retract() -> TxParser<Entity, I> {
|
def_parser_fn!(Tx, entity, Value, Entity, input, {
|
||||||
fn_parser(Tx::<I>::retract_, "[:db/retract e a v]")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn entity_(input: I) -> ParseResult<Entity, I> {
|
|
||||||
let mut p = Tx::<I>::add()
|
let mut p = Tx::<I>::add()
|
||||||
.or(Tx::<I>::retract());
|
.or(Tx::<I>::retract());
|
||||||
p.parse_stream(input)
|
p.parse_stream(input)
|
||||||
}
|
});
|
||||||
|
|
||||||
fn entity() -> TxParser<Entity, I> {
|
def_parser_fn!(Tx, entities, Value, Vec<Entity>, input, {
|
||||||
fn_parser(Tx::<I>::entity_,
|
satisfy_map(|x: Value| -> Option<Vec<Entity>> {
|
||||||
"[:db/add|:db/retract ...]")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn entities_(input: I) -> ParseResult<Vec<Entity>, I> {
|
|
||||||
return satisfy_map(|x: Value| -> Option<Vec<Entity>> {
|
|
||||||
if let Value::Vector(y) = x {
|
if let Value::Vector(y) = x {
|
||||||
let mut p = (many(Tx::<&[Value]>::entity()), eof()).map(|(es, _)| es);
|
let mut p = (many(Tx::<&[Value]>::entity()), eof()).map(|(es, _)| es);
|
||||||
// TODO: use ok() with a type annotation rather than explicit match.
|
// TODO: use ok() with a type annotation rather than explicit match.
|
||||||
|
@ -197,14 +150,12 @@ impl<I> Tx<I>
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.parse_stream(input);
|
.parse_stream(input)
|
||||||
}
|
});
|
||||||
|
|
||||||
fn entities() -> TxParser<Vec<Entity>, I> {
|
|
||||||
fn_parser(Tx::<I>::entities_,
|
|
||||||
"[[:db/add|:db/retract ...]*]")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
impl<I> Tx<I>
|
||||||
|
where I: Stream<Item = Value>
|
||||||
|
{
|
||||||
pub fn parse(input: I) -> Result<Vec<Entity>, combine::ParseError<I>> {
|
pub fn parse(input: I) -> Result<Vec<Entity>, combine::ParseError<I>> {
|
||||||
(Tx::<I>::entities(), eof())
|
(Tx::<I>::entities(), eof())
|
||||||
.map(|(es, _)| es)
|
.map(|(es, _)| es)
|
||||||
|
@ -219,7 +170,6 @@ mod tests {
|
||||||
use combine::Parser;
|
use combine::Parser;
|
||||||
use edn::symbols::NamespacedKeyword;
|
use edn::symbols::NamespacedKeyword;
|
||||||
use edn::types::Value;
|
use edn::types::Value;
|
||||||
use mentat_tx::entities::*;
|
|
||||||
|
|
||||||
fn kw(namespace: &str, name: &str) -> Value {
|
fn kw(namespace: &str, name: &str) -> Value {
|
||||||
Value::NamespacedKeyword(NamespacedKeyword::new(namespace, name))
|
Value::NamespacedKeyword(NamespacedKeyword::new(namespace, name))
|
||||||
|
|
Loading…
Reference in a new issue