private static MacroExpr GetMacroExpr(string originalExpression, string processedExpression)
    {
        var macroExpr = new MacroExpr();

        try
        {
            // Handle security
            macroExpr.Expression     = MacroSecurityProcessor.RemoveMacroSecurityParams(originalExpression, out macroExpr.SignedBy);
            macroExpr.SignatureValid = MacroSecurityProcessor.CheckMacroIntegrity(originalExpression, macroExpr.SignedBy);

            // Parse rule text
            macroExpr.RuleText   = MacroRuleTree.GetRuleText(macroExpr.Expression, true, true);
            macroExpr.Expression = MacroRuleTree.GetRuleCondition(macroExpr.Expression, true);

            // Macro expression does not support anonymous signature, remove the flag
            if (processedExpression.EndsWith("@", StringComparison.Ordinal))
            {
                processedExpression = processedExpression.Substring(0, processedExpression.Length - 1);
            }

            // Check syntax
            MacroExpression.ParseExpression(processedExpression);
        }
        catch (Exception ex)
        {
            macroExpr.Error        = true;
            macroExpr.ErrorMessage = ex.Message + "\r\n\r\n" + ex.StackTrace;
        }
        return(macroExpr);
    }
Beispiel #2
0
 internal static Macro Macro(MacroExpression expression)
 {
     return(new Macro()
     {
         Parameters = expression.Parameters.Select(AmbiguousParameter).ToArray(),
         Body = Serialize(expression.Body),
     });
 }
    private void RefreshText()
    {
        string hdnValueTrim = hdnValue.Value.Trim();

        if (hdnValueTrim.StartsWithCSafe("rule(", true))
        {
            // Empty rule designer condition is not considered as rule conditions
            if (hdnValueTrim.EqualsCSafe("Rule(\"\", \"<rules></rules>\")", true))
            {
                hdnValue.Value = "";
            }
            else
            {
                try
                {
                    string ruleText = MacroRuleTree.GetRuleText(hdnValueTrim, true, true, TimeZoneTransformation);

                    // Display rule text
                    ltlMacro.Text    = ruleText;
                    txtMacro.Visible = false;
                    pnlRule.Visible  = true;

                    pnlUpdate.Update();

                    return;
                }
                catch
                {
                    // If failed to parse the rule, extract the condition
                    MacroExpression xml = MacroExpression.ExtractParameter(hdnValueTrim, "rule", 0);
                    if (xml != null)
                    {
                        string user;
                        hdnValue.Value = MacroSecurityProcessor.RemoveMacroSecurityParams(ValidationHelper.GetString(xml.Value, ""), out user);
                    }
                }
            }
        }


        if (string.IsNullOrEmpty(hdnValue.Value) && !string.IsNullOrEmpty(DefaultConditionText))
        {
            ltlMacro.Text    = DefaultConditionText;
            txtMacro.Text    = "";
            txtMacro.Visible = false;
            pnlRule.Visible  = true;
        }
        else
        {
            txtMacro.Text    = hdnValue.Value;
            hdnValue.Value   = null;
            txtMacro.Visible = true;
            pnlRule.Visible  = false;
        }

        pnlUpdate.Update();
    }
Beispiel #4
0
		/////////////////////////////////////////////////////////////////////////////

		public static object EvaluateRemainingExpressions( IMacroProcessor mp, object rootObj, MacroExpression exp, int skipCount )
		{
			// ******
			var exp2 = exp.Clone();
			exp2.Items.RemoveRange( 0, skipCount );

			// ******
			var evaluator = new ExpressionEvaluator( mp );
			return evaluator.Evaluate( rootObj, exp2 );
		}
Beispiel #5
0
		/////////////////////////////////////////////////////////////////////////////

		protected object ExecuteMacro( string name, MacroExpression exp )
		{
			IMacro macro;
			if( !macroProcessor.FindMacro( name, out macro ) ) {
				ThreadContext.MacroError( "\"{0}\" is not the name of a macro", name );
			}

			// ******
			//return macroProcessor.ProcessMacro( macro, new MacroArguments(macro, null, exp), false );
			return macroProcessor.InvokeMacro( macro, exp, false );
		}
