private void parseNextTokens() throws IOException { int startOffset = scanner.currentParseOffset(); buffer.setLength(0); outer_loop: while (!scanner.eof() && tokens.isEmpty()) { char c = scanner.read(); switch (c) { case '=': bufferToTokens(startOffset, true); tokens.add(new Token(c, scanner.currentParseOffset() - 1, TokenType.EQUALS)); return; case ' ': /* whitespace */ case '\t': bufferToTokens(startOffset, true); if (skipWhitespace) { while (!scanner.eof()) { c = scanner.peek(); if (c != ' ' && c != '\t') { continue outer_loop; } scanner.read(); } return; } startOffset = scanner.currentParseOffset() - 1; buffer.append(c); outer: while (!scanner.eof()) { c = scanner.peek(); switch (c) { case ' ': case '\t': buffer.append(scanner.read()); break; default: break outer; } } tokens.add(new Token(buffer, startOffset, TokenType.WHITESPACE)); return; case '(': bufferToTokens(startOffset, true); tokens.add(new Token(c, scanner.currentParseOffset() - 1, TokenType.PARENS_OPEN)); return; case ')': bufferToTokens(startOffset, true); tokens.add(new Token(c, scanner.currentParseOffset() - 1, TokenType.PARENS_CLOSE)); return; } buffer.append(c); if (bufferToTokens(startOffset, false)) { return; } } bufferToTokens(startOffset, true); }
@Override public int currentParseOffset() { Token tok = currentToken(); if (tok == null) { return scanner.currentParseOffset(); } return tok.getParseOffset(); }
@Override public boolean eof() { try { return currentToken() == null && scanner.eof(); } catch (IOException e) { throw new ParseException(e.getMessage(), scanner.currentParseOffset(), e); } }
@Override public Token read(TokenType type) { if (!peek().hasType(type)) { throw new ParseException( "Expected token of type " + type + " but got " + peek().getType(), scanner.currentParseOffset()); } return read(); }
private Token currentToken() { if (tokens.isEmpty()) { try { if (scanner.eof()) { return null; } parseNextTokens(); } catch (IOException e) { throw new ParseException(e.getMessage(), scanner.currentParseOffset(), e); } if (tokens.isEmpty()) { return null; } } return tokens.get(0); }
private void checkEOF() { if (eof()) { throw new ParseException("Already at EOF", scanner.currentParseOffset()); } }