Fixes after review

This commit is contained in:
Pawel Wieczorek 2017-04-28 22:12:03 +02:00
parent e265fa81fe
commit 523635eacc
5 changed files with 38 additions and 36 deletions

View File

@ -1,6 +1,5 @@
use std::str::FromStr;
grammar;
pub Term = { Num, "(" <Term> ")" };

View File

@ -411,11 +411,14 @@ give you the idea:
| `<p:A> B => bar(<>)` | `<p:A> B => bar(p)` |
| `<A> <B> => bar(<>)` | `<a:A> <b:B> => bar(a, b)` |
| `<p:A> <q:B> => bar(<>)` | `<p:A> <q:B> => bar(p, q)` |
| `<p:A> B => Foo{<>}` | `<p:A> B => Foo{p:p}` |
| `<p:A> <q:B> => Foo{<>} | `<p:A> <q:B> => Foo{p:p, q:q}` |
| `<p:A> B => Foo {<>}` | `<p:A> B => Foo {p:p}` |
| `<p:A> <q:B> => Foo {<>} | `<p:A> <q:B> => Foo {p:p, q:q}` |
The funky expression also works with struct constructors
(like `Foo{...}` in examples above). In such case user had to explicitly
(like `Foo {...}` in examples above). This works out well if the
names of your parsed values match the names of your struct fields.
In such case user had to explicitly
assign names of structure's field to parsed values.

View File

