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:14:38 UTC

[groovy] branch master 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 master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 571e36f  Tweak syntax highlighter of groovy console
571e36f is described below

commit 571e36faed9ce823f22bd8cbb00f6425420834b2
Author: Daniel Sun <su...@apache.org>
AuthorDate: Fri Jun 25 07:58:05 2021 +0800

    Tweak syntax highlighter of groovy console
---
 src/antlr/GroovyLexer.g4                              | 19 ++++++++++++++-----
 .../groovy/console/ui/text/SmartDocumentFilter.java   |  1 +
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/antlr/GroovyLexer.g4 b/src/antlr/GroovyLexer.g4
index 5d8e883..53532b1 100644
--- a/src/antlr/GroovyLexer.g4
+++ b/src/antlr/GroovyLexer.g4
@@ -56,6 +56,7 @@ options {
 @members {
     private static final Logger LOGGER = Logger.getLogger(GroovyLexer.class.getName());
 
+    private boolean errorIgnored;
     private long tokenIndex;
     private int  lastTokenType;
     private int  invalidDigitCount;
@@ -238,6 +239,14 @@ 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;
+    }
 }
 
 
@@ -486,10 +495,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
@@ -628,7 +637,7 @@ 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
@@ -972,10 +981,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 904ce90..4496873 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
@@ -346,6 +346,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;