Beispiel #6
0
		/////////////////////////////////////////////////////////////////////////////

		protected object ExecuteMacro( string name, MacroExpression exp )
		{
			IMacro macro;
			if( ! macroProcessor.FindMacro(name, out macro) ) {
				ThreadContext.MacroError( "\"{0}\" is not the name of a macro", name );
			}

			// ******
			//using( var input = macroProcessor.ScanHelper.NewEmptyIInput ) {
			//	return macroProcessor.ProcessMacro( macro, new MacroArguments(macro, input, exp), false );
			//}
			return macroProcessor.InvokeMacro( macro, exp, false );
		}
    /// <summary>
    /// Parses the rule tree from Rule expression.
    /// </summary>
    public void ParseFromExpression(string expression)
    {
        MacroExpression xml = MacroExpression.ExtractParameter(expression, "rule", 1);

        if (xml?.Type == ExpressionType.Value)
        {
            // Load from the XML
            LoadFromXML(xml.Value.ToString());
            return;
        }

        // If something went wrong, assign null to the state variable
        ViewState["RuleTree"] = null;
    }
Beispiel #8
0
    private void RefreshText()
    {
        string hdnValueTrim = hdnValue.Value.Trim();

        if (hdnValueTrim.StartsWithCSafe("rule(", true))
        {
            // Empty rule designer condition is not considered as rule conditions
            if (hdnValueTrim.EqualsCSafe("Rule(\"\", \"<rules></rules>\")", true))
            {
                hdnValue.Value = "";
            }
            else
            {
                bool   failToParseXml = false;
                string ruleText       = MacroRuleTree.GetRuleText(hdnValueTrim, true, out failToParseXml);
                if (failToParseXml)
                {
                    // If failed to parse the rule, extract the condition
                    MacroExpression xml = MacroExpression.ExtractParameter(hdnValueTrim, "rule", 0);
                    if (xml != null)
                    {
                        string user = null;
                        hdnValue.Value = MacroResolver.RemoveMacroSecurityParams(ValidationHelper.GetString(xml.Value, ""), out user);
                    }
                }
                else
                {
                    // Display rule text
                    ltlMacro.Text    = ruleText;
                    txtMacro.Visible = false;
                    pnlRule.Visible  = true;
                    return;
                }
            }
        }

        txtMacro.Text    = hdnValue.Value;
        hdnValue.Value   = null;
        txtMacro.Visible = true;
        pnlRule.Visible  = false;

        pnlUpdate.Update();
    }
    /// <summary>
    /// Extracts the condition from Rule method.
    /// </summary>
    public string ConditionFromExpression(string expression)
    {
        MacroExpression xml = null;

        try
        {
            xml = MacroExpression.ExtractParameter(expression, "rule", 1);
        }
        catch
        {
        }

        MacroIdentityOption identityOption;

        if (xml == null)
        {
            return(MacroSecurityProcessor.RemoveMacroSecurityParams(expression, out identityOption));
        }

        // Returns first parameter of the expression
        return(MacroSecurityProcessor.RemoveMacroSecurityParams(ValidationHelper.GetString(xml.Value, ""), out identityOption));
    }
    /// <summary>
    /// Extracs the condition from Rule method.
    /// </summary>
    public string ConditionFromExpression(string expression)
    {
        MacroExpression xml = null;

        try
        {
            xml = MacroExpression.ExtractParameter(expression, "rule", 1);
        }
        catch { }


        string user = null;

        if (xml == null)
        {
            return(MacroResolver.RemoveMacroSecurityParams(expression, out user));
        }
        else
        {
            // Returns first parameter of the expression
            return(MacroResolver.RemoveMacroSecurityParams(ValidationHelper.GetString(xml.Value, ""), out user));
        }
    }
