groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [1/3] groovy git commit: Minor refactoring
Date Sat, 09 Sep 2017 06:03:59 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X 21971d236 -> 4959cd215


http://git-wip-us.apache.org/repos/asf/groovy/blob/4959cd21/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovydocManager.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovydocManager.java
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovydocManager.java
index 2fdef75..a9e5e90 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovydocManager.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovydocManager.java
@@ -52,7 +52,8 @@ public class GroovydocManager {
     private static final boolean ATTACHING_RUNTIME_GROOVYDOC_ENABLED;
     private static final String VALUE = "value";
     private static final String RUNTIME_GROOVYDOC_PATTERN = "(?s)/[*][*]\\s+(\\s+[*]\\s*)*@Groovydoc\\b.+?[*]/";
-    private AstBuilder astBuilder;
+
+    private static final GroovydocManager INSTANCE = new GroovydocManager();
 
     static {
         ATTACHING_GROOVYDOC_ENABLED = isFeatureEnabled(ATTACH_GROOVYDOC) || isFeatureEnabled(EXTRACT_DOC_COMMENT);
@@ -71,8 +72,10 @@ public class GroovydocManager {
         return result;
     }
 
-    public GroovydocManager(AstBuilder astBuilder) {
-        this.astBuilder = astBuilder;
+    private GroovydocManager() {}
+
+    public static GroovydocManager getInstance() {
+        return INSTANCE;
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/groovy/blob/4959cd21/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Pair.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Pair.java
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Pair.java
new file mode 100644
index 0000000..14500be
--- /dev/null
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Pair.java
@@ -0,0 +1,68 @@
+/*
+ *  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.apache.groovy.parser.antlr4;
+
+import java.util.Objects;
+
+/**
+ * key and value pair
+ *
+ * @param <K> key type
+ * @param <V> value type
+ */
+public class Pair<K, V> {
+    private K key;
+    private V value;
+
+    public Pair(K key, V value) {
+        this.key = key;
+        this.value = value;
+    }
+
+    public K getKey() {
+        return key;
+    }
+
+    public void setKey(K key) {
+        this.key = key;
+    }
+
+    public V getValue() {
+        return value;
+    }
+
+    public void setValue(V value) {
+        this.value = value;
+    }
+
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        Pair<?, ?> pair = (Pair<?, ?>) o;
+        return Objects.equals(key, pair.key) &&
+                Objects.equals(value, pair.value);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(key, value);
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/groovy/blob/4959cd21/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/PositionConfigureUtils.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/PositionConfigureUtils.java
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/PositionConfigureUtils.java
new file mode 100644
index 0000000..bbe83eb
--- /dev/null
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/PositionConfigureUtils.java
@@ -0,0 +1,127 @@
+/*
+ *  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.apache.groovy.parser.antlr4.util;
+
+import org.antlr.v4.runtime.Token;
+import org.antlr.v4.runtime.tree.TerminalNode;
+import org.apache.groovy.parser.antlr4.GroovyParser;
+import org.apache.groovy.parser.antlr4.Pair;
+import org.codehaus.groovy.ast.ASTNode;
+
+import static org.codehaus.groovy.runtime.DefaultGroovyMethods.asBoolean;
+
+/**
+ * Utilities for configuring node positions
+ */
+public class PositionConfigureUtils {
+    /**
+     * Sets location(lineNumber, colNumber, lastLineNumber, lastColumnNumber) for node using
standard context information.
+     * Note: this method is implemented to be closed over ASTNode. It returns same node as
it received in arguments.
+     *
+     * @param astNode Node to be modified.
+     * @param ctx     Context from which information is obtained.
+     * @return Modified astNode.
+     */
+    public static <T extends ASTNode> T configureAST(T astNode, GroovyParser.GroovyParserRuleContext
ctx) {
+        Token start = ctx.getStart();
+        Token stop = ctx.getStop();
+
+        astNode.setLineNumber(start.getLine());
+        astNode.setColumnNumber(start.getCharPositionInLine() + 1);
+
+        configureEndPosition(astNode, stop);
+
+        return astNode;
+    }
+
+    public static Pair<Integer, Integer> endPosition(Token token) {
+        String stopText = token.getText();
+        int stopTextLength = 0;
+        int newLineCnt = 0;
+        if (null != stopText) {
+            stopTextLength = stopText.length();
+            newLineCnt = (int) StringUtils.countChar(stopText, '\n');
+        }
+
+        if (0 == newLineCnt) {
+            return new Pair<>(token.getLine(), token.getCharPositionInLine() + 1 +
token.getText().length());
+        } else { // e.g. GStringEnd contains newlines, we should fix the location info
+            return new Pair<>(token.getLine() + newLineCnt, stopTextLength - stopText.lastIndexOf('\n'));
+        }
+    }
+
+    public static <T extends ASTNode> T configureAST(T astNode, TerminalNode terminalNode)
{
+        return configureAST(astNode, terminalNode.getSymbol());
+    }
+
+    public static <T extends ASTNode> T configureAST(T astNode, Token token) {
+        astNode.setLineNumber(token.getLine());
+        astNode.setColumnNumber(token.getCharPositionInLine() + 1);
+        astNode.setLastLineNumber(token.getLine());
+        astNode.setLastColumnNumber(token.getCharPositionInLine() + 1 + token.getText().length());
+
+        return astNode;
+    }
+
+    public static <T extends ASTNode> T configureAST(T astNode, ASTNode source) {
+        astNode.setLineNumber(source.getLineNumber());
+        astNode.setColumnNumber(source.getColumnNumber());
+        astNode.setLastLineNumber(source.getLastLineNumber());
+        astNode.setLastColumnNumber(source.getLastColumnNumber());
+
+        return astNode;
+    }
+
+    public static <T extends ASTNode> T configureAST(T astNode, GroovyParser.GroovyParserRuleContext
ctx, ASTNode stop) {
+        Token start = ctx.getStart();
+
+        astNode.setLineNumber(start.getLine());
+        astNode.setColumnNumber(start.getCharPositionInLine() + 1);
+
+        if (asBoolean(stop)) {
+            astNode.setLastLineNumber(stop.getLastLineNumber());
+            astNode.setLastColumnNumber(stop.getLastColumnNumber());
+        } else {
+            configureEndPosition(astNode, start);
+        }
+
+        return astNode;
+    }
+
+    public static <T extends ASTNode> void configureEndPosition(T astNode, Token token)
{
+        Pair<Integer, Integer> endPosition = endPosition(token);
+        astNode.setLastLineNumber(endPosition.getKey());
+        astNode.setLastColumnNumber(endPosition.getValue());
+    }
+
+    public static <T extends ASTNode> T configureAST(T astNode, ASTNode start, ASTNode
stop) {
+        astNode.setLineNumber(start.getLineNumber());
+        astNode.setColumnNumber(start.getColumnNumber());
+
+        if (asBoolean(stop)) {
+            astNode.setLastLineNumber(stop.getLastLineNumber());
+            astNode.setLastColumnNumber(stop.getLastColumnNumber());
+        } else {
+            astNode.setLastLineNumber(start.getLastLineNumber());
+            astNode.setLastColumnNumber(start.getLastColumnNumber());
+        }
+
+        return astNode;
+    }
+}


Mime
View raw message