mirror of
https://github.com/lightbend/config.git
synced 2025-01-15 23:01:05 +08:00
Refactor parseConcatenation() method
Refactor the parseConcatenation() method in ConfigDocumentParser to not throw out parsed values if only one value was seen. Rename parseConcatenation to consolidateValues().
This commit is contained in:
parent
f115731071
commit
d3b33cc6c2
@ -124,9 +124,8 @@ final class ConfigDocumentParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// merge a bunch of adjacent values into one
|
// parse a concatenation. If there is no concatenation, return the next value
|
||||||
// value
|
private AbstractConfigNodeValue consolidateValues(Collection<AbstractConfigNode> nodes) {
|
||||||
private ConfigNodeComplexValue parseConcatenation(Collection<AbstractConfigNode> nodes) {
|
|
||||||
// this trick is not done in JSON
|
// this trick is not done in JSON
|
||||||
if (flavor == ConfigSyntax.JSON)
|
if (flavor == ConfigSyntax.JSON)
|
||||||
return null;
|
return null;
|
||||||
@ -139,18 +138,18 @@ final class ConfigDocumentParser {
|
|||||||
Token t = nextTokenIgnoringWhitespace(nodes);
|
Token t = nextTokenIgnoringWhitespace(nodes);
|
||||||
while (true) {
|
while (true) {
|
||||||
AbstractConfigNodeValue v = null;
|
AbstractConfigNodeValue v = null;
|
||||||
if (Tokens.isValue(t) || Tokens.isUnquotedText(t)
|
if (Tokens.isIgnoredWhitespace(t) || isUnquotedWhitespace(t)) {
|
||||||
|
values.add(new ConfigNodeSingleToken(t));
|
||||||
|
t = nextToken();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if (Tokens.isValue(t) || Tokens.isUnquotedText(t)
|
||||||
|| Tokens.isSubstitution(t) || t == Tokens.OPEN_CURLY
|
|| Tokens.isSubstitution(t) || t == Tokens.OPEN_CURLY
|
||||||
|| t == Tokens.OPEN_SQUARE) {
|
|| t == Tokens.OPEN_SQUARE) {
|
||||||
// there may be newlines _within_ the objects and arrays
|
// there may be newlines _within_ the objects and arrays
|
||||||
v = parseValue(t);
|
v = parseValue(t);
|
||||||
valueCount++;
|
valueCount++;
|
||||||
} else if (Tokens.isIgnoredWhitespace(t)) {
|
} else {
|
||||||
values.add(new ConfigNodeSingleToken(t));
|
|
||||||
t = nextToken();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,15 +162,20 @@ final class ConfigDocumentParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
putBack(t);
|
putBack(t);
|
||||||
ConfigNodeComplexValue value = new ConfigNodeComplexValue(values);
|
|
||||||
|
|
||||||
// put back all tokens, as they did not form a concatenation
|
// No concatenation was seen, but a single value may have been parsed, so return it, and put back
|
||||||
|
// all succeeding tokens
|
||||||
if (valueCount < 2) {
|
if (valueCount < 2) {
|
||||||
ArrayList<Token> tokens = new ArrayList(value.tokens());
|
AbstractConfigNodeValue value = null;
|
||||||
for (int i = tokens.size() - 1; i >= 0; i--) {
|
for (AbstractConfigNode node : values) {
|
||||||
putBack(tokens.get(i));
|
if (node instanceof AbstractConfigNodeValue)
|
||||||
|
value = (AbstractConfigNodeValue)node;
|
||||||
|
else if (node == null)
|
||||||
|
nodes.add(node);
|
||||||
|
else
|
||||||
|
putBack((new ArrayList<Token>(node.tokens())).get(0));
|
||||||
}
|
}
|
||||||
return null;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ConfigNodeComplexValue(values);
|
return new ConfigNodeComplexValue(values);
|
||||||
@ -409,10 +413,10 @@ final class ConfigDocumentParser {
|
|||||||
boolean insideEquals = false;
|
boolean insideEquals = false;
|
||||||
|
|
||||||
Token valueToken;
|
Token valueToken;
|
||||||
AbstractConfigNodeValue newValue;
|
AbstractConfigNodeValue nextValue;
|
||||||
if (flavor == ConfigSyntax.CONF && afterKey == Tokens.OPEN_CURLY) {
|
if (flavor == ConfigSyntax.CONF && afterKey == Tokens.OPEN_CURLY) {
|
||||||
// can omit the ':' or '=' before an object value
|
// can omit the ':' or '=' before an object value
|
||||||
newValue = parseValue(afterKey);
|
nextValue = parseValue(afterKey);
|
||||||
} else {
|
} else {
|
||||||
if (!isKeyValueSeparatorToken(afterKey)) {
|
if (!isKeyValueSeparatorToken(afterKey)) {
|
||||||
throw parseError(addQuoteSuggestion(afterKey.toString(),
|
throw parseError(addQuoteSuggestion(afterKey.toString(),
|
||||||
@ -427,13 +431,13 @@ final class ConfigDocumentParser {
|
|||||||
equalsCount += 1;
|
equalsCount += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
newValue = parseConcatenation(keyValueNodes);
|
nextValue = consolidateValues(keyValueNodes);
|
||||||
if (newValue == null) {
|
if (nextValue == null) {
|
||||||
newValue = parseValue(nextTokenIgnoringWhitespace(keyValueNodes));
|
nextValue = parseValue(nextTokenIgnoringWhitespace(keyValueNodes));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
keyValueNodes.add(newValue);
|
keyValueNodes.add(nextValue);
|
||||||
if (insideEquals) {
|
if (insideEquals) {
|
||||||
equalsCount -= 1;
|
equalsCount -= 1;
|
||||||
}
|
}
|
||||||
@ -506,10 +510,10 @@ final class ConfigDocumentParser {
|
|||||||
arrayCount += 1;
|
arrayCount += 1;
|
||||||
Token t;
|
Token t;
|
||||||
|
|
||||||
ConfigNodeComplexValue concatenation = parseConcatenation(children);
|
AbstractConfigNodeValue nextValue = consolidateValues(children);
|
||||||
if (concatenation != null) {
|
if (nextValue != null) {
|
||||||
children.add(concatenation);
|
children.add(nextValue);
|
||||||
concatenation = null;
|
nextValue = null;
|
||||||
} else {
|
} else {
|
||||||
t = nextTokenIgnoringWhitespace(children);
|
t = nextTokenIgnoringWhitespace(children);
|
||||||
|
|
||||||
@ -521,8 +525,9 @@ final class ConfigDocumentParser {
|
|||||||
} else if (Tokens.isValue(t) || t == Tokens.OPEN_CURLY
|
} else if (Tokens.isValue(t) || t == Tokens.OPEN_CURLY
|
||||||
|| t == Tokens.OPEN_SQUARE || Tokens.isUnquotedText(t)
|
|| t == Tokens.OPEN_SQUARE || Tokens.isUnquotedText(t)
|
||||||
|| Tokens.isSubstitution(t)) {
|
|| Tokens.isSubstitution(t)) {
|
||||||
AbstractConfigNodeValue v = parseValue(t);
|
nextValue = parseValue(t);
|
||||||
children.add(v);
|
children.add(nextValue);
|
||||||
|
nextValue = null;
|
||||||
} else {
|
} else {
|
||||||
throw parseError(addKeyName("List should have ] or a first element after the open [, instead had token: "
|
throw parseError(addKeyName("List should have ] or a first element after the open [, instead had token: "
|
||||||
+ t
|
+ t
|
||||||
@ -553,17 +558,18 @@ final class ConfigDocumentParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// now just after a comma
|
// now just after a comma
|
||||||
concatenation = parseConcatenation(children);
|
nextValue = consolidateValues(children);
|
||||||
if (concatenation != null) {
|
if (nextValue != null) {
|
||||||
children.add(concatenation);
|
children.add(nextValue);
|
||||||
concatenation = null;
|
nextValue = null;
|
||||||
} else {
|
} else {
|
||||||
t = nextTokenIgnoringWhitespace(children);
|
t = nextTokenIgnoringWhitespace(children);
|
||||||
if (Tokens.isValue(t) || t == Tokens.OPEN_CURLY
|
if (Tokens.isValue(t) || t == Tokens.OPEN_CURLY
|
||||||
|| t == Tokens.OPEN_SQUARE || Tokens.isUnquotedText(t)
|
|| t == Tokens.OPEN_SQUARE || Tokens.isUnquotedText(t)
|
||||||
|| Tokens.isSubstitution(t)) {
|
|| Tokens.isSubstitution(t)) {
|
||||||
AbstractConfigNodeValue v = parseValue(t);
|
nextValue = parseValue(t);
|
||||||
children.add(v);
|
children.add(nextValue);
|
||||||
|
nextValue = null;
|
||||||
} else if (flavor != ConfigSyntax.JSON && t == Tokens.CLOSE_SQUARE) {
|
} else if (flavor != ConfigSyntax.JSON && t == Tokens.CLOSE_SQUARE) {
|
||||||
// we allow one trailing comma
|
// we allow one trailing comma
|
||||||
putBack(t);
|
putBack(t);
|
||||||
|
@ -49,9 +49,6 @@ class ConfigDocumentParserTest extends TestUtils {
|
|||||||
parseTest("{foo:bar} ")
|
parseTest("{foo:bar} ")
|
||||||
parseTest("""{include "foo.conf"}""")
|
parseTest("""{include "foo.conf"}""")
|
||||||
|
|
||||||
//Is this valid?
|
|
||||||
//parseTest(" { foo : bar } ")
|
|
||||||
|
|
||||||
//Can parse a map with all simple types
|
//Can parse a map with all simple types
|
||||||
parseTest(
|
parseTest(
|
||||||
"""{
|
"""{
|
||||||
|
Loading…
Reference in New Issue
Block a user