@Override public Term makeList(List<Term> terms) throws MSTTranslationException { SourceInfo source = null; if (!terms.isEmpty()) { source = terms.get(0).getSourceInfo(); } // Start with element in list, since innermost term of Prolog list is // the last term. Term list = PrologImplFactory.getAtom(JPL.LIST_NIL.name(), source); for (int i = terms.size() - 1; i >= 0; i--) { list = PrologImplFactory.getCompound(JPL.LIST_PAIR, new Term[] { terms.get(i), list }, source); } return list; }
/** * Validate a Prolog program. Assumes that the parser has been set up properly. * * @return List<DatabaseFormula>, or {@code null} if a parser error occurs. */ public List<DatabaseFormula> program() { List<DatabaseFormula> dbfs = new LinkedList<>(); for (Term t : this.visitor.visitPrologtext()) { if (t instanceof PrologCompound) { try { dbfs.add(SemanticTools.DBFormula((PrologCompound) t)); } catch (ParserException e) { this.errors.add(e); } } else { this.errors.add(new ParserException(ParserErrorMessages.EXPECTED_COMPOUND.toReadableString(t), (t == null) ? null : t.getSourceInfo())); } } return dbfs; }
/** * 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; } }
/** * Validate a section that should contain Prolog goals, i.e., queries. * * @return List<Query>, or {@code null} if a parser error occurs. */ public List<Query> goalSection() { List<Query> goals = new LinkedList<>(); for (Term t : this.visitor.visitPrologtext()) { if (t instanceof PrologCompound) { try { goals.add(PrologImplFactory.getQuery(SemanticTools.toGoal(t))); } catch (ParserException e) { this.errors.add(e); } } else { this.errors.add(new ParserException(ParserErrorMessages.EXPECTED_COMPOUND.toReadableString(t), (t == null) ? null : t.getSourceInfo())); } } return goals; }
/** * Validate a (possibly empty) query. * * @return A {@link PrologQuery}, or {@code null} if an error occurred. */ public PrologQuery queryOrEmpty() { Term term = this.visitor.visitPossiblyEmptyDisjunct(); if (term instanceof PrologCompound) { try { return SemanticTools.toQuery((PrologCompound) term); } catch (ParserException e) { this.errors.add(e); } } else { this.errors.add(new ParserException(ParserErrorMessages.EXPECTED_COMPOUND.toReadableString(term), (term == null) ? null : term.getSourceInfo())); } return null; }
/** * Validate an update or empty term. * * @return {@link Update} or null if there is error. */ public Update updateOrEmpty() { Term conj = this.visitor.visitPossiblyEmptyConjunct(); if (!(conj instanceof PrologCompound)) { this.errors.add(new ParserException(ParserErrorMessages.EXPECTED_COMPOUND.toReadableString(conj), (conj == null) ? null : conj.getSourceInfo())); return null; } else if (conj.toString().equals("true")) { // special case. return PrologImplFactory.getUpdate((PrologCompound) conj); } else { try { return SemanticTools.conj2Update((PrologCompound) conj); } catch (ParserException e) { this.errors.add(e); return null; } } }
Term stubfunc = PrologImplFactory.getCompound(content.getName(), new Term[] { anon }, term.getSourceInfo()); terms.add(stubfunc); } else if (signature.equals(";/2") || signature.equals(",/2") || signature.equals("forall/2")) { Term anon = PrologImplFactory.getVar("_", null); Term stubfunc = PrologImplFactory.getCompound(content.getName(), new Term[] { anon, anon, anon }, term.getSourceInfo()); terms.add(stubfunc); } else {
/** * Checks that compound is a well formed Prolog goal. ISO requires rebuild * of the term but in our case we do not allow variables and hence a real * rebuild is not necessary. Instead, we simply return the original term * after checking. * * @return the compound "rewritten" as a Prolog goal according to ISO 7.6.2. * @throws ParserException * If t is not a well formed Prolog goal. */ public static PrologCompound toGoal(Term t) throws ParserException { if (!(t instanceof PrologCompound)) { throw new ParserException(ParserErrorMessages.EXPECTED_COMPOUND.toReadableString(t), t.getSourceInfo()); } PrologCompound c = (PrologCompound) t; switch (c.getSignature()) { case ":-/1": throw new ParserException(ParserErrorMessages.DIRECTIVE_NOT_AS_GOAL.toReadableString(t), t.getSourceInfo()); case ":-/2": throw new ParserException(ParserErrorMessages.CLAUSE_NOT_AS_GOAL.toReadableString(t), t.getSourceInfo()); case ",/2": case ";/2": case "->/2": toGoal(c.getArg(0)); toGoal(c.getArg(1)); break; } return c; }