FEATURE: Allow Quoted strings as a field name in Tuples.

Also add tuple specific regression tests.
This commit is contained in:
Jeremy Wall 2018-08-24 20:03:15 -05:00
parent 4795945caf
commit 6bcf822c41
5 changed files with 33 additions and 5 deletions

View File

@ -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.

View File

@ -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|;

View File

@ -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"));

View File

@ -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.
//!

View File

@ -273,7 +273,7 @@ named!(boolean_value<TokenIter, Value, error::Error>,
named!(
field_value<TokenIter, (Token, Expression), error::Error>,
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<SelectorList> {
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) => {