From 6bcf822c4132fc8d10e03f776b63443ad2fcf025 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Fri, 24 Aug 2018 20:03:15 -0500 Subject: [PATCH] FEATURE: Allow Quoted strings as a field name in Tuples. Also add tuple specific regression tests. --- TODO.md | 1 - integration_tests/tuple_test.ucg | 24 ++++++++++++++++++++++++ src/build/compile_test.rs | 5 +++++ src/lib.rs | 4 ++-- src/parse/mod.rs | 4 ++-- 5 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 integration_tests/tuple_test.ucg diff --git a/TODO.md b/TODO.md index 1320243..9d68e1a 100644 --- a/TODO.md +++ b/TODO.md @@ -14,7 +14,6 @@ compiled configuration. # Minor Fixes and Polish * Compiler caching (interface has been defined) -* Strings as tuple fields? * Streaming Parsing? * Casting between types? * Better error messages. diff --git a/integration_tests/tuple_test.ucg b/integration_tests/tuple_test.ucg new file mode 100644 index 0000000..5b14114 --- /dev/null +++ b/integration_tests/tuple_test.ucg @@ -0,0 +1,24 @@ +let simpletpl = { + foo = "bar" +}; + +let stringfieldtpl = { + "field 1" = 1, +}; + +let nestedtpl = { + inner = { + field = "value", + }, + scalar = 1, + list = [1, 2, 3, 4], +}; + +assert |simpletpl.foo == "bar"|; +assert |stringfieldtpl."field 1" == 1|; +assert |nestedtpl.scalar == 1|; +assert |nestedtpl.inner.field == "value"|; +assert |nestedtpl.list.0 == 1|; +assert |nestedtpl.list.1 == 2|; +assert |nestedtpl.list.2 == 3|; +assert |nestedtpl.list.3 == 4|; \ No newline at end of file diff --git a/src/build/compile_test.rs b/src/build/compile_test.rs index 00495c0..146ab43 100644 --- a/src/build/compile_test.rs +++ b/src/build/compile_test.rs @@ -28,6 +28,11 @@ fn assert_build(input: &str) { } } +#[test] +fn test_tuples() { + assert_build(include_str!("../../integration_tests/tuple_test.ucg")); +} + #[test] fn test_comparisons() { assert_build(include_str!("../../integration_tests/comparisons_test.ucg")); diff --git a/src/lib.rs b/src/lib.rs index 0f5b0bc..089a09b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -139,7 +139,7 @@ //! //! Tuple's are an ordered collection of name, value pairs. They are bounded by curly braces `{ }` //! and contain name = value pairs separated by commas. Trailing commas are permitted. The name must -//! be a bareword without quotes. +//! be either a bareword without quotes or a quoted string. //! //! ```ucg //! let mytuple = { @@ -160,7 +160,7 @@ //! //! Selectors are references to a bound value in ucg. They can index arbitrarily deep into either tuples or lists. //! The head of a selector can be a tuple or list or symbol. Optionally a selector can also be followed by either a -//! bareword to index a tuple field or an integer to index a list position. +//! bareword or string to index a tuple field or an integer to index a list position. //! //! The simplest selector is just a reference to a bound value by name. //! diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 8e99d39..0a68737 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -273,7 +273,7 @@ named!(boolean_value, named!( field_value, do_parse!( - field: match_type!(BAREWORD) >> + field: alt!(match_type!(BAREWORD) | match_type!(STR)) >> punct!("=") >> value: expression >> (field, value) @@ -442,7 +442,7 @@ fn selector_list(input: TokenIter) -> NomResult { let (rest, list) = match separated_list!( rest, punct!("."), - alt!(match_type!(BAREWORD) | match_type!(DIGIT)) + alt!(match_type!(BAREWORD) | match_type!(DIGIT) | match_type!(STR)) ) { IResult::Done(rest, val) => (rest, val), IResult::Incomplete(i) => {