use of org.osate.ba.parser.AadlBaParser in project osate2 by osate.
the class AadlBaParserAction method parseAnnexSubclause.
public AnnexSubclause parseAnnexSubclause(String annexName, String source, String filename, int line, int column, ParseErrorReporter errReporter) throws antlr.RecognitionException {
if (false == ANNEX_NAME.equalsIgnoreCase(annexName)) {
return null;
}
CharStream cs = new CaseInsensitiveCharStream(source);
// AnnexOffset is the offset of the first token found in String source
// considering the whole source file.
int annexOffset = column;
AadlBaHighlighter highlighter;
// Set a Xtext highlighter if AADLBA Front End is running under OSATE2.
if (Platform.isRunning()) {
highlighter = XtextAadlBaHighlighter.getHighlighter(AnnexUtil.getCurrentAnnexSubclause());
} else {
// Default highlighter does nothing.
highlighter = new DefaultAadlBaHighlighter();
}
AadlAntlrErrorReporter parserErrorReporter = new AadlAntlrErrorReporter(errReporter, filename);
AadlBaLexer lexer = new AadlBaLexer(cs);
lexer.setLine(line);
lexer.setCharPositionInLine(column);
lexer.removeErrorListeners();
lexer.addErrorListener(parserErrorReporter);
lexer.setHighlighter(highlighter);
lexer.setAnnexOffset(annexOffset);
CommonTokenStream tokens = new CommonTokenStream(lexer);
AadlBaParser parser = new AadlBaParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(parserErrorReporter);
try {
// Build the primary AST: AST without AADLBA or declarative meta objects
// Instanced.
Behavior_annexContext bac = parser.behavior_annex();
BehaviorAnnex ba = null;
if (parser.getNumberOfSyntaxErrors() == 0) {
AadlBaParserVisitor<Boolean> visitor = new AadlBaParserVisitor<Boolean>(filename, annexOffset);
visitor.visit(bac);
ba = bac.result;
} else {
// Create an empty behavior annex object in order to
// highlight keywords even if there is some syntax errors.
ba = AadlBaFactory.eINSTANCE.createBehaviorAnnex();
}
if (ba != null) {
AadlBaLocationReference location = new AadlBaLocationReference(annexOffset, filename, line);
location.setOffset(0);
ba.setLocationReference(location);
}
return ba;
}// Translates ANTLR runtime exception to ANTLR Exception.
catch (org.antlr.v4.runtime.RecognitionException e) {
int errLine = e.getOffendingToken().getLine();
int errColumn = e.getOffendingToken().getCharPositionInLine();
throw new antlr.RecognitionException(e.getMessage(), filename, errLine, errColumn);
} catch (IllegalArgumentException e) {
// Nothing to do as the parser is supposed to report any error.
// DEBUG
e.printStackTrace();
return null;
}
}
Aggregations