@Override <T> Response<T> process(Request<T> req) { switch (req.mode) { case INVOKE: return new Response<T>(new Status.ComputeArg()); case COMPLETE: return new Response<T>(new Status.ReadingArg()); default: throw new AssertionError(); } } }
ReadingArg next() { return new ReadingArg(index + 1); }
private void determine() { while (next.isEmpty()) { Status.Response<T> nextStatus = status.process(new Status.Request<T>(mode, tokenizer, command)); if (nextStatus.status != null) { this.status = nextStatus.status; } if (nextStatus.events != null) { next.addAll(nextStatus.events); } if (nextStatus.command != null) { command = (CommandDescriptor<T>)nextStatus.command; } } } }
Parser<T> parser = new Parser<T>(tokenizer, descriptor, Mode.INVOKE); InvocationMatch<T> current = new InvocationMatch<T>(descriptor); Event event = parser.next(); if (event instanceof Event.Separator) { } else if (event instanceof Event.Option) { Event.Option optionEvent = (Event.Option)event; OptionDescriptor desc = optionEvent.getParameter(); Iterable<OptionMatch> options = current.options(); OptionMatch option = null; if (om.getParameter().equals(desc)) { List<LiteralValue> v = new ArrayList<LiteralValue>(om.getValues()); v.addAll(bilto(optionEvent.getValues())); List<String> names = new ArrayList<String>(om.getNames()); names.add(optionEvent.getToken().getName()); option = new OptionMatch(desc, names, v); break; option = new OptionMatch(desc, optionEvent.getToken().getName(), bilto(optionEvent.getValues())); current = current.subordinate(((Event.Subordinate)event).getDescriptor().getName()); } else if (event instanceof Event.Argument) { Event.Argument argumentEvent = (Event.Argument)event; List<Token.Literal> values = argumentEvent.getValues(); ArgumentMatch match; if (values.size() > 0) { match = new ArgumentMatch(
Parser<T> parser = new Parser<T>(tokenizer, foo, Mode.COMPLETE); Event event = parser.next(); if (event instanceof Event.Separator) { separator = (Event.Separator)event; foo = (CommandDescriptor<T>)((Event.Subordinate)event).getDescriptor(); last = event; separator = null; return new OptionCompletion<T>(foo, nso.getToken()); } else if (stop instanceof Event.Stop.Unresolved) { if (stop instanceof Event.Stop.Unresolved.TooManyArguments) { Event.Stop.Unresolved.TooManyArguments tma = (Event.Stop.Unresolved.TooManyArguments)stop; return new CommandCompletion<T>(foo, s.substring(stop.getIndex()), delimiter); } else { return new EmptyCompletion(); return new CommandCompletion<T>(foo, s.substring(stop.getIndex()), Delimiter.EMPTY); } else { List<ArgumentDescriptor> args = foo.getArguments(); List<Token.Literal.Word> values = optionEvent.getValues(); OptionDescriptor option = optionEvent.getParameter(); if (separator == null) { if (values.size() == 0) { return new SpaceCompletion(); } else if (values.size() <= option.getArity()) { Token.Literal.Word word = optionEvent.peekLast();
<T> Response<T> process(Request<T> req) { Response<T> response = new Response<T>(); Token token = req.tokenizer.peek(); if (token == null) { response.add(new Event.Stop.Done(req.tokenizer.getIndex())); } else if (token instanceof Token.Whitespace) { response.add(new Event.Separator((Token.Whitespace) token)); req.tokenizer.next(); } else { req.tokenizer.next(); if (req.tokenizer.hasNext()) { response.status = new Status.WantReadArg(); } else { if (req.mode == Mode.INVOKE) { response.status = new Status.Done(); response.add(new Event.Stop.Done(req.tokenizer.getIndex())); } else { response.add(new Event.Stop.Unresolved.NoSuchOption(optionToken)); response.add(new Event.Option(req.command, desc, optionToken, values)); } else { response.add(new Event.Stop.Unresolved.NoSuchOption(optionToken)); response.command = m; req.tokenizer.next(); response.add(new Event.Subordinate.Explicit(m, wordLiteral)); } else { response.status = new Status.WantReadArg();
@Override <T> Response<T> process(Request<T> req) { Token token = req.tokenizer.peek(); Response<T> response = new Response<T>(); if (token == null) { response.add(new Event.Stop.Done(req.tokenizer.getIndex())); } else if (token instanceof Token.Whitespace) { response.add(new Event.Separator((Token.Whitespace) token)); req.tokenizer.next(); } else { case SINGLE: req.tokenizer.next(); response.add(new Event.Argument(req.command, argument, Arrays.asList(literal))); response.status = next(); break; case MULTI: response.add(new Event.Argument(req.command, argument, values)); response.add(new Event.Stop.Unresolved.TooManyArguments(literal));
@Override <T> Response<T> process(Request<T> req) { Token token = req.tokenizer.peek(); Response<T> response = new Response<T>(); if (token == null) { response.add(new Event.Stop.Done(req.tokenizer.getIndex())); } else if (token instanceof Token.Whitespace) { response.add(new Event.Separator((Token.Whitespace) token)); req.tokenizer.next(); } else { events.addLast(new Event.Argument(req.command, argument, values)); events.addLast(new Event.Separator((Token.Whitespace) req.tokenizer.next())); events.addLast(new Event.Stop.Done(req.tokenizer.getIndex())); response.status = new Status.Done(); response.addAll(events);
public void assertArgument(String name, String... values) { Event.Argument event = (Event.Argument)parser.next(); assertEquals(name, event.getParameter().getName()); assertEquals(Arrays.asList(values), event.getStrings()); }
public void assertMethod(String name) { Event.Subordinate event = (Event.Subordinate)parser.next(); assertEquals(name, event.getDescriptor().getName()); }
public void assertOption(String name, String... values) { Event.Option event = (Event.Option)parser.next(); assertTrue(event.getParameter().getNames().contains(name)); assertEquals(Arrays.asList(values), event.getStrings()); }
public Event next() { if (!hasNext()) { throw new NoSuchElementException(); } return next.removeFirst(); }
public Parser(Tokenizer tokenizer, CommandDescriptor<T> command, Mode mode) { this.tokenizer = tokenizer; this.command = command; this.status = new Status.ReadingOption(); this.mode = mode; this.next = new LinkedList<Event>(); }
public boolean hasNext() { if (next.isEmpty()) { determine(); } return next.size() > 0; }
@Override public int getFrom() { return peekFirst().getFrom(); }
@Override public int getTo() { return peekLast().getTo(); } }
public void assertSeparator() { Event event = parser.next(); assertTrue("was expecting a separator instead of " + event, event instanceof Event.Separator); }
@Override public int getTo() { return values.size() == 0 ? token.getTo() : peekLast().getTo(); } }
private Tester(CommandDescriptor<Instance<T>> command, String s, Mode mode) { this.parser = new Parser<Instance<T>>(new TokenizerImpl(s), command, mode); }