diff --git a/src/runtime/java/org/grammaticalframework/pgf/Completer.java b/src/runtime/java/org/grammaticalframework/pgf/Completer.java new file mode 100644 index 000000000..21d3ea127 --- /dev/null +++ b/src/runtime/java/org/grammaticalframework/pgf/Completer.java @@ -0,0 +1,37 @@ +package org.grammaticalframework.pgf; + +import java.util.*; + +class Completer implements Iterable { + private Concr concr; + private String s; + private String prefix; + private String startCat; + private TokenIterator iter; + + public Completer(Concr concr, String startCat, String s, String prefix) throws ParseError { + this.concr = concr; + this.startCat = startCat; + this.s = s; + this.prefix = prefix; + this.iter = complete(concr, startCat, s, prefix); + } + + public Iterator iterator() { + if (iter == null) { + // If someone has asked for a second iterator over + // the same parse results then we have to parse again. + try { + return complete(concr, startCat, s, prefix); + } catch (ParseError e) { + return null; + } + } else { + TokenIterator tmp_iter = iter; + iter = null; + return tmp_iter; + } + } + + static native TokenIterator complete(Concr concr, String startCat, String s, String prefix) throws ParseError; +}