/** * @return The source of this exception, or {@code null} if no source is * available. */ @Override public String getSource() { if (this.info != null) { return this.info.getSource(); } else { return null; } }
/** * @return The source file used to construct this program. */ public File getSourceFile() { return new File(this.info.getSource()); }
@Override public int compareTo(SourceInfo o) { return this.source.compareTo(o.getSource()); }
/** * @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; }
/** * 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; }
/** * 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)); } }
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 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()); } };
@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()); }
@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); } }
@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); } }
/** * 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()); }
/*************** 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); } }
@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)); }