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();
}
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();
}
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;
}
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;
}
Aggregations