Exemple #1
0
        internal void InitSpecialRuleRef(Backend backend, ParseElementCollection parent)
        {
            Rule rule = null;

            switch (_type)
            {
            case SpecialRuleRefType.Null:
                parent.AddArc(backend.EpsilonTransition(1f));
                break;

            case SpecialRuleRefType.Void:
                rule = backend.FindRule("VOID");
                if (rule == null)
                {
                    rule = backend.CreateRule("VOID", (SPCFGRULEATTRIBUTES)0);
                    ((IElement)rule).PostParse((IElement)parent);
                }
                parent.AddArc(backend.RuleTransition(rule, parent._rule, 1f));
                break;

            case SpecialRuleRefType.Garbage:
            {
                OneOf oneOf = new OneOf(parent._rule, backend);
                oneOf.AddArc(backend.RuleTransition(CfgGrammar.SPRULETRANS_WILDCARD, parent._rule, 0.5f));
                oneOf.AddArc(backend.EpsilonTransition(0.5f));
                ((IElement)oneOf).PostParse((IElement)parent);
                break;
            }
            }
        }
Exemple #2
0
        /// <summary>
        /// Add transition corresponding to Special or Uri.
        /// </summary>
        internal RuleRef(ParseElementCollection parent, Backend backend, Uri uri, List <Rule> undefRules, string semanticKey, string initParameters)
            : base(parent._rule)
        {
            string id = uri.OriginalString;

            Rule ruleRef  = null;
            int  posPound = id.IndexOf('#');

            // Get the initial state for the RuleRef.
            if (posPound == 0)
            {
                // Internal RuleRef.  Get InitialState of RuleRef.
                // GetRuleRef() may temporarily create a Rule placeholder for later resolution.
                ruleRef = GetRuleRef(backend, id.Substring(1), undefRules);
            }
            else
            {
                // External RuleRef.  Build URL:GrammarUri#RuleName
                StringBuilder sbExternalRuleUri = new("URL:");

                // Add the parameters to initialize a rule
                if (!string.IsNullOrEmpty(initParameters))
                {
                    // look for the # and insert the parameters
                    sbExternalRuleUri.Append(posPound > 0 ? id.Substring(0, posPound) : id);
                    sbExternalRuleUri.Append('>');
                    sbExternalRuleUri.Append(initParameters);
                    if (posPound > 0)
                    {
                        sbExternalRuleUri.Append(id.Substring(posPound));
                    }
                }
                else
                {
                    sbExternalRuleUri.Append(id);
                }

                // Get InitialState of external RuleRef.
                string sExternalRuleUri = sbExternalRuleUri.ToString();
                ruleRef = backend.FindRule(sExternalRuleUri);
                if (ruleRef == null)
                {
                    ruleRef = backend.CreateRule(sExternalRuleUri, SPCFGRULEATTRIBUTES.SPRAF_Import);
                }
            }
            Arc rulerefArc = backend.RuleTransition(ruleRef, _rule, 1.0f);

#pragma warning disable 0618
            if (!string.IsNullOrEmpty(semanticKey))
            {
                CfgGrammar.CfgProperty propertyInfo = new();
                propertyInfo._pszName  = "SemanticKey";
                propertyInfo._comValue = semanticKey;
                propertyInfo._comType  = VarEnum.VT_EMPTY;
                backend.AddPropertyTag(rulerefArc, rulerefArc, propertyInfo);
            }
#pragma warning restore 0618
            parent.AddArc(rulerefArc);
        }
Exemple #3
0
        private static Rule GetRuleRef(Backend backend, string sRuleId, List <Rule> undefRules)
        {
            Rule rule = backend.FindRule(sRuleId);

            if (rule == null)
            {
                rule = backend.CreateRule(sRuleId, (SPCFGRULEATTRIBUTES)0);
                undefRules.Insert(0, rule);
            }
            return(rule);
        }
