Search in sources :

Example 31 with PushbackReader

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")));
}
Also used : StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) StringReader(java.io.StringReader) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) PushbackReader(java.io.PushbackReader) Test(org.junit.Test)

Example 32 with PushbackReader

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);
}
Also used : TermException(com.googlecode.prolog_cafe.exceptions.TermException) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) SyntaxException(com.googlecode.prolog_cafe.exceptions.SyntaxException) BufferingPrologControl(com.googlecode.prolog_cafe.lang.BufferingPrologControl) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) Term(com.googlecode.prolog_cafe.lang.Term) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) PushbackReader(java.io.PushbackReader)

Example 33 with PushbackReader

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);
    }
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) IOException(java.io.IOException) PushbackReader(java.io.PushbackReader)

Example 34 with PushbackReader

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) Hashtable(java.util.Hashtable) BufferedReader(java.io.BufferedReader) PushbackReader(java.io.PushbackReader)

Example 35 with PushbackReader

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;
}
Also used : LineNumberReader(java.io.LineNumberReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) PushbackReader(java.io.PushbackReader) URL(java.net.URL) PushbackReader(java.io.PushbackReader)

Aggregations

PushbackReader (java.io.PushbackReader)37 IOException (java.io.IOException)22 StringReader (java.io.StringReader)12 CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)3 JavaObjectTerm (com.googlecode.prolog_cafe.lang.JavaObjectTerm)3 SymbolTerm (com.googlecode.prolog_cafe.lang.SymbolTerm)3 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 StructureTerm (com.googlecode.prolog_cafe.lang.StructureTerm)2 FileNotFoundException (java.io.FileNotFoundException)2 LineNumberReader (java.io.LineNumberReader)2 Lexer (net.sourceforge.processdash.data.compiler.lexer.Lexer)2 Parser (net.sourceforge.processdash.data.compiler.parser.Parser)2 SyntaxException (com.googlecode.prolog_cafe.exceptions.SyntaxException)1 TermException (com.googlecode.prolog_cafe.exceptions.TermException)1 BufferingPrologControl (com.googlecode.prolog_cafe.lang.BufferingPrologControl)1 ListTerm (com.googlecode.prolog_cafe.lang.ListTerm)1 Term (com.googlecode.prolog_cafe.lang.Term)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1