groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 07/18: create parser-agnostic API for parsing snippet
Date Fri, 22 Nov 2019 15:11:23 GMT
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

commit d0f36130f3413051614e3d222afb72aff62a8cb8
Author: Eric Milles <eric.milles@thomsonreuters.com>
AuthorDate: Thu Nov 21 02:51:53 2019 +0800

    create parser-agnostic API for parsing snippet
    
    (cherry picked from commit f6f55ab632fe451aa8fe98ca87ecdd26b06d659a)
---
 .../codehaus/groovy/ast/tools/GenericsUtils.java   |  3 +-
 .../org/codehaus/groovy/control/ParserPlugin.java  | 14 ++++----
 .../main/groovy/groovy/util/OptionAccessor.groovy  | 38 ----------------------
 .../MarkupTemplateTypeCheckingExtension.groovy     | 13 ++++++--
 4 files changed, 19 insertions(+), 49 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
index 1e084dc..c142de5 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
@@ -18,6 +18,7 @@
  */
 package org.codehaus.groovy.ast.tools;
 
+import groovy.lang.GroovyRuntimeException;
 import groovy.lang.Tuple2;
 import groovy.transform.stc.IncorrectTypeHintException;
 import org.codehaus.groovy.GroovyBugError;
@@ -582,7 +583,7 @@ public class GenericsUtils {
 
     public static ClassNode[] parseClassNodesFromString(final String option, final SourceUnit
sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage)
{
         try {
-            ModuleNode moduleNode = ParserPlugin.buildAST("Dummy<" + option + "> dummy;",
compilationUnit.getConfiguration(), compilationUnit.getClassLoader(), null);
+            ModuleNode moduleNode = ParserPlugin.buildAST("Dummy<" + option + "> dummy;",
compilationUnit.getClassLoader(), compilationUnit.getConfiguration(), null);
             DeclarationExpression dummyDeclaration = (DeclarationExpression) ((ExpressionStatement)
moduleNode.getStatementBlock().getStatements().get(0)).getExpression();
 
             // the returned node is DummyNode<Param1, Param2, Param3, ...)
diff --git a/src/main/java/org/codehaus/groovy/control/ParserPlugin.java b/src/main/java/org/codehaus/groovy/control/ParserPlugin.java
index ee02b95..1d4f488 100644
--- a/src/main/java/org/codehaus/groovy/control/ParserPlugin.java
+++ b/src/main/java/org/codehaus/groovy/control/ParserPlugin.java
@@ -22,6 +22,7 @@ import groovy.lang.GroovyClassLoader;
 import org.codehaus.groovy.ast.ModuleNode;
 import org.codehaus.groovy.syntax.ParserException;
 import org.codehaus.groovy.syntax.Reduction;
+import org.codehaus.groovy.util.CharSequenceReader;
 
 import java.io.Reader;
 
@@ -34,12 +35,11 @@ public interface ParserPlugin {
 
     ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduction cst) throws
ParserException;
 
-    static ModuleNode buildAST(CharSequence sourceText, CompilerConfiguration config, GroovyClassLoader
loader, ErrorCollector errors) throws CompilationFailedException {
-        SourceUnit sourceUnit = new SourceUnit("Script" + System.nanoTime() + ".groovy",
sourceText.toString(), config, loader, errors);
-        sourceUnit.parse();
-        sourceUnit.completePhase();
-        sourceUnit.nextPhase();
-        sourceUnit.convert();
-        return sourceUnit.getAST();
+    static ModuleNode buildAST(CharSequence source, GroovyClassLoader loader, CompilerConfiguration
config, ErrorCollector errors) throws ParserException {
+        String scriptName = "Script" + System.nanoTime() + ".groovy";
+        SourceUnit sourceUnit = new SourceUnit(scriptName, source.toString(), config, loader,
errors);
+
+        ParserPlugin parserPlugin = config.getPluginFactory().createParserPlugin();
+        return parserPlugin.buildAST(sourceUnit, loader, parserPlugin.parseCST(sourceUnit,
new CharSequenceReader(source)));
     }
 }
diff --git a/subprojects/groovy-cli-commons/src/main/groovy/groovy/util/OptionAccessor.groovy
b/subprojects/groovy-cli-commons/src/main/groovy/groovy/util/OptionAccessor.groovy
deleted file mode 100644
index 935cf10..0000000
--- a/subprojects/groovy-cli-commons/src/main/groovy/groovy/util/OptionAccessor.groovy
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- *  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 groovy.util
-
-/**
- * @deprecated use {@code groovy.cli.picocli.OptionAccessor} or {@link groovy.cli.commons.OptionAccessor}
- */
-@Deprecated
-class OptionAccessor {
-    @Delegate
-    groovy.cli.commons.OptionAccessor delegate
-
-    // explicit delegate since groovyObject methods ignored by @Delegate
-    def getProperty(String name) {
-        delegate.getProperty(name)
-    }
-
-    // explicit delegate since groovyObject methods ignored by @Delegate
-    def invokeMethod(String name, Object args) {
-        delegate.invokeMethod(name, args)
-    }
-}
diff --git a/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupTemplateTypeCheckingExtension.groovy
b/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupTemplateTypeCheckingExtension.groovy
index 19e2d00..697e0b4 100644
--- a/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupTemplateTypeCheckingExtension.groovy
+++ b/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupTemplateTypeCheckingExtension.groovy
@@ -174,11 +174,18 @@ class MarkupTemplateTypeCheckingExtension extends GroovyTypeCheckingExtensionSup
 
     @CompileStatic
     private static ClassNode buildNodeFromString(String option, TypeCheckingContext ctx)
{
-        ModuleNode moduleNode = ParserPlugin.buildAST("$option variable;", ctx.compilationUnit.configuration,
ctx.compilationUnit.classLoader, ctx.errorCollector)
-        ClassNode optionNode = ((DeclarationExpression) ((ExpressionStatement) moduleNode.statementBlock.statements[0]).expression).leftExpression.type
+        def moduleNode = ParserPlugin.buildAST("Dummy<$option> dummy;", ctx.compilationUnit.classLoader,
ctx.compilationUnit.configuration, ctx.errorCollector)
+        ClassNode parsedNode = ((DeclarationExpression) ((ExpressionStatement) moduleNode.statementBlock.statements[0]).expression).leftExpression.type
         ClassNode dummyClass = new ClassNode("dummy", 0, OBJECT_TYPE)
         dummyClass.setModule(new ModuleNode(ctx.source))
-        MethodNode dummyMN = new MethodNode("dummy", 0, optionNode, Parameter.EMPTY_ARRAY,
ClassNode.EMPTY_ARRAY, EmptyStatement.INSTANCE)
+        MethodNode dummyMN = new MethodNode(
+                "dummy",
+                0,
+                parsedNode,
+                Parameter.EMPTY_ARRAY,
+                ClassNode.EMPTY_ARRAY,
+                EmptyStatement.INSTANCE
+        )
         dummyClass.addMethod(dummyMN)
         ResolveVisitor visitor = new ResolveVisitor(ctx.compilationUnit) {
             @Override


Mime
View raw message