FEATURE: Allow trailing commas in the tuple, copy, and list syntax

This commit is contained in:
Jeremy Wall 2018-05-28 08:08:36 -05:00
parent 40494fb90e
commit aa8d2cab29
4 changed files with 54 additions and 4 deletions

11
TODO.md
View File

@ -22,13 +22,18 @@ Some options here could be:
* DSL's * DSL's
* Annotations * Annotations
## Built In testing as a part of the language
* A DSL for assertions.
* A DSL for identifying tests.
* A way to run tests only during validation stage.
# Minor Fixes and Polish # Minor Fixes and Polish
* Strings as tuple fields?
* Streaming Parsing * Streaming Parsing
* Casting between types? * Casting between types?
* Better error messages. * Better error messages.
* Allow trailing commas.
* Flags should allow different seperators for prefixed flags. * Flags should allow different seperators for prefixed flags.
* YAML export * YAML export
* HCL export * HCL export
* Integration Testing framework

View File

@ -115,7 +115,8 @@
//! //!
//! #### Lists //! #### Lists
//! //!
//! Lists are surrounded with square brackets `[ ]` and have comma separated elements. //! Lists are surrounded with square brackets `[ ]` and have comma separated elements. Trailing
//! commas are permitted in lists.
//! //!
//! ```ucg //! ```ucg
//! [1, 2, 3]; // A simple list of numbers. //! [1, 2, 3]; // A simple list of numbers.

View File

@ -180,6 +180,7 @@ named!(
pos: pos >> pos: pos >>
punct!("{") >> punct!("{") >>
v: field_list >> v: field_list >>
opt_res!(punct!(",")) >> // nom's opt! macro doesn't preserve error types properly but this one does.
punct!("}") >> punct!("}") >>
(pos, Some(v)) (pos, Some(v))
), ),
@ -199,6 +200,7 @@ named!(list_value<TokenIter, Value, error::Error>,
do_parse!( do_parse!(
start: punct!("[") >> start: punct!("[") >>
elements: separated_list!(punct!(","), expression) >> elements: separated_list!(punct!(","), expression) >>
opt_res!(punct!(",")) >> // nom's opt! macro doesn't preserve error types properly but this one does.
punct!("]") >> punct!("]") >>
(start.pos, elements) (start.pos, elements)
), ),
@ -497,6 +499,7 @@ named!(copy_expression<TokenIter, Expression, error::Error>,
selector: selector_list >> selector: selector_list >>
punct!("{") >> punct!("{") >>
fields: field_list >> fields: field_list >>
opt_res!(punct!(",")) >> // noms opt! macro does not preserve error types properly but this one does.
punct!("}") >> punct!("}") >>
(SelectorDef::new(selector, pos.line, pos.column as usize), fields) (SelectorDef::new(selector, pos.line, pos.column as usize), fields)
), ),

View File

@ -768,6 +768,19 @@ fn test_copy_parse() {
pos: Position::new(1, 1), pos: Position::new(1, 1),
}) })
); );
assert_parse!(
copy_expression("foo{bar=1,}"),
Expression::Copy(CopyDef {
selector: make_selector!(make_expr!("foo", 1, 1), 1, 1),
fields: vec![
(
make_tok!("bar", 1, 5),
Expression::Simple(Value::Int(value_node!(1, 1, 9))),
),
],
pos: Position::new(1, 1),
})
);
} }
#[test] #[test]
@ -822,6 +835,17 @@ fn test_list_value_parse() {
}) })
); );
assert_parse!(
list_value("[1, 1,]"),
Value::List(ListDef {
elems: vec![
Expression::Simple(Value::Int(value_node!(1, 1, 2))),
Expression::Simple(Value::Int(value_node!(1, 1, 5))),
],
pos: Position::new(1, 1),
})
);
assert_parse!( assert_parse!(
list_value("// comment\n[1, 1]"), list_value("// comment\n[1, 1]"),
Value::List(ListDef { Value::List(ListDef {
@ -992,6 +1016,23 @@ fn test_tuple_parse() {
1 1
)) ))
); );
assert_parse!(
tuple("{ foo = 1, bar = {}, }"),
Value::Tuple(value_node!(
vec![
(
make_tok!("foo", 1, 3),
Expression::Simple(Value::Int(value_node!(1, Position::new(1, 9)))),
),
(
make_tok!("bar", 1, 12),
Expression::Simple(Value::Tuple(value_node!(Vec::new(), Position::new(1, 17)))),
),
],
1,
1
))
);
} }
#[test] #[test]