Repository navigation
Fix IllegalStateException when typing an unterminated string in Go #9660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
emilianbold
wants to merge
1
commit into
apache:master
Choose a base branch
from
emilianbold:fix-9654-go-lexer-null-token
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
ide/languages.go/test/unit/src/org/netbeans/modules/languages/go/GoLangLexerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| /* | ||
| * 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.go; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.EnumSet; | ||
| import org.netbeans.api.lexer.Language; | ||
| import org.netbeans.api.lexer.Token; | ||
| import org.netbeans.api.lexer.TokenHierarchy; | ||
| import org.netbeans.api.lexer.TokenSequence; | ||
| import org.netbeans.spi.lexer.LanguageHierarchy; | ||
| import org.netbeans.spi.lexer.Lexer; | ||
| import org.netbeans.spi.lexer.LexerRestartInfo; | ||
| import org.junit.Test; | ||
| import static org.junit.Assert.*; | ||
|
|
||
| /** | ||
| * Tests for the Go lexer. | ||
| * | ||
| * <p>Contains regression tests for apache/netbeans#9654: typing a double | ||
| * quote as the last characters of a document made the lexer report end of | ||
| * input (a null token) while characters were still pending on the lexer | ||
| * input, which the lexer infrastructure rejects with an | ||
| * {@link IllegalStateException} ("returned null token but | ||
| * lexerInput.readLength()=..."). | ||
| */ | ||
| public class GoLangLexerTest { | ||
|
|
||
| private static final Language<GoTokenId> LANGUAGE = new LanguageHierarchy<GoTokenId>() { | ||
| @Override | ||
| protected Collection<GoTokenId> createTokenIds() { | ||
| return EnumSet.allOf(GoTokenId.class); | ||
| } | ||
|
|
||
| @Override | ||
| protected Lexer<GoTokenId> createLexer(LexerRestartInfo<GoTokenId> info) { | ||
| return new GoLangLexer(info); | ||
| } | ||
|
|
||
| @Override | ||
| protected String mimeType() { | ||
| return "text/x-go"; | ||
| } | ||
| }.language(); | ||
|
|
||
| /** | ||
| * The document shape from issue #9654: the typed quote leaves six | ||
| * characters, {@code ")}, LF, {@code }}, LF, LF, untokenized at the end | ||
| * of the document. | ||
| */ | ||
| private static final String ISSUE_9654_TEXT = "package main\n" | ||
| + "\n" | ||
| + "import (\n" | ||
| + " \"fmt\"\n" | ||
| + ")\n" | ||
| + "\n" | ||
| + "func main() {\n" | ||
| + " fmt.Println(\")\n" | ||
| + "}\n" | ||
| + "\n"; | ||
|
|
||
| /** | ||
| * An unterminated interpreted string literal at the end of the text must | ||
| * still receive a token instead of an end-of-input signal. | ||
| */ | ||
| @Test | ||
| public void testUnterminatedStringAtEndOfText() { | ||
| Token<?> last = lastToken(ISSUE_9654_TEXT); | ||
| assertEquals(GoTokenId.ERROR, last.id()); | ||
| assertEquals("\")\n}\n\n", last.text().toString()); | ||
| } | ||
|
|
||
| /** | ||
| * Issue #9654: typing {@code "} to declare a string variable, before the | ||
| * literal is closed, used to throw while re-tokenizing the document. | ||
| */ | ||
| @Test | ||
| public void testTypedQuoteDoesNotThrow() { | ||
| String text = "package main\n" | ||
| + "\n" | ||
| + "func main() {\n" | ||
| + " var foo string = \"\n" | ||
| + "}\n"; | ||
| Token<?> last = lastToken(text); | ||
| assertEquals(GoTokenId.ERROR, last.id()); | ||
| assertEquals("\"\n}\n", last.text().toString()); | ||
| } | ||
|
|
||
| /** An unterminated rune literal at the end of the text. */ | ||
| @Test | ||
| public void testUnterminatedRuneAtEndOfText() { | ||
| Token<?> last = lastToken("package main\nvar x = '"); | ||
| assertEquals(GoTokenId.ERROR, last.id()); | ||
| assertEquals("'", last.text().toString()); | ||
| } | ||
|
|
||
| /** An unterminated raw string literal at the end of the text. */ | ||
| @Test | ||
| public void testUnterminatedRawStringAtEndOfText() { | ||
| Token<?> last = lastToken("package main\nvar x = `raw"); | ||
| assertEquals(GoTokenId.ERROR, last.id()); | ||
| assertEquals("`raw", last.text().toString()); | ||
| } | ||
|
|
||
| /** A closed string literal at the end of the text is still a STRING. */ | ||
| @Test | ||
| public void testClosedStringAtEndOfText() { | ||
| Token<?> last = lastToken("package main\nvar foo = \"bar\""); | ||
| assertEquals(GoTokenId.STRING, last.id()); | ||
| assertEquals("\"bar\"", last.text().toString()); | ||
| } | ||
|
|
||
| /** | ||
| * Well formed text must be tokenized completely and without any ERROR | ||
| * token: end of input without pending characters stays a null token. | ||
| */ | ||
| @Test | ||
| public void testWellFormedText() { | ||
| String text = "package main\n" | ||
| + "\n" | ||
| + "import (\n" | ||
| + " \"fmt\"\n" | ||
| + ")\n" | ||
| + "\n" | ||
| + "func main() {\n" | ||
| + " fmt.Println(\"hello world\")\n" | ||
| + "}\n"; | ||
| TokenSequence<GoTokenId> seq = TokenHierarchy.create(text, LANGUAGE) | ||
| .tokenSequence(LANGUAGE); | ||
| boolean foundString = false; | ||
| int count = 0; | ||
| while (seq.moveNext()) { | ||
| count++; | ||
| Token<GoTokenId> token = seq.token(); | ||
| assertNotEquals("Unexpected ERROR token in:\n" + text, | ||
| GoTokenId.ERROR, token.id()); | ||
| if (token.id() == GoTokenId.STRING | ||
| && "\"hello world\"".equals(token.text().toString())) { | ||
| foundString = true; | ||
| } | ||
| } | ||
| assertTrue("No tokens recognized", count > 0); | ||
| assertTrue("String literal was not tokenized", foundString); | ||
| } | ||
|
|
||
| /** | ||
| * Lex the given text to its end. Fails with the IllegalStateException | ||
| * from issue #9654 if the lexer returns a null token while characters | ||
| * are still pending on the lexer input. | ||
| */ | ||
| private static Token<?> lastToken(String text) { | ||
| TokenSequence<GoTokenId> seq = TokenHierarchy.create(text, LANGUAGE) | ||
| .tokenSequence(LANGUAGE); | ||
| Token<?> last = null; | ||
| while (seq.moveNext()) { | ||
| last = seq.token(); | ||
| } | ||
| assertNotNull("No tokens recognized", last); | ||
| return last; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets use multi line strings for new tests. I just converted the maven tests -> much easier to read once something fails.