Beispiel #1
0
        public INode Reduce(IRule rule, INodeContext <INode> page)
        {
            var ruleName = ((Value)page.Get(0)).Text;
            var result   = new ProtoRule("Ref", rule => rule.Ref(ruleName));

            result.RuleName = ruleName;
            return(result);
        }
Beispiel #2
0
 private void DefineReferencedRulesAsDummies(IGrammarBuilder <INode> grammar, ProtoRule rule, HashSet <string> defined)
 {
     foreach (var child in rule.Children)
     {
         if (child.DisplayText == "Ref" && child.RuleName != null && !defined.Contains(child.RuleName))
         {
             defined.Add(child.RuleName);
             grammar.DefineRule(child.RuleName, new MockRule(child.RuleName));
         }
         DefineReferencedRulesAsDummies(grammar, child, defined);
     }
 }
Beispiel #3
0
        public INode Reduce(IRule rule, INodeContext <INode> page)
        {
            // sequence with a single item => no sequence needed
            if (page.Count == 1)
            {
                return(page.Get(0));
            }

            var result = new ProtoRule(nameof(Core.Rules.Sequence), rule => new Core.Rules.Sequence(rule.RuleName, rule.All()));

            result.AddChildren(page.GetAll().Of <ProtoRule>());

            return(result);
        }
Beispiel #4
0
        public INode Reduce(IRule rule, INodeContext <INode> page)
        {
            // choice with a single item => no choice needed
            if (page.Count == 1)
            {
                return(page.Get(0));
            }

            var result = new ProtoRule(nameof(Core.Rules.Choice), rule => new Core.Rules.Choice(rule.RuleName, rule.All()));

            // filter out the "|" values
            result.AddChildren(page.GetAll().Of <ProtoRule>());

            return(result);
        }
Beispiel #5
0
        private string?RefNamePropagation(ProtoRule rule, int level = 0)
        {
            foreach (var child in rule.Children)
            {
                var name = RefNamePropagation(child, level + 1);
                if (name != null)
                {
                    if (child.RefName != null)
                    {
                        throw new Exception($"Cannot propagate ref name inside of named RuleRef. {child.DisplayText} is already named {child.RefName}");
                    }

                    child.RefName = name;
                }
            }

            if (level > 0)
            {
                var namedChildren = rule.Children.Where(r => r.RefName != null).ToList();
                if (namedChildren.Select(c => c.RefName).Distinct().Count() > 1)
                {
                    throw new Exception($"Cannot propagate multiple distinct ref names inside of {rule.DisplayText}");
                }
                else if (namedChildren.Count > 0)
                {
                    var name = namedChildren.First().RefName;
                    if (name != IRuleRef.LiftRefName)
                    {
                        foreach (var child in namedChildren)
                        {
                            child.RefName = IRuleRef.LiftRefName;
                        }
                        return(name);
                    }
                }
            }

            return(null);
        }
        public INode Reduce(IRule rule, INodeContext <INode> page)
        {
            var prefix = page.Get("prefix").Optional <Value>()?.Text;
            var range  = page.Get("suffix").Optional <Ast.Range>();
            var atom   = page.Get("atom").Single <ProtoRule>();
            var result = atom;

            if (range != null)
            {
                result = new ProtoRule(nameof(Repeat), rule => new Repeat(rule.RuleName, rule.Single(), range.Min, range.Max));
                result.Children.Add(atom);
                atom = result;
            }

            if (prefix != null)
            {
                result = _prefixMap[prefix]();
                result.Children.Add(atom);
            }

            return(result);
        }