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 2019/11/29 19:41:19 UTC
[groovy] 02/02: Add a util `compile` method to `AstStringCompiler`
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 03cbd0c57ca2cf05ed26cc82a743e6f81464c8f3
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Nov 30 03:35:47 2019 +0800
Add a util `compile` method to `AstStringCompiler`
(cherry picked from commit df191fba050fee6a7d13a9554928ba557d389a79)
---
.../groovy/ast/builder/AstStringCompiler.java | 14 +++++++-
.../ast/builder/AstStringCompilerTest.groovy | 39 ++++++++++++++++++++++
2 files changed, 52 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/codehaus/groovy/ast/builder/AstStringCompiler.java b/src/main/java/org/codehaus/groovy/ast/builder/AstStringCompiler.java
index c48114e..8b4558a 100644
--- a/src/main/java/org/codehaus/groovy/ast/builder/AstStringCompiler.java
+++ b/src/main/java/org/codehaus/groovy/ast/builder/AstStringCompiler.java
@@ -35,7 +35,7 @@ import java.util.stream.Collectors;
/**
* This class handles converting Strings to ASTNode lists.
*/
-class AstStringCompiler {
+public class AstStringCompiler {
/**
* Performs the String source to {@link java.util.List} of {@link ASTNode}.
*
@@ -44,6 +44,7 @@ class AstStringCompiler {
* @param compilePhase
* the int based CompilePhase to compile it to.
* @param statementsOnly
+ * @return {@link java.util.List} of {@link ASTNode}
*/
public List<ASTNode> compile(String script, CompilePhase compilePhase, boolean statementsOnly) {
final String scriptClassName = makeScriptClassName();
@@ -71,6 +72,17 @@ class AstStringCompiler {
return result;
}
+ /**
+ * Performs the String source to {@link java.util.List} of statement {@link ASTNode}.
+ *
+ * @param script a Groovy script in String form
+ * @return {@link java.util.List} of {@link ASTNode}
+ * @since 3.0.0
+ */
+ public List<ASTNode> compile(String script) {
+ return this.compile(script, CompilePhase.CONVERSION, true);
+ }
+
private static String makeScriptClassName() {
return "Script" + System.nanoTime();
}
diff --git a/src/test/org/codehaus/groovy/ast/builder/AstStringCompilerTest.groovy b/src/test/org/codehaus/groovy/ast/builder/AstStringCompilerTest.groovy
new file mode 100644
index 0000000..575768e
--- /dev/null
+++ b/src/test/org/codehaus/groovy/ast/builder/AstStringCompilerTest.groovy
@@ -0,0 +1,39 @@
+/*
+ * 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.codehaus.groovy.ast.builder
+
+import groovy.test.GroovyTestCase
+
+import static org.codehaus.groovy.ast.tools.GeneralUtils.block
+import static org.codehaus.groovy.ast.tools.GeneralUtils.constX
+import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt
+
+class AstStringCompilerTest extends GroovyTestCase {
+ void testCompile() {
+ def actual = new AstStringCompiler().compile(""" "Hello World" """)
+ def expected = [
+ block(
+ stmt(
+ constX("Hello World")
+ )
+ )
+ ]
+ AstAssert.assertSyntaxTree(expected, actual)
+ }
+}