Exemplo n.º 1
0
        // simply search for end and stuff everything in the middle into a single token
        private static bool ParseString(int indent, string[] strs, int iStart,
			ValueDelimiter thisDelim, List<DelimiterNode> nodes,
			out int iEnd, out DelimiterList result)
        {
            iEnd = -1;
            bool bExtra = false;
            for (int i = iStart; i < strs.Length; i++)
            {
                string s = strs[i];
                if (s == thisDelim.End)
                {
                    iEnd = i;
                    break;
                }
                else if (s.Substring(s.Length - 1, 1) == thisDelim.End)
                {
                    iEnd = i + 1;
                    bExtra = true;
                    break;
                }
            }

            // no specified end delim means take the remainder of the line
            if (thisDelim.End == "")
                iEnd = strs.Length;

            // if we found end, wrap entire string in a single node
            if (iEnd != -1)
            {
                string subStr = GetSubStr(iStart, iEnd, strs);
                if (bExtra)
                {
                    subStr = subStr.Substring(0, subStr.Length - 1);
                    --iEnd;
                }
                Token token = new Token(subStr);
                DelimiterNode node = new DelimiterNodeToken(token);
                nodes.Add(node);
                result = new DelimiterList(thisDelim, nodes, indent, strs[iStart - 1], subStr, null);
                return true;
            }
            result = null;
            return false;
        }
Exemplo n.º 2
0
        // handle as individual tokens and nested lists
        private static bool ParseMisc(int indent, string original, string[] strs, int iStart, ValueDelimiter thisDelim,
			IParseLineDelimiters delims, ILineRequestor requestor, List<DelimiterNode> nodes,
			out int iEnd, out DelimiterList result)
        {
            result = null;
            iEnd = strs.Length;

            for (int i = iStart; i < strs.Length; i++)
            {
                string s = strs[i];

                // is this the end of current set of delimited tokens?
                if (s == thisDelim.End)
                {	// end delimiter
                    iEnd = i;
                    string subStr = GetSubStr(iStart, iEnd, strs);
                    result = new DelimiterList(thisDelim, nodes, indent, strs[iStart - 1], subStr, null);
                    return true;
                }
                // TODO: rework this so I don't need to check for : (e.g. for :}, when creating a delim)
                if (s.Substring(s.Length - 1, 1) == thisDelim.End && (s[0] != ':' || s.Length > 2))
                {	// end delimiter is part of final token
                    iEnd = i + 1;
                    string without = s.Substring(0, s.Length - 1);

                    Token token = new Token(without);
                    DelimiterNode node = new DelimiterNodeToken(token);
                    nodes.Add(node);

                    strs[i] = without;
                    string subStr = GetSubStr(iStart, iEnd, strs);
                    result = new DelimiterList(thisDelim, nodes, indent, strs[iStart - 1], subStr, null);
                    strs[i] = s;
                    --iEnd;
                    return true;
                }

                // is it a stand alone starting delimiter?
                bool bAnyToken = false;
                ValueDelimiter subDelim = (delims == null ? null : delims.GetDelim(s, out bAnyToken));
                string[] strsToUse = strs;
                bool bExtra = true;
                if (subDelim == null && !bAnyToken)
                {	// whole thing wasn't a delimiter, function, etc., how about the 1st char?
                    string s1 = s.Substring(0, 1);
                    subDelim = (delims == null ? null : delims.GetDelim(s1, out bAnyToken));
                    if (subDelim != null)
                    {	// copy across array, but break iStart into delim & remainder
                        bExtra = false;
                        strsToUse = new string[strs.Length + 1];
                        for (int j = 0; j < i; j++)
                            strsToUse[j] = strs[j];
                        strsToUse[i] = s1;
                        strsToUse[i + 1] = s.Substring(1);
                        for (int j = i + 1; j < strs.Length; j++)
                            strsToUse[j + 1] = strs[j];
                    }
                }
                if (subDelim != null)
                {	// start delimiter
                    int end;
                    DelimiterList sublist = Do(0, original, strsToUse, i + 1, subDelim, delims, requestor, out end);
                    if (sublist != null)
                    {
                        DelimiterNodeList node = new DelimiterNodeList(sublist);
                        nodes.Add(node);
                    }
                    // skip past the sublist
                    i = (bExtra ? end : end - 1);
                }
                else
                {	// stand alone token
                    Token token = new Token(s);
                    DelimiterNode node = new DelimiterNodeToken(token);
                    nodes.Add(node);
                }
            }
            return false;
        }