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

Last change on this file since 1169 was 1169, checked in by stoecker, 15 years ago

removed usage of tab stops

  • Property svn:eol-style set to native
File size: 3.2 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.PushbackReader;
6import java.util.LinkedList;
7
8public class PushbackTokenizer {
9 private PushbackReader search;
10
11 private LinkedList<String> pushBackBuf = new LinkedList<String>();
12
13 public PushbackTokenizer(PushbackReader search) {
14 this.search = search;
15 }
16
17 /**
18 * The token returned is <code>null</code> or starts with an identifier character:
19 * - for an '-'. This will be the only character
20 * : for an key. The value is the next token
21 * | for "OR"
22 * ' ' for anything else.
23 * @return The next token in the stream.
24 */
25 public String nextToken() {
26 if (!pushBackBuf.isEmpty()) {
27 return pushBackBuf.removeLast();
28 }
29
30 try {
31 int next;
32 char c = ' ';
33 while (c == ' ' || c == '\t' || c == '\n') {
34 next = search.read();
35 if (next == -1)
36 return null;
37 c = (char)next;
38 }
39 StringBuilder s;
40 switch (c) {
41 case ':':
42 next = search.read();
43 c = (char) next;
44 if (next == -1 || c == ' ' || c == '\t') {
45 pushBack(" ");
46 } else {
47 search.unread(next);
48 }
49 return ":";
50 case '-':
51 return "-";
52 case '(':
53 return "(";
54 case ')':
55 return ")";
56 case '|':
57 return "|";
58 case '"':
59 s = new StringBuilder(" ");
60 for (int nc = search.read(); nc != -1 && nc != '"'; nc = search.read())
61 s.append((char)nc);
62 return s.toString();
63 default:
64 s = new StringBuilder();
65 for (;;) {
66 s.append(c);
67 next = search.read();
68 if (next == -1) {
69 if (s.toString().equals("OR"))
70 return "|";
71 return " "+s.toString();
72 }
73 c = (char)next;
74 if (c == ' ' || c == '\t' || c == '"' || c == ':' || c == '(' || c == ')' || c == '|') {
75 search.unread(next);
76 if (s.toString().equals("OR"))
77 return "|";
78 return " "+s.toString();
79 }
80 }
81 }
82 } catch (IOException e) {
83 throw new RuntimeException(e.getMessage(), e);
84 }
85 }
86
87 public boolean readIfEqual(String tok) {
88 String nextTok = nextToken();
89 if (nextTok == null ? tok == null : nextTok.equals(tok))
90 return true;
91 pushBack(nextTok);
92 return false;
93 }
94
95 public String readText() {
96 String nextTok = nextToken();
97 if (nextTok != null && nextTok.startsWith(" "))
98 return nextTok.substring(1);
99 pushBack(nextTok);
100 return null;
101 }
102
103 public void pushBack(String tok) {
104 pushBackBuf.addLast(tok);
105 }
106}
Note: See TracBrowser for help on using the repository browser.