Exemple #1
0
 public virtual object GetRuleValue(IMatchable.MatchProperties property)
 {
     MatchNode.RuleValue rule = rules.GetOrNull(property);
     return(rule == null ? null : rule.value);
 }
Exemple #2
0
        public MatchEngine(string description)
        {
            // each line is a separate statement/exprent
            string[]          lines = description.Split("\n");
            int               depth = 0;
            Stack <MatchNode> stack = new Stack <MatchNode>();

            foreach (string line in lines)
            {
                List <string> properties = new List <string>(Sharpen.Arrays.AsList(line.Split("\\s+"
                                                                                              )));
                // split on any number of whitespaces
                if ((properties[0].Length == 0))
                {
                    properties.RemoveAtReturningValue(0);
                }
                int node_type = "statement".Equals(properties[0]) ? MatchNode.Matchnode_Statement
                                         : MatchNode.Matchnode_Exprent;
                // create new node
                MatchNode matchNode = new MatchNode(node_type);
                for (int i = 1; i < properties.Count; ++i)
                {
                    string[] values = properties[i].Split(":");
                    IMatchable.MatchProperties property = (node_type == MatchNode.Matchnode_Statement
                                                 ? stat_properties : expr_properties).GetOrNull(values[0]);
                    if (property == null)
                    {
                        // unknown property defined
                        throw new Exception("Unknown matching property");
                    }
                    else
                    {
                        object value;
                        int    parameter = 0;
                        string strValue  = values[1];
                        if (values.Length == 3)
                        {
                            parameter = System.Convert.ToInt32(values[1]);
                            strValue  = values[2];
                        }
                        switch (property.ordinal())
                        {
                        case 0:
                        {
                            value = stat_type.GetOrNullable(strValue);
                            break;
                        }

                        case 2:
                        case 3:
                        {
                            value = int.Parse(strValue);
                            break;
                        }

                        case 4:
                        case 8:
                        case 13:
                        case 14:
                        case 15:
                        case 16:
                        case 17:
                        case 12:
                        case 1:
                        case 7:
                        {
                            value = strValue;
                            break;
                        }

                        case 5:
                        {
                            value = stat_if_type.GetOrNullable(strValue);
                            break;
                        }

                        case 9:
                        {
                            value = expr_func_type.GetOrNullable(strValue);
                            break;
                        }

                        case 10:
                        {
                            value = expr_exit_type.GetOrNullable(strValue);
                            break;
                        }

                        case 11:
                        {
                            value = expr_const_type.GetOrNull(strValue);
                            break;
                        }

                        case 6:
                        {
                            value = expr_type.GetOrNullable(strValue);
                            break;
                        }

                        default:
                        {
                            throw new Exception("Unhandled matching property");
                        }
                        }
                        matchNode.AddRule(property, new MatchNode.RuleValue(parameter, value));
                    }
                }
                if ((stack.Count == 0))
                {
                    // first line, root node
                    stack.Push(matchNode);
                }
                else
                {
                    // return to the correct parent on the stack
                    int new_depth = line.LastIndexOf(' ', depth) + 1;
                    for (int i = new_depth; i <= depth; ++i)
                    {
                        stack.Pop();
                    }
                    // insert new node
                    stack.First().AddChild(matchNode);
                    stack.Push(matchNode);
                    depth = new_depth;
                }
            }
            this.rootNode = stack.Last();
        }
Exemple #3
0
 public virtual void AddRule(IMatchable.MatchProperties property, MatchNode.RuleValue
                             value)
 {
     Sharpen.Collections.Put(rules, property, value);
 }