/** * @return Character position where exception occurred, or {@code -1} if no * position is available. */ @Override public int getCharacterPosition() { if (this.info != null) { return this.info.getCharacterPosition(); } else { return -1; } }
/** * @param info1 * A source info object. * @param info2 * A source info object. * @return {@code true} if source position of info1 object occurs before * position of info2 object. */ private static boolean before(SourceInfo info1, SourceInfo info2) { boolean source = info1.getSource() != null && info2.getSource() != null && (info1.getSource().compareTo(info2.getSource()) < 0); boolean sourceEqual = info1.getSource() != null && info2.getSource() != null && (info1.getSource().compareTo(info2.getSource()) == 0); boolean lineNr = sourceEqual && (info1.getLineNumber() < info2.getLineNumber()); boolean lineNrEqual = (info1.getLineNumber() == info2.getLineNumber()); boolean position = sourceEqual && lineNrEqual && (info1.getCharacterPosition() < info2.getCharacterPosition()); return source || lineNr || position; }
/** * Checks wether the position if the first source info is before the second * one. * * @param info1 * A source info object. * @param info2 * A source info object. * @return {@code true} if source position of info1 object occurs before * position of info2 object. */ private static boolean before(SourceInfo info1, SourceInfo info2) { boolean source = info1.getSource() != null && info2.getSource() != null && (info1.getSource().compareTo(info2.getSource()) < 0); boolean sourceEqual = info1.getSource() != null && info2.getSource() != null && (info1.getSource().compareTo(info2.getSource()) == 0); boolean lineNr = sourceEqual && (info1.getLineNumber() < info2.getLineNumber()); boolean lineNrEqual = (info1.getLineNumber() == info2.getLineNumber()); boolean position = sourceEqual && lineNrEqual && (info1.getCharacterPosition() < info2.getCharacterPosition()); return source || lineNr || position; } }
@Override public int compareTo(SourceInfo o) { if (getSource() == null) { if (o.getSource() != null) { return -1; } } else { if (o.getSource() == null) { return 1; } // both files not null. int filecompare = getSource().compareTo(o.getSource()); if (filecompare != 0) { return filecompare; } } // files are equal (or both null). int linecompare = getLineNumber() - o.getLineNumber(); if (linecompare != 0) { return linecompare; } // lines are equal return getCharacterPosition() - o.getCharacterPosition(); } }
@Override public int compareTo(SourceInfo o) { if (getSource() == null) { if (o.getSource() != null) { return -1; } } else { if (o.getSource() == null) { return 1; } // both files not null. int filecompare = getSource().compareTo(o.getSource()); if (filecompare != 0) { return filecompare; } } // files are equal (or both null). int linecompare = getLineNumber() - o.getLineNumber(); if (linecompare != 0) { return linecompare; } // lines are equal return getCharacterPosition() - o.getCharacterPosition(); }
@Override public boolean reportError(SyntaxError type, SourceInfo info, String... args) { SourceInfo isp = null; if (info != null) { isp = new InputStreamPosition(info.getLineNumber(), info.getCharacterPosition(), ctxSource.getStartIndex() + info.getStartIndex(), ctxSource.getStartIndex() + info.getStopIndex() + 1, info.getSource()); } return super.reportError(type, isp, args); }
@Override public boolean reportError(SyntaxError type, SourceInfo info, String... args) { SourceInfo isp = null; if (info != null) { isp = new InputStreamPosition(info.getLineNumber(), info.getCharacterPosition(), ctxSource.getStartIndex() + info.getStartIndex(), ctxSource.getStartIndex() + info.getStopIndex() + 1, info.getSource()); } return super.reportError(type, isp, args); }
@Override public TestMentalStateCondition visitPost(PostContext ctx) { String post = (ctx.KR_BLOCK() == null) ? "" : removeLeadTrailCharacters(ctx.KR_BLOCK().getText()); if (post.trim().isEmpty()) { return null; } else { SourceInfo info = getSourceInfo(ctx.KR_BLOCK()); info = (info == null) ? null : new InputStreamPosition(info.getLineNumber(), info.getCharacterPosition() + 1, info.getStartIndex() + 1, info.getStopIndex() - 1, info.getSource()); TEST2GParser parser = prepareTestParser(post, info); TestmscContext mscContext = parser.testmsc(); TestValidator sub = getTestSub("postcondition", info); return sub.visitTestmsc(mscContext); } }
/** * Creates an embedded module parser that can parse the given string. * * @param pString * is the string to be parsed. * @return a MOD2GParser. */ private MOD2GParser prepareModuleParser(String pString, SourceInfo sourceInfo) { String name = (sourceInfo.getSource() == null) ? "" : sourceInfo.getSource(); CharStream stream = CharStreams.fromString(pString, name); GOALLexer lexer = new GOALLexer(stream); lexer.removeErrorListeners(); lexer.addErrorListener(this); lexer.setLine(sourceInfo.getLineNumber()); lexer.setCharPositionInLine(sourceInfo.getCharacterPosition() + 1); MOD2GParser parser = new MOD2GParser(new CommonTokenStream(lexer)); parser.setErrorHandler(new ModuleErrorStrategy()); parser.removeErrorListeners(); parser.addErrorListener(this); return parser; }
/** * Report syntax error. * * Collects details about the exact position in the input stream from an ANTLR * ParserRuleContext object. * * @param type * The type of syntax error that is added. * @param info * Source info object. * @param args * Additional info to be inserted into warning message. */ public boolean reportError(SyntaxError type, SourceInfo info, String... args) { // FIXME: Make SourceInfo in KRTools abstract class and move code from // InputStreamPosition to SourceInfo. // Map onto input stream position for pretty printing of code location. SourceInfo isp = null; if (info != null) { isp = new InputStreamPosition(info.getLineNumber(), info.getCharacterPosition(), info.getStartIndex(), info.getStopIndex(), info.getSource()); } return this.registry.addSyntaxError(new ParserError(type, isp, args)); }
@Override public TestMentalStateCondition visitPre(PreContext ctx) { String pre = (ctx.KR_BLOCK() == null) ? "" : removeLeadTrailCharacters(ctx.KR_BLOCK().getText()); if (pre.trim().isEmpty()) { return null; } else { SourceInfo info = getSourceInfo(ctx.KR_BLOCK()); info = (info == null) ? null : new InputStreamPosition(info.getLineNumber(), info.getCharacterPosition() + 1, info.getStartIndex() + 1, info.getStopIndex() - 1, info.getSource()); TEST2GParser parser = prepareTestParser(pre, info); TestmscContext mscContext = parser.testmsc(); TestValidator sub = getTestSub("precondition", info); return sub.visitTestmsc(mscContext); } }
/** * Creates an embedded test parser that can parse the given string. * * @param pString * is the string to be parsed. * @return a TEST2GParser. */ private TEST2GParser prepareTestParser(String pString, SourceInfo sourceInfo) { String name = (sourceInfo.getSource() == null) ? "" : sourceInfo.getSource(); CharStream stream = CharStreams.fromString(pString, name); GOALLexer lexer = new GOALLexer(stream); lexer.removeErrorListeners(); lexer.addErrorListener(this); lexer.setLine(sourceInfo.getLineNumber()); lexer.setCharPositionInLine(sourceInfo.getCharacterPosition() + 1); TEST2GParser parser = new TEST2GParser(new CommonTokenStream(lexer)); parser.setErrorHandler(new TestErrorStrategy()); parser.removeErrorListeners(); parser.addErrorListener(this); return parser; }
/** * Delegates parsing of terminal node of parameter list to KR parser. * * @param parlist * Terminal node representing a parameter list. * @param info * Source information. * @return {@code null} if there are no parameters, a list of terms otherwise. */ public List<Term> visitPARLIST(TerminalNode parlist, SourceInfo info) { List<Term> parameters = null; if (parlist != null) { try { CognitiveKR ckr = getCognitiveKR(); info = (info == null) ? null : new InputStreamPosition(info.getLineNumber(), info.getCharacterPosition() + 1, info.getStartIndex() + 1, info.getStopIndex() - 1, info.getSource()); parameters = ckr.visitArguments(removeLeadTrailCharacters(parlist.getText()), info); reportEmbeddedLanguageErrors(ckr); } catch (ParserException e) { reportParsingException(e); } } if (parameters == null) { return new ArrayList<>(0); } else { return parameters; } }
this.lexer.addErrorListener(this); this.lexer.setLine(this.sourceInfo.getLineNumber()); this.lexer.setCharPositionInLine(this.sourceInfo.getCharacterPosition() + 1);
SourceInfo krInfo = getSourceInfo(ctx.PARLIST()); krInfo = (krInfo == null) ? null : new InputStreamPosition(krInfo.getLineNumber(), krInfo.getCharacterPosition() + 1, krInfo.getStartIndex() + 1, krInfo.getStopIndex() - 1, krInfo.getSource());
@Override public ActionPostCondition visitPostadd(PostaddContext ctx) { String krFragment = (ctx.KR_BLOCK() == null) ? "" : removeLeadTrailCharacters(ctx.KR_BLOCK().getText()); Update post = null; try { CognitiveKR ckr = getCognitiveKR(); SourceInfo info = getSourceInfo(ctx.KR_BLOCK()); info = (info == null) ? null : new InputStreamPosition(info.getLineNumber(), info.getCharacterPosition() + 1, info.getStartIndex() + 1, info.getStopIndex() - 1, info.getSource()); post = ckr.visitBeliefInsert(krFragment, info); reportEmbeddedLanguageErrors(ckr); } catch (ParserException e) { reportParsingException(e); } return (post == null) ? null : new ActionPostCondition(post, false, getSourceInfo(ctx)); }
@Override public ActionPostCondition visitPostdel(PostdelContext ctx) { String krFragment = (ctx.KR_BLOCK() == null) ? "" : removeLeadTrailCharacters(ctx.KR_BLOCK().getText()); Update post = null; try { CognitiveKR ckr = getCognitiveKR(); SourceInfo info = getSourceInfo(ctx.KR_BLOCK()); info = (info == null) ? null : new InputStreamPosition(info.getLineNumber(), info.getCharacterPosition() + 1, info.getStartIndex() + 1, info.getStopIndex() - 1, info.getSource()); post = ckr.visitBeliefDelete(krFragment, info); reportEmbeddedLanguageErrors(ckr); } catch (ParserException e) { reportParsingException(e); } return (post == null) ? null : new ActionPostCondition(post, true, getSourceInfo(ctx)); }
@Override public ActionPostCondition visitPost(PostContext ctx) { String krFragment = (ctx.KR_BLOCK() == null) ? "" : removeLeadTrailCharacters(ctx.KR_BLOCK().getText()); Update post = null; try { CognitiveKR ckr = getCognitiveKR(); SourceInfo info = getSourceInfo(ctx.KR_BLOCK()); info = (info == null) ? null : new InputStreamPosition(info.getLineNumber(), info.getCharacterPosition() + 1, info.getStartIndex() + 1, info.getStopIndex() - 1, info.getSource()); post = ckr.visitBeliefInsert(krFragment, info); reportEmbeddedLanguageErrors(ckr); } catch (ParserException e) { reportParsingException(e); } return (post == null) ? null : new ActionPostCondition(post, false, getSourceInfo(ctx)); }
@Override public ActionPreCondition visitPre(PreContext ctx) { String krFragment = (ctx.KR_BLOCK() == null) ? "" : removeLeadTrailCharacters(ctx.KR_BLOCK().getText()); Query pre = null; Set<String> signatures = null; try { CognitiveKR ckr = getCognitiveKR(); SourceInfo info = getSourceInfo(ctx.KR_BLOCK()); info = (info == null) ? null : new InputStreamPosition(info.getLineNumber(), info.getCharacterPosition() + 1, info.getStartIndex() + 1, info.getStopIndex() - 1, info.getSource()); pre = ckr.visitBeliefQuery(krFragment, info); if (pre != null) { signatures = ckr.getUsedSignatures(pre); } reportEmbeddedLanguageErrors(ckr); } catch (ParserException e) { reportParsingException(e); } return (pre == null) ? null : new ActionPreCondition(pre, signatures, getSourceInfo(ctx)); }
SourceInfo krInfo = getSourceInfo(ctx.PARLIST()); krInfo = (krInfo == null) ? null : new InputStreamPosition(krInfo.getLineNumber(), krInfo.getCharacterPosition() + 1, krInfo.getStartIndex() + 1, krInfo.getStopIndex() - 1, krInfo.getSource()); try {