From 89a5e3ae0d88087d030087683ba6f5e9d75bf9b9 Mon Sep 17 00:00:00 2001 From: Arpad Ryszka Date: Sun, 29 Oct 2017 00:21:32 +0200 Subject: [PATCH] automatic whitespace for keyval --- choice.go | 1 + keyval.parser | 32 +++++++++++++------------------- keyval_test.go | 38 ++++++++++++++++++-------------------- 3 files changed, 32 insertions(+), 39 deletions(-) diff --git a/choice.go b/choice.go index 9789280..dff28dc 100644 --- a/choice.go +++ b/choice.go @@ -74,6 +74,7 @@ func (d *choiceDefinition) init(r *registry) error { } for _, e := range d.elements { + // TODO: handle undefined reference d.cbuilder.elements = append(d.cbuilder.elements, r.definitions[e].builder()) } diff --git a/keyval.parser b/keyval.parser index 6422e24..609d8c5 100644 --- a/keyval.parser +++ b/keyval.parser @@ -1,27 +1,21 @@ -ws:alias = [ \b\f\r\t\v]; -wsnl:alias = ws | "\n"; +whitespace:ws = [ \b\f\r\t\v]; comment-line:alias = "#" [^\n]*; -comment = comment-line (ws* "\n" ws* comment-line)*; +comment = comment-line ("\n" comment-line)*; -wsc:alias = ws | comment-line; -wsnlc:alias = wsnl | comment-line; +quoted:alias:nows = "\"" ([^\\"] | "\\" .)* "\""; +word:alias:nows = ([^\\"\n=#.\[\] \b\f\r\t\v] | "\\" .)+; +symbol = word+ | quoted; -quoted:alias = "\"" ([^\\"] | "\\" .)* "\""; -symbol-non-ws:alias = ([^\\"\n=#.\[\] \b\f\r\t\v] | "\\" .)+; -symbol = symbol-non-ws (ws* symbol-non-ws)* | quoted; - -key-form:alias = symbol (ws* "." ws* symbol)*; +key-form:alias = symbol | symbol ("." symbol)+; key = key-form; -group-key = (comment "\n" ws*)? "[" ws* key-form ws* "]"; -value-chars:alias = ([^\\"\n=# \b\f\r\t\v] | "\\" .)+; -value = value-chars (ws* value-chars)* | quoted; -key-val = (comment "\n" ws*)? (key | key? ws* "=" ws* value?); +group-key-form:alias = "[" key-form "]"; +group-key = (comment "\n")? group-key-form | group-key-form; +value-chars:alias:nows = ([^\\"\n=# \b\f\r\t\v] | "\\" .)+; +value = value-chars+ | quoted; + +key-val = (comment "\n")? (key | key? "=" value?) comment-line?; entry:alias = group-key | key-val; -doc:root = (entry (ws* comment-line)? | wsnlc)*; - -single-entry = (entry (ws* comment-line)? - | wsnlc* entry (ws* comment-line)?) - []; +doc:root = (entry | comment-line | "\n")*; diff --git a/keyval_test.go b/keyval_test.go index f5a877a..02d5465 100644 --- a/keyval_test.go +++ b/keyval_test.go @@ -84,45 +84,43 @@ func TestKeyVal(t *testing.T) { }}, }, { title: "key value pairs with a comment at the end of line", - text: ` - a key = a value # a comment - another key = another value # another comment - `, + text: "a key = a value # a comment\n" + + "another key = another value # another comment", nodes: []*Node{{ Name: "key-val", - From: 11, - To: 32, + From: 0, + To: 39, Nodes: []*Node{{ Name: "key", - From: 11, - To: 16, + From: 0, + To: 5, Nodes: []*Node{{ Name: "symbol", - From: 11, - To: 16, + From: 0, + To: 5, }}, }, { Name: "value", - From: 25, - To: 32, + From: 14, + To: 21, }}, }, { Name: "key-val", - From: 61, - To: 88, + From: 40, + To: 85, Nodes: []*Node{{ Name: "key", - From: 61, - To: 72, + From: 40, + To: 51, Nodes: []*Node{{ Name: "symbol", - From: 61, - To: 72, + From: 40, + To: 51, }}, }, { Name: "value", - From: 75, - To: 88, + From: 54, + To: 67, }}, }}, }, {