Example #1
0
 public void For(ISelfReferencingRule selector, string alias = null)
 {
     try
     {
         CollectSingleSelectorVisitor visitor = rule.Accept(new CollectSingleSelectorVisitor());
         For(visitor.SelectorPath, alias);
     }
     catch (InvalidOperationException ex)
     {
         throw new InvalidOperationException("A self referencing rule (It) can only be used when a single field is used in the When clause.", ex);
     }
 }
Example #2
0
            public Rule ReverseRule(Rule rule)
            {
                rule.Accept(this);

                // we have the values.. now we can reverse it
                string content   = _textContent.ToString();
                string important = "";

                var value = rule.Value as Value;

                if (value != null)
                {
                    important = value.Important;
                }

                bool valueChanged = false;

                if (_nodeContent.Count > 1)
                {
                    if (_nodeContent.Count == 4)
                    {
                        Node tmp = _nodeContent[1];
                        _nodeContent[1] = _nodeContent[3];
                        _nodeContent[3] = tmp;
                        return(new Rule(rule.Name, new Expression(_nodeContent)).ReducedFrom <Rule>(rule));
                    }
                }
                else
                {
                    if (content == "left")
                    {
                        content      = ("right " + important).TrimEnd();
                        valueChanged = true;
                    }
                    else if (content == "right")
                    {
                        content      = ("left " + important).TrimEnd();
                        valueChanged = true;
                    }
                    else
                    {
                        string[] items = content.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        if (items.Length == 4)
                        {
                            string temp = items[1];
                            items[1]     = items[3];
                            items[3]     = temp;
                            content      = String.Join(" ", items);
                            valueChanged = true;
                        }
                    }

                    if (valueChanged)
                    {
                        return(new Rule(rule.Name, new TextNode(content)).ReducedFrom <Rule>(rule));
                    }
                }

                return(rule);
            }
Example #3
0
 public IFieldValidatorConfig Then(ISelfReferencingRule @ref, CapturedConstraint constraint)
 {
     try
     {
         CollectSingleSelectorVisitor visitor = rule.Accept(new CollectSingleSelectorVisitor());
         return(Then(visitor.SelectorPath, visitor.Alias, constraint));
     }
     catch (InvalidOperationException ex)
     {
         throw new InvalidOperationException("A self referencing rule (It) can only be used when a single field is used in the When clause.", ex);
     }
 }
Example #4
0
    static public void Main(String[] args)
    {
        Dictionary <String, String> arguments = new Dictionary <String, String>();
        String error = "";
        bool   ok    = args.Length > 0;

        if (ok)
        {
            arguments["Trace"] = "Off";
            arguments["Rule"]  = "did";

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].Equals("-trace"))
                {
                    arguments["Trace"] = "On";
                }
                else if (args[i].Equals("-visitor"))
                {
                    arguments["Visitor"] = args[++i];
                }
                else if (args[i].Equals("-file"))
                {
                    arguments["File"] = args[++i];
                }
                else if (args[i].Equals("-string"))
                {
                    arguments["String"] = args[++i];
                }
                else if (args[i].Equals("-rule"))
                {
                    arguments["Rule"] = args[++i];
                }
                else
                {
                    error = "unknown argument: " + args[i];
                    ok    = false;
                }
            }
        }

        if (ok)
        {
            if (!arguments.ContainsKey("File") &&
                !arguments.ContainsKey("String"))
            {
                error = "insufficient arguments: -file or -string required";
                ok    = false;
            }
        }

        if (!ok)
        {
            System.Console.WriteLine("error: " + error);
            System.Console.WriteLine("usage: Parser [-rule rulename] [-trace] <-file file | -string string> [-visitor visitor]");
        }
        else
        {
            try
            {
                Rule rule = null;

                if (arguments.ContainsKey("File"))
                {
                    rule =
                        Parse(
                            arguments["Rule"],
                            new FileStream(arguments["File"], FileMode.Open),
                            arguments["Trace"].Equals("On"));
                }
                else if (arguments.ContainsKey("String"))
                {
                    rule =
                        Parse(
                            arguments["Rule"],
                            arguments["String"],
                            arguments["Trace"].Equals("On"));
                }

                if (arguments.ContainsKey("Visitor"))
                {
                    Type type = Type.GetType(arguments["Visitor"]);

                    if (type == null)
                    {
                        System.Console.WriteLine(
                            "visitor error: class not found - " +
                            arguments["Visitor"]);
                    }
                    else
                    {
                        Visitor visitor = (Visitor)System.Activator.CreateInstance(type);

                        rule.Accept(visitor);
                    }
                }
            }
            catch (ArgumentException e)
            {
                System.Console.WriteLine("argument error: " + e.Message);
            }
            catch (IOException e)
            {
                System.Console.WriteLine("io error: " + e.Message);
            }
            catch (ParserException e)
            {
                System.Console.WriteLine("parser error: " + e.Message);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("error: " + e.Message);
            }
        }
    }
Example #5
0
 public override TResult Accept<TArg, TResult>(IProductionVisitor<TArg, TResult> visitor, TArg argument)
 {
     return Rule.Accept(visitor, argument);
 }