public override TestResult <bool> Validate(ref string input, bool consume)
        {
            TestResult <bool> result = new TestResult <bool>(false, TestResultType.Failed);
            string            temp   = input;

            if (!_delimiters.Contains(" "))
            {
                temp = temp.Replace(" ", "[space]");
            }
            bool          check = true;
            ParseTreeNode leaf  = new ParseTreeNode();

            if (this.Name == null || this.Name.Length == 0)
            {
                leaf.Name = "__SYSTEM_GRAMMAR_CMPGE";
            }
            else
            {
                leaf.Name = this.Name;
            }
            foreach (GrammarElement g in Terminals)
            {
                temp = temp.Trim(_delimiters.ToArray()).Replace("[space]", " ");
                TestResult <bool> terminalcheck = g.Validate(ref temp, true);
                check = check && terminalcheck.Result;
                if (terminalcheck.Result == false)
                {
                    break;
                }
                if (g.Name != null)
                {
                    if (g.Name.Equals("SemiColon"))
                    {
                        Console.Write("");
                    }
                }
                if (terminalcheck.Data.ContainsKey("$PARSETREE.NODE$") && !g.IsInterim)
                {
                    leaf.Children.Add((ParseTreeNode)terminalcheck.Data["$PARSETREE.NODE$"]);
                }
                if (!_delimiters.Contains(" "))
                {
                    temp = temp.Replace(" ", "[space]");
                }
            }
            if (check)
            {
                if (consume)
                {
                    input = temp.Trim(_delimiters.ToArray()).Replace("[space]", " ");
                }
                result.Result = true;
                result.Type   = TestResultType.Complete;
                GrammarPath gpa = new GrammarPath();
                gpa.Put(this.Name);
                result.Data.Add("PATH", gpa);
                result.Data.Add("$PARSETREE.NODE$", leaf);
            }
            return(result);
        }
