package com.five35.dex;

import com.google.common.base.Preconditions;
import com.google.common.collect.Iterators;
import com.google.common.collect.PeekingIterator;
import java.util.List;
import javax.annotation.Nonnull;

/**
 * A parser for Dex code.
 */
public final class Parser {
	private final PeekingIterator<Token> tokens;

	private Parser(final String source) {
		final List<Token> tokenList = Tokenizer.tokenize(Preconditions.checkNotNull(source));

		this.tokens = Iterators.peekingIterator(tokenList.iterator());
	}

	/**
	 * Parse the supplied Dex source into an expression.
	 * 
	 * @param source The Dex source to parse.
	 * @return An expression representing the parsed source.
	 * @throws ParserException When the supplied source cannot be parsed as a
	 *         Dex expression.
	 */
	@Nonnull
	public static Expression parse(final String source) throws ParserException {
		return new Parser(Preconditions.checkNotNull(source)).getExpression();
	}

	@Nonnull
	Expression getExpression() throws ParserException {
		return null;
	}

	Token getCurrentToken() {
		return this.tokens.peek();
	}
}

@@ -18,16 +18,56 @@
 5.dex;%0A%0A
+import com.google.common.base.Optional;%0A
 import c
@@ -998,16 +998,717 @@
 ();%0A%09%7D%0A%0A
+%09@Nonnull%0A%09Symbol advance(final Optional%3CSymbol%3E expected) throws ParserException %7B%0A%09%09final Token oldToken = this.tokens.next();%0A%09%09final Token token = this.tokens.peek();%0A%0A%09%09if (token == null) %7B%0A%09%09%09throw new MissingSymbolException(oldToken.getIndex() + oldToken.toString().length(), expected, Optional.%3CToken%3Eabsent());%0A%09%09%7D%0A%0A%09%09final Symbol symbol = Symbol.getSymbol(token);%0A%0A%09%09// There is only one instance of each symbol, so object equality really%0A%09%09// is what we want.%0A%09%09if (!expected.isPresent() %7C%7C expected.get() == symbol) %7B%0A%09%09%09return symbol;%0A%09%09%7D%0A%0A%09%09throw new MissingSymbolException(token.getIndex(), expected, Optional.of(token));%0A%09%7D%0A%0A%09Token getCurrentToken() %7B%0A%09%09return this.tokens.peek();%0A%09%7D%0A%0A
 %09@Nonnul
@@ -1783,67 +1783,7 @@
 ;%0A%09%7D
-%0A%0A%09Token getCurrentToken() %7B%0A%09%09return this.tokens.peek();%0A%09%7D
 %0A%7D%0A

implement advancing the parser to the next token