Search in sources :

Example 6 with ParseResults

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

the class PropertiesAstTest method testLines4.

@Test
public void testLines4() throws Exception {
    ParseResults results = parser.parse("# Comment-1\n\nkey = value  1  \n# Comment-2");
    assertEquals(4, results.ast.getAllNodes().size());
    assertEquals(2, results.ast.getNodes(Comment.class).size());
    assertEquals(1, results.ast.getNodes(KeyValuePair.class).size());
    assertEquals(1, results.ast.getNodes(EmptyLine.class).size());
}
Also used : ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) Test(org.junit.Test)

Example 7 with ParseResults

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

the class PropertiesAstTest method positionKey.

@Test
public void positionKey() throws Exception {
    ParseResults results = parser.parse("# Comment\n" + "key  = value\n" + "\t\n");
    Node node = results.ast.findNode(10);
    assertTrue(node instanceof Key);
    assertTrue(node.getOffset() <= 10 && 10 <= node.getOffset() + node.getLength());
    node = results.ast.findNode(12);
    assertTrue(node instanceof Key);
    assertTrue(node.getOffset() <= 12 && 12 <= node.getOffset() + node.getLength());
    node = results.ast.findNode(13);
    assertTrue(node instanceof Key);
    assertTrue(node.getOffset() <= 13 && 13 <= node.getOffset() + node.getLength());
}
Also used : Node(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Node) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) Key(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Key) Test(org.junit.Test)

Example 8 with ParseResults

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

the class PropertiesAstTest method testLines2.

@Test
public void testLines2() throws Exception {
    ParseResults results = parser.parse("\n\n \t   \t \n# Comment\n\t\t\n");
    assertTrue(results.syntaxErrors.isEmpty());
    assertTrue(results.problems.isEmpty());
    assertEquals(5, results.ast.getAllNodes().size());
    assertEquals(1, results.ast.getNodes(Comment.class).size());
    assertEquals(4, results.ast.getNodes(EmptyLine.class).size());
}
Also used : ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) Test(org.junit.Test)

Example 9 with ParseResults

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

the class PropertiesAstTest method testLines5.

@Test
public void testLines5() throws Exception {
    ParseResults results = parser.parse("#comment\nliquibase.enabled=\n#comment");
    assertEquals(3, results.ast.getAllNodes().size());
    assertEquals(2, results.ast.getNodes(Comment.class).size());
    assertEquals(1, results.ast.getNodes(KeyValuePair.class).size());
}
Also used : ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) Test(org.junit.Test)

Example 10 with ParseResults

use of org.springframework.ide.vscode.java.properties.parser.ParseResults 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)

Aggregations

ParseResults (org.springframework.ide.vscode.java.properties.parser.ParseResults)21 Test (org.junit.Test)14 Node (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Node)9 KeyValuePair (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.KeyValuePair)5 Value (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Value)5 Problem (org.springframework.ide.vscode.java.properties.parser.Problem)3 Key (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Key)3 ArrayList (java.util.ArrayList)2 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)2 Comment (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Comment)2 EmptyLine (org.springframework.ide.vscode.java.properties.parser.PropertiesAst.EmptyLine)2 File (java.io.File)1 URI (java.net.URI)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