2121
2222package com .igormaznitsa .jcp .expression ;
2323
24- import com .igormaznitsa .jcp .context .PreprocessingState ;
2524import com .igormaznitsa .jcp .context .PreprocessorContext ;
2625import com .igormaznitsa .jcp .exceptions .FilePositionInfo ;
2726import com .igormaznitsa .jcp .exceptions .PreprocessorException ;
@@ -51,21 +50,12 @@ public class Expression {
5150 */
5251 private static final Class <?>[] OPERATOR_SIGNATURE_2 = new Class <?>[] {Value .class , Value .class };
5352
54- /**
55- * The variable contains the preprocessor context for the expression, it can be null
56- */
57- private final PreprocessorContext context ;
58-
5953 /**
6054 * The variable contains the expression tree
6155 */
6256 private final ExpressionTree expressionTree ;
6357
64- private Expression (final PreprocessorContext context , final ExpressionTree tree ) {
65- if (tree == null ) {
66- throw context .makeException ("[Expression]The expression tree is null" , null );
67- }
68- this .context = context ;
58+ private Expression (final ExpressionTree tree ) {
6959 this .expressionTree = tree ;
7060 }
7161
@@ -97,13 +87,13 @@ public static Value evalExpression(final String expression, final PreprocessorCo
9787 */
9888
9989 public static Value evalTree (final ExpressionTree tree , final PreprocessorContext context ) {
100- final Expression exp = new Expression (context , tree );
101- return exp .eval (context . getPreprocessingState () );
90+ final Expression exp = new Expression (tree );
91+ return exp .eval (context );
10292 }
10393
10494
10595 private ExpressionTreeElement evalFunction (final ExpressionTreeElement functionElement ,
106- final PreprocessingState state ) {
96+ final PreprocessorContext context ) {
10797 final AbstractFunction function = (AbstractFunction ) functionElement .getItem ();
10898
10999 final int arity = function .getArity ();
@@ -114,8 +104,8 @@ private ExpressionTreeElement evalFunction(final ExpressionTreeElement functionE
114104 final FilePositionInfo [] stack ;
115105 final String sources ;
116106
117- stack = state .makeIncludeStack ();
118- sources = state .getLastReadString ();
107+ stack = context . getPreprocessingState () .makeIncludeStack ();
108+ sources = context . getPreprocessingState () .getLastReadString ();
119109
120110 final StringBuilder signature = new StringBuilder (AbstractFunction .EXECUTION_PREFIX );
121111
@@ -125,14 +115,14 @@ private ExpressionTreeElement evalFunction(final ExpressionTreeElement functionE
125115
126116 for (int i = 0 ; i < arity ; i ++) {
127117 final ExpressionTreeElement item =
128- calculateTreeElement (functionElement .getChildForIndex (i ), state );
118+ this . calculateTreeElement (functionElement .getChildForIndex (i ), context );
129119
130120 final ExpressionItem itemValue = item .getItem ();
131121
132122 if (itemValue instanceof Value ) {
133123 arguments [i ] = (Value ) itemValue ;
134124 } else {
135- throw this . context .makeException (
125+ throw context .makeException (
136126 "[Expression]Wrong argument type detected for the '" + function .getName () +
137127 "' function" , null );
138128 }
@@ -162,7 +152,7 @@ private ExpressionTreeElement evalFunction(final ExpressionTreeElement functionE
162152 }
163153
164154 if (allowed == null ) {
165- throw this . context .makeException (
155+ throw context .makeException (
166156 "[Expression]Unsupported argument detected for '" + function .getName () + '\'' , null );
167157 }
168158
@@ -171,7 +161,7 @@ private ExpressionTreeElement evalFunction(final ExpressionTreeElement functionE
171161 try {
172162 return new ExpressionTreeElement (userFunction .execute (context , arguments ), stack , sources );
173163 } catch (Exception unexpected ) {
174- throw this . context
164+ throw context
175165 .makeException ("[Expression]Unexpected exception during a user function processing" ,
176166 unexpected );
177167 }
@@ -186,21 +176,21 @@ private ExpressionTreeElement evalFunction(final ExpressionTreeElement functionE
186176 final Value result = (Value ) method .invoke (function , callArgs );
187177
188178 if (!result .getType ().isCompatible (function .getResultType ())) {
189- throw this . context .makeException ("[Expression]Unsupported function result detected [" +
179+ throw context .makeException ("[Expression]Unsupported function result detected [" +
190180 result .getType ().getSignature () + ']' , null );
191181 }
192182
193183 return new ExpressionTreeElement (result , stack , sources );
194184 } catch (NoSuchMethodException unexpected ) {
195- throw this . context .makeException (
185+ throw context .makeException (
196186 "[Expression]Can't find a function method to process data [" + signature +
197187 ']' , unexpected );
198188 } catch (Exception unexpected ) {
199189 final Throwable cause = unexpected .getCause ();
200190 if (cause instanceof PreprocessorException ) {
201191 throw (PreprocessorException ) cause ;
202192 }
203- throw this . context .makeException (
193+ throw context .makeException (
204194 "[Expression]Can't execute a function method to process data [" +
205195 function .getClass ().getName () + '.' + signature + ']' , unexpected );
206196 }
@@ -209,7 +199,7 @@ private ExpressionTreeElement evalFunction(final ExpressionTreeElement functionE
209199
210200
211201 private ExpressionTreeElement evalOperator (final ExpressionTreeElement operatorElement ,
212- final PreprocessingState state ) {
202+ final PreprocessorContext context ) {
213203 final AbstractOperator operator = (AbstractOperator ) operatorElement .getItem ();
214204
215205 final int arity = operator .getArity ();
@@ -224,25 +214,25 @@ private ExpressionTreeElement evalOperator(final ExpressionTreeElement operatorE
224214 final FilePositionInfo [] stack ;
225215 final String sources ;
226216
227- stack = state .makeIncludeStack ();
228- sources = state .getLastReadString ();
217+ stack = context . getPreprocessingState () .makeIncludeStack ();
218+ sources = context . getPreprocessingState () .getLastReadString ();
229219
230220 for (int i = 0 ; i < arity ; i ++) {
231221 final ExpressionTreeElement arg = operatorElement .getChildForIndex (i );
232222 if (arg == ExpressionTreeElement .EMPTY_SLOT ) {
233- throw this . context .makeException (
223+ throw context .makeException (
234224 "[Expression]There is not needed argument for the operator [" + operator .getKeyword () +
235225 ']' , null );
236226 }
237227
238- final ExpressionTreeElement currentElement = calculateTreeElement (arg , state );
228+ final ExpressionTreeElement currentElement = calculateTreeElement (arg , context );
239229
240230 final ExpressionItem item = currentElement .getItem ();
241231
242232 if (item instanceof Value ) {
243233 arguments [i ] = (Value ) item ;
244234 } else {
245- throw this . context .makeException (
235+ throw context .makeException (
246236 "[Expression]Non-value detected for the '" + operator .getKeyword () + "' operator" ,
247237 null );
248238 }
@@ -284,7 +274,7 @@ private ExpressionTreeElement evalOperator(final ExpressionTreeElement operatorE
284274 }
285275
286276 if (executeMethod == null ) {
287- throw this . context .makeException (
277+ throw context .makeException (
288278 "[Expression]Unsupported arguments detected for operator '" + operator .getKeyword () +
289279 "' " + Arrays .toString (arguments ), null );
290280 }
@@ -302,15 +292,15 @@ private ExpressionTreeElement evalOperator(final ExpressionTreeElement operatorE
302292 throw new RuntimeException (
303293 "Invocation exception during '" + operator .getKeyword () + "' processing" , thr );
304294 } catch (Exception unexpected ) {
305- throw this . context
295+ throw context
306296 .makeException ("[Exception]Exception during '" + operator .getKeyword () + "' processing" ,
307297 unexpected );
308298 }
309299 }
310300
311301
312302 private ExpressionTreeElement calculateTreeElement (final ExpressionTreeElement element ,
313- final PreprocessingState state ) {
303+ final PreprocessorContext context ) {
314304 ExpressionTreeElement treeElement = element ;
315305
316306 switch (element .getItem ().getExpressionItemType ()) {
@@ -326,38 +316,39 @@ private ExpressionTreeElement calculateTreeElement(final ExpressionTreeElement e
326316 throw new RuntimeException ("Unknown variable [" + name + ']' );
327317 } else {
328318 treeElement =
329- new ExpressionTreeElement (value , state .makeIncludeStack (), state .getLastReadString ());
319+ new ExpressionTreeElement (value , context .getPreprocessingState ().makeIncludeStack (),
320+ context .getPreprocessingState ().getLastReadString ());
330321 }
331322 }
332323 break ;
333324 case OPERATOR : {
334- treeElement = evalOperator (element , state );
325+ treeElement = this . evalOperator (element , context );
335326 }
336327 break ;
337328 case FUNCTION : {
338- treeElement = evalFunction (element , state );
329+ treeElement = evalFunction (element , context );
339330 }
340331 break ;
341332 }
342333 return treeElement ;
343334 }
344335
345336
346- private Value eval (final PreprocessingState state ) {
337+ private Value eval (final PreprocessorContext context ) {
347338 if (expressionTree .isEmpty ()) {
348- throw this . context .makeException ("[Expression]The expression is empty" , null );
339+ throw context .makeException ("[Expression]The expression is empty" , null );
349340 }
350- final ExpressionTreeElement result = calculateTreeElement (expressionTree .getRoot (), state );
341+ final ExpressionTreeElement result = calculateTreeElement (expressionTree .getRoot (), context );
351342 final ExpressionItem resultItem = result .getItem ();
352343
353344 if (resultItem == null ) {
354- throw this . context .makeException ("[Expression]Expression doesn't have result" , null );
345+ throw context .makeException ("[Expression]Expression doesn't have result" , null );
355346 }
356347
357348 if (resultItem instanceof Value ) {
358349 return (Value ) resultItem ;
359350 } else {
360- throw this . context
351+ throw context
361352 .makeException ("[Expression]The expression returns non-value result [" + resultItem + ']' ,
362353 null );
363354 }
0 commit comments