[Add parser which tries all parsers. bjorn@bringert.net**20040809091024] < > { hunk ./TestTranslateGUI.java 19 - ps.addParser("tree", new ParseTree()); +// FIXME: most inputs parse as trees, so we get too many ambiguous parses with the tree parser +// ps.addParser("tree", new ParseTree()); addfile ./parse/ParseAll.java hunk ./parse/ParseAll.java 1 +package parse; + +import abssyn.Tree; +import util.Pair; + +import java.util.*; + +public class ParseAll implements Parser { + + private Parsers parsers; + + public ParseAll(Parsers parsers) { + this.parsers = parsers; + } + + public List parse(String str) { + List> ts = parsers.parseWithAll(str); + List rs = new LinkedList(); + for (Pair p : ts) + rs.add(p.snd); + return rs; + } + + /** + * Ignores the category argument. + */ + public List parse(String str, String cat) { + return parse(str); + } + +} hunk ./parse/ParseDecimal.java 10 - long l = Long.parseLong(s); - - if (l < 1) - throw new RuntimeException("Numerals < 1 not supported."); - - Tree t = conv3(l); - return Arrays.asList(t); + try { + long l = Long.parseLong(s); + + if (l < 1) + throw new RuntimeException("Numerals < 1 not supported."); + + Tree t = conv3(l); + return Arrays.asList(t); + } catch (NumberFormatException ex) { + return new LinkedList(); + } hunk ./parse/ParseTree.java 14 - Tree t = GFTreeToTree.toTree(TreeParser.parseTree(s)); - return Arrays.asList(t); + try { + Tree t = GFTreeToTree.toTree(TreeParser.parseTree(s)); + return Arrays.asList(t); + } catch (RuntimeException ex) { + System.err.println(ex); + return new LinkedList(); + } hunk ./parse/Parsers.java 3 +import abssyn.Tree; +import util.Pair; +import static util.Pair.pair; + hunk ./parse/Parsers.java 36 + + /** + * Try parsing the input with all the available parsers. + * + * @return A list of pairs (language, parse tree) + */ + public List> parseWithAll(String str) { + List> rs + = new LinkedList>(); + for (Map.Entry e : map.entrySet()) { + List ts = e.getValue().parse(str); + for (Tree t : ts) + rs.add(pair(e.getKey(), t)); + } + return rs; + } + + hunk ./test.html 2 + +GF Translation Applet + hunk ./translate/Translator.java 23 - Parser p = parsers.getParser(fromLang); + Parser p; + if (fromLang.equals("all")) + p = new ParseAll(parsers); + else + p = parsers.getParser(fromLang); hunk ./translate/Translator.java 29 + return translate(p, l, text); + } + + private String translate(Parser p, Linearizer l, String text) { hunk ./translategui/TranslatePanel.java 34 + inputLang.addItem("all"); hunk ./translategui/TranslatePanel.java 68 - String outputText = translator.translate(fromLang, toLang, inputText); + String outputText; + outputText = translator.translate(fromLang, toLang, inputText); hunk ./util/Pair.java 10 - /** - * The first component. - */ - public final A fst; - - /** - * The second component. - */ - public final B snd; - - /** - * Creates a new pair. - * - * @param fst The first component. - * @param snd The second component. - */ - public Pair (A fst, B snd) { - this.fst = fst; - this.snd = snd; - } + /** + * The first component. + */ + public final A fst; + + /** + * The second component. + */ + public final B snd; + + /** + * Creates a new pair. + * + * @param fst The first component. + * @param snd The second component. + */ + public Pair (A fst, B snd) { + this.fst = fst; + this.snd = snd; + } + + /** + * Compares this pair to another object for equality. + * + * @param o The object to compare this pair with. + * @return true iff o is not null and o is a Pair and + * the components of this and o are pairwise equal or both null. + */ + public boolean equals (Object o) { + if (o != null && o instanceof Pair) { + Pair p = (Pair)o; + return (fst == null ? p.fst == null : fst.equals(p.fst)) + && (snd == null ? p.snd == null : snd.equals(p.snd)); + } + + return false; + } hunk ./util/Pair.java 48 - /** - * Compares this pair to another object for equality. - * - * @param o The object to compare this pair with. - * @return true iff o is not null and o is a Pair and - * the components of this and o are pairwise equal or both null. - */ - public boolean equals (Object o) { - if (o != null && o instanceof Pair) { - Pair p = (Pair)o; - return (fst == null ? p.fst == null : fst.equals(p.fst)) - && (snd == null ? p.snd == null : snd.equals(p.snd)); - } - - return false; - } - - /** - * Returns a hash code for this pair. The hash code is calculated from - * the hash codes of the components. - */ - public int hashCode () { - return (fst == null ? 0 : fst.hashCode()) - ^ (snd == null ? 0 : snd.hashCode()); - } - - public String toString() { - return "Pair(" + fst + ", " + snd + ")"; - } + /** + * Returns a hash code for this pair. The hash code is calculated from + * the hash codes of the components. + */ + public int hashCode () { + return (fst == null ? 0 : fst.hashCode()) + ^ (snd == null ? 0 : snd.hashCode()); + } + + public String toString() { + return "Pair(" + fst + ", " + snd + ")"; + } + + /** + * Gets an array representation of this pair. + * + * @return A 2-element array containing the elements of + * this pair in order. + */ + public Object[] toArray () { + return new Object[]{ fst, snd }; + } + + /** + * Construct a pair. Convenience method to allow type inference and + */ + public static Pair pair(A a, B b) { + return new Pair(a,b); + } hunk ./util/Pair.java 78 - /** - * Gets an array representation of this pair. - * - * @return A 2-element array containing the elements of - * this pair in order. - */ - public Object[] toArray () { - return new Object[]{ fst, snd }; - } }