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/04/12 13:54:01 UTC
[groovy] 01/01: Tweak safe index
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch danielsun/tweak-lexer
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 3342969b515ef2e2c973df97bbc3d03c2e2b616b
Author: Daniel Sun <su...@apache.org>
AuthorDate: Mon Apr 12 21:53:07 2021 +0800
Tweak safe index
---
src/antlr/GroovyLexer.g4 | 8 +++++---
src/test-resources/core/SafeIndex_01x.groovy | 3 +++
2 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/src/antlr/GroovyLexer.g4 b/src/antlr/GroovyLexer.g4
index 147e126..78f33f2 100644
--- a/src/antlr/GroovyLexer.g4
+++ b/src/antlr/GroovyLexer.g4
@@ -183,8 +183,10 @@ options {
return false;
}
- return ("(".equals(paren.getText()) && TRY != paren.getLastTokenType()) // we don't treat try-paren(i.e. try (....)) as parenthesis
- || "[".equals(paren.getText());
+ String text = paren.getText();
+
+ return ("(".equals(text) && TRY != paren.getLastTokenType()) // we don't treat try-paren(i.e. try (....)) as parenthesis
+ || "[".equals(text) || "?[".equals(text);
}
private void ignoreTokenInsideParens() {
if (!this.isInsideParens()) {
@@ -814,7 +816,7 @@ RANGE_EXCLUSIVE_RIGHT : '..<';
RANGE_EXCLUSIVE_FULL : '<..<';
SPREAD_DOT : '*.';
SAFE_DOT : '?.';
-SAFE_INDEX : '?[';
+SAFE_INDEX : '?[' { this.enterParen(); } -> pushMode(DEFAULT_MODE);
SAFE_CHAIN_DOT : '??.';
ELVIS : '?:';
METHOD_POINTER : '.&';
diff --git a/src/test-resources/core/SafeIndex_01x.groovy b/src/test-resources/core/SafeIndex_01x.groovy
index b1d0856..1ed90fa 100644
--- a/src/test-resources/core/SafeIndex_01x.groovy
+++ b/src/test-resources/core/SafeIndex_01x.groovy
@@ -26,3 +26,6 @@ assert null == a?[1, 2]
def f() {return null}
assert null == f()?[1]
+def list = [1, 2, 3]
+assert 2 == list?[0
+ + 1]