You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2022/09/09 20:26:13 UTC

[GitHub] [netbeans] matthiasblaesing commented on a diff in pull request #4549: ANTLR v4 and v3 Grammar Editing Support

matthiasblaesing commented on code in PR #4549:
URL: https://github.com/apache/netbeans/pull/4549#discussion_r967425762


##########
java/languages.antlr/src/org/netbeans/modules/languages/antlr/AntlrCompletionProvider.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.languages.antlr;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.prefs.Preferences;
+import javax.swing.text.Document;
+import javax.swing.text.JTextComponent;
+import org.netbeans.api.editor.mimelookup.MimeLookup;
+import org.netbeans.api.editor.mimelookup.MimePath;
+import org.netbeans.api.editor.mimelookup.MimeRegistration;
+import org.netbeans.api.editor.settings.SimpleValueNames;
+import org.netbeans.api.lexer.Token;
+import org.netbeans.api.lexer.TokenHierarchy;
+import org.netbeans.api.lexer.TokenSequence;
+import org.netbeans.modules.csl.spi.ParserResult;
+import org.netbeans.modules.parsing.api.ParserManager;
+import org.netbeans.modules.parsing.api.ResultIterator;
+import org.netbeans.modules.parsing.api.Source;
+import org.netbeans.modules.parsing.api.UserTask;
+import org.netbeans.modules.parsing.spi.ParseException;
+import org.netbeans.spi.editor.completion.CompletionItem;
+import org.netbeans.spi.editor.completion.CompletionProvider;
+import org.netbeans.spi.editor.completion.CompletionResultSet;
+import org.netbeans.spi.editor.completion.CompletionTask;
+import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery;
+import org.netbeans.spi.editor.completion.support.AsyncCompletionTask;
+import org.netbeans.spi.editor.completion.support.CompletionUtilities;
+import org.openide.util.Exceptions;
+
+/**
+ *
+ * @author Laszlo Kishalmi
+ */
+@MimeRegistration(mimeType = AntlrTokenId.MIME_TYPE, service = CompletionProvider.class)
+public class AntlrCompletionProvider implements CompletionProvider {
+
+    @Override
+    public CompletionTask createTask(int queryType, JTextComponent component) {
+        return new AsyncCompletionTask(new AntlrCompletionQuery(), component);
+    }
+
+    @Override
+    public int getAutoQueryTypes(JTextComponent component, String typedText) {
+        return 0;
+    }
+
+    private static boolean isCaseSensitive() {
+        Preferences prefs = MimeLookup.getLookup(MimePath.EMPTY).lookup(Preferences.class);
+        return prefs.getBoolean(SimpleValueNames.COMPLETION_CASE_SENSITIVE, false);

Review Comment:
   This might be a problem. I tested in the `Css3.g` file. That has a rule for `HASH`. I entered `ha` and got completion to `haSH` (this seems to be in line for case-insensitve completion), but then antlr complains:
   
   ```
   generate-antlr-parser:
        [java] ANTLR Parser Generator  Version 3.5.2/home/matthias/src/netbeans/ide/css.lib/src/org/netbeans/modules/css/lib/Css3.g
        [java] 
        [java] error(106): /home/matthias/src/netbeans/ide/css.lib/src/org/netbeans/modules/css/lib/Css3.g:814:7: reference to undefined rule: haSH
   ```
   
   So the completion created a bogus result.



##########
java/languages.antlr/src/org/netbeans/modules/languages/antlr/AntlrParserResult.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.languages.antlr;
+
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.antlr.v4.runtime.ANTLRErrorListener;
+import org.antlr.v4.runtime.BaseErrorListener;
+import org.antlr.v4.runtime.Parser;
+import org.antlr.v4.runtime.RecognitionException;
+import org.antlr.v4.runtime.Recognizer;
+import org.antlr.v4.runtime.Token;
+import org.antlr.v4.runtime.tree.ParseTreeListener;
+import org.netbeans.modules.csl.api.Error;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.api.Severity;
+import org.netbeans.modules.csl.spi.DefaultError;
+import org.netbeans.modules.csl.spi.ParserResult;
+import org.netbeans.modules.parsing.api.Snapshot;
+import org.netbeans.modules.parsing.api.Source;
+import org.openide.filesystems.FileObject;
+
+/**
+ *
+ * @author lkishalmi
+ */
+public abstract class AntlrParserResult<T extends Parser> extends ParserResult {
+
+    public final List<DefaultError> errors = new ArrayList<>();
+    public final Map<String, Reference> references = new TreeMap<>();
+
+    public final List<OffsetRange> folds = new ArrayList<>();
+    public final List<AntlrStructureItem> structure = new ArrayList<>();
+
+    final Deque<T> parsingQueue = new LinkedList<>();
+
+    final AtomicBoolean finished = new AtomicBoolean();
+
+    public AntlrParserResult(Snapshot snapshot) {
+        super(snapshot);
+        addParseTask(snapshot, false);
+    }
+    
+
+
+    public AntlrParserResult get() {
+        while (!parsingQueue.isEmpty()) {

Review Comment:
   This looked hacky to me. If I understand this correctly, this is done to evaluate imported grammar parts. I only glanced at the antlr4 documentation, but it reads to me, that the files should be parsed independently. If I remember correctly the indexer infrastructure should offer the tools to lookup data from other parsed files, i.e. when code completion is invoked in a file, that imports another, completion needs to lookup the completion from the local file and merge that with data from the index.



##########
java/languages.antlr/src/org/netbeans/modules/languages/antlr/AntlrLanguage.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.languages.antlr;
+
+import org.netbeans.api.lexer.Language;
+import org.netbeans.core.spi.multiview.MultiViewElement;
+import org.netbeans.core.spi.multiview.text.MultiViewEditorElement;
+import org.netbeans.modules.csl.api.DeclarationFinder;
+import org.netbeans.modules.csl.api.OccurrencesFinder;
+import org.netbeans.modules.csl.api.StructureScanner;
+import org.netbeans.modules.csl.spi.DefaultLanguageConfig;
+import org.netbeans.modules.csl.spi.LanguageRegistration;
+import org.netbeans.modules.parsing.spi.Parser;
+import org.openide.awt.ActionID;
+import org.openide.awt.ActionReference;
+import org.openide.awt.ActionReferences;
+import org.openide.filesystems.MIMEResolver;
+import org.openide.util.Lookup;
+import org.openide.util.NbBundle;
+import org.openide.windows.TopComponent;
+
+/**
+ *
+ * @author lkishalmi
+ */
+@NbBundle.Messages(
+        "ANTLRResolver=ANTLR Grammar"
+)
+@MIMEResolver.ExtensionRegistration(displayName = "#ANTLRResolver",
+    extension = {"g4", "g"},
+    mimeType = AntlrTokenId.MIME_TYPE,
+    position = 285
+)

Review Comment:
   Please split the language definitions. Mapping antlr3 and antlr4 to the same mimetype, will make it impossible to hook on only one type of grammar. Given, that there was a reason, that antlr4 was created (I assume it is more expressive than antlr3), I can imagine such requirements. Setting the unificiation into stone will come to haunt us.



##########
java/languages.antlr/src/org/netbeans/modules/languages/antlr/AntlrTokenId.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.languages.antlr;
+
+import java.util.Collection;
+import java.util.EnumSet;
+import org.netbeans.api.editor.mimelookup.MimeRegistration;
+import org.netbeans.api.lexer.InputAttributes;
+import org.netbeans.api.lexer.Language;
+import org.netbeans.api.lexer.LanguagePath;
+import org.netbeans.api.lexer.Token;
+import org.netbeans.api.lexer.TokenId;
+import org.netbeans.modules.languages.antlr.v3.Antlr3Lexer;
+import org.netbeans.modules.languages.antlr.v4.Antlr4Lexer;
+import org.netbeans.spi.lexer.LanguageEmbedding;
+import org.netbeans.spi.lexer.LanguageHierarchy;
+import org.netbeans.spi.lexer.LanguageProvider;
+import org.netbeans.spi.lexer.Lexer;
+import org.netbeans.spi.lexer.LexerRestartInfo;
+import org.openide.filesystems.FileObject;
+import org.openide.util.Lookup;
+
+/**
+ *
+ * @author lkishalmi
+ */
+public enum AntlrTokenId implements TokenId {
+
+    ACTION("action"),
+    COMMENT("comment"),
+    ERROR("error"),
+    KEYWORD("keyword"),
+    NUMBER("number"),
+    PUNCTUATION("punctuation"),
+    REGEXP_CHARS("regexp-chars"),
+    RULE("rule"),
+    STRING("string"),
+    TOKEN("token"),
+    WHITESPACE("whitespace");
+
+    private final String category;
+
+    AntlrTokenId(String category) {
+        this.category = category;
+    }
+
+    @Override
+    public String primaryCategory() {
+        return category;
+    }
+
+    public static final String MIME_TYPE = "text/x-antlr";
+
+    private static final Language<AntlrTokenId> language =
+            new LanguageHierarchy<AntlrTokenId>() {
+
+                @Override
+                protected String mimeType() {
+                    return AntlrTokenId.MIME_TYPE;
+                }
+
+                @Override
+                protected Collection<AntlrTokenId> createTokenIds() {
+                    return EnumSet.allOf(AntlrTokenId.class);
+                }
+
+                @Override
+                protected Lexer<AntlrTokenId> createLexer(LexerRestartInfo<AntlrTokenId> info) {
+                    FileObject fo =(FileObject) info.getAttributeValue(FileObject.class);
+                    if (fo != null) {
+                        return "g4".equals(fo.getExt()) ? new Antlr4Lexer(info) : new Antlr3Lexer(info);

Review Comment:
   I saw this at least twice and I think it is a hack. It will fail hard, if the user declares a new file mapping (for example `.antlr3` => `text/x-antlr3`). In my mind this would be much cleaner, if there would be two mime-types (`text/x-antlr3` and `text/x-antlr4`).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists