1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/* ____ ____ ____ ____ ______ *\
** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
** /_____/\____/\___/\____/____/ **
\* */
// $Id$
package scalac.util;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
public class PrefixMatcher {
public static class Entry {
private Entry prev;
private Entry next;
public final String key;
public final Object value;
public final String argument;
public final String description;
public Entry(String key, Object value, String argument,
String description)
{
this.key = key;
this.value = value;
this.argument = argument;
this.description = description;
}
}
private final Map entries;
private Entry first;
private Entry last;
public PrefixMatcher() {
this.entries = new HashMap();
}
public void insert(String key, Object value) {
insert(key, value, null, null);
}
public void insert(String key, Object value, String description) {
insert(key, value, null, description);
}
public void insert(String key, Object value, String argument,
String description)
{
assert key != null && !entries.containsKey(key) : key;
Entry entry = new Entry(key, value, argument, description);
if (first == null) {
first = last = entry;
} else {
last.next = entry;
entry.prev = last;
last = entry;
}
entries.put(key, entry);
}
public Entry[] lookup(String key) {
Object value = entries.get(key);
if (value != null) return new Entry[] { (Entry)value };
List list = new ArrayList();
for (Entry i = first; i != null; i = i.next) {
if (i.key.startsWith(key)) list.add(i);
}
return (Entry[])list.toArray(new Entry[list.size()]);
}
public String getErrorMessage(String key, Entry[] entries, String what) {
switch (entries.length) {
case 0:
return "unknown " + what + " '" + key + "'";
case 1:
return null;
case 2:
return "ambigous " + what + " '" + key + "', both '" +
entries[0].key + "' and '" + entries[1].key + "' match";
default:
StringBuffer buffer = new StringBuffer();
buffer.append("ambigous ").append(what);
buffer.append(" '").append(key).append("'");
for (int i = 0; i < entries.length; i++) {
buffer.append(i < entries.length - 1 ? ", " : " and ");
buffer.append('\'').append(entries[i].key).append('\'');
}
buffer.append(" match");
return buffer.toString();
}
}
public List getHelpStrings(String separator1, String separator2) {
List strings = new ArrayList();
for (Entry entry = first; entry != null; entry = entry.next) {
if (entry.description != null)
if (entry.argument != null)
strings.add(entry.key + separator1 + entry.argument +
separator2 + entry.description);
else
strings.add(entry.key + separator2 + entry.description);
}
return strings;
}
}
|