automatic whitespace for keyval

This commit is contained in:
Arpad Ryszka 2017-10-29 00:21:32 +02:00
parent 304f11f224
commit 89a5e3ae0d
3 changed files with 32 additions and 39 deletions

View File

@ -74,6 +74,7 @@ func (d *choiceDefinition) init(r *registry) error {
} }
for _, e := range d.elements { for _, e := range d.elements {
// TODO: handle undefined reference
d.cbuilder.elements = append(d.cbuilder.elements, r.definitions[e].builder()) d.cbuilder.elements = append(d.cbuilder.elements, r.definitions[e].builder())
} }

View File

@ -1,27 +1,21 @@
ws:alias = [ \b\f\r\t\v]; whitespace:ws = [ \b\f\r\t\v];
wsnl:alias = ws | "\n";
comment-line:alias = "#" [^\n]*; comment-line:alias = "#" [^\n]*;
comment = comment-line (ws* "\n" ws* comment-line)*; comment = comment-line ("\n" comment-line)*;
wsc:alias = ws | comment-line; quoted:alias:nows = "\"" ([^\\"] | "\\" .)* "\"";
wsnlc:alias = wsnl | comment-line; word:alias:nows = ([^\\"\n=#.\[\] \b\f\r\t\v] | "\\" .)+;
symbol = word+ | quoted;
quoted:alias = "\"" ([^\\"] | "\\" .)* "\""; key-form:alias = symbol | symbol ("." symbol)+;
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 = key-form; key = key-form;
group-key = (comment "\n" ws*)? "[" ws* key-form ws* "]";
value-chars:alias = ([^\\"\n=# \b\f\r\t\v] | "\\" .)+; group-key-form:alias = "[" key-form "]";
value = value-chars (ws* value-chars)* | quoted; group-key = (comment "\n")? group-key-form | group-key-form;
key-val = (comment "\n" ws*)? (key | key? ws* "=" ws* value?);
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; entry:alias = group-key | key-val;
doc:root = (entry (ws* comment-line)? | wsnlc)*; doc:root = (entry | comment-line | "\n")*;
single-entry = (entry (ws* comment-line)?
| wsnlc* entry (ws* comment-line)?)
[];

View File

@ -84,45 +84,43 @@ func TestKeyVal(t *testing.T) {
}}, }},
}, { }, {
title: "key value pairs with a comment at the end of line", title: "key value pairs with a comment at the end of line",
text: ` text: "a key = a value # a comment\n" +
a key = a value # a comment "another key = another value # another comment",
another key = another value # another comment
`,
nodes: []*Node{{ nodes: []*Node{{
Name: "key-val", Name: "key-val",
From: 11, From: 0,
To: 32, To: 39,
Nodes: []*Node{{ Nodes: []*Node{{
Name: "key", Name: "key",
From: 11, From: 0,
To: 16, To: 5,
Nodes: []*Node{{ Nodes: []*Node{{
Name: "symbol", Name: "symbol",
From: 11, From: 0,
To: 16, To: 5,
}}, }},
}, { }, {
Name: "value", Name: "value",
From: 25, From: 14,
To: 32, To: 21,
}}, }},
}, { }, {
Name: "key-val", Name: "key-val",
From: 61, From: 40,
To: 88, To: 85,
Nodes: []*Node{{ Nodes: []*Node{{
Name: "key", Name: "key",
From: 61, From: 40,
To: 72, To: 51,
Nodes: []*Node{{ Nodes: []*Node{{
Name: "symbol", Name: "symbol",
From: 61, From: 40,
To: 72, To: 51,
}}, }},
}, { }, {
Name: "value", Name: "value",
From: 75, From: 54,
To: 88, To: 67,
}}, }},
}}, }},
}, { }, {