summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2018-05-23 16:57:20 +0700
committerDaniil Baturin <daniil@baturin.org>2018-05-23 17:03:25 +0700
commitf0ed6868a5896dfde3d6519c1068f5e5dfea0417 (patch)
treed00ce9af65467b20d7e74f03c8806b1dd6985fb7
parentc754178f2fe2e741105c5ce34029b32aad033f91 (diff)
downloadvyconf-f0ed6868a5896dfde3d6519c1068f5e5dfea0417.tar.gz
vyconf-f0ed6868a5896dfde3d6519c1068f5e5dfea0417.zip
Render valueless nodes correctly.
The canonical format for valueless nodes is "foo;". There are two cases: when it's known from the reference tree that a node is valueless, we should render it the canonical way. However, a valueless node is not the same as a normal node with no children, semantically, so nodes that are note valueless should be rendered as "foo {}". If the reference tree is not present, resort to rendering all nodes without values as if they were normal nodes with no children rather than as if they were valueless. The parser is unaware of the distinction, so there are no implications for it.
-rw-r--r--src/config_tree.ml10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/config_tree.ml b/src/config_tree.ml
index 31b6f0f..131eff4 100644
--- a/src/config_tree.ml
+++ b/src/config_tree.ml
@@ -144,7 +144,7 @@ struct
name]
|> S.concat ""
- let render_values values =
+ let render_values ?(valueless=false) values =
let quote_if_needed s =
try
let _ = Pcre.exec ~pat:"[\\s;{}#\\[\\]\"\']" s in
@@ -152,7 +152,7 @@ struct
with Not_found -> s
in
match values with
- | [] -> raise (Failure "Internal error: get_values got an empty list")
+ | [] -> if valueless then ";" else "{ }"
| [v] -> PF.sprintf "%s;" (quote_if_needed v)
| _ as vs -> S.concat "; " (List.map quote_if_needed vs) |> PF.sprintf "[%s];"
@@ -218,11 +218,13 @@ struct
| Other ->
(name, level, render_children_rt level None ordered rt children)
| Leaf ->
- (name, level, render_values data.values)
+ (name, level, render_values ~valueless:rt_data.valueless data.values)
in
let indents = indentation indent level' in
let outer = render_outer indents data outer_name tag in
- PF.sprintf "%s%s %s" indents outer inner
+ (* Do not insert a space before ; for valueless nodes *)
+ if rt_data.valueless then PF.sprintf "%s%s%s" indents outer inner
+ else PF.sprintf "%s%s %s" indents outer inner
and render_children_rt level tag ordered rt = function
| [] -> "{ }"
| children ->