| 10 | public class TokenParser |
| 11 | { |
| 12 | public static Token[] parse(String str) throws IOException, ParseException |
| 13 | { |
| 14 | Reader reader = new StringReader(str); |
| 15 | PushbackReader pushbackreader = new PushbackReader(reader); |
| 16 | List<Token> list = new ArrayList(); |
| 17 | |
| 18 | while (true) |
| 19 | { |
| 20 | int i = pushbackreader.read(); |
| 21 | |
| 22 | if (i < 0) |
| 23 | { |
| 24 | Token[] atoken = (Token[])((Token[])list.toArray(new Token[list.size()])); |
| 25 | return atoken; |
| 26 | } |
| 27 | |
| 28 | char c0 = (char)i; |
| 29 | |
| 30 | if (!Character.isWhitespace(c0)) |
| 31 | { |
| 32 | TokenType tokentype = TokenType.getTypeByFirstChar(c0); |
| 33 | |
| 34 | if (tokentype == null) |
| 35 | { |
| 36 | throw new ParseException("Invalid character: \'" + c0 + "\', in: " + str); |
| 37 | } |
| 38 | |
| 39 | Token token = readToken(c0, tokentype, pushbackreader); |
| 40 | list.add(token); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | private static Token readToken(char chFirst, TokenType type, PushbackReader pr) throws IOException |
| 46 | { |