use of org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.CommentLineContext in project sts4 by spring-projects.
the class AntlrParser method parse.
@Override
public ParseResults parse(String text) {
ArrayList<Problem> syntaxErrors = new ArrayList<>();
ArrayList<Problem> problems = new ArrayList<>();
ArrayList<PropertiesAst.Node> astNodes = new ArrayList<>();
JavaPropertiesLexer lexer = new JavaPropertiesLexer(new ANTLRInputStream(text.toCharArray(), text.length()));
CommonTokenStream tokens = new CommonTokenStream(lexer);
JavaPropertiesParser parser = new JavaPropertiesParser(tokens);
// To avoid printing parse errors in the console
parser.removeErrorListener(ConsoleErrorListener.INSTANCE);
// Add listener to collect various parser errors
parser.addErrorListener(new ANTLRErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
syntaxErrors.add(createProblem(msg, ProblemCodes.PROPERTIES_SYNTAX_ERROR, (Token) offendingSymbol));
}
@Override
public void reportAmbiguity(org.antlr.v4.runtime.Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs) {
problems.add(createProblem("Ambiguity detected!", ProblemCodes.PROPERTIES_AMBIGUITY_ERROR, recognizer.getCurrentToken()));
}
@Override
public void reportAttemptingFullContext(org.antlr.v4.runtime.Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs) {
problems.add(createProblem("Full-Context attempt detected!", ProblemCodes.PROPERTIES_FULL_CONTEXT_ERROR, recognizer.getCurrentToken()));
}
@Override
public void reportContextSensitivity(org.antlr.v4.runtime.Parser recognizer, DFA dfa, int startIndex, int stopIndex, int prediction, ATNConfigSet configs) {
problems.add(createProblem("Context sensitivity detected!", ProblemCodes.PROPERTIES_CONTEXT_SENSITIVITY_ERROR, recognizer.getCurrentToken()));
}
});
// Add listener to the parse tree to collect AST nodes
parser.addParseListener(new JavaPropertiesBaseListener() {
private Key key = null;
private Value value = null;
@Override
public void exitPropertyLine(PropertyLineContext ctx) {
KeyValuePair pair = new KeyValuePair(ctx, key, value);
key.parent = value.parent = pair;
astNodes.add(pair);
key = null;
value = null;
}
@Override
public void exitCommentLine(CommentLineContext ctx) {
astNodes.add(new Comment(ctx));
}
@Override
public void exitKey(KeyContext ctx) {
key = new Key(ctx);
}
@Override
public void exitSeparatorAndValue(SeparatorAndValueContext ctx) {
value = new Value(ctx);
}
@Override
public void exitEmptyLine(EmptyLineContext ctx) {
astNodes.add(new EmptyLine(ctx));
}
});
parser.parse();
// Collect and return parse results
return new ParseResults(new PropertiesAst(ImmutableList.copyOf(astNodes)), ImmutableList.copyOf(syntaxErrors), ImmutableList.copyOf(problems));
}
Aggregations