Improved parsing a bit

This commit is contained in:
Nathan Adams 2018-01-17 11:22:54 +01:00
parent 8d4d3cba80
commit 72817f8dd7

View file

@ -171,7 +171,7 @@ public class CommandDispatcher<S> {
private ParseResults<S> parseNodes(final CommandNode<S> node, final StringReader originalReader, final CommandContextBuilder<S> contextSoFar) { private ParseResults<S> parseNodes(final CommandNode<S> node, final StringReader originalReader, final CommandContextBuilder<S> contextSoFar) {
final S source = contextSoFar.getSource(); final S source = contextSoFar.getSource();
Map<CommandNode<S>, CommandSyntaxException> errors = null; Map<CommandNode<S>, CommandSyntaxException> errors = null;
final List<PartialParse<S>> potentials = Lists.newArrayList(); List<PartialParse<S>> potentials = null;
final int cursor = originalReader.getCursor(); final int cursor = originalReader.getCursor();
for (final CommandNode<S> child : node.getRelevantNodes(originalReader)) { for (final CommandNode<S> child : node.getRelevantNodes(originalReader)) {
@ -211,18 +211,22 @@ public class CommandDispatcher<S> {
return new ParseResults<>(context, parse.getReader(), parse.getExceptions()); return new ParseResults<>(context, parse.getReader(), parse.getExceptions());
} else { } else {
final ParseResults<S> parse = parseNodes(child, reader, context); final ParseResults<S> parse = parseNodes(child, reader, context);
if (potentials == null) {
potentials = new ArrayList<>(1);
}
potentials.add(new PartialParse<>(context, parse)); potentials.add(new PartialParse<>(context, parse));
} }
} else { } else {
if (potentials == null) {
potentials = new ArrayList<>(1);
}
potentials.add(new PartialParse<>(context, new ParseResults<>(context, reader, Collections.emptyMap()))); potentials.add(new PartialParse<>(context, new ParseResults<>(context, reader, Collections.emptyMap())));
} }
} }
if (!potentials.isEmpty()) { if (potentials != null) {
final PartialParse<S> likely;
if (potentials.size() > 1) { if (potentials.size() > 1) {
final List<PartialParse<S>> sorted = Lists.newArrayList(potentials); potentials.sort((a, b) -> {
sorted.sort((a, b) -> {
if (!a.parse.getReader().canRead() && b.parse.getReader().canRead()) { if (!a.parse.getReader().canRead() && b.parse.getReader().canRead()) {
return -1; return -1;
} }
@ -237,11 +241,8 @@ public class CommandDispatcher<S> {
} }
return 0; return 0;
}); });
likely = sorted.get(0);
} else {
likely = potentials.get(0);
} }
return likely.parse; return potentials.get(0).parse;
} }
return new ParseResults<>(contextSoFar, originalReader, errors == null ? Collections.emptyMap() : errors); return new ParseResults<>(contextSoFar, originalReader, errors == null ? Collections.emptyMap() : errors);