diff options
author | Martin Odersky <odersky@gmail.com> | 2003-02-13 14:41:36 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2003-02-13 14:41:36 +0000 |
commit | 4177daab2f54bdb20c71f623296a8bb32616fd12 (patch) | |
tree | 23f08b43f3758e825d5965b336030603a65bbcf7 /sources/scalac/util/PrefixMatcher.java | |
parent | 33d6e170c97ca7b2f991896a0729941a7240b6d6 (diff) | |
download | scala-4177daab2f54bdb20c71f623296a8bb32616fd12.tar.gz scala-4177daab2f54bdb20c71f623296a8bb32616fd12.tar.bz2 scala-4177daab2f54bdb20c71f623296a8bb32616fd12.zip |
Initial version.
Diffstat (limited to 'sources/scalac/util/PrefixMatcher.java')
-rw-r--r-- | sources/scalac/util/PrefixMatcher.java | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/sources/scalac/util/PrefixMatcher.java b/sources/scalac/util/PrefixMatcher.java new file mode 100644 index 0000000000..1b0acd8d7a --- /dev/null +++ b/sources/scalac/util/PrefixMatcher.java @@ -0,0 +1,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; + } + +} |