source: josm/trunk/src/org/openstreetmap/josm/actions/search/PushbackTokenizer.java@ 2695

Last change on this file since 2695 was 2645, checked in by jttt, 14 years ago

SearchCompiler refactoring, use search pattern for OsmPrimitive.hasDirectionKeys(), added toString() and type to dataset events

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions.search;
3
4import java.io.IOException;
5import java.io.Reader;
6
7import org.openstreetmap.josm.actions.search.SearchCompiler.ParseError;
8
9public class PushbackTokenizer {
10 private final Reader search;
11
12 private Token currentToken;
13 private String currentText;
14 private int c;
15
16 public PushbackTokenizer(Reader search) {
17 this.search = search;
18 getChar();
19 }
20
21 public enum Token {NOT, OR, LEFT_PARENT, RIGHT_PARENT, COLON, EQUALS, KEY, EOF}
22
23 private void getChar() {
24 try {
25 c = search.read();
26 } catch (IOException e) {
27 throw new RuntimeException(e.getMessage(), e);
28 }
29 }
30
31 /**
32 * The token returned is <code>null</code> or starts with an identifier character:
33 * - for an '-'. This will be the only character
34 * : for an key. The value is the next token
35 * | for "OR"
36 * ' ' for anything else.
37 * @return The next token in the stream.
38 */
39 public Token nextToken() {
40 if (currentToken != null) {
41 Token result = currentToken;
42 currentToken = null;
43 return result;
44 }
45
46 while (Character.isWhitespace(c)) {
47 getChar();
48 }
49 switch (c) {
50 case -1:
51 getChar();
52 return Token.EOF;
53 case ':':
54 getChar();
55 return Token.COLON;
56 case '=':
57 getChar();
58 return Token.EQUALS;
59 case '-':
60 getChar();
61 return Token.NOT;
62 case '(':
63 getChar();
64 return Token.LEFT_PARENT;
65 case ')':
66 getChar();
67 return Token.RIGHT_PARENT;
68 case '|':
69 getChar();
70 return Token.OR;
71 case '"':
72 {
73 StringBuilder s = new StringBuilder();
74 while (c != -1 && c != '"') {
75 s.append((char)c);
76 getChar();
77 }
78 getChar();
79 currentText = s.toString();
80 return Token.KEY;
81 }
82 default:
83 {
84 StringBuilder s = new StringBuilder();
85 while (!(c == -1 || Character.isWhitespace(c) || c == '"'|| c == ':' || c == '(' || c == ')' || c == '|' || c == '=')) {
86 s.append((char)c);
87 getChar();
88 }
89 currentText = s.toString();
90 if ("or".equals(currentText))
91 return Token.OR;
92 else
93 return Token.KEY;
94 }
95 }
96 }
97
98 public boolean readIfEqual(Token token) {
99 Token nextTok = nextToken();
100 if (nextTok == null ? token == null : nextTok == token)
101 return true;
102 currentToken = nextTok;
103 return false;
104 }
105
106 public String readText() {
107 Token nextTok = nextToken();
108 if (nextTok == Token.KEY)
109 return currentText;
110 currentToken = nextTok;
111 return null;
112 }
113
114 public String readText(String errorMessage) throws ParseError {
115 String text = readText();
116 if (text == null)
117 throw new ParseError(errorMessage);
118 else
119 return text;
120 }
121
122 public String getText() {
123 return currentText;
124 }
125}
Note: See TracBrowser for help on using the repository browser.