///////////////////////////////////////////////////////////////////////////// /// <summary> /// Evaluate the text in the IEvaluationContext, newOutput is currently /// ignored - each evaluation gets a new Output object /// </summary> /// <param name="evx"></param> /// <param name="newOutput"></param> /// <returns></returns> public string Evaluate( IEvaluationContext evx, bool newOutput ) { // ****** // // EvalLock() will throw an exception if this instance of NMP is already // evaluating something, it also calls Restore()/Save() for our thread // data // using( new EvalLock( this ) ) { try { using( var input = gc.GetMasterParseReader( new ParseStringReader( evx.Text, evx.FileName ) ) ) { MasterOutput output = GetMasterOutput( newOutput ); string result = string.Empty; // ****** var mir = new MIR( null, input, "Root" ); using ( Get<InvocationContext>().Init( mir ) ) { SetMacroProcessorOutputInstance( output ); Scanner( input, output ); StringBuilder sb = output.AllText; result = sb.ToString(); SetMacroProcessorOutputInstance( null ); } // ****** return result; } } finally { } } }
///////////////////////////////////////////////////////////////////////////// /// <summary> /// Invokes a macro with the 'args' - currently not implemented /// </summary> /// <param name="macroName"></param> /// <param name="args"></param> /// <param name="newOutput"></param> /// <returns></returns> public string InvokeMacro( string macroName, object [] args, bool newOutput ) { // ****** IMacro macro; if( ! MacroProcessor.FindMacro(macroName, out macro) ) { throw new MacroNotFoundException(macroName); } // ****** MacroExpression expression = ETB.CreateMacroCallExpression( macro, args ); var input = gc.GetParseReader( macroName ); var mir = new MIR( macro, false, null, input, "InvokeMacro direct", 0, 1, 1 ); // ****** // // EvalLock() will throw an exception if this instance of NMP is already // evaluating something, it also calls Restore()/Save() for our thread // data // using( new EvalLock(this) ) { // ****** MasterOutput output = GetMasterOutput( newOutput ); using( new UsingHelper(() => SetMacroProcessorOutputInstance(output), () => SetMacroProcessorOutputInstance(null)) ) { object result = MacroProcessor.InvokeMacro( input, mir, expression, true ); // ****** // // if we were calling Scanner() then we'd just call 'output.AllText' but since we're // calling InvokeMacro() the text we're interested in is what is returned by the macro // NOT what's in the output buffer // // so, we have to call FinalProcessText() ourself with the result of the macro invocation // StringBuilder sb = output.FinalProcessText( new StringBuilder(result.ToString()), true ); string strResult = sb.ToString(); return strResult; } } }
///////////////////////////////////////////////////////////////////////////// public void MultileEvaluate( IEvaluationContext evx ) { // ****** // // EvalLock() will throw an exception if this instance of NMP is already // evaluating something, it also calls Restore()/Save() for our thread // data // using( new EvalLock( this ) ) { try { using( var input = gc.GetMasterParseReader( new ParseStringReader( evx.Text, evx.FileName ) ) ) { if( null == multileEvalOutput ) { multileEvalOutput = new MasterOutput( gc ); } // ****** var mir = new MIR( null, input, "Root" ); using( Get<InvocationContext>().Init( mir ) ) { SetMacroProcessorOutputInstance( multileEvalOutput ); Scanner( input, multileEvalOutput ); SetMacroProcessorOutputInstance( null ); } } } finally { } } }