View Javadoc
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 org.apache.commons.jexl3.JexlFeatures;
20  import org.apache.commons.jexl3.internal.Scope;
21  import java.util.Map;
22  import org.apache.commons.jexl3.internal.Frame;
23  
24  /**
25   * Enhanced script to allow parameters declaration.
26   */
27  public class ASTJexlScript extends JexlLexicalNode  {
28      /** serial uid.*/
29      private static final long serialVersionUID = 202112111533L;
30      /** The pragmas. */
31      private Map<String, Object> pragmas = null;
32      /** Features. */
33      private transient JexlFeatures features = null;
34      /** The script scope. */
35      private transient Scope scope = null;
36  
37      public ASTJexlScript(final int id) {
38          super(id);
39      }
40  
41      public ASTJexlScript(final Parser p, final int id) {
42          super(p, id);
43      }
44  
45      /**
46       * Consider script with no parameters that return lambda as parametric-scripts.
47       * @return the script
48       */
49      public ASTJexlScript script() {
50          if (scope == null && jjtGetNumChildren() == 1 && jjtGetChild(0) instanceof ASTJexlLambda) {
51              final ASTJexlLambda lambda = (ASTJexlLambda) jjtGetChild(0);
52              lambda.jjtSetParent(null);
53              return lambda;
54          }
55          return this;
56      }
57  
58      @Override
59      public Object jjtAccept(final ParserVisitor visitor, final Object data) {
60          return visitor.visit(this, data);
61      }
62  
63      /**
64       * Sets this script pragmas.
65       * @param thePragmas the pragmas
66       */
67      public void setPragmas(final Map<String, Object> thePragmas) {
68          this.pragmas = thePragmas;
69      }
70  
71      /**
72       * @return this script pragmas.
73       */
74      public Map<String, Object> getPragmas() {
75          return pragmas;
76      }
77  
78      /**
79       * Sets this script features.
80       * @param theFeatures the features
81       */
82      public void setFeatures(final JexlFeatures theFeatures) {
83          this.features = theFeatures;
84      }
85  
86      /**
87       * @return this script scope
88       */
89      public JexlFeatures getFeatures() {
90          return features;
91      }
92  
93      /**
94       * Sets this script scope.
95       * @param theScope the scope
96       */
97      public void setScope(final Scope theScope) {
98          this.scope = theScope;
99          if (theScope != null) {
100             for(int a = 0; a < theScope.getArgCount(); ++a) {
101                 this.declareSymbol(a);
102             }
103         }
104     }
105 
106     /**
107      * @return this script scope
108      */
109     public Scope getScope() {
110         return scope;
111     }
112 
113     /**
114      * Creates an array of arguments by copying values up to the number of parameters.
115      * @param caller the calling frame
116      * @param values the argument values
117      * @return the arguments array
118      */
119     public Frame createFrame(final Frame caller, final Object... values) {
120         return scope != null? scope.createFrame(caller, values) : null;
121     }
122 
123     /**
124      * Creates an array of arguments by copying values up to the number of parameters.
125      * @param values the argument values
126      * @return the arguments array
127      */
128     public Frame createFrame(final Object... values) {
129         return createFrame(null, values);
130     }
131 
132     /**
133      * Gets the (maximum) number of arguments this script expects.
134      * @return the number of parameters
135      */
136     public int getArgCount() {
137         return scope != null? scope.getArgCount() : 0;
138     }
139 
140     /**
141      * Gets this script symbols, i.e. parameters and local variables.
142      * @return the symbol names
143      */
144     public String[] getSymbols() {
145         return scope != null? scope.getSymbols() : null;
146     }
147 
148     /**
149      * Gets this script parameters, i.e. symbols assigned before creating local variables.
150      * @return the parameter names
151      */
152     public String[] getParameters() {
153         return scope != null? scope.getParameters() : null;
154     }
155 
156     /**
157      * Gets this script local variable, i.e. symbols assigned to local variables.
158      * @return the local variable names
159      */
160     public String[] getLocalVariables() {
161         return scope != null? scope.getLocalVariables() : null;
162     }
163 
164     /**
165      * Checks whether a given symbol is captured.
166      * @param symbol the symbol number
167      * @return true if captured, false otherwise
168      */
169     public boolean isCapturedSymbol(final int symbol) {
170         return scope != null && scope.isCapturedSymbol(symbol);
171     }
172 
173     /**
174      * Gets this script captured variable, i.e. symbols captured from outer scopes.
175      * @return the captured variable names
176      */
177     public String[] getCapturedVariables() {
178         return scope != null? scope.getCapturedVariables() : null;
179     }
180 }