Search in sources :

Example 1 with AadlBaParser

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;
    }
}
Also used : AadlBaLocationReference(org.osate.ba.utils.AadlBaLocationReference) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Behavior_annexContext(org.osate.ba.parser.AadlBaParser.Behavior_annexContext) BehaviorAnnex(org.osate.ba.aadlba.BehaviorAnnex) CaseInsensitiveCharStream(org.osate.ba.utils.CaseInsensitiveCharStream) AadlBaParser(org.osate.ba.parser.AadlBaParser) CharStream(org.antlr.v4.runtime.CharStream) CaseInsensitiveCharStream(org.osate.ba.utils.CaseInsensitiveCharStream) DefaultAadlBaHighlighter(org.osate.ba.texteditor.DefaultAadlBaHighlighter) AadlAntlrErrorReporter(org.osate.ba.parser.AadlAntlrErrorReporter) AadlBaParserVisitor(org.osate.ba.parser.AadlBaParserVisitor) DefaultAadlBaHighlighter(org.osate.ba.texteditor.DefaultAadlBaHighlighter) AadlBaHighlighter(org.osate.ba.texteditor.AadlBaHighlighter) XtextAadlBaHighlighter(org.osate.ba.texteditor.XtextAadlBaHighlighter) AadlBaLexer(org.osate.ba.parser.AadlBaLexer)

Aggregations

CharStream (org.antlr.v4.runtime.CharStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 BehaviorAnnex (org.osate.ba.aadlba.BehaviorAnnex)1 AadlAntlrErrorReporter (org.osate.ba.parser.AadlAntlrErrorReporter)1 AadlBaLexer (org.osate.ba.parser.AadlBaLexer)1 AadlBaParser (org.osate.ba.parser.AadlBaParser)1 Behavior_annexContext (org.osate.ba.parser.AadlBaParser.Behavior_annexContext)1 AadlBaParserVisitor (org.osate.ba.parser.AadlBaParserVisitor)1 AadlBaHighlighter (org.osate.ba.texteditor.AadlBaHighlighter)1 DefaultAadlBaHighlighter (org.osate.ba.texteditor.DefaultAadlBaHighlighter)1 XtextAadlBaHighlighter (org.osate.ba.texteditor.XtextAadlBaHighlighter)1 AadlBaLocationReference (org.osate.ba.utils.AadlBaLocationReference)1 CaseInsensitiveCharStream (org.osate.ba.utils.CaseInsensitiveCharStream)1