コード例 #1
0
        internal IList <RuleMatch> GetMatches(CompiledRule rule)
        {
            var matches = new List <RuleMatch>();

            if (!IsEmpty)
            {
                foreach (var node in GetLeaves())
                {
                    var refs = node.FindRefs();
                    var path = node.GetPath()
                               .Skip(1) // always a zero in the first node
                               .Select(n => n.Location).ToList();
                    matches.Add(new RuleMatch {
                        rule = rule,
                        path = path,
                        refs = refs
                    });
                }
                if (matches.Count == 0)
                {
                    throw Error.Assert("no paths");
                }
                //if (matches.Count >= 2 &&  paths.Any(p => p.Count != paths[0].Count)) throw Error.Assert("unequal paths");
            }
            return(matches);
        }
コード例 #2
0
ファイル: RuleBuilder.cs プロジェクト: david-pfx/Puzzlang
        //==========================================================================
        //
        // compile a rule
        internal CompiledRule CompileRule(AtomicRule rule)
        {
            _parser.DebugLog("{0}", rule);
            if (rule.Directions.Count != 1)
            {
                throw Error.Assert("direction count");
            }
            for (int i = 0; i < rule.Patterns.Count; i++)
            {
                // TODO: check singleton rules here
            }
            var crule = new CompiledRule {
                RuleId        = rule.RuleId,
                RuleDirection = rule.Directions.First(),
                PatternCode   = CompilePattern(rule.Directions.First(), rule.Patterns),
                ActionCode    = (rule.HasAction)
          ? CompileActions(rule.Directions.First(), rule.Patterns, rule.Actions)
          : RuleCode.Empty,
                CommandCode = (rule.HasCommand)
          ? CompileCommand(rule.Commands)
          : RuleCode.Empty,
            };

            return(crule);
        }