Exemple #4
0
        internal RuleRef(ParseElementCollection parent, Backend backend, Uri uri, List <Rule> undefRules, string semanticKey, string initParameters)
            : base(parent._rule)
        {
            string originalString = uri.OriginalString;
            Rule   rule           = null;
            int    num            = originalString.IndexOf('#');

            if (num == 0)
            {
                rule = GetRuleRef(backend, originalString.Substring(1), undefRules);
            }
            else
            {
                StringBuilder stringBuilder = new StringBuilder("URL:");
                if (!string.IsNullOrEmpty(initParameters))
                {
                    stringBuilder.Append((num > 0) ? originalString.Substring(0, num) : originalString);
                    stringBuilder.Append('>');
                    stringBuilder.Append(initParameters);
                    if (num > 0)
                    {
                        stringBuilder.Append(originalString.Substring(num));
                    }
                }
                else
                {
                    stringBuilder.Append(originalString);
                }
                string text = stringBuilder.ToString();
                rule = backend.FindRule(text);
                if (rule == null)
                {
                    rule = backend.CreateRule(text, SPCFGRULEATTRIBUTES.SPRAF_Import);
                }
            }
            Arc arc = backend.RuleTransition(rule, _rule, 1f);

            if (!string.IsNullOrEmpty(semanticKey))
            {
                backend.AddPropertyTag(arc, arc, new CfgGrammar.CfgProperty
                {
                    _pszName  = "SemanticKey",
                    _comValue = semanticKey,
                    _comType  = VarEnum.VT_EMPTY
                });
            }
            parent.AddArc(arc);
        }
Exemple #5
0
        /// <summary>
        /// Return the initial state of the rule with the specified name.
        /// If the rule is not defined yet, create a placeholder Rule.
        /// </summary>
        private static Rule GetRuleRef(Backend backend, string sRuleId, List <Rule> undefRules)
        {
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(sRuleId));

            // Get specified rule.
            Rule rule = backend.FindRule(sRuleId);

            if (rule == null)
            {
                // Rule doesn't exist.  Create a placeholder rule and add StateHandle to UndefinedRules.
                rule = backend.CreateRule(sRuleId, 0);
                undefRules.Insert(0, rule);
            }

            return(rule);
        }
        private Rule GetRule(string sRuleId, SPCFGRULEATTRIBUTES dwAttributes)
        {
            Rule rule = _backend.FindRule(sRuleId);

            if (rule != null)
            {
                int num = _undefRules.IndexOf(rule);
                if (num != -1)
                {
                    _backend.SetRuleAttributes(rule, dwAttributes);
                    _undefRules.RemoveAt(num);
                }
                else
                {
                    XmlParser.ThrowSrgsException(SRID.RuleRedefinition, sRuleId);
                }
            }
            else
            {
                rule = _backend.CreateRule(sRuleId, dwAttributes);
            }
            return(rule);
        }
Exemple #7
0
        /// <summary>
        /// Returns the initial state of a special rule.
        /// For each type of special rule we make a rule with a numeric id and return a reference to it.
        /// </summary>
        internal void InitSpecialRuleRef(Backend backend, ParseElementCollection parent)
        {
            Rule rule = null;

            // Create a transition corresponding to Special or Uri
            switch (_type)
            {
            case SpecialRuleRefType.Null:
                parent.AddArc(backend.EpsilonTransition(1.0f));
                break;

            case SpecialRuleRefType.Void:
                rule = backend.FindRule(szSpecialVoid);
                if (rule == null)
                {
                    rule = backend.CreateRule(szSpecialVoid, 0);
                    // Rule with no transitions is a void rule.
                    ((IRule)rule).PostParse(parent);
                }
                parent.AddArc(backend.RuleTransition(rule, parent._rule, 1.0f));
                break;

            case SpecialRuleRefType.Garbage:
                // Garbage transition is optional whereas Wildcard is not.  So we need additional epsilon transition.
                OneOf oneOf = new(parent._rule, backend);
                // Add the garbage transition
                oneOf.AddArc(backend.RuleTransition(CfgGrammar.SPRULETRANS_WILDCARD, parent._rule, 0.5f));
                // Add a parallel epsilon path
                oneOf.AddArc(backend.EpsilonTransition(0.5f));
                ((IOneOf)oneOf).PostParse(parent);
                break;

            default:
                System.Diagnostics.Debug.Assert(false, "Unknown special ruleref type");
                break;
            }
        }