@Override public int getStartIndex() { if (this.info != null) { return this.info.getStartIndex(); } else { return -1; } }
@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 SourceInfo getSourceInfo(TerminalNode leaf) { Token symbol = (leaf == null) ? null : leaf.getSymbol(); return (symbol == null) ? null : new InputStreamPosition(symbol.getLine(), symbol.getCharPositionInLine(), ctxSource.getStartIndex() + symbol.getStartIndex(), ctxSource.getStartIndex() + symbol.getStopIndex() + 1, ctxSource.getSource()); } };
@Override public SourceInfo getSourceInfo(TerminalNode leaf) { Token symbol = (leaf == null) ? null : leaf.getSymbol(); return (symbol == null) ? null : new InputStreamPosition(symbol.getLine(), symbol.getCharPositionInLine(), ctxSource.getStartIndex() + symbol.getStartIndex(), ctxSource.getStartIndex() + symbol.getStopIndex() + 1, ctxSource.getSource()); } };
private SourceInfo getSourceInfo(TerminalNode leaf) { Token symbol = (leaf == null) ? null : leaf.getSymbol(); return (symbol == null) ? null : new SourceInfoObject(this.source.getSource(), symbol.getLine(), symbol.getCharPositionInLine(), this.source.getStartIndex() + symbol.getStartIndex(), this.source.getStartIndex() + symbol.getStopIndex()); }
@Override public SourceInfo getSourceInfo(ParserRuleContext ctx) { Token start = (ctx == null) ? null : ctx.getStart(); Token stop = (ctx == null) ? null : ctx.getStop(); if (stop == null) { stop = start; } return (start == null) ? null : new InputStreamPosition(start.getLine(), start.getCharPositionInLine(), ctxSource.getStartIndex() + start.getStartIndex(), ctxSource.getStartIndex() + stop.getStopIndex() + 1, ctxSource.getSource()); }
@Override public SourceInfo getSourceInfo(ParserRuleContext ctx) { Token start = (ctx == null) ? null : ctx.getStart(); Token stop = (ctx == null) ? null : ctx.getStop(); if (stop == null) { stop = start; } return (start == null) ? null : new InputStreamPosition(start.getLine(), start.getCharPositionInLine(), ctxSource.getStartIndex() + start.getStartIndex(), ctxSource.getStartIndex() + stop.getStopIndex() + 1, ctxSource.getSource()); }
/** * 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)); } }
/*************** Implements {@link ANTLRErrorListener} *******************/ @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { int start = recognizer.getInputStream().index(); int stop = start; if (offendingSymbol != null) { CommonToken token = (CommonToken) offendingSymbol; start = token.getStartIndex(); stop = token.getStopIndex(); } SourceInfoObject pos = new SourceInfoObject(this.sourceInfo.getSource(), line, charPositionInLine, this.sourceInfo.getStartIndex() + start + 1, this.sourceInfo.getStartIndex() + stop + 1); if (recognizer instanceof Lexer) { handleLexerError(recognizer, offendingSymbol, pos, msg, e); } else { handleParserError(recognizer, offendingSymbol, pos, msg, e); } }
/** * Create {@link SourceInfoObject} for given context. * * @param ctx * the {@link DirectiveContext} from the parsed object * @return {@link SourceInfoObject} */ private SourceInfo getSourceInfo(ParserRuleContext ctx) { Token start = (ctx == null) ? null : ctx.getStart(); Token stop = (ctx == null) ? null : ctx.getStop(); if (stop == null) { // happens if we are at EOF... stop = start; } return (start == null) ? null : new SourceInfoObject(this.source.getSource(), start.getLine(), start.getCharPositionInLine(), this.source.getStartIndex() + start.getStartIndex(), this.source.getStartIndex() + stop.getStopIndex()); }
@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); } }
/** * 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); } }
/** * 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; } }
@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)); }
krInfo = (krInfo == null) ? null : new InputStreamPosition(krInfo.getLineNumber(), krInfo.getCharacterPosition() + 1, krInfo.getStartIndex() + 1, krInfo.getStopIndex() - 1, krInfo.getSource()); try { Action<?> returned = null;