示例#1
0
 internal void CreateChildrenElements(IElementFactory elementFactory, IItem parent, IRule rule, IdentifierCollection ruleIds)
 {
     foreach (GrammarBuilderBase builder in Items)
     {
         IElement element = builder.CreateElement(elementFactory, parent, rule, ruleIds);
         if (element != null)
         {
             element.PostParse(parent);
             elementFactory.AddElement(parent, element);
         }
     }
 }
示例#2
0
            internal override IElement CreateElement(IElementFactory elementFactory, IElement parent, IRule rule, IdentifierCollection ruleIds)
            {
                Collection <RuleElement> newRules = new();

                CalcCount(null);
                Optimize(newRules);

                foreach (GrammarBuilderBase baseRule in newRules)
                {
                    Items.Add(baseRule);
                }

                // The id of the root rule
                string rootId = ruleIds.CreateNewIdentifier("root");

                // Set the grammar's root rule
                elementFactory.Grammar.Root      = rootId;
                elementFactory.Grammar.TagFormat = System.Speech.Recognition.SrgsGrammar.SrgsTagFormat.KeyValuePairs;

                // Create the root rule
                IRule root = elementFactory.Grammar.CreateRule(rootId, RulePublic.False, RuleDynamic.NotSet, false);

                // Create all the rules
                foreach (GrammarBuilderBase item in Items)
                {
                    if (item is RuleElement)
                    {
                        item.CreateElement(elementFactory, root, root, ruleIds);
                    }
                }
                // Create an item which represents the grammar
                foreach (GrammarBuilderBase item in Items)
                {
                    if (!(item is RuleElement))
                    {
                        IElement element = item.CreateElement(elementFactory, root, root, ruleIds);

                        if (element != null)
                        {
                            element.PostParse(root);
                            elementFactory.AddElement(root, element);
                        }
                    }
                }
                // Post parse the root rule
                root.PostParse(elementFactory.Grammar);

                elementFactory.Grammar.PostParse(null);
                return(null);
            }
示例#3
0
            internal override IElement CreateElement(IElementFactory elementFactory, IElement parent, IRule rule, IdentifierCollection ruleIds)
            {
                Collection <RuleElement> collection = new Collection <RuleElement>();

                CalcCount(null);
                Optimize(collection);
                foreach (RuleElement item in collection)
                {
                    base.Items.Add(item);
                }
                string text = ruleIds.CreateNewIdentifier("root");

                elementFactory.Grammar.Root      = text;
                elementFactory.Grammar.TagFormat = SrgsTagFormat.KeyValuePairs;
                IRule rule2 = elementFactory.Grammar.CreateRule(text, RulePublic.False, RuleDynamic.NotSet, false);

                foreach (GrammarBuilderBase item2 in base.Items)
                {
                    if (item2 is RuleElement)
                    {
                        item2.CreateElement(elementFactory, rule2, rule2, ruleIds);
                    }
                }
                foreach (GrammarBuilderBase item3 in base.Items)
                {
                    if (!(item3 is RuleElement))
                    {
                        IElement element = item3.CreateElement(elementFactory, rule2, rule2, ruleIds);
                        if (element != null)
                        {
                            element.PostParse(rule2);
                            elementFactory.AddElement(rule2, element);
                        }
                    }
                }
                rule2.PostParse(elementFactory.Grammar);
                elementFactory.Grammar.PostParse(null);
                return(null);
            }
        private void ProcessChildNodes(SrgsElement srgsElement, IElement parent, IRule rule)
        {
            Type     type    = srgsElement.GetType();
            IElement element = null;
            IRule    rule2   = parent as IRule;
            IItem    item    = parent as IItem;

            if (type == typeof(SrgsRuleRef))
            {
                element = ParseRuleRef((SrgsRuleRef)srgsElement, parent);
            }
            else if (type == typeof(SrgsOneOf))
            {
                element = ParseOneOf((SrgsOneOf)srgsElement, parent, rule);
            }
            else if (type == typeof(SrgsItem))
            {
                element = ParseItem((SrgsItem)srgsElement, parent, rule);
            }
            else if (type == typeof(SrgsToken))
            {
                element = ParseToken((SrgsToken)srgsElement, parent);
            }
            else if (type == typeof(SrgsNameValueTag))
            {
                element = ParseNameValueTag((SrgsNameValueTag)srgsElement, parent);
            }
            else if (type == typeof(SrgsSemanticInterpretationTag))
            {
                element = ParseSemanticTag((SrgsSemanticInterpretationTag)srgsElement, parent);
            }
            else if (type == typeof(SrgsSubset))
            {
                element = ParseSubset((SrgsSubset)srgsElement, parent);
            }
            else if (type == typeof(SrgsText))
            {
                SrgsText     srgsText = (SrgsText)srgsElement;
                string       text     = srgsText.Text;
                IElementText value    = _parser.CreateText(parent, text);
                ParseText(parent, text, null, null, -1f);
                if (rule2 != null)
                {
                    _parser.AddElement(rule2, value);
                }
                else if (item != null)
                {
                    _parser.AddElement(item, value);
                }
                else
                {
                    XmlParser.ThrowSrgsException(SRID.InvalidElement);
                }
            }
            else
            {
                XmlParser.ThrowSrgsException(SRID.InvalidElement);
            }
            IOneOf oneOf = parent as IOneOf;

            if (oneOf != null)
            {
                IItem item2 = element as IItem;
                if (item2 != null)
                {
                    _parser.AddItem(oneOf, item2);
                }
                else
                {
                    XmlParser.ThrowSrgsException(SRID.InvalidElement);
                }
            }
            else if (rule2 != null)
            {
                _parser.AddElement(rule2, element);
            }
            else if (item != null)
            {
                _parser.AddElement(item, element);
            }
            else
            {
                XmlParser.ThrowSrgsException(SRID.InvalidElement);
            }
        }