Search in sources :

Example 1 with HOGMLexer

use of com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer in project aic-praise by aic-sri-international.

the class HOGMCodeArea method computeHighlighting.

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    int lastTokenEnd = 0;
    ANTLRInputStream input = new ANTLRInputStream(text);
    HOGMLexer lexer = new HOGMLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.fill();
    for (int i = 0; i < tokens.size(); i++) {
        Token t = tokens.get(i);
        if (t.getType() == Token.EOF) {
            break;
        }
        String styleClass;
        if (t.getType() == HOGMLexer.COMMENT || t.getType() == HOGMLexer.LINE_COMMENT) {
            styleClass = "hogmCodeComment";
        } else if (HOGMTerminalSymbols.isTerminalSymbol(t.getText())) {
            styleClass = "hogmCodeKeyword";
        } else {
            styleClass = "hogmCodeOther";
        }
        int spacing = t.getStartIndex() - lastTokenEnd;
        if (spacing > 0) {
            spansBuilder.add(Collections.emptyList(), spacing);
        }
        int stylesize = (t.getStopIndex() - t.getStartIndex()) + 1;
        spansBuilder.add(Collections.singleton(styleClass), stylesize);
        lastTokenEnd = t.getStopIndex() + 1;
    }
    return spansBuilder.create();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) HOGMLexer(com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer) Collection(java.util.Collection) StyleSpansBuilder(org.fxmisc.richtext.StyleSpansBuilder) Token(org.antlr.v4.runtime.Token) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 2 with HOGMLexer

use of com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer in project aic-praise by aic-sri-international.

the class HOGMCodeArea method computeHighlighting.

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    int lastTokenEnd = 0;
    ANTLRInputStream input = new ANTLRInputStream(text);
    HOGMLexer lexer = new HOGMLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.fill();
    for (int i = 0; i < tokens.size(); i++) {
        Token t = tokens.get(i);
        if (t.getType() == Token.EOF) {
            break;
        }
        String styleClass;
        if (t.getType() == HOGMLexer.COMMENT || t.getType() == HOGMLexer.LINE_COMMENT) {
            styleClass = "hogmCodeComment";
        } else if (HOGMTerminalSymbols.isTerminalSymbol(t.getText())) {
            styleClass = "hogmCodeKeyword";
        } else {
            styleClass = "hogmCodeOther";
        }
        int spacing = t.getStartIndex() - lastTokenEnd;
        if (spacing > 0) {
            spansBuilder.add(Collections.emptyList(), spacing);
        }
        int stylesize = (t.getStopIndex() - t.getStartIndex()) + 1;
        spansBuilder.add(Collections.singleton(styleClass), stylesize);
        lastTokenEnd = t.getStopIndex() + 1;
    }
    return spansBuilder.create();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) HOGMLexer(com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer) Collection(java.util.Collection) StyleSpansBuilder(org.fxmisc.richtext.StyleSpansBuilder) Token(org.antlr.v4.runtime.Token) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 3 with HOGMLexer

use of com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer in project aic-praise by aic-sri-international.

the class HOGMParserWrapper method parse.

// 
// PRIVATE
// 
private Expression parse(String string, Parser.ErrorListener errorListener, ParseTreeRetriever parseTreeRetriever) throws RecognitionException, UnableToParseAllTheInputError, HOGModelException {
    Expression result = null;
    AntlrErrorListener antlrErrorListener = new AntlrErrorListener(errorListener);
    ANTLRInputStream input = new ANTLRInputStream(string);
    HOGMLexer lexer = new HOGMLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    HOGMParser parser = new HOGMParser(tokens);
    lexer.removeErrorListeners();
    parser.removeErrorListeners();
    lexer.addErrorListener(antlrErrorListener);
    parser.addErrorListener(antlrErrorListener);
    ParseTree tree = parseTreeRetriever.retrieve(parser);
    boolean eofReached = parser.getInputStream().LA(1) == Recognizer.EOF;
    if (!antlrErrorListener.errorsDetected) {
        if (!eofReached) {
            throw new UnableToParseAllTheInputError();
        } else {
            lexer.removeErrorListeners();
            parser.removeErrorListeners();
            HOGModelVisitor hogmModelVisitor = new HOGModelVisitor();
            result = hogmModelVisitor.visit(tree);
        }
    }
    return result;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Expression(com.sri.ai.expresso.api.Expression) HOGMLexer(com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer) HOGMParser(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 4 with HOGMLexer

use of com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer in project aic-praise by aic-sri-international.

the class HOGMParserWrapper method parse.

//
// PRIVATE
//
private Expression parse(String string, Parser.ErrorListener errorListener, ParseTreeRetriever parseTreeRetriever) throws RecognitionException, UnableToParseAllTheInputError, HOGModelException {
    Expression result = null;
    AntlrErrorListener antlrErrorListener = new AntlrErrorListener(errorListener);
    ANTLRInputStream input = new ANTLRInputStream(string);
    HOGMLexer lexer = new HOGMLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    HOGMParser parser = new HOGMParser(tokens);
    lexer.removeErrorListeners();
    parser.removeErrorListeners();
    lexer.addErrorListener(antlrErrorListener);
    parser.addErrorListener(antlrErrorListener);
    ParseTree tree = parseTreeRetriever.retrieve(parser);
    boolean eofReached = parser.getInputStream().LA(1) == Recognizer.EOF;
    if (!antlrErrorListener.errorsDetected) {
        if (!eofReached) {
            throw new UnableToParseAllTheInputError();
        } else {
            lexer.removeErrorListeners();
            parser.removeErrorListeners();
            HOGModelVisitor hogmModelVisitor = new HOGModelVisitor();
            result = hogmModelVisitor.visit(tree);
        }
    }
    return result;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Expression(com.sri.ai.expresso.api.Expression) HOGMLexer(com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer) HOGMParser(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

HOGMLexer (com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer)4 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)4 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)4 Expression (com.sri.ai.expresso.api.Expression)2 HOGMParser (com.sri.ai.praise.model.v1.hogm.antlr.HOGMParser)2 Collection (java.util.Collection)2 Token (org.antlr.v4.runtime.Token)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 StyleSpansBuilder (org.fxmisc.richtext.StyleSpansBuilder)2