@ -320,18 +320,18 @@ impl<'s> LowerState<'s> {
let action = {
match norm_util::check_funky_expression(&action) {
norm_util::FunkyExpressionPresence::None => {
match norm_util::check_between_braces(&action) {
norm_util::Presence::None => {
action
}
norm_util::FunkyExpressionPresence::Normal => {
norm_util::Presence::Normal => {
let name_str : String = intern::read(|interner| {
let name_strs: Vec<_> = names.iter().map(|&(_,name,_)| interner.data(name)).collect();
name_strs.join(", ")
});
action.replace("<>", &name_str)
}
norm_util::FunkyExpressionPresence::InCurlyBrackets => {
norm_util::Presence::InCurlyBrackets => {
let name_str = intern::read(|interner| {
let name_strs: Vec<_> = names.iter().map(|&(_,name,_)| format!("{0}:{0}", interner.data(name))).collect();
name_strs.join(", ")

View File

@ -56,20 +56,20 @@ pub fn analyze_expr<'a>(expr: &'a ExprSymbol) -> Symbols<'a> {
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum FunkyExpressionPresence
pub enum Presence
{
None,
InCurlyBrackets,
Normal
}
impl FunkyExpressionPresence {
impl Presence {
pub fn is_in_curly_brackets(&self) -> bool {
*self == FunkyExpressionPresence::InCurlyBrackets
*self == Presence::InCurlyBrackets
}
}
pub fn check_funky_expression(action: &str) -> FunkyExpressionPresence
pub fn check_between_braces(action: &str) -> Presence
{
if let Some(funky_index) = action.find("<>") {
let (before, after) = {
@ -80,12 +80,12 @@ pub fn check_funky_expression(action: &str) -> FunkyExpressionPresence
let last_before = before.chars().last();
let next_after = after.chars().next();
if let (Some('{'), Some('}')) = (last_before, next_after) {
FunkyExpressionPresence::InCurlyBrackets
Presence::InCurlyBrackets
} else {
FunkyExpressionPresence::Normal
Presence::Normal
}
} else {
FunkyExpressionPresence::None
Presence::None
}
}
@ -95,32 +95,32 @@ mod test {
#[test]
fn detecting_normal_funky_expression() {
assert_eq!(FunkyExpressionPresence::Normal, check_funky_expression("<>"));
assert_eq!(FunkyExpressionPresence::Normal, check_funky_expression("ble <> blaa"));
assert_eq!(FunkyExpressionPresence::Normal, check_funky_expression("ble <> } b"));
assert_eq!(FunkyExpressionPresence::Normal, check_funky_expression("bl{ e <> } b"));
assert_eq!(FunkyExpressionPresence::Normal, check_funky_expression("bl{ e <>} b"));
assert_eq!(FunkyExpressionPresence::Normal, check_funky_expression("bl{ e <> e } b"));
assert_eq!(FunkyExpressionPresence::Normal, check_funky_expression("bl{ <> e } b"));
assert_eq!(FunkyExpressionPresence::Normal, check_funky_expression("bl{<> e } b"));
assert_eq!(FunkyExpressionPresence::Normal, check_funky_expression("bl{<>"));
assert_eq!(FunkyExpressionPresence::Normal, check_funky_expression("<>}"));
assert_eq!(Presence::Normal, check_between_braces("<>"));
assert_eq!(Presence::Normal, check_between_braces("ble <> blaa"));
assert_eq!(Presence::Normal, check_between_braces("ble <> } b"));
assert_eq!(Presence::Normal, check_between_braces("bl{ e <> } b"));
assert_eq!(Presence::Normal, check_between_braces("bl{ e <>} b"));
assert_eq!(Presence::Normal, check_between_braces("bl{ e <> e } b"));
assert_eq!(Presence::Normal, check_between_braces("bl{ <> e } b"));
assert_eq!(Presence::Normal, check_between_braces("bl{<> e } b"));
assert_eq!(Presence::Normal, check_between_braces("bl{<>"));
assert_eq!(Presence::Normal, check_between_braces("<>}"));
}
#[test]
fn detecting_nopresence_of_funky_expression() {
assert_eq!(FunkyExpressionPresence::None, check_funky_expression("< >"));
assert_eq!(FunkyExpressionPresence::None, check_funky_expression("ble <b> blaa"));
assert_eq!(Presence::None, check_between_braces("< >"));
assert_eq!(Presence::None, check_between_braces("ble <b> blaa"));
}
#[test]
fn detecting_incurlybrackets_funky_expression() {
assert_eq!(FunkyExpressionPresence::InCurlyBrackets, check_funky_expression("{<>}"));
assert_eq!(FunkyExpressionPresence::InCurlyBrackets, check_funky_expression("ble{<> }blaa"));
assert_eq!(FunkyExpressionPresence::InCurlyBrackets, check_funky_expression("ble{ <> } b"));
assert_eq!(FunkyExpressionPresence::InCurlyBrackets, check_funky_expression("bl{ <>} b"));
assert_eq!(FunkyExpressionPresence::InCurlyBrackets, check_funky_expression("bl{<>} b"));
assert_eq!(FunkyExpressionPresence::InCurlyBrackets, check_funky_expression("bl{<> } b"));
assert_eq!(Presence::InCurlyBrackets, check_between_braces("{<>}"));
assert_eq!(Presence::InCurlyBrackets, check_between_braces("ble{<> }blaa"));
assert_eq!(Presence::InCurlyBrackets, check_between_braces("ble{ <> } b"));
assert_eq!(Presence::InCurlyBrackets, check_between_braces("bl{ <>} b"));
assert_eq!(Presence::InCurlyBrackets, check_between_braces("bl{<>} b"));
assert_eq!(Presence::InCurlyBrackets, check_between_braces("bl{<> } b"));
}
}

View File

@ -167,7 +167,7 @@ impl<'grammar> Validator<'grammar> {
sym);
}
}
Symbols::Anon(syms) => {
Symbols::Anon(_) => {
let empty_string = "".to_string();
let action = {
match alternative.action {
@ -176,10 +176,10 @@ impl<'grammar> Validator<'grammar> {
_ => &empty_string
}
};
if norm_util::check_funky_expression(action).is_in_curly_brackets() {
if norm_util::check_between_braces(action).is_in_curly_brackets() {
return_err!(
alternative.span,
"the `<>` expression requires to explicitly assign fields' names to values");
"Using `<>` between curly braces (e.g., `{{<>}}`) only works when your parsed values have been given names (e.g., `<x:Foo>`, not just `<Foo>`)");
}
}
}