static IEnumerable <object> Unfold(IEnumerable <object> items) { return(items.SelectMany((item) => { item = ASTExpr.ConvertAnythingIteratableToIterator(item); if (item is Antlr.Runtime.JavaExtensions.Iterator) { return ((Antlr.Runtime.JavaExtensions.Iterator)item).Source.Cast <object>(); } else if (item is string) { return Enumerable.Repeat(item, 1); } else if (item is IEnumerable) { return ((IEnumerable)item).Cast <object>(); } else if (item is IEnumerator) { return Enumerable.Repeat(item, 1); } else { return Enumerable.Repeat(item, 1); } })); }
public ActionEvaluator(StringTemplate self, ASTExpr chunk, IStringTemplateWriter writer, ITree input) : base(new CommonTreeNodeStream(new StringTemplateTreeAdaptor(), input)) { this.self = self; this.chunk = chunk; this.writer = writer; }
public virtual void AddElseIfSubtemplate(ASTExpr conditionalTree, StringTemplate subtemplate) { if (_elseIfSubtemplates == null) { _elseIfSubtemplates = new List <ElseIfClauseData>(); } ElseIfClauseData d = new ElseIfClauseData() { expr = conditionalTree, st = subtemplate }; _elseIfSubtemplates.Add(d); }
static IEnumerable <object> Unfold(IEnumerable <object> items) { return(items.SelectMany((item) => { item = ASTExpr.ConvertAnythingIteratableToIterator(item); if (item is string) { return Enumerable.Repeat(item, 1); } else if (item is IEnumerable) { return ((IEnumerable)item).Cast <object>(); } else if (item is IEnumerator) { return Enumerate((IEnumerator)item); } else { return Enumerable.Repeat(item, 1); } })); }
static System.Func <StringTemplate, IStringTemplateWriter, bool> GetEvaluator(ASTExpr chunk, ITree condition) { if (EnableDynamicMethods) { try { DynamicMethod method = null; #if CACHE_FUNCTORS if (!_methods.TryGetValue(condition, out method)) #endif { Type[] parameterTypes = { typeof(ASTExpr), typeof(StringTemplate), typeof(IStringTemplateWriter) }; method = new DynamicMethod("ConditionEvaluator" + _evaluatorNumber, typeof(bool), parameterTypes, typeof(ConditionalExpr), true); #if !NETSTANDARD2_0 method.DefineParameter(1, ParameterAttributes.None, "chunk"); method.DefineParameter(2, ParameterAttributes.None, "self"); method.DefineParameter(3, ParameterAttributes.None, "writer"); #endif _evaluatorNumber++; var gen = method.GetILGenerator(); ActionEvaluator evalCompiled = new ActionEvaluator(null, chunk, null, condition); evalCompiled.ifConditionCompiled(gen); gen.Emit(OpCodes.Ret); #if CACHE_FUNCTORS _methods[condition] = method; #endif } var dynamicEvaluator = (System.Func <StringTemplate, IStringTemplateWriter, bool>)method.CreateDelegate(typeof(System.Func <StringTemplate, IStringTemplateWriter, bool>), chunk); return(dynamicEvaluator); } catch { // fall back to functional (or interpreted) version } } if (EnableFunctionalMethods) { try { ActionEvaluator evalFunctional = new ActionEvaluator(null, chunk, null, condition); var functionalEvaluator = evalFunctional.ifConditionFunctional(); HoldsConditionFuncAndChunk holder = new HoldsConditionFuncAndChunk() { func = functionalEvaluator, chunk = chunk }; return((System.Func <StringTemplate, IStringTemplateWriter, bool>)System.Delegate.CreateDelegate(typeof(System.Func <StringTemplate, IStringTemplateWriter, bool>), holder, typeof(ConditionalExpr).GetMethod("CallFunctionalConditionEvaluator"))); } catch { // fall back to interpreted version } } return(new System.Func <StringTemplate, IStringTemplateWriter, bool>((self, @out) => { ActionEvaluator eval = new ActionEvaluator(self, chunk, @out, condition); return eval.ifCondition(); })); }
static System.Func<StringTemplate, IStringTemplateWriter, bool> GetEvaluator( ASTExpr chunk, ITree condition ) { if ( EnableDynamicMethods ) { try { DynamicMethod method = null; #if CACHE_FUNCTORS if ( !_methods.TryGetValue( condition, out method ) ) #endif { Type[] parameterTypes = { typeof( ASTExpr ), typeof( StringTemplate ), typeof( IStringTemplateWriter ) }; method = new DynamicMethod( "ConditionEvaluator" + _evaluatorNumber, typeof( bool ), parameterTypes, typeof( ConditionalExpr ), true ); method.DefineParameter( 1, ParameterAttributes.None, "chunk" ); method.DefineParameter( 2, ParameterAttributes.None, "self" ); method.DefineParameter( 3, ParameterAttributes.None, "writer" ); _evaluatorNumber++; var gen = method.GetILGenerator(); ActionEvaluator evalCompiled = new ActionEvaluator( null, chunk, null, condition ); evalCompiled.ifConditionCompiled( gen ); gen.Emit( OpCodes.Ret ); #if CACHE_FUNCTORS _methods[condition] = method; #endif } var dynamicEvaluator = (System.Func<StringTemplate, IStringTemplateWriter, bool>)method.CreateDelegate( typeof( System.Func<StringTemplate, IStringTemplateWriter, bool> ), chunk ); return dynamicEvaluator; } catch { // fall back to functional (or interpreted) version } } if ( EnableFunctionalMethods ) { try { ActionEvaluator evalFunctional = new ActionEvaluator( null, chunk, null, condition ); var functionalEvaluator = evalFunctional.ifConditionFunctional(); HoldsConditionFuncAndChunk holder = new HoldsConditionFuncAndChunk() { func = functionalEvaluator, chunk = chunk }; return (System.Func<StringTemplate, IStringTemplateWriter, bool>)System.Delegate.CreateDelegate( typeof( System.Func<StringTemplate, IStringTemplateWriter, bool> ), holder, typeof( ConditionalExpr ).GetMethod( "CallFunctionalConditionEvaluator" ) ); } catch { // fall back to interpreted version } } return new System.Func<StringTemplate, IStringTemplateWriter, bool>( ( self, @out ) => { ActionEvaluator eval = new ActionEvaluator( self, chunk, @out, condition ); return eval.ifCondition(); } ); }
public virtual void AddElseIfSubtemplate( ASTExpr conditionalTree, StringTemplate subtemplate) { if ( _elseIfSubtemplates == null ) { _elseIfSubtemplates = new List<ElseIfClauseData>(); } ElseIfClauseData d = new ElseIfClauseData() { expr = conditionalTree, st = subtemplate }; _elseIfSubtemplates.Add( d ); }