Search in sources :

Example 1 with Problem

use of org.springframework.ide.vscode.java.properties.parser.Problem 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));
}
Also used : ATNConfigSet(org.antlr.v4.runtime.atn.ATNConfigSet) CommentLineContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.CommentLineContext) ArrayList(java.util.ArrayList) ANTLRErrorListener(org.antlr.v4.runtime.ANTLRErrorListener) PropertiesAst(org.springframework.ide.vscode.java.properties.parser.PropertiesAst) KeyContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.KeyContext) SeparatorAndValueContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.SeparatorAndValueContext) EmptyLineContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.EmptyLineContext) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BitSet(java.util.BitSet) PropertyLineContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.PropertyLineContext) Problem(org.springframework.ide.vscode.java.properties.parser.Problem) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) RecognitionException(org.antlr.v4.runtime.RecognitionException) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 2 with Problem

use of org.springframework.ide.vscode.java.properties.parser.Problem in project sts4 by spring-projects.

the class PropertiesAntlrParserTest method testMultipleSyntaxErrors.

@Test
public void testMultipleSyntaxErrors() throws Exception {
    String text = "abrakadabra\nkey:value\nsdcsdc";
    ParseResults results = parser.parse(text);
    assertEquals(2, results.syntaxErrors.size());
    assertTrue(results.problems.isEmpty());
    // One property line recorded. With key and empty value
    assertEquals(3, results.ast.getAllNodes().size());
    List<KeyValuePair> lines = results.ast.getNodes(KeyValuePair.class);
    assertEquals(3, lines.size());
    // Test valid part
    KeyValuePair validLine = lines.get(1);
    assertNotNull(validLine.getKey());
    assertNotNull(validLine.getValue());
    assertEquals("key", text.substring(validLine.getKey().getOffset(), validLine.getKey().getOffset() + validLine.getKey().getLength()));
    assertEquals("value", text.substring(validLine.getValue().getOffset(), validLine.getValue().getOffset() + validLine.getValue().getLength()));
    // Test errors
    Problem syntaxError1 = results.syntaxErrors.get(0);
    assertEquals(0, syntaxError1.getOffset());
    assertEquals(11, syntaxError1.getLength());
    Problem syntaxError2 = results.syntaxErrors.get(1);
    assertEquals(22, syntaxError2.getOffset());
    assertEquals(6, syntaxError2.getLength());
}
Also used : KeyValuePair(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.KeyValuePair) Problem(org.springframework.ide.vscode.java.properties.parser.Problem) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) Test(org.junit.Test)

Example 3 with Problem

use of org.springframework.ide.vscode.java.properties.parser.Problem in project sts4 by spring-projects.

the class PropertiesAntlrParserTest method testSyntaxError.

@Test
public void testSyntaxError() throws Exception {
    String text = "abrakadabra";
    ParseResults results = parser.parse(text);
    assertEquals(1, results.syntaxErrors.size());
    assertTrue(results.problems.isEmpty());
    // One property line recorded. With key and empty value
    assertEquals(1, results.ast.getAllNodes().size());
    Problem syntaxError = results.syntaxErrors.get(0);
    assertEquals(0, syntaxError.getOffset());
    assertEquals(text.length(), syntaxError.getLength());
}
Also used : Problem(org.springframework.ide.vscode.java.properties.parser.Problem) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) Test(org.junit.Test)

Aggregations

ParseResults (org.springframework.ide.vscode.java.properties.parser.ParseResults)3 Problem (org.springframework.ide.vscode.java.properties.parser.Problem)3 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 ANTLRErrorListener (org.antlr.v4.runtime.ANTLRErrorListener)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 ATNConfigSet (org.antlr.v4.runtime.atn.ATNConfigSet)1 DFA (org.antlr.v4.runtime.dfa.DFA)1 CommentLineContext (org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.CommentLineContext)1 EmptyLineContext (org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.EmptyLineContext)1 KeyContext (org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.KeyContext)1 PropertyLineContext (org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.PropertyLineContext)1 SeparatorAndValueContext (org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.SeparatorAndValueContext)1 PropertiesAst (org.springframework.ide.vscode.java.properties.parser.PropertiesAst)1 KeyValuePair (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.KeyValuePair)1