Beispiel #11
0
		/////////////////////////////////////////////////////////////////////////////

		private bool HandleFreshToken( bool isRootNode, string token, bool nextIsWhiteSpace, MacroExpression exList, IInput input )
		{
			//
			// where extList is empty and isAltInvocationExpression is
			// true then we have (#someMacro ...) and we have to treat 
			// it as a method invocation IF THE NEXT CHAR IS WHITE-SPACE,
			// but only if it's white space - dont do it for (#someMacro.thing ), 
			// or (#someMacro() ) which is weird
			//
			if( 0 == exList.Count && isAltInvocationExpression && nextIsWhiteSpace ) {
				exList.Add( MethodCall(isRootNode, token, true, input) );
				return true;	// done
			}
			else {
				exList.Add( MemberAccess(isRootNode, token, input) );
				return false;
			}
		}
Beispiel #12
0
		/////////////////////////////////////////////////////////////////////////////

		public MacroExpression ParseExpression( IInput input )
		{
			// ******
			var exList = new MacroExpression( new NmpStringList() );

			// ******
			//
			// first token is the name of the macro
			//
			string	token = rootName;
			bool		haveFreshToken = true;

			// ******
			while( true ) {
				bool isRootNode = 0 == exList.Count;
				bool allDone = false;

				char ch = input.Peek();

				if( haveFreshToken ) {
					allDone = HandleFreshToken( isRootNode, token, char.IsWhiteSpace(ch), exList, input );
					haveFreshToken = false;
				}
				
				if( SC.DOT == ch ) {
					//
					// '.'
					//
					input.Skip( 1 );
					token = GetToken( input );
				
					if( string.IsNullOrEmpty(token) ) {
						//
						// does not return
						//
						UnexpectedCharacter( input.Peek(), "expecting identifier (member name)" );
					}
				
					// ******
					haveFreshToken = true;
				}

// jpm 11 April 13; change back to NOT check for (# because its more correct that what
// we ended up with - that is, without the check its possible for the user to create a
// macro (across multiple lines using '-' at end of line and comments ;;) where we
// get "something()(#defmacro..)" glued together as the result of a macro atempting to
// call a method - not what was intended
//
				//else if( SC.OPEN_PAREN == ch && SC.HASH != input.Peek(1) ) {
				else if( SC.OPEN_PAREN == ch ) {
					//
					// '(' NOT "(#"
					//
					input.Skip( 1 );
					exList.Add( MethodCall(isRootNode, token, haveFreshToken, input) );
					haveFreshToken = false;
				}

				else if( SC.OPEN_BRACKET == ch ) {
					//
					// '['
					//
					input.Skip( 1 );
					exList.Add( IndexMember(isRootNode, token, haveFreshToken, input) );
					haveFreshToken = false;                                               
				}

				else if( SC.ATCHAR == ch && SC.OPEN_BRACKET == input.Peek(1) ) {
					input.Skip( 2 );
					ParseMacroOptions( input, exList.MacroInstructions );
				}
				else {
					//
					// end of expression
					//

					/*	

						should put whatever we do here just before the end of the
						method - get it out of here for cleanthlyness sake


						if is altInvocationExpression and we did not detect it as a method in HandleFreshToken()
							
							we've hit ')' or some char we don't know what to do with

						we could gather up "... )" and add to MacroOptions

							or error

							or just eat it
					*/

					//if( isAltInvocationExpression && ! allDone ) {
					//	//NmpStringList strArgs = argScanner( input, RecognizedCharType.CloseParenChar );
					//	NmpStringList strArgs = scanner.ArgScanner( input, RecognizedCharType.CloseParenChar );
					//}

					if( isAltInvocationExpression ) {
						if( ! allDone ) {
							NmpStringList strArgs = scanner.ArgScanner( input, RecognizedCharType.CloseParenChar );
						}

						if( SC.ATCHAR == input.Peek() && SC.OPEN_BRACKET == input.Peek(1) ) {
							input.Skip( 2 );
							ParseMacroOptions( input, exList.MacroInstructions );
						}

					}

					break;
				}

				// ******
				//
				// since there is no member/token name for the result of an operation
				// we need to create one in case of cascading operations
				//
				if( !haveFreshToken ) {
					token = "result of " + token;
				}
			}

			// ******
			//return 1 == exList.Count ? exList[0] : exList;
			return exList;
		}
