From aa8d2cab29dd9a95e1d5391f80a7753800f53d8c Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Mon, 28 May 2018 08:08:36 -0500 Subject: [PATCH] FEATURE: Allow trailing commas in the tuple, copy, and list syntax --- TODO.md | 11 ++++++++--- src/lib.rs | 3 ++- src/parse/mod.rs | 3 +++ src/parse/test.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/TODO.md b/TODO.md index 7ea3da2..97faec1 100644 --- a/TODO.md +++ b/TODO.md @@ -22,13 +22,18 @@ Some options here could be: * DSL's * 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 +* Strings as tuple fields? * Streaming Parsing * Casting between types? * Better error messages. -* Allow trailing commas. * Flags should allow different seperators for prefixed flags. * YAML export -* HCL export -* Integration Testing framework \ No newline at end of file +* HCL export \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 9b223d4..cbd1ad9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -115,7 +115,8 @@ //! //! #### 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 //! [1, 2, 3]; // A simple list of numbers. diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 51b2de6..5cb7373 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -180,6 +180,7 @@ named!( pos: pos >> punct!("{") >> v: field_list >> + opt_res!(punct!(",")) >> // nom's opt! macro doesn't preserve error types properly but this one does. punct!("}") >> (pos, Some(v)) ), @@ -199,6 +200,7 @@ named!(list_value, do_parse!( start: punct!("[") >> elements: separated_list!(punct!(","), expression) >> + opt_res!(punct!(",")) >> // nom's opt! macro doesn't preserve error types properly but this one does. punct!("]") >> (start.pos, elements) ), @@ -497,6 +499,7 @@ named!(copy_expression, selector: selector_list >> punct!("{") >> fields: field_list >> + opt_res!(punct!(",")) >> // noms opt! macro does not preserve error types properly but this one does. punct!("}") >> (SelectorDef::new(selector, pos.line, pos.column as usize), fields) ), diff --git a/src/parse/test.rs b/src/parse/test.rs index b7235a4..d2a0f10 100644 --- a/src/parse/test.rs +++ b/src/parse/test.rs @@ -768,6 +768,19 @@ fn test_copy_parse() { 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] @@ -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!( list_value("// comment\n[1, 1]"), Value::List(ListDef { @@ -992,6 +1016,23 @@ fn test_tuple_parse() { 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]