Beispiel #1
0
        /// <summary>
        /// Parses the where clauses and metadata in a text wildcard to extract nested wildcards (helper)
        /// </summary>
        /// <param name="w">The string containing nested TextWildcards to parse.</param>
        private void ParseNestedWildcardsHelper(ref string s)
        {
            int          cc = 0;
            TextWildcard inner;

            s = s ?? String.Empty;
            StringBuilder sb = new StringBuilder(s.Length);

            do
            {
                // Read the string from cc till the next open brace (wildcard delimiter).
                while ((cc < s.Length) && (s[cc] != '{'))
                {
                    sb.Append(s[cc++]);
                }
                // Otherwise, extract the nested text wildcard
                inner = TextWildcard.XtractWildcard(s, ref cc);
                // If the extraction failed, continue
                if (inner == null)
                {
                    continue;
                }
                // Replace the wildcard entry with its unique keycode
                sb.AppendFormat("{{{0}}}", inner.Keycode);
                // Add the text wildcard to the reference lists
                // When a wildcard is added, all nested wildcards are also processed
                AddWildcard(inner);
            } while(cc < s.Length);
            s = sb.ToString();
        }
        private List <string> GetValidReplacements(ProductionRule productionRule)
        {
            List <string> vr = new List <string>(productionRule.Replacements.Count);

            for (int i = 0; i < productionRule.Replacements.Count; ++i)
            {
                string replacement = (productionRule.Replacements[i] ?? String.Empty).Trim();

                for (int cc = 0; cc < replacement.Length; ++cc)
                {
                    cc = replacement.IndexOf('{', cc);
                    if (cc == -1)
                    {
                        break;
                    }
                    int          bcc = cc;
                    TextWildcard w   = TextWildcard.XtractWildcard(replacement, ref cc);
                    if (w.Name != "void")
                    {
                        continue;
                    }
                    replacement = replacement.Remove(bcc) + replacement.Substring(cc);
                    cc          = bcc;
                }

                if (String.IsNullOrEmpty(replacement))
                {
                    continue;
                }

                vr.Add(replacement);
            }
            return(vr);
        }
        /*
         * private void SRGSExpandWhereWildcard(TextWildcard w)
         * {
         *      string keyword = ComputeKeyword(w);
         *      string uri = "#_";
         *      SRGSWriteRuleRef(uri);
         * }
         */

        private void SRGSExpandWildcard(string s, ref int cc)
        {
            string       uri = "#_";
            TextWildcard w   = TextWildcard.XtractWildcard(s, ref cc);

            /*
             * if (!String.IsNullOrEmpty(w.Where))
             * {
             *      SRGSExpandWhereWildcard(w);
             *      return;
             * }
             */
            string keyword = ComputeKeyword(w);

            switch (keyword)
            {
            case "category":
                uri += "categories";
                break;

            case "pron":
                uri += "pronobjs";
                break;

            case "gesture":
            case "name":
            case "female":
            case "male":
            case "location":
            case "beacon":
            case "placement":
            case "room":
            case "object":
            case "aobject":
            case "kobject":
            case "sobject":
            case "pronobj":
            case "pronsub":
                uri += w.Name + "s";
                break;

            case "question":
                writer.WriteString("question");
                return;

            case "void": return;

            default: return;
            }
            SRGSWriteRuleRef(uri);
        }
Beispiel #4
0
        private bool ReplaceNestedWildcardsHelper(ref string s)
        {
            int          cc = 0;
            TextWildcard inner;

            if (String.IsNullOrEmpty(s))
            {
                return(false);
            }
            StringBuilder sb = new StringBuilder(s.Length);

            do
            {
                // Fetch the string from cc till the next open brace (wildcard delimiter).
                while ((cc < s.Length) && (s[cc] != '{'))
                {
                    sb.Append(s[cc++]);
                }
                // If the end of the string has been reached, quit
                if (cc >= s.Length)
                {
                    break;
                }
                // Otherwise, extract the text wildcard
                inner = TextWildcard.XtractWildcard(s, ref cc);
                // If the extraction failed, continue
                if (inner == null)
                {
                    continue;
                }
                // Otherwise, if the wildcard has where clauses or metadata, they need to be replaced first.
                if (!String.IsNullOrEmpty(inner.Metadata) || !String.IsNullOrEmpty(inner.Where))
                {
                    ReplaceNestedWildcards(inner);
                }
                // After replacing nested wildcards in where clauses and metadata, the inner wildcard value is replaced
                if (!wildcards.ContainsKey(inner.Keycode) || (wildcards[inner.Keycode] == null))
                {
                    return(false);
                }
                sb.Append(wildcards[inner.Keycode].Replacement.Name);
            } while(cc < s.Length);
            // Finally, the metadata of the wildcard is updated.
            s = sb.ToString();
            return(true);
        }
Beispiel #5
0
        /// <summary>
        /// Parses the given string, splitting it into tokens whic are added to the provided token list.
        /// All found wildcards are added to the wildcard lists but no replacements are made.
        /// </summary>
        /// <param name="s">The string to parse.</param>
        /// <param name="cc">The read header where the parse must start</param>
        /// <param name="tokens">The list where found tokens will be added</param>
        private void ParseString(string s, ref int cc, List <Token> tokens)
        {
            int          bcc = 0;
            Token        token;
            TextWildcard tWildcard;

            do
            {
                // Update read header backup
                bcc = cc;
                // Read the string from cc till the next open brace (wildcard delimiter).
                while ((cc < s.Length) && (s [cc] != '{'))
                {
                    ++cc;
                }
                // Wildcard found. Extract the string to the left
                string left = s.Substring(bcc, cc - bcc);
                // Store the string at the left of the wildcard as token
                tokens.Add(new Token(left));
                // If the end of the string has been reached, quit
                if (cc >= s.Length)
                {
                    break;
                }
                // Otherwise, extract the text wildcard
                tWildcard = TextWildcard.XtractWildcard(s, ref cc);
                // If the extraction failed, continue
                if (tWildcard == null)
                {
                    continue;
                }
                // Convert the text wildcard into a token
                token = TokenizeTextWildcard(tWildcard);
                // Add the text wildcard to the reference lists
                // When a wildcard is added, all nested wildcards are also processed
                AddWildcard(tWildcard);
                // Add the token
                tokens.Add(token);
            } while(cc < s.Length);
        }
Beispiel #6
0
        public void FindWildcards(string s)
        {
            int cc = 0;

            this.textWildcards.Clear();
            this.wildcards.Clear();
            TextWildcard w;

            while (cc < s.Length)
            {
                if (s[cc] == '{')
                {
                    w = TextWildcard.XtractWildcard(s, ref cc);
                    if (w == null)
                    {
                        continue;
                    }
                    AddWildcard(w);
                }
                ++cc;
            }
        }