Example #1
0
 public Parser(int numSwitches)
 {
     _switches = new SwitchResult[numSwitches];
     for (int i = 0; i < numSwitches; i++)
     {
         _switches[i] = new SwitchResult();
     }
 }
Example #2
0
        bool ParseString(string srcString, SwitchForm[] switchForms)
        {
            int len = srcString.Length;

            if (len == 0)
            {
                return(false);
            }
            int pos = 0;

            if (!IsItSwitchChar(srcString[pos]))
            {
                return(false);
            }
            while (pos < len)
            {
                if (IsItSwitchChar(srcString[pos]))
                {
                    pos++;
                }
                const int kNoLen             = -1;
                int       matchedSwitchIndex = 0;
                int       maxLen             = kNoLen;
                for (int switchIndex = 0; switchIndex < _switches.Length; switchIndex++)
                {
                    int switchLen = switchForms[switchIndex].IDString.Length;
                    if (switchLen <= maxLen || pos + switchLen > len)
                    {
                        continue;
                    }
                    if (String.Compare(switchForms[switchIndex].IDString, 0,
                                       srcString, pos, switchLen, true) == 0)
                    {
                        matchedSwitchIndex = switchIndex;
                        maxLen             = switchLen;
                    }
                }
                if (maxLen == kNoLen)
                {
                    throw new Exception("maxLen == kNoLen");
                }
                SwitchResult matchedSwitch = _switches[matchedSwitchIndex];
                SwitchForm   switchForm    = switchForms[matchedSwitchIndex];
                if ((!switchForm.Multi) && matchedSwitch.ThereIs)
                {
                    throw new Exception("switch must be single");
                }
                matchedSwitch.ThereIs = true;
                pos += maxLen;
                int        tailSize = len - pos;
                SwitchType type     = switchForm.Type;
                switch (type)
                {
                case SwitchType.PostMinus:
                {
                    if (tailSize == 0)
                    {
                        matchedSwitch.WithMinus = false;
                    }
                    else
                    {
                        matchedSwitch.WithMinus = (srcString[pos] == kSwitchMinus);
                        if (matchedSwitch.WithMinus)
                        {
                            pos++;
                        }
                    }
                    break;
                }

                case SwitchType.PostChar:
                {
                    if (tailSize < switchForm.MinLen)
                    {
                        throw new Exception("switch is not full");
                    }
                    string    charSet         = switchForm.PostCharSet;
                    const int kEmptyCharValue = -1;
                    if (tailSize == 0)
                    {
                        matchedSwitch.PostCharIndex = kEmptyCharValue;
                    }
                    else
                    {
                        int index = charSet.IndexOf(srcString[pos]);
                        if (index < 0)
                        {
                            matchedSwitch.PostCharIndex = kEmptyCharValue;
                        }
                        else
                        {
                            matchedSwitch.PostCharIndex = index;
                            pos++;
                        }
                    }
                    break;
                }

                case SwitchType.LimitedPostString:
                case SwitchType.UnLimitedPostString:
                {
                    int minLen = switchForm.MinLen;
                    if (tailSize < minLen)
                    {
                        throw new Exception("switch is not full");
                    }
                    if (type == SwitchType.UnLimitedPostString)
                    {
                        matchedSwitch.PostStrings.Add(srcString.Substring(pos));
                        return(true);
                    }
                    String stringSwitch = srcString.Substring(pos, minLen);
                    pos += minLen;
                    for (int i = minLen; i < switchForm.MaxLen && pos < len; i++, pos++)
                    {
                        char c = srcString[pos];
                        if (IsItSwitchChar(c))
                        {
                            break;
                        }
                        stringSwitch += c;
                    }
                    matchedSwitch.PostStrings.Add(stringSwitch);
                    break;
                }
                }
            }
            return(true);
        }