This is an automated email from the ASF dual-hosted git repository. lkishalmi pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/netbeans.git
commit 9285f6dcd7d229791a66cb504a5e9a6a73f60bf2 Author: Laszlo Kishalmi <[email protected]> AuthorDate: Mon Aug 29 20:44:14 2022 -0700 TOML Support Improvement and Cleanup --- ide/languages.toml/nbproject/project.xml | 16 -- .../languages/toml/LexerInputCharStream.java | 54 +++--- .../modules/languages/toml/TomlDataObject.java | 141 ---------------- .../modules/languages/toml/TomlLanguage.java | 90 +++++++++- .../netbeans/modules/languages/toml/TomlLexer.java | 183 ++++++++++++--------- .../org/netbeans/modules/languages/toml/layer.xml | 7 + 6 files changed, 235 insertions(+), 256 deletions(-) diff --git a/ide/languages.toml/nbproject/project.xml b/ide/languages.toml/nbproject/project.xml index 1b7f3de630..eb8eee8622 100644 --- a/ide/languages.toml/nbproject/project.xml +++ b/ide/languages.toml/nbproject/project.xml @@ -119,22 +119,6 @@ <specification-version>9.29</specification-version> </run-dependency> </dependency> - <dependency> - <code-name-base>org.openide.loaders</code-name-base> - <build-prerequisite/> - <compile-dependency/> - <run-dependency> - <specification-version>7.87</specification-version> - </run-dependency> - </dependency> - <dependency> - <code-name-base>org.openide.nodes</code-name-base> - <build-prerequisite/> - <compile-dependency/> - <run-dependency> - <specification-version>7.62</specification-version> - </run-dependency> - </dependency> <dependency> <code-name-base>org.openide.util</code-name-base> <build-prerequisite/> diff --git a/ide/languages.toml/src/org/netbeans/modules/languages/toml/LexerInputCharStream.java b/ide/languages.toml/src/org/netbeans/modules/languages/toml/LexerInputCharStream.java index 307a13e5e2..e830b63e3b 100644 --- a/ide/languages.toml/src/org/netbeans/modules/languages/toml/LexerInputCharStream.java +++ b/ide/languages.toml/src/org/netbeans/modules/languages/toml/LexerInputCharStream.java @@ -18,7 +18,6 @@ */ package org.netbeans.modules.languages.toml; -import java.util.Stack; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.misc.Interval; import org.netbeans.spi.lexer.*; @@ -29,21 +28,23 @@ import org.netbeans.spi.lexer.*; */ public class LexerInputCharStream implements CharStream { private final LexerInput input; - private final Stack<Integer> markers = new Stack<>(); + private final StringBuilder readBuffer = new StringBuilder(); + + private int index = 0; public LexerInputCharStream(LexerInput input) { this.input = input; - } @Override public String getText(Interval intrvl) { - return input.readText(intrvl.a, intrvl.b).toString(); + int end = Math.min(intrvl.b + 1, readBuffer.length()); + return readBuffer.substring(intrvl.a, end); } @Override public void consume() { - input.read(); + read(); } @Override @@ -55,51 +56,62 @@ public class LexerInputCharStream implements CharStream { int c = 0; if (count > 0) { for (int i = 0; i < count; i++) { - c = input.read(); + c = read(); } - input.backup(count); + backup(count); } else { - input.backup(count); - c = input.read(); + backup(count); + c = read(); } return c; } + //Marks are for buffering in ANTLR4, we do not really need them @Override public int mark() { - markers.push(index()); - return markers.size() - 1; + return -1; } @Override public void release(int marker) { - while (marker < markers.size()) { - markers.pop(); - } } @Override public int index() { - return input.readLengthEOF(); + return index; } @Override public void seek(int i) { - if (i < input.readLengthEOF()) { - input.backup(index() - i); + if (i < index()) { + backup(index() - i); } else { - while (input.readLengthEOF() < i) { - if (input.read() == LexerInput.EOF) { + while (index() < i) { + if (read() == LexerInput.EOF) { break; } } } } + + private int read() { + int ret = input.read(); + if ((readBuffer.length() == index) && (ret != EOF)) { + readBuffer.append((char)ret); + } + index += 1; + return ret; + } + + private void backup(int count) { + index -= count; + input.backup(count); + } + @Override public int size() { - return -1; - //throw new UnsupportedOperationException("Stream size is unknown."); + throw new UnsupportedOperationException("Stream size is unknown."); } @Override diff --git a/ide/languages.toml/src/org/netbeans/modules/languages/toml/TomlDataObject.java b/ide/languages.toml/src/org/netbeans/modules/languages/toml/TomlDataObject.java deleted file mode 100644 index 644c4b7903..0000000000 --- a/ide/languages.toml/src/org/netbeans/modules/languages/toml/TomlDataObject.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * 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.toml; - -import java.io.IOException; -import org.netbeans.core.spi.multiview.MultiViewElement; -import org.netbeans.core.spi.multiview.text.MultiViewEditorElement; -import org.openide.awt.ActionID; -import org.openide.awt.ActionReference; -import org.openide.awt.ActionReferences; -import org.openide.filesystems.FileObject; -import org.openide.filesystems.MIMEResolver; -import org.openide.loaders.DataNode; -import org.openide.loaders.DataObject; -import org.openide.loaders.DataObjectExistsException; -import org.openide.loaders.MultiDataObject; -import org.openide.loaders.MultiFileLoader; -import org.openide.nodes.Children; -import org.openide.nodes.Node; -import org.openide.util.*; -import org.openide.windows.TopComponent; - -/** - * - * @author lkishalmi - */ [email protected]( - "TOMLResolver=Toml File" -) [email protected](displayName = "#TOMLResolver", - extension = "toml", - mimeType = TomlTokenId.TOML_MIME_TYPE, - position = 285 -) - -@ActionReferences({ - @ActionReference( - path = "Loaders/text/x-toml/Actions", - id = @ActionID(category = "System", id = "org.openide.actions.OpenAction"), - position = 100, - separatorAfter = 300 - ), - @ActionReference( - path = "Loaders/text/x-toml/Actions", - id = @ActionID(category = "Edit", id = "org.openide.actions.CutAction"), - position = 400 - ), - @ActionReference( - path = "Loaders/text/x-toml/Actions", - id = @ActionID(category = "Edit", id = "org.openide.actions.CopyAction"), - position = 500, - separatorAfter = 600 - ), - @ActionReference( - path = "Loaders/text/x-toml/Actions", - id = @ActionID(category = "Edit", id = "org.openide.actions.DeleteAction"), - position = 700 - ), - @ActionReference( - path = "Loaders/text/x-toml/Actions", - id = @ActionID(category = "System", id = "org.openide.actions.RenameAction"), - position = 800, - separatorAfter = 900 - ), - @ActionReference( - path = "Loaders/text/x-toml/Actions", - id = @ActionID(category = "System", id = "org.openide.actions.SaveAsTemplateAction"), - position = 1000, - separatorAfter = 1100 - ), - @ActionReference( - path = "Loaders/text/x-toml/Actions", - id = @ActionID(category = "System", id = "org.openide.actions.FileSystemAction"), - position = 1200, - separatorAfter = 1300 - ), - @ActionReference( - path = "Loaders/text/x-toml/Actions", - id = @ActionID(category = "System", id = "org.openide.actions.ToolsAction"), - position = 1400 - ), - @ActionReference( - path = "Loaders/text/x-toml/Actions", - id = @ActionID(category = "System", id = "org.openide.actions.PropertiesAction"), - position = 1500 - ) -}) - [email protected]( - mimeType = TomlTokenId.TOML_MIME_TYPE, - iconBase = "org/netbeans/modules/languages/toml/toml-icon.png", - displayName = "#TOMLResolver", - position = 303 -) -public final class TomlDataObject extends MultiDataObject { - - public TomlDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException, IOException { - super(pf, loader); - registerEditor(TomlTokenId.TOML_MIME_TYPE, true); - } - - @Override - protected Node createNodeDelegate() { - return new DataNode(this, Children.LEAF, getLookup()); - } - - @Override - protected int associateLookup() { - return 1; - } - - @NbBundle.Messages("Source=&Source") - @MultiViewElement.Registration( - displayName = "#Source", - iconBase = "org/netbeans/modules/languages/toml/toml-icon.png", - persistenceType = TopComponent.PERSISTENCE_ONLY_OPENED, - mimeType = TomlTokenId.TOML_MIME_TYPE, - preferredID = "neon.source", - position = 1 - ) - public static MultiViewEditorElement createMultiViewEditorElement(Lookup context) { - return new MultiViewEditorElement(context); - } - -} diff --git a/ide/languages.toml/src/org/netbeans/modules/languages/toml/TomlLanguage.java b/ide/languages.toml/src/org/netbeans/modules/languages/toml/TomlLanguage.java index 55e62bf148..8eca474076 100644 --- a/ide/languages.toml/src/org/netbeans/modules/languages/toml/TomlLanguage.java +++ b/ide/languages.toml/src/org/netbeans/modules/languages/toml/TomlLanguage.java @@ -19,14 +19,89 @@ package org.netbeans.modules.languages.toml; 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.spi.DefaultLanguageConfig; import org.netbeans.modules.csl.spi.LanguageRegistration; +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 */ -@LanguageRegistration(mimeType = TomlTokenId.TOML_MIME_TYPE) //NOI18N [email protected]( + "TOMLResolver=Toml File" +) [email protected](displayName = "#TOMLResolver", + extension = "toml", + mimeType = TomlTokenId.TOML_MIME_TYPE, + position = 285 +) + +@ActionReferences({ + @ActionReference( + path = "Loaders/text/x-toml/Actions", + id = @ActionID(category = "System", id = "org.openide.actions.OpenAction"), + position = 100, + separatorAfter = 200 + ), + @ActionReference( + path = "Loaders/text/x-toml/Actions", + id = @ActionID(category = "Edit", id = "org.openide.actions.CutAction"), + position = 300 + ), + @ActionReference( + path = "Loaders/text/x-toml/Actions", + id = @ActionID(category = "Edit", id = "org.openide.actions.CopyAction"), + position = 400 + ), + @ActionReference( + path = "Loaders/text/x-toml/Actions", + id = @ActionID(category = "Edit", id = "org.openide.actions.PasteAction"), + position = 500, + separatorAfter = 600 + ), + @ActionReference( + path = "Loaders/text/x-toml/Actions", + id = @ActionID(category = "Edit", id = "org.openide.actions.DeleteAction"), + position = 700 + ), + @ActionReference( + path = "Loaders/text/x-toml/Actions", + id = @ActionID(category = "System", id = "org.openide.actions.RenameAction"), + position = 800, + separatorAfter = 900 + ), + @ActionReference( + path = "Loaders/text/x-toml/Actions", + id = @ActionID(category = "System", id = "org.openide.actions.SaveAsTemplateAction"), + position = 1000, + separatorAfter = 1100 + ), + @ActionReference( + path = "Loaders/text/x-toml/Actions", + id = @ActionID(category = "System", id = "org.openide.actions.FileSystemAction"), + position = 1200, + separatorAfter = 1300 + ), + @ActionReference( + path = "Loaders/text/x-toml/Actions", + id = @ActionID(category = "System", id = "org.openide.actions.ToolsAction"), + position = 1400 + ), + @ActionReference( + path = "Loaders/text/x-toml/Actions", + id = @ActionID(category = "System", id = "org.openide.actions.PropertiesAction"), + position = 1500 + ) +}) +@LanguageRegistration(mimeType = TomlTokenId.TOML_MIME_TYPE, useMultiview = true) public class TomlLanguage extends DefaultLanguageConfig { @Override @@ -43,4 +118,17 @@ public class TomlLanguage extends DefaultLanguageConfig { public String getLineCommentPrefix() { return "#"; // NOI18N } + + @NbBundle.Messages("Source=&Source") + @MultiViewElement.Registration( + displayName = "#Source", + iconBase = "org/netbeans/modules/languages/toml/toml-icon.png", + persistenceType = TopComponent.PERSISTENCE_ONLY_OPENED, + mimeType = TomlTokenId.TOML_MIME_TYPE, + preferredID = "toml.source", + position = 100 + ) + public static MultiViewEditorElement createMultiViewEditorElement(Lookup context) { + return new MultiViewEditorElement(context); + } } diff --git a/ide/languages.toml/src/org/netbeans/modules/languages/toml/TomlLexer.java b/ide/languages.toml/src/org/netbeans/modules/languages/toml/TomlLexer.java index d7bc97eb3e..a30199cf5f 100644 --- a/ide/languages.toml/src/org/netbeans/modules/languages/toml/TomlLexer.java +++ b/ide/languages.toml/src/org/netbeans/modules/languages/toml/TomlLexer.java @@ -18,7 +18,8 @@ */ package org.netbeans.modules.languages.toml; -import org.antlr.v4.runtime.misc.IntegerList; +import java.lang.reflect.Field; +import org.antlr.v4.runtime.misc.IntegerStack; import org.netbeans.api.lexer.Token; import org.netbeans.spi.lexer.Lexer; import org.netbeans.spi.lexer.LexerRestartInfo; @@ -46,80 +47,76 @@ public final class TomlLexer implements Lexer<TomlTokenId> { @Override public Token<TomlTokenId> nextToken() { - try { - org.antlr.v4.runtime.Token nextToken = lexer.nextToken(); - if (nextToken.getType() == EOF) { - return null; - } - switch (nextToken.getType()) { - case TripleQuotationMark: - case TripleApostrophe: - return token(ML_STRING_START); - - case StringChar: - case QuotationMark: - case Apostrophe: - return token(STRING); - - case MLBasicStringEnd: - case MLLiteralStringEnd: - return token(ML_STRING_END); - - case Comma: - case ArrayStart: - case ArrayEnd: - case InlineTableStart: - case InlineTableEnd: - - case Dot: - return token(DOT); - - case Equals: - return token(EQUALS); - - case TableKeyStart: - case TableKeyEnd: - case ArrayTableKeyStart: - case ArrayTableKeyEnd: - return token(TABLE_MARK); - case UnquotedKey: - return token(KEY); - case Comment: - return token(COMMENT); - case WS: - case NewLine: - return token(TomlTokenId.WHITESPACE); - case Error: - return token(ERROR); - case DecimalInteger: - case HexInteger: - case OctalInteger: - case BinaryInteger: - case FloatingPoint: - case FloatingPointInf: - case FloatingPointNaN: - return token(NUMBER); - - case TrueBoolean: - case FalseBoolean: - return token(BOOLEAN); - - case EscapeSequence: - return token(ESCAPE_SEQUENCE); - - case Dash: - case Plus: - case Colon: - case Z: - case TimeDelimiter: - case DateDigits: - case DateComma: - return token(DATE); - default: - return token(ERROR); - } - } catch (IndexOutOfBoundsException ex) { - return token(ERROR); + org.antlr.v4.runtime.Token nextToken = lexer.nextToken(); + if (nextToken.getType() == EOF) { + return null; + } + switch (nextToken.getType()) { + case TripleQuotationMark: + case TripleApostrophe: + return token(ML_STRING_START); + + case StringChar: + case QuotationMark: + case Apostrophe: + return token(STRING); + + case MLBasicStringEnd: + case MLLiteralStringEnd: + return token(ML_STRING_END); + + case Comma: + case ArrayStart: + case ArrayEnd: + case InlineTableStart: + case InlineTableEnd: + + case Dot: + return token(DOT); + + case Equals: + return token(EQUALS); + + case TableKeyStart: + case TableKeyEnd: + case ArrayTableKeyStart: + case ArrayTableKeyEnd: + return token(TABLE_MARK); + case UnquotedKey: + return token(KEY); + case Comment: + return token(COMMENT); + case WS: + case NewLine: + return token(TomlTokenId.WHITESPACE); + case Error: + return token(ERROR); + case DecimalInteger: + case HexInteger: + case OctalInteger: + case BinaryInteger: + case FloatingPoint: + case FloatingPointInf: + case FloatingPointNaN: + return token(NUMBER); + + case TrueBoolean: + case FalseBoolean: + return token(BOOLEAN); + + case EscapeSequence: + return token(ESCAPE_SEQUENCE); + + case Dash: + case Plus: + case Colon: + case Z: + case TimeDelimiter: + case DateDigits: + case DateComma: + return token(DATE); + default: + return token(ERROR); } } @@ -137,21 +134,53 @@ public final class TomlLexer implements Lexer<TomlTokenId> { } private static class LexerState { + private static Field ARRAY_DEPTH; + private static Field ARRAY_DEPTH_STACK; + final int state; final int mode; - final IntegerList modes; + final IntegerStack modes; + + final int arrayDepth; + final IntegerStack arrayDepthStack; + + static { + try { + // Hack accessing private state parts of TomlLexer + // See: https://github.com/tomlj/tomlj/pull/42 + ARRAY_DEPTH = org.tomlj.internal.TomlLexer.class.getDeclaredField("arrayDepth"); + ARRAY_DEPTH.setAccessible(true); + ARRAY_DEPTH_STACK = org.tomlj.internal.TomlLexer.class.getDeclaredField("arrayDepthStack"); + ARRAY_DEPTH_STACK.setAccessible(true); + } catch (ReflectiveOperationException ex) { + } + } LexerState(org.tomlj.internal.TomlLexer lexer) { this.state= lexer.getState(); this.mode = lexer._mode; - this.modes = new IntegerList(lexer._modeStack); + this.modes = new IntegerStack(lexer._modeStack); + + try { + this.arrayDepth = ARRAY_DEPTH.getInt(lexer); + this.arrayDepthStack = new IntegerStack((IntegerStack)ARRAY_DEPTH_STACK.get(lexer)); + } catch (IllegalAccessException ex) { + throw new RuntimeException(ex); + } } public void restore(org.tomlj.internal.TomlLexer lexer) { lexer.setState(state); lexer._modeStack.addAll(modes); lexer._mode = mode; + + try { + ARRAY_DEPTH.setInt(lexer, arrayDepth); + ((IntegerStack) ARRAY_DEPTH_STACK.get(lexer)).addAll(arrayDepthStack); + } catch (IllegalAccessException ex) { + throw new RuntimeException(ex); + } } @Override diff --git a/ide/languages.toml/src/org/netbeans/modules/languages/toml/layer.xml b/ide/languages.toml/src/org/netbeans/modules/languages/toml/layer.xml index 59d0f2b1c6..8aa5b7f258 100644 --- a/ide/languages.toml/src/org/netbeans/modules/languages/toml/layer.xml +++ b/ide/languages.toml/src/org/netbeans/modules/languages/toml/layer.xml @@ -42,6 +42,13 @@ </folder> </folder> </folder> + <folder name="Loaders"> + <folder name="text"> + <folder name="x-toml"> + <attr name="iconBase" stringvalue="org/netbeans/modules/languages/toml/toml-icon.png"/> + </folder> + </folder> + </folder> <folder name="OptionsDialog"> <folder name="PreviewExamples"> <folder name="text"> --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
