From 38a255a2f35f7d3fbb7aa3bde546ef2c3a0acd17 Mon Sep 17 00:00:00 2001 From: Alexander Clouter Date: Mon, 3 May 2021 14:40:49 +0100 Subject: [PATCH] support nested properties --- src/eini_parser.yrl | 16 +++++++++++++--- test/eini_tests.erl | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/eini_parser.yrl b/src/eini_parser.yrl index 8617ab7..a3dbd37 100644 --- a/src/eini_parser.yrl +++ b/src/eini_parser.yrl @@ -27,7 +27,8 @@ Nonterminals title_words title property_with_skip_lines - properties property + properties property_base property + properties_nested property_nested key_part values single_value skip_lines @@ -86,9 +87,18 @@ properties -> property_with_skip_lines properties : ['$1' | '$2']. property_with_skip_lines -> property : '$1'. property_with_skip_lines -> property skip_lines : '$1'. -property -> key_part '=' values break : +property_base -> key_part '=' values break : {list_to_atom(value_of('$1')), strip_values('$3')}. +property -> property_base : '$1'. +property -> key_part '=' break properties_nested : + {list_to_atom(value_of('$1')), '$4'}. + +properties_nested -> property_nested : ['$1']. +properties_nested -> property_nested properties_nested : ['$1' | '$2']. + +property_nested -> blank property_base : '$2'. + key_part -> word : '$1'. key_part -> word blank : '$1'. key_part -> blank word : '$2'. @@ -98,7 +108,7 @@ values -> single_value : ['$1']. values -> single_value values : ['$1' | '$2']. %% At value position, any characters are accepted AS IS. -single_value -> word : value_of('$1'). +single_value -> word : value_of('$1'). single_value -> value : value_of('$1'). single_value -> blank : value_of('$1'). single_value -> comment : value_of('$1'). diff --git a/test/eini_tests.erl b/test/eini_tests.erl index eab3e41..f7ce5dd 100644 --- a/test/eini_tests.erl +++ b/test/eini_tests.erl @@ -349,6 +349,20 @@ one_section_title_and_one_prop_test_() -> "[title] \n" "key1= value1 \n" )), + %% nested properties: https://docs.aws.amazon.com/credref/latest/refdocs/file-format.html + ?_assertEqual({ok, [ + {title, [{key1,[{key11,<<"value11">>},{key12,<<"value12">>},{key13,<<"value13">>},{key14,<<"value14">>}]},{key2,<<"value2">>}]} + ]}, + parse( + "[title]\n" + "key1 =\n" + " key11=value11\n" + " key12 =value12\n" + " key13= value13\n" + " key14 = value14\n" + "key2=value2\n" + )), + %% value has characters which can not used in titles or keys ?_assertEqual({ok, [ {title, [{key1, <<"value1$% '""#!+*=@/:+">>}]}