1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.jexl3.parser;
18
19 import java.util.Map;
20
21 import org.apache.commons.jexl3.JexlFeatures;
22 import org.apache.commons.jexl3.internal.Frame;
23 import org.apache.commons.jexl3.internal.Scope;
24
25 /**
26 * Enhanced script to allow parameters declaration.
27 */
28 public class ASTJexlScript extends JexlLexicalNode {
29 /** Serial uid.*/
30 private static final long serialVersionUID = 202112111533L;
31 /** The pragmas. */
32 private Map<String, Object> pragmas;
33 /** Features. */
34 private transient JexlFeatures features;
35 /** The script scope. */
36 private transient Scope scope;
37
38 public ASTJexlScript(final int id) {
39 super(id);
40 }
41
42 public ASTJexlScript(final Parser p, final int id) {
43 super(id);
44 }
45
46 /**
47 * Creates an array of arguments by copying values up to the number of parameters.
48 * @param caller the calling frame
49 * @param values the argument values
50 * @return the arguments array
51 */
52 public Frame createFrame(final Frame caller, final Object... values) {
53 return scope != null ? scope.createFrame(features.supportsReferenceCapture(), caller, values) : null;
54 }
55
56 /**
57 * Creates an array of arguments by copying values up to the number of parameters.
58 * @param values the argument values
59 * @return the arguments array
60 */
61 public Frame createFrame(final Object... values) {
62 return createFrame(null, values);
63 }
64
65 /**
66 * Gets the (maximum) number of arguments this script expects.
67 * @return the number of parameters
68 */
69 public int getArgCount() {
70 return scope != null ? scope.getArgCount() : 0;
71 }
72
73 /**
74 * Gets this script captured variable, i.e. symbols captured from outer scopes.
75 * @return the captured variable names
76 */
77 public String[] getCapturedVariables() {
78 return scope != null ? scope.getCapturedVariables() : null;
79 }
80
81 /**
82 * @return this script scope
83 */
84 public JexlFeatures getFeatures() {
85 return features;
86 }
87
88 /**
89 * Gets this script local variable, i.e. symbols assigned to local variables.
90 * @return the local variable names
91 */
92 public String[] getLocalVariables() {
93 return scope != null ? scope.getLocalVariables() : null;
94 }
95
96 /**
97 * Gets this script parameters, i.e. symbols assigned before creating local variables.
98 * @return the parameter names
99 */
100 public String[] getParameters() {
101 return scope != null ? scope.getParameters() : null;
102 }
103
104 /**
105 * @return this script pragmas.
106 */
107 public Map<String, Object> getPragmas() {
108 return pragmas;
109 }
110
111 /**
112 * @return this script scope
113 */
114 public Scope getScope() {
115 return scope;
116 }
117
118 /**
119 * Gets this script symbols, i.e. parameters and local variables.
120 * @return the symbol names
121 */
122 public String[] getSymbols() {
123 return scope != null ? scope.getSymbols() : null;
124 }
125
126 /**
127 * Checks whether a given symbol is captured.
128 * @param symbol the symbol number
129 * @return true if captured, false otherwise
130 */
131 public boolean isCapturedSymbol(final int symbol) {
132 return scope != null && scope.isCapturedSymbol(symbol);
133 }
134
135 @Override
136 public Object jjtAccept(final ParserVisitor visitor, final Object data) {
137 return visitor.visit(this, data);
138 }
139
140 /**
141 * Consider script with no parameters that return lambda as parametric-scripts.
142 * @return the script
143 */
144 public ASTJexlScript script() {
145 if (scope == null && jjtGetNumChildren() == 1 && jjtGetChild(0) instanceof ASTJexlLambda) {
146 final ASTJexlLambda lambda = (ASTJexlLambda) jjtGetChild(0);
147 lambda.jjtSetParent(null);
148 lambda.setFeatures(getFeatures());
149 return lambda;
150 }
151 return this;
152 }
153
154 /**
155 * Sets this script features.
156 * @param theFeatures the features
157 */
158 public void setFeatures(final JexlFeatures theFeatures) {
159 this.features = theFeatures;
160 }
161
162 /**
163 * Sets this script pragmas.
164 * @param thePragmas the pragmas
165 */
166 public void setPragmas(final Map<String, Object> thePragmas) {
167 this.pragmas = thePragmas;
168 }
169
170 /**
171 * Sets this script scope.
172 * @param theScope the scope
173 */
174 public void setScope(final Scope theScope) {
175 this.scope = theScope;
176 if (theScope != null) {
177 for(int a = 0; a < theScope.getArgCount(); ++a) {
178 declareSymbol(a);
179 }
180 }
181 }
182 }