/** <summary> * To write out the value of an ASTExpr, invoke the evaluator in eval.g * to walk the tree writing out the values. For efficiency, don't * compute a bunch of strings and then pack them together. Write out directly. * </summary> * * <remarks> * Compute separator and wrap expressions, save as strings so we don't * recompute for each value in a multi-valued attribute or expression. * * If they set anchor option, then inform the writer to push current * char position. * </remarks> */ public override int Write( StringTemplate self, IStringTemplateWriter @out ) { if ( _exprTree == null || self == null || @out == null ) { return 0; } // handle options, anchor, wrap, separator... StringTemplateAST anchorAST = (StringTemplateAST)GetOption( "anchor" ); if ( anchorAST != null ) { // any non-empty expr means true; check presence @out.PushAnchorPoint(); } @out.PushIndentation( Indentation ); HandleExprOptions( self ); //System.out.println("evaluating tree: "+exprTree.toStringList()); #if COMPILE_EXPRESSIONS if ( EvaluateAction == null ) EvaluateAction = GetEvaluator( this, AST ); #else ActionEvaluator eval = new ActionEvaluator( self, this, @out, _exprTree ); #endif int n = 0; try { // eval and write out tree #if COMPILE_EXPRESSIONS n = EvaluateAction( self, @out ); #else n = eval.action(); #endif } catch ( RecognitionException re ) { self.Error( "can't evaluate tree: " + _exprTree.ToStringTree(), re ); } @out.PopIndentation(); if ( anchorAST != null ) { @out.PopAnchorPoint(); } return n; }
/// <summary> /// To write out the value of an ASTExpr, invoke the evaluator in eval.g /// to walk the tree writing out the values. For efficiency, don't /// compute a bunch of strings and then pack them together. Write out directly. /// /// Compute separator and wrap expressions, save as strings so we don't /// recompute for each value in a multi-valued attribute or expression. /// /// If they set anchor option, then inform the writer to push current /// char position. /// </summary> public override int Write(StringTemplate self, IStringTemplateWriter output) { if (exprTree == null || self == null || output == null) { return 0; } output.PushIndentation(Indentation); // handle options, anchor, wrap, separator... StringTemplateAST anchorAST = (StringTemplateAST)GetOption("anchor"); if (anchorAST != null) // any non-empty expr means true; check presence { output.PushAnchorPoint(); } HandleExprOptions(self); ActionEvaluator eval = new ActionEvaluator(self, this, output); ActionParser.initializeASTFactory(eval.getASTFactory()); int n = 0; try { n = eval.action(exprTree); // eval and write out tree } catch (RecognitionException re) { self.Error("can't evaluate tree: " + exprTree.ToStringList(), re); } output.PopIndentation(); if (anchorAST != null) { output.PopAnchorPoint(); } return n; }
/** <summary> * To write out the value of an ASTExpr, invoke the evaluator in eval.g * to walk the tree writing out the values. For efficiency, don't * compute a bunch of strings and then pack them together. Write out directly. * </summary> * * <remarks> * Compute separator and wrap expressions, save as strings so we don't * recompute for each value in a multi-valued attribute or expression. * * If they set anchor option, then inform the writer to push current * char position. * </remarks> */ public override int Write( StringTemplate self, IStringTemplateWriter @out ) { if ( _exprTree == null || self == null || @out == null ) { return 0; } // handle options, anchor, wrap, separator... StringTemplateAST anchorAST = (StringTemplateAST)GetOption( "anchor" ); if ( anchorAST != null ) { // any non-empty expr means true; check presence @out.PushAnchorPoint(); } @out.PushIndentation( Indentation ); HandleExprOptions( self ); //System.out.println("evaluating tree: "+exprTree.toStringList()); ActionEvaluator eval = null; #if COMPILE_EXPRESSIONS bool compile = self.Group != null && self.Group.EnableCompiledExpressions; bool cache = compile && self.Group.EnableCachedExpressions; System.Func<StringTemplate, IStringTemplateWriter, int> evaluator = EvaluateAction; if (compile) { if (!cache || EvaluateAction == null) { // caching won't help here because AST is read only EvaluatorCacheMisses++; evaluator = GetEvaluator(this, AST); if (cache) EvaluateAction = evaluator; } else { EvaluatorCacheHits++; } } else #endif { eval = new ActionEvaluator(self, this, @out, _exprTree); } int n = 0; try { // eval and write out tree #if COMPILE_EXPRESSIONS if (compile) { n = evaluator(self, @out); } else #endif { n = eval.action(); } } catch ( RecognitionException re ) { self.Error( "can't evaluate tree: " + _exprTree.ToStringTree(), re ); } @out.PopIndentation(); if ( anchorAST != null ) { @out.PopAnchorPoint(); } return n; }