Exemple #2
0
        public override TestResult <bool> Validate(ref string input, bool consume)
        {
            string temp = input;

            if (_type == VariableLengthGrammarElementType.Star && input.Length == 0)
            {
                TestResult <bool> res = new TestResult <bool>(true, TestResultType.Complete);
                GrammarPath       gp  = new GrammarPath();
                gp.Put(this.Name);
                res.Data.Add("PATH", gp);
                ParseTreeNode leaf = new ParseTreeNode();
                if (this.Name == null || this.Name.Length == 0)
                {
                    leaf.Name = "__SYSTEM_GRAMMAR_VARGE";
                }
                else
                {
                    leaf.Name = this.Name;
                }
                leaf.Value = "";
                res.Data.Add("$PARSETREE.NODE$", leaf);
                return(res);
            }
            TestResult <bool> result = Sets.SatisfiedBy(ref input, consume, true);

            if (result.Result == false && _type == VariableLengthGrammarElementType.Star)
            {
                result.Result = true;
                result.Type   = TestResultType.Failed;
                GrammarPath gp = new GrammarPath();
                gp.Put(this.Name);
                result.Data.Add("PATH", gp);
                return(result);
            }
            if (temp.Length == 0)
            {
                if (input.Length != 0)
                {
                    result.Type   = TestResultType.Complete;
                    result.Result = true;
                    result.Data.Clear();
                }
                else if (_type == VariableLengthGrammarElementType.Plus)
                {
                    result.Type   = TestResultType.Failed;
                    result.Result = false;
                    result.Data.Clear();
                }
            }
            else if (temp.Length == input.Length)
            {
                result.Type   = TestResultType.Failed;
                result.Result = false;
                result.Data.Clear();
            }
            else
            {
                result.Type   = TestResultType.Partial;
                result.Result = true;
                result.Data.Clear();
            }
            if (!consume && result.Result)
            {
                input = temp;
            }
            if (result.Result)
            {
                GrammarPath gp = new GrammarPath();
                gp.Put(this.Name);
                result.Data.Add("PATH", gp);
                if (input.Length != 0)
                {
                    int indx = temp.Length - input.Length;
                    temp = temp.Remove(indx).Trim();
                }
                ParseTreeNode leaf = new ParseTreeNode();
                if (this.Name == null || this.Name.Length == 0)
                {
                    leaf.Name = "$SYSTEM.STRING$";
                }
                else
                {
                    leaf.Name = this.Name;
                }
                leaf.Value = temp;
                result.Data.Add("$PARSETREE.NODE$", leaf);
                return(result);
            }
            return(new TestResult <bool>(false, TestResultType.Failed));
        }
        public override TestResult <bool> Validate(ref string input, bool consume)
        {
            List <TestResult <bool> > matchindices = new List <TestResult <bool> >();
            string        temp = input;
            ParseTreeNode root = new ParseTreeNode();

            if (this.Name == null || this.Name.Length == 0)
            {
                root.Name = "__SYSTEM_GRAMMAR_MPGE";
            }
            else
            {
                root.Name = this.Name;
            }
            for (int i = 0; i < _te.Count; i++)
            {
                temp = input;
                TestResult <bool> result = _te[i].Validate(ref temp, true);
                if (result.Result)
                {
                    if (result.Data.ContainsKey("$PARSETREE.NODE$"))
                    {
                        root.Children.Add((ParseTreeNode)result.Data["$PARSETREE.NODE$"]);
                    }
                    if (!result.Data.ContainsKey("$INTERNALRESULT$"))
                    {
                        result.Data.Add("$INTERNALRESULT$", temp);
                    }
                    if (!_te[i].IsInterim)
                    {
                        matchindices.Add(result);
                    }
                }
            }
            if (matchindices.Count == 1)
            {
                TestResult <bool> result = new TestResult <bool>(true, TestResultType.Complete);
                GrammarPath       gpa    = null;
                if (matchindices[0].Data.ContainsKey("PATH"))
                {
                    gpa = (GrammarPath)matchindices[0].Data["PATH"];
                }
                else
                {
                    gpa = new GrammarPath();
                }
                gpa.Put(this.Name);
                result.Data.Add("PATH", gpa);
                if (matchindices[0].Data.ContainsKey("$INTERNALRESULT$"))
                {
                    input = (string)matchindices[0].Data["$INTERNALRESULT$"];
                }
                else
                {
                    input = temp;
                }
                result.Data.Add("$PARSETREE.NODE$", root);
                return(result);
            }
            else if (matchindices.Count > 1)
            {
                TestResult <bool> result = new TestResult <bool>(true, TestResultType.Multiple);
                result.Data.Add("TerminalIndices", matchindices);
                if (_precedence == MultiParsePrecedenceType.Normal)
                {
                    List <string> sslist = new List <string>();
                    foreach (TestResult <bool> t in matchindices)
                    {
                        if (t.Data.ContainsKey("$INTERNALRESULT$"))
                        {
                            sslist.Add((string)t.Data["$INTERNALRESULT$"]);
                        }
                    }
                    int min    = int.MaxValue;
                    int idxmin = -1;
                    for (int i = 0; i < sslist.Count; i++)
                    {
                        if (sslist[i].Length < min)
                        {
                            idxmin = i;
                            min    = sslist[i].Length;
                        }
                    }
                    if (idxmin != -1)
                    {
                        input = sslist[idxmin];
                    }
                    else
                    {
                        input = temp;
                    }
                }
                else if (_precedence == MultiParsePrecedenceType.Ascending || _precedence == MultiParsePrecedenceType.Descending)
                {
                    int idx = (_precedence == MultiParsePrecedenceType.Ascending) ? 0 : matchindices.Count - 1;
                    result = new TestResult <bool>(true, TestResultType.Complete);
                    GrammarPath gpa = null;
                    if (matchindices[idx].Data.ContainsKey("PATH"))
                    {
                        gpa = (GrammarPath)matchindices[idx].Data["PATH"];
                    }
                    else
                    {
                        gpa = new GrammarPath();
                    }
                    gpa.Put(this.Name);
                    result.Data.Add("PATH", gpa);
                    if (matchindices[idx].Data.ContainsKey("$INTERNALRESULT$"))
                    {
                        input = (string)matchindices[idx].Data["$INTERNALRESULT$"];
                    }
                    else
                    {
                        input = temp;
                    }
                    TreeNode <string> satisfied = root.Children[idx];
                    root.Children.Clear();
                    root.Children.Add(satisfied);
                    result.Data.Add("$PARSETREE.NODE$", root);
                    return(result);
                }
                result.Data.Add("$PARSETREE.NODE$", root);
                return(result);
            }
            return(new TestResult <bool>(false, TestResultType.Failed));
        }