You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/06/25 00:27:40 UTC
[groovy] branch GROOVY_3_0_X updated: Tweak syntax highlighter of
groovy console
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
new 9ce8e39 Tweak syntax highlighter of groovy console
9ce8e39 is described below
commit 9ce8e3936ed29372820b7111855a9effdfacf75b
Author: Daniel Sun <su...@apache.org>
AuthorDate: Fri Jun 25 08:27:28 2021 +0800
Tweak syntax highlighter of groovy console
(cherry picked from commit 571e36faed9ce823f22bd8cbb00f6425420834b2)
---
src/antlr/GroovyLexer.g4 | 72 +++++++++++++---------
.../console/ui/text/SmartDocumentFilter.java | 1 +
2 files changed, 45 insertions(+), 28 deletions(-)
diff --git a/src/antlr/GroovyLexer.g4 b/src/antlr/GroovyLexer.g4
index b28a69e..3a35d2b 100644
--- a/src/antlr/GroovyLexer.g4
+++ b/src/antlr/GroovyLexer.g4
@@ -56,9 +56,11 @@ options {
@members {
private static final Logger LOGGER = Logger.getLogger(GroovyLexer.class.getName());
- private long tokenIndex = 0;
- private int lastTokenType = 0;
- private int invalidDigitCount = 0;
+
+ private boolean errorIgnored;
+ private long tokenIndex;
+ private int lastTokenType;
+ private int invalidDigitCount;
/**
* Record the index and token type of the current token while emitting tokens.
@@ -79,18 +81,27 @@ options {
super.emit(token);
}
- private static final int[] REGEX_CHECK_ARRAY =
- IntStream.of(
- Identifier, CapitalizedIdentifier, NullLiteral, BooleanLiteral, THIS, RPAREN, RBRACK, RBRACE,
- IntegerLiteral, FloatingPointLiteral, StringLiteral, GStringEnd, INC, DEC
- ).sorted().toArray();
+ private static final int[] REGEX_CHECK_ARRAY = {
+ DEC,
+ INC,
+ THIS,
+ RBRACE,
+ RBRACK,
+ RPAREN,
+ GStringEnd,
+ NullLiteral,
+ StringLiteral,
+ BooleanLiteral,
+ IntegerLiteral,
+ FloatingPointLiteral,
+ Identifier, CapitalizedIdentifier
+ };
+ static {
+ Arrays.sort(REGEX_CHECK_ARRAY);
+ }
private boolean isRegexAllowed() {
- if (Arrays.binarySearch(REGEX_CHECK_ARRAY, this.lastTokenType) >= 0) {
- return false;
- }
-
- return true;
+ return (Arrays.binarySearch(REGEX_CHECK_ARRAY, this.lastTokenType) < 0);
}
/**
@@ -227,23 +238,28 @@ options {
private static boolean isJavaIdentifierPartAndNotIdentifierIgnorable(int codePoint) {
return Character.isJavaIdentifierPart(codePoint) && !Character.isIdentifierIgnorable(codePoint);
}
+
+ public boolean isErrorIgnored() {
+ return errorIgnored;
+ }
+
+ public void setErrorIgnored(boolean errorIgnored) {
+ this.errorIgnored = errorIgnored;
+ }
}
// ยง3.10.5 String Literals
StringLiteral
- : GStringQuotationMark DqStringCharacter* GStringQuotationMark
- | SqStringQuotationMark SqStringCharacter* SqStringQuotationMark
-
- | Slash { this.isRegexAllowed() && _input.LA(1) != '*' }?
- SlashyStringCharacter+ Slash
+ : GStringQuotationMark DqStringCharacter* GStringQuotationMark
+ | SqStringQuotationMark SqStringCharacter* SqStringQuotationMark
+ | Slash { this.isRegexAllowed() && _input.LA(1) != '*' }? SlashyStringCharacter+ Slash
- | TdqStringQuotationMark TdqStringCharacter* TdqStringQuotationMark
- | TsqStringQuotationMark TsqStringCharacter* TsqStringQuotationMark
- | DollarSlashyGStringQuotationMarkBegin DollarSlashyStringCharacter+ DollarSlashyGStringQuotationMarkEnd
+ | TdqStringQuotationMark TdqStringCharacter* TdqStringQuotationMark
+ | TsqStringQuotationMark TsqStringCharacter* TsqStringQuotationMark
+ | DollarSlashyGStringQuotationMarkBegin DollarSlashyStringCharacter+ DollarSlashyGStringQuotationMarkEnd
;
-// Groovy gstring
GStringBegin
: GStringQuotationMark DqStringCharacter* Dollar -> pushMode(DQ_GSTRING_MODE), pushMode(GSTRING_TYPE_SELECTOR_MODE)
;
@@ -478,10 +494,10 @@ IntegerLiteral
| HexIntegerLiteral
| OctalIntegerLiteral
| BinaryIntegerLiteral
- ) (Underscore { require(false, "Number ending with underscores is invalid", -1, true); })?
+ ) (Underscore { require(errorIgnored, "Number ending with underscores is invalid", -1, true); })?
// !!! Error Alternative !!!
- | Zero ([0-9] { invalidDigitCount++; })+ { require(false, "Invalid octal number", -(invalidDigitCount + 1), true); } IntegerTypeSuffix?
+ | Zero ([0-9] { invalidDigitCount++; })+ { require(errorIgnored, "Invalid octal number", -(invalidDigitCount + 1), true); } IntegerTypeSuffix?
;
fragment
@@ -620,12 +636,12 @@ BinaryDigitOrUnderscore
FloatingPointLiteral
: ( DecimalFloatingPointLiteral
| HexadecimalFloatingPointLiteral
- ) (Underscore { require(false, "Number ending with underscores is invalid", -1, true); })?
+ ) (Underscore { require(errorIgnored, "Number ending with underscores is invalid", -1, true); })?
;
fragment
DecimalFloatingPointLiteral
- : Digits Dot Digits ExponentPart? FloatTypeSuffix?
+ : Digits? Dot Digits ExponentPart? FloatTypeSuffix?
| Digits ExponentPart FloatTypeSuffix?
| Digits FloatTypeSuffix
;
@@ -961,10 +977,10 @@ SL_COMMENT
// Script-header comments.
// The very first characters of the file may be "#!". If so, ignore the first line.
SH_COMMENT
- : '#!' { require(0 == this.tokenIndex, "Shebang comment should appear at the first line", -2, true); } ShCommand (LineTerminator '#!' ShCommand)* -> skip
+ : '#!' { require(errorIgnored || 0 == this.tokenIndex, "Shebang comment should appear at the first line", -2, true); } ShCommand (LineTerminator '#!' ShCommand)* -> skip
;
// Unexpected characters will be handled by groovy parser later.
UNEXPECTED_CHAR
- : . { require(false, "Unexpected character: '" + getText().replace("'", "\\'") + "'", -1, false); }
+ : . { require(errorIgnored, "Unexpected character: '" + getText().replace("'", "\\'") + "'", -1, false); }
;
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/text/SmartDocumentFilter.java b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/text/SmartDocumentFilter.java
index 9b103e6..261d421 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/text/SmartDocumentFilter.java
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/text/SmartDocumentFilter.java
@@ -347,6 +347,7 @@ public class SmartDocumentFilter extends DocumentFilter {
CharStream charStream = CharStreams.fromReader(new StringReader(text));
GroovyLangLexer lexer = new GroovyLangLexer(charStream);
+ lexer.setErrorIgnored(true);
lexer.removeErrorListener(ConsoleErrorListener.INSTANCE);
return lexer;