antlr / antlr4-intellij-adaptor

A library to support the use of ANTLR grammars in jetbrains IDE plugins for building custom languages.
BSD 2-Clause "Simplified" License
209 stars 38 forks source link

How generate the LL1 table and how use it ? #16

Open BorderCloud opened 4 years ago

BorderCloud commented 4 years ago

Hello

I have difficulty to use the functions generated by ANTLR4 for Intellij. I searched in the doc but It's not clear.

What is the name of the function to call to have the possible rules and tokens in function of offset in the file ? I see the error messages with the possible rules and tokens but I didn't found the good function to call.

I don't know if ANTLR4 for Intellij can generate the LL1 tools for my language. Have you examples of CompletionContributor class that use the basic completion tool and the LL1 tool ?

My code:

public class TestCompletionContributor extends CompletionContributor {
    public TestCompletionContributor() {
        extend(CompletionType.BASIC,
                PlatformPatterns.psiElement(),
                new CompletionProvider<CompletionParameters>() {
                    public void addCompletions(@NotNull CompletionParameters parameters,
                                               ProcessingContext context,
                                               @NotNull CompletionResultSet resultSet) {

                        //  How do the list of Rules and Tokens probable ? (with a LL1 tool ?)

                        resultSet.addElement(
                                        PrioritizedLookupElement.withPriority(
                                                LookupElementBuilder
                                                        .create("prevision1"),
                                                probability
                                        )
                                );
                    }
                }
        );
    }

    public static final String DUMMY_IDENTIFIER = "";

    @Override
    public void beforeCompletion(@NotNull CompletionInitializationContext context) {
        context.setDummyIdentifier(DUMMY_IDENTIFIER);
    }
}

Thanks

BorderCloud commented 4 years ago

I found this function in the project antlr4 :

    protected void reportInputMismatch(Parser recognizer,
                                       InputMismatchException e)
    {
        String msg = "mismatched input "+getTokenErrorDisplay(e.getOffendingToken())+
        " expecting "+e.getExpectedTokens().toString(recognizer.getVocabulary());
        recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
    }

This function builds the description of PsiErrorElement.

Is it possible to add the function getExpectedTokens(offset) somewhere in your project ?

Or a method to read this list of tokens in my CompletionContributor class ?

bjansen commented 4 years ago

You could try https://github.com/mike-lischke/antlr4-c3, which has a Java port. I might be possible to plug this library to a CompletionContributor.

BorderCloud commented 4 years ago

Thanks, it works with https://github.com/mike-lischke/antlr4-c3/blob/master/ports/java/src/main/java/com/vmware/antlr4c3/CodeCompletionCore.java

I will try to implement a basic list of token for the moment.

It's a very good idea to plug this library to a CompletionContributor :100:

BorderCloud commented 4 years ago

I tested... May be I used bad this class but sometimes, there are not responses :(