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 7d08750 Trivial refactoring: simplify code with arrays
7d08750 is described below
commit 7d087509edf4a1798b71a836479d5f0cddcc4594
Author: Daniel Sun <[email protected]>
AuthorDate: Tue Jan 14 12:02:50 2020 +0800
Trivial refactoring: simplify code with arrays
(cherry picked from commit 9837a408cb923793733f1aa5a65259ba2dbdcd9c)
---
src/antlr/GroovyLexer.g4 | 12 ++++++++----
.../org/apache/groovy/parser/antlr4/SemanticPredicates.java | 10 +++++-----
2 files changed, 13 insertions(+), 9 deletions(-)
diff --git a/src/antlr/GroovyLexer.g4 b/src/antlr/GroovyLexer.g4
index 2d1797c..ff65e15 100644
--- a/src/antlr/GroovyLexer.g4
+++ b/src/antlr/GroovyLexer.g4
@@ -47,6 +47,7 @@ options {
import java.util.HashSet;
import java.util.Collections;
import java.util.Arrays;
+ import java.util.stream.IntStream;
}
@members {
@@ -73,11 +74,14 @@ options {
super.emit(token);
}
- private static final Set<Integer> REGEX_CHECK_SET =
- Collections.unmodifiableSet(
- new
HashSet<>(Arrays.asList(Identifier, CapitalizedIdentifier, NullLiteral,
BooleanLiteral, THIS, RPAREN, RBRACK, RBRACE, IntegerLiteral,
FloatingPointLiteral, StringLiteral, GStringEnd, INC, DEC)));
+ 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 boolean isRegexAllowed() {
- if (REGEX_CHECK_SET.contains(this.lastTokenType)) {
+ if (Arrays.binarySearch(REGEX_CHECK_ARRAY, this.lastTokenType) >= 0) {
return false;
}
diff --git
a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java
index 48a04c3..5eb20a4 100644
---
a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java
+++
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java
@@ -24,9 +24,8 @@ import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.codehaus.groovy.ast.ModifierNode;
-import java.util.Collections;
+import java.util.Arrays;
import java.util.List;
-import java.util.Set;
import java.util.regex.Pattern;
import static org.apache.groovy.parser.antlr4.GroovyParser.ASSIGN;
@@ -150,8 +149,9 @@ public class SemanticPredicates {
&& LPAREN == (ts.LT(2).getType());
}
- private static final Set<Integer> MODIFIER_SET =
-
Collections.unmodifiableSet(ModifierNode.MODIFIER_OPCODE_MAP.keySet());
+ private static final int[] MODIFIER_ARRAY =
+ ModifierNode.MODIFIER_OPCODE_MAP.keySet().stream()
+ .mapToInt(Integer::intValue).sorted().toArray();
/**
* Distinguish between local variable declaration and method call, e.g. `a
b`
*/
@@ -185,7 +185,7 @@ public class SemanticPredicates {
tokenType3 = ts.LT(index + 2).getType();
return // VOID == tokenType ||
- !(BuiltInPrimitiveType == tokenType ||
MODIFIER_SET.contains(tokenType))
+ !(BuiltInPrimitiveType == tokenType ||
Arrays.binarySearch(MODIFIER_ARRAY, tokenType) >= 0)
&&
Character.isLowerCase(token.getText().codePointAt(0))
&& !(ASSIGN == tokenType3 || (LT == tokenType2 ||
LBRACK == tokenType2));