public void ProcessMacroBegin( int id, IMacro macro, IMacroArguments macroArgs, bool postProcess ) { if( lastDoneId != lastBeginId ) { // // macros are being called recursivly // sb.AppendLine(); } sbIndent(); sb.AppendFormat( "{0}\n", macro.Name ); lastBeginId = id; ++callDepth; }
///////////////////////////////////////////////////////////////////////////// public override object Evaluate( IMacro macro, IMacroArguments macroArgs ) { // ****** if( 0 == macroArgs.BlockText.Length ) { return string.Empty; } // ****** string text = string.Empty; // // run the contents of the block through the scanner // text = macroArgs.BlockText; int hash = text.GetHashCode(); var scanner = mp.Get<IScanner>(); for( int i = 0; i < 128; i++ ) { text = scanner.Scanner( text, string.Format( "ExpandoBlock_{0}", i ) ); int newHash = text.GetHashCode(); if( hash == newHash ) { ///ThreadContext.WriteLine( "exit eval block on iteration {0}", i ); break; } hash = newHash; } // ****** return text; }
///////////////////////////////////////////////////////////////////////////// //public string [] ObjectMacroNames //{ // get { // return macroNames; // } //} ///////////////////////////////////////////////////////////////////////////// public string DefineMacro( IMacroArguments macroArgs, string macroName, object macroObject, IList<string> argNames, bool isPushMacro ) { // ****** if( string.IsNullOrEmpty(macroName) ) { ThreadContext.MacroError( "define macro expected the name of a macro" ); } // ****** IMacro newMacro = null; string macroText = macroObject as string; if( null != macroText ) { newMacro = mp.CreateTextMacro( macroName, macroText, argNames ); } else { IMacro objMacro = macroObject as IMacro; if( null != objMacro ) { newMacro = objMacro.MacroHandler.Create( macroName, objMacro, null, false ); } else { newMacro = mp.CreateObjectMacro( macroName, macroObject ); } } // ****** if( isPushMacro ) { IMacro existingMacro; if( mp.FindMacro(newMacro.Name, out existingMacro) ) { // // our reference will stay alive // mp.DeleteMacro( existingMacro ); // // "push" it // newMacro.Pushed = existingMacro; } } // ****** IMacro callingMacro = Get<InvocationStack>().GetCallingMacro(); if( null != callingMacro && MacroType.Text == callingMacro.MacroType ) { newMacro.SourceFile = callingMacro.SourceFile; newMacro.MacroStartLine = callingMacro.MacroStartLine + macroArgs.Input.Line; } else { newMacro.SourceFile = macroArgs.Input.SourceName; newMacro.MacroStartLine = macroArgs.Input.Line; } if( macroArgs.Options.NoExpression ) { newMacro.Flags |= MacroFlags.NonExpressive; } // ****** mp.AddMacro( newMacro ); // ****** return string.Empty; }
///////////////////////////////////////////////////////////////////////////// // // (#Deflist `macroName' ) // ///////////////////////////////////////////////////////////////////////////// public override object Evaluate( IMacro macro, IMacroArguments macroArgs ) { // ****** var args = GetMacroArgsAsTuples( macroArgs.Expression ) as NmpTuple<string, bool>; string macroName = args.Item1; bool trimLines = args.Item2; // ****** if( string.IsNullOrEmpty(macroName) ) { ThreadContext.MacroError( "attempt to define or pushdef a macro without a name" ); } // ****** // // parse text into NmpArray instance // var list = new NmpStringList( macroArgs.BlockText, trimLines ); // ****** // // create the new macro // IMacro newMacro = mp.AddObjectMacro( macroName, list ); // ****** return string.Empty; }
///////////////////////////////////////////////////////////////////////////// public virtual object Evaluate( IMacro macro, IMacroArguments macroArgs ) { // ****** var oee = new ExpressionEvaluator( mp ); return oee.Evaluate( macro, macroArgs.Expression ); }
///////////////////////////////////////////////////////////////////////////// // // (#foreach objToEnumerate [, `extraArgs'] ) // ///////////////////////////////////////////////////////////////////////////// public override object Evaluate( IMacro macro, IMacroArguments macroArgs ) { // ****** var args = GetMacroArgsAsTuples( macroArgs.Expression ) as NmpTuple<object, object[]>; object objToEnumerate = args.Item1; object [] extraArgs = args.Item2; // ****** if( null == objToEnumerate ) { ThreadContext.MacroError( "(#foreach ...) requires an object to iterate over as its first argument" ); } // ****** var handler = new ForeachHandler( mp ); return handler.Foreach_TextChunk( objToEnumerate, macroArgs.BlockText, extraArgs ); }
///////////////////////////////////////////////////////////////////////////// public virtual object Evaluate( IMacro macro, IMacroArguments macroArgs ) { // ****** MacroCall method = macro.MacroHandlerData as MacroCall; if( null == method ) { throw ExceptionHelpers.CreateException( "MacroHandlerData for macro \"{0}\" does not contain a valid MacroCall delegate instance", macro.Name ); } // ****** // // does not return unless validation succeeds // int skipCount; ArgumentList argList = GetArguments( macro.Name, macroArgs.Expression, out skipCount ); // ****** // // call the macro method // object result = method( macro, argList, macroArgs ); // ****** // // finish the expression with the result of the macro method call // return EvaluateRemainingExpressions( mp, result, (MacroExpression) macroArgs.Expression, skipCount ); }
///////////////////////////////////////////////////////////////////////////// public override object Evaluate( IMacro macro, IMacroArguments macroArgs ) { // ****** string text = string.Empty; if( macroArgs.BlockText.Length > 0 ) { NamedTextBlocks blocks = gc.GetTextBlocks(); text = blocks.AddTextBlock( macroArgs.BlockText ); } // ****** return text; }
///////////////////////////////////////////////////////////////////////////// // // (#defmacro `macroName' [, `positional, arg, names'] ) // ///////////////////////////////////////////////////////////////////////////// public override object Evaluate( IMacro macro, IMacroArguments macroArgs ) { // ****** var args = GetMacroArgsAsTuples( macroArgs.Expression ) as NmpTuple<string, string []>; string macroName = args.Item1; string [] optArgs = args.Item2; // ****** return builtins.DefineMacro( macroArgs, macroName, macroArgs.BlockText, optArgs, Pushdef.PUSHDEF == macro.Name ); }
///////////////////////////////////////////////////////////////////////////// public override object Evaluate( IMacro macro, IMacroArguments macroArgs ) { // ****** // // we did all the work in ParseBloc(), the result was placed in // BlockText for us to return // return macroArgs.BlockText; }
///////////////////////////////////////////////////////////////////////////// // // (#forloop `startValue', `endValue', `increment' [, extraArgs ...] ) // ///////////////////////////////////////////////////////////////////////////// public override object Evaluate( IMacro macro, IMacroArguments macroArgs ) { // ****** var args = GetMacroArgsAsTuples( macroArgs.Expression ) as NmpTuple<int, int, int, object[]>; int start = args.Item1; int end = args.Item2; int increment = args.Item3; object [] extraArgs = args.Item4; // ****** return new ForloopHandler(mp).Forloop_TextChunk( start, end, increment, macroArgs.BlockText, extraArgs ); }
///////////////////////////////////////////////////////////////////////////// // // (#defarray `macroName' ) // ///////////////////////////////////////////////////////////////////////////// public override object Evaluate( IMacro macro, IMacroArguments macroArgs ) { // ****** var args = GetMacroArgsAsTuples( macroArgs.Expression ) as NmpTuple<string>; string macroName = args.Item1; // ****** if( string.IsNullOrEmpty(macroName) ) { ThreadContext.MacroError( "attempt to define or pushdef a macro without a name" ); } // ****** // // parse text into NmpArray instance // try { var newArray = NmpArray.BuildArray( macroArgs.BlockText ); // ****** // // create the new macro // IMacro newMacro = mp.AddObjectMacro( macroName, newArray ); } catch ( NmpJSONException ex ) { ThreadContext.MacroError( ex.Message ); } // ****** return string.Empty; }
class TextMacroHandler : BuiltinMacroHandler { // IMacroHandler { //protected IMacroProcessor mp; ///////////////////////////////////////////////////////////////////////////// //public string Name { get { return "text macro handler"; } } //public bool HandlesBlocks { get { return false; } } /////////////////////////////////////////////////////////////////////////////// // //public virtual string ParseBlock( Expression exp, IInput input ) //{ // return string.Empty; //} // // /////////////////////////////////////////////////////////////////////////////// // //public IMacro Create( string name, IMacro macro, object instance, bool clone ) //{ // return mp.CreateTextMacro( name, macro.MacroText, macro.ArgumentNames ); //} // ///////////////////////////////////////////////////////////////////////////// //public object Evaluate( IMacro macro, IMacroArguments macroArgs ) //{ // // ****** // string macroName = macro.Name; // // // ****** // // // // does not return unless validation succeeds // // // int skipCount; // ArgumentList argList = MacroHandlerBase.ValidateExpression( macro, macroArgs, out skipCount ); // object [] objArgs = ArgumentsEvaluator.Evaluate( mp, argList ); // // // ****** // if( macroArgs.Options.Data ) { // // // // data - no substitution and no scanning // // // return MacroHandlerBase.EvaluateRemainingExpressions( mp, macro.MacroText, (MacroExpression) macroArgs.Expression, skipCount ); // } // else { // // // // not data - do it the normal way // // // using( var macroRunner = new TextMacroRunner(mp, macro, macroArgs, objArgs) ) { // string result = macroRunner.Run(); // return MacroHandlerBase.EvaluateRemainingExpressions( mp, result, (MacroExpression) macroArgs.Expression, skipCount ); // } // } //} // public override object Evaluate( IMacro macro, IMacroArguments macroArgs ) { // ****** string macroName = macro.Name; // ****** // // does not return unless validation succeeds // int skipCount; ArgumentList argList = GetArguments( macro.Name, macroArgs.Expression, out skipCount ); object [] objArgs = ArgumentsEvaluator.Evaluate( mp, argList ); // ****** if( macroArgs.Options.Data ) { // // data - no substitution and no scanning // return EvaluateRemainingExpressions( mp, macro.MacroText, (MacroExpression) macroArgs.Expression, skipCount ); } else { // // not data - do it the normal way // using( var macroRunner = new TextMacroRunner(mp, macro, macroArgs, objArgs) ) { string result = macroRunner.Run(); return EvaluateRemainingExpressions( mp, result, (MacroExpression) macroArgs.Expression, skipCount ); } } }
///////////////////////////////////////////////////////////////////////////// public TextMacroRunner( IMacroProcessor mp, IMacro macro, IMacroArguments args, object [] objArgsIn ) { // ****** this.mp = mp; this.macro = macro; this.options = args.Options; this.objectArgs = objArgsIn; this.arguments = new NmpStringList( objArgsIn, false ); // ****** expressionNodeTypeName = args.Expression.NodeTypeName; // ****** ///flags = new BitArray( (int)MDIndexes.COUNT_DIRECTIVES, false ); // ****** argsMacro = mp.AddObjectMacro( mp.GenerateArgListName( macro.Name ), this.arguments ); specials = mp.AddObjectMacro( mp.GenerateLocalName( macro.Name + "_specials" ), args.SpecialArgs ); localArrayMacro = mp.AddObjectMacro( mp.GenerateLocalName( macro.Name ), localArray = new NmpArray() ); objArgsMacro = mp.AddObjectMacro( mp.GenerateArgListName( macro.Name + "Objects" ), objArgsIn ); }
///////////////////////////////////////////////////////////////////////////// public override object Evaluate( IMacro macro, IMacroArguments argsIn ) { const string POWERSHELL = "psscript"; const string MARKDOWN = "markdown"; // ****** //var args = GetMacroArgsAsTuples( macroArgs.Expression ) as NmpTuple<string, string, string>; //string blockOperation = args.Item1; //string name = args.Item2; //int skipCount; //ArgumentList argList = GetArguments( macro.Name, argsIn.Expression, out skipCount ); //var macroArgs = new MacroArgs( mp, Name, ExpectedArgTypes, DefaultArgs ); //var result = macroArgs.AsTuples( argList ); ////var args = result as NmpTuple<string, string, string>; //var args = result as NmpTuple<string, object[]>; //string blockOperation = args.Item1; //string name = args.Item1; //2 [ 0 ].ToString(); // ****** var args = GetMacroArgsAsTuples( argsIn.Expression ) as NmpTuple<string, string []>; string blockOperation = args.Item1; string [] optArgs = args.Item2; // ****** IMacro target = null; if( POWERSHELL == blockOperation ) { Powershell( argsIn.BlockText, optArgs ); } else if( MARKDOWN == blockOperation ) { return Markdown( argsIn.BlockText, optArgs ); } else if( mp.FindMacro( blockOperation, out target ) ) { return MacroCall( target, argsIn ); } else { ThreadContext.MacroError( "invalid block operation, or there is no macro by the name of: \"{0}\"", blockOperation ); } // ****** return string.Empty; }
///////////////////////////////////////////////////////////////////////////// // // where blockOperation is the name of a macro that we are to call, // BlockText is the text between (#block `macroName' ..) ... (#endblock) // that is to be passed as the first argument; any additional arguments // to (#block ...) are passed next // private object MacroCall( IMacro macro, IMacroArguments macroArgs ) { // ****** //var args = GetMacroArgsAsTuples( macroArgs.Expression, new Type [] { typeof( string ), typeof( object [] ) } ) as NmpTuple<string, object []>; var args = GetMacroArgsAsTuples( macroArgs.Expression ) as NmpTuple<string, string []>; string macroName = args.Item1; object [] argArray = args.Item2; // ****** object [] argsToMacro = new object [ 1 + argArray.Length ]; argsToMacro[ 0 ] = macroArgs.BlockText; Array.Copy( argArray, 0, argsToMacro, 1, argArray.Length ); // ****** // // macro( blockText [, additiona args ... ] ) // //MacroExpression expression = ETB.CreateMacroCallExpression( macro, argsToMacro ); return mp.InvokeMacro( macro, argsToMacro, true ); }