use of java.io.PushbackReader in project gerrit by GerritCodeReview.
the class GerritCommonTest method reductionLimit.
@Test
public void reductionLimit() throws Exception {
PrologEnvironment env = envFactory.create(machine);
setUpEnvironment(env);
String script = "loopy :- b(5).\nb(N) :- N > 0, !, S = N - 1, b(S).\nb(_) :- true.\n";
SymbolTerm nameTerm = SymbolTerm.create("testReductionLimit");
JavaObjectTerm inTerm = new JavaObjectTerm(new PushbackReader(new StringReader(script), Prolog.PUSHBACK_SIZE));
if (!env.execute(Prolog.BUILTIN, "consult_stream", nameTerm, inTerm)) {
throw new CompileException("Cannot consult " + nameTerm);
}
exception.expect(ReductionLimitException.class);
exception.expectMessage("exceeded reduction limit of 1300");
env.once(Prolog.BUILTIN, "call", new StructureTerm(":", SymbolTerm.create("user"), SymbolTerm.create("loopy")));
}
use of java.io.PushbackReader in project gerrit by GerritCodeReview.
the class RulesCache method consultRules.
private PrologMachineCopy consultRules(String name, Reader rules) throws CompileException {
BufferingPrologControl ctl = newEmptyMachine(systemLoader);
PushbackReader in = new PushbackReader(rules, Prolog.PUSHBACK_SIZE);
try {
if (!ctl.execute(Prolog.BUILTIN, "consult_stream", SymbolTerm.intern(name), new JavaObjectTerm(in))) {
return null;
}
} catch (SyntaxException e) {
throw new CompileException(e.toString(), e);
} catch (TermException e) {
Term m = e.getMessageTerm();
if (m instanceof StructureTerm && "syntax_error".equals(m.name()) && m.arity() >= 1) {
StringBuilder msg = new StringBuilder();
if (m.arg(0) instanceof ListTerm) {
msg.append(Joiner.on(' ').join(((ListTerm) m.arg(0)).toJava()));
} else {
msg.append(m.arg(0).toString());
}
if (m.arity() == 2 && m.arg(1) instanceof StructureTerm && "at".equals(m.arg(1).name())) {
Term at = m.arg(1).arg(0).dereference();
if (at instanceof ListTerm) {
msg.append(" at: ");
msg.append(prettyProlog(at));
}
}
throw new CompileException(msg.toString(), e);
}
throw new CompileException("Error while consulting rules from " + name, e);
} catch (RuntimeException e) {
throw new CompileException("Error while consulting rules from " + name, e);
}
return save(ctl);
}
use of java.io.PushbackReader in project jabref by JabRef.
the class BibtexParser method parse.
/**
* Will parse the BibTex-Data found when reading from reader. Ignores any encoding supplied in the file by
* "Encoding: myEncoding".
* <p>
* The reader will be consumed.
* <p>
* Multiple calls to parse() return the same results
*
* @return ParserResult
* @throws IOException
*/
public ParserResult parse(Reader in) throws IOException {
Objects.requireNonNull(in);
pushbackReader = new PushbackReader(in, BibtexParser.LOOKAHEAD);
// Bibtex related contents.
initializeParserResult();
parseDatabaseID();
skipWhitespace();
try {
return parseFileContent();
} catch (KeyCollisionException kce) {
throw new IOException("Duplicate ID in bibtex file: " + kce);
}
}
use of java.io.PushbackReader in project aries by apache.
the class ConfigurationHandler method readInternal.
private Dictionary readInternal(InputStream ins) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(ins, ENCODING));
PushbackReader pr = new PushbackReader(br, 1);
token = 0;
tokenValue = null;
line = 0;
pos = 0;
Hashtable configuration = new Hashtable();
token = 0;
while (nextToken(pr) == TOKEN_NAME) {
String key = tokenValue;
// expect equal sign
if (nextToken(pr) != TOKEN_EQ) {
throw readFailure(token, TOKEN_EQ);
}
// expect the token value
Object value = readValue(pr);
if (value != null) {
configuration.put(key, value);
}
}
return configuration;
}
use of java.io.PushbackReader in project chipKIT32-MAX by chipKIT32.
the class StdXMLReader method openStream.
/**
* Opens a stream from a public and system ID.
*
* @param publicID the public ID, which may be null
* @param systemID the system ID, which is never null
*
* @throws java.net.MalformedURLException
* if the system ID does not contain a valid URL
* @throws java.io.FileNotFoundException
* if the system ID refers to a local file which does not exist
* @throws java.io.IOException
* if an error occurred opening the stream
*/
public Reader openStream(String publicID, String systemID) throws MalformedURLException, FileNotFoundException, IOException {
URL url = new URL(this.currentReader.systemId, systemID);
if (url.getRef() != null) {
String ref = url.getRef();
if (url.getFile().length() > 0) {
url = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile());
url = new URL("jar:" + url + '!' + ref);
} else {
url = StdXMLReader.class.getResource(ref);
}
}
this.currentReader.publicId = publicID;
this.currentReader.systemId = url;
StringBuffer charsRead = new StringBuffer();
Reader reader = this.stream2reader(url.openStream(), charsRead);
if (charsRead.length() == 0) {
return reader;
}
String charsReadStr = charsRead.toString();
PushbackReader pbreader = new PushbackReader(reader, charsReadStr.length());
for (int i = charsReadStr.length() - 1; i >= 0; i--) {
pbreader.unread(charsReadStr.charAt(i));
}
return pbreader;
}
Aggregations