Beispiel #13
0
		/////////////////////////////////////////////////////////////////////////////

		public static object _SkipFirstAndEvaluate( IMacroProcessor mp, object rootObj, MacroExpression cexp )
		{
			// ******
			//if( ExpressionType.Compound != exp.NodeType ) {
			//	//
			//	// only the one expression
			//	//
			//	return rootObj;
			//}

			// ******
			//CompoundExpression cexp = exp.Cast<CompoundExpression>();
			ExpressionList expList = cexp.Items;
			
			if( expList.Count < 2 ) {
				//
				// only the one (or zero) expressions in the list
				//
				return rootObj;
			}

			// ******
			Expression first = expList[ 0 ];
			expList.RemoveAt( 0 );

			// ******
			var evaluator = new ExpressionEvaluator( mp );
			object result = evaluator.Evaluate( rootObj, cexp );

			// ******
			expList.Insert( 0, first );

			// ******
			return result;
		}
Beispiel #14
0
		/////////////////////////////////////////////////////////////////////////////

		public MacroArguments(	IMacro macro,
														IInput input, 
														MacroExpression macroExp, 
														
														NmpStringList specialArgs = null,
														string blockText = ""
													)
		{
			// ******
			if( null == input ) {
				throw new ArgumentNullException( "input" );
			}
			Input = input;

			// ******
			Expression = macroExp;

			// ******
			Options = new MacroOptions( macro.MacroProcessor, macro.Flags, macroExp.MacroInstructions );
			
			// ******
			SpecialArgs = null == specialArgs ? new NmpStringList() : specialArgs;
			BlockText = blockText;
		}
Beispiel #15
0
		/////////////////////////////////////////////////////////////////////////////

		public static MacroExpression CreateMacroCallExpression( string name, object [] args )
		{
			// ******
			if( string.IsNullOrEmpty(name) ) {
				throw new ArgumentNullException( "name" );
			}

			// ******
			var argList = new ArgumentList( null == args ? new object [ 0 ] : args );
			var list = new MacroExpression( new NmpStringList() );

			// ******
			list.Add( new MethodCallExpression( name, argList ) );

			// ******
			return list;
		}
Beispiel #16
0
		///////////////////////////////////////////////////////////////////////////////
		//
		//public static MacroExpression CreateMacroCallExpression( string macroName, object [] args )
		//{
		//	//return new MethodCallExpression( macroName, new ArgumentList(args) );
		//
		//	// ******
		//	if( null == args ) {
		//		args = new object[ 0 ];
		//	}
		//
		//	// ******
		//	var list = new MacroExpression( new NmpStringList() );
		//	list.Add( new MethodCallExpression( macroName, new ArgumentList(args) ) );
		//	return list;
		//}
		//

		/////////////////////////////////////////////////////////////////////////////

		public static MacroExpression CreateNullExpression()
		{
			//return new MethodCallExpression( macroName, new ArgumentList(args) );

			var list = new MacroExpression( new NmpStringList() );
			list.Add( new NullExpression() );
			return list;
		}
Beispiel #17
0
		/////////////////////////////////////////////////////////////////////////////

		public static MacroExpression CreateMacroCallExpression( IMacro macro, object [] args )
		{
			// ******
			if( null == macro ) {
				throw new ArgumentNullException( "macro" );
			}

			// ******
			var argList = new ArgumentList( null == args ? new object [0] : args );
			var list = new MacroExpression( new NmpStringList() );

			// ******
			if( MacroType.Builtin == macro.MacroType || MacroType.Text == macro.MacroType ) {
				list.Add( new MethodCallExpression( macro.Name, argList) );
			}
			else {
				//
				// its an object, if it's not a MethodInvoker instance or a delegate
				// it will error when the invocation fails
				//
				list.Add( new UnnamedMemberAccessExpression(macro.Name) );
				list.Add( new UnnamedMethodCallExpression(argList) );
			}

			// ******
			return list;
		}