/** * Creates a new {@link ParserException} using the message provided. Do not * use this constructor if you can pass a SourceInfo. * * @param msg * Informative message about the exception. * @param file * the file that caused the error. Must be not null. */ public ParserException(String msg, File file) { this(msg, new FileSourceInfo(msg, file)); }
/** * Check that this database can be modified * * @throws KRDatabaseException */ private void checkModifyable() throws KRDatabaseException { if (this.isStatic) { throw new KRDatabaseException("Database is static and can not be modified"); } }
@Override public String toString() { return "ParserException: " + getMessage() + " " + this.info; }
@Override public Parser getParser(Reader r, SourceInfo info) throws ParserException { try { return new KRInterfaceParser4(r, info); } catch (IOException e) { throw new ParserException("failed to parse the reader data as SWI Prolog.", info, e); } }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } else if (obj == null || !(obj instanceof ParserException)) { return false; } else { ParserException other = (ParserException) obj; if (this.info == null) { if (other.getSourceInfo() != null) { return false; } } else if (!this.info.equals(other.getSourceInfo())) { return false; } if (getMessage() == null) { if (other.getMessage() != null) { return false; } } else if (!getMessage().equals(other.getMessage())) { return false; } return true; } }
@Override public int compareTo(SourceInfo o) { ParserException other = (ParserException) o; if (equals(other)) { return 0; } else if (other.getSourceInfo() == null) { return (this.info == null) ? 0 : -1; } else if (this.info == null) { return 1; } else { return before(this.info, other.getSourceInfo()) ? -1 : 1; } }
/** * Reports a parsing exception that occurred while parsing embedded language * fragments. * * @param e * The exception generated by the embedded language parser. TODO: the * ctx is no longer passed, how is this handled now? * @param ctx * The context of the parser where the embedded language fragment is * located. */ public void reportParsingException(ParserException e) { String msg = e.getMessage(); if (e.getCause() != null && e.getCause().getMessage() != null) { msg += " because " + e.getCause().getMessage(); } reportError(SyntaxError.EMBEDDED_LANGUAGE_ERROR, e, KRFactory.getName(this.program.getKRInterface()), msg); }
/** * Provides an available knowledge representation technology. See the * getSupportedKRs method for the set of supported KRTs. * * @param name * The name of the KRT. * @return A KR interface implementation. * @throws KRInterfaceNotSupportedException * If the name does not map to a known interface. * @throws KRInitFailedException * If the creation of the requested implementation failed. */ public static KRInterface getKR(String name) throws KRInterfaceNotSupportedException, KRInitFailedException { try { KRInterface krInterface = kr.containsKey(name) ? kr.get(name).newInstance() : null; if (krInterface == null) { throw new KRInterfaceNotSupportedException( "could not find KRT '" + name + "' as only these are available: " + kr.keySet() + "."); } else { return krInterface; } } catch (IllegalAccessException | InstantiationException e) { throw new KRInitFailedException("failed to initialize KRT '" + name + "'.", e); } }
@Override public String toString() { return "<" + getClass().getSimpleName() + ": " + getMessage() + ", " + getCause() + ">"; }
/** * Adds new error for token recognition problem (lexer). * * @param pos * input stream position * @param stop * stopIndex of last recognition error * @param text * character(s) that could not be recognized */ public void handleLexerError(Recognizer<?, ?> recognizer, Object offendingSymbol, SourceInfoObject pos, String text, RecognitionException e) { text = text.replace("\\r", "").replace("\\n", " ").replace("\\t", " ").replace("\\f", ""); this.errors.add(new ParserException(ParserErrorMessages.CANNOT_BE_USED.toReadableString(text), pos)); }
/** * Check there is no predicate defined both in knowledge and beliefs. * * @param knowledgeDefined * @param knowledgeDeclared * @param allBeliefs * @throws ParserException */ protected void checkNoPredicatesBeliefAndKnowledge(Map<String, SourceInfo> knowledgeDefined, Map<String, SourceInfo> knowledgeDeclared, Collection<DatabaseFormula> allBeliefs) { try { Map<String, SourceInfo> signatures = getDefinedSignatures(allBeliefs); signatures.putAll(getDeclaredSignatures(allBeliefs)); signatures.putAll(knowledgeDeclared); for (String signature : signatures.keySet()) { if (knowledgeDefined.containsKey(signature)) { reportError(MASError.PREDICATE_ALREADY_KNOWLEDGE, signatures.get(signature), signature, knowledgeDefined.get(signature).toString()); } } } catch (ParserException e) { reportError(MASError.NO_SIGNATURE, e.getSourceInfo(), e.getMessage()); } } }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((this.info == null) ? 0 : this.info.hashCode()); result = prime * result + ((getMessage() == null) ? 0 : getMessage().hashCode()); return result; }
/** * Provides a dependency graph for a certain knowledge representation. * * @param kri * The knowledge representation interface. * @return A DependencyGraph implementation. * @throws KRInterfaceNotSupportedException * If the interface does not map to a known implementation. * @throws KRInitFailedException * If the creation of the requested implementation failed. */ public static DependencyGraph<?> getDependencyGraph(KRInterface kri) throws KRInterfaceNotSupportedException, KRInitFailedException { try { DependencyGraph<?> graph = graphs.containsKey(kri.getClass()) ? graphs.get(kri.getClass()).newInstance() : null; if (graph == null) { throw new KRInterfaceNotSupportedException("could not find a dependency graph implementation for '" + KRFactory.getName(kri) + "' as only these are available: " + graphs.keySet() + "."); } else { return graph; } } catch (IllegalAccessException | InstantiationException e) { throw new KRInitFailedException( "failed to initialize a dependency graph for '" + KRFactory.getName(kri) + "'.", e); } } }
/** * */ @Override public void add(Query query) throws KRException { PrologCompound compound = ((PrologQuery) query).getCompound(); if (compound.isQuery()) { throw new KRDatabaseException("a clause with main operator :-/2 cannot be queried."); } else { addTerm(compound, false, true); } }
@Override public Update visitGoalAdopt(String krFragment, SourceInfo info) throws ParserException { Update adopt = super.visitGoalAdopt(krFragment, info); if (adopt != null && !adopt.getDeleteList().isEmpty()) { // TODO: message(properties) system throw new ParserException("cannot adopt negative facts", info); } return adopt; }
private void addToWriteCache(PrologCompound formula) throws KRDatabaseException { if (this.writecache == null) { this.writecache = formula; } else { this.writecache = PrologImplFactory.getCompound(",", new Term[] { this.writecache, formula }, formula.getSourceInfo()); } if (++this.cachecount == Byte.MAX_VALUE) { try { // prevents stackoverflows flushWriteCache(); } catch (KRQueryFailedException e) { throw new KRDatabaseException("", e); } } }
@Override public Update visitGoalDrop(String krFragment, SourceInfo info) throws ParserException { Update drop = super.visitGoalAdopt(krFragment, info); if (drop != null && !drop.getDeleteList().isEmpty()) { // TODO: message(properties) system throw new ParserException("cannot drop negative facts", info); } return drop; }
public CognitiveKR getCognitiveKR() throws ParserException { if (this.program == null || this.program.getKRInterface() == null) { throw new ParserException("cannot get cognitive KR for a null program or KRI.", new File(this.filename)); } else { try { return CognitiveKRFactory.getCognitiveKR(this.program.getKRInterface()); } catch (InstantiationFailedException e) { throw new ParserException("could not instantiate cognitive KR.", this.program.getSourceInfo(), e); } } }
/** * Validate the variable. Returns a variable obtained by parsing the input. * * @return {@link Var} or null if error occurred. */ public Var var() { Term term = this.visitor.visitTerm0(); if (term instanceof Var) { return (Var) term; } else { this.errors.add(new ParserException(ParserErrorMessages.EXPECTED_VAR.toReadableString(term), (term == null) ? null : term.getSourceInfo())); return null; } }
/** * Check if we processed the whole stream we were given */ private void checkEndOfInputReached() { if (this.stream.index() < this.stream.size()) { final SourceInfoObject info = new SourceInfoObject(this.sourceInfo.getSource(), this.lexer.getLine(), this.lexer.getCharPositionInLine(), this.sourceInfo.getStartIndex() + this.stream.index() + 1, this.sourceInfo.getStartIndex() + this.stream.size() + 1); this.errors.add(new ParserException("Unrecognized spurious input", info)); } }