Beispiel #1
0
        private static Name TranslateUdtNameV1(string strname)
        {
            StringBuilder tag = new StringBuilder();
            Name result = new Name();
            List<TemplateParam> templateParams = null;
            int state = 0;
            //int param = 0;
            //bool unsigned = false;
            StringBuilder constTemplParam = null;
            int templParamUdtNameLen = 0;
            StringBuilder specialName = null;

            int i = 0;
            for (; i < strname.Length; i++)
            {
                char ch = strname[i];
                switch (state)
                {
                    case 0:
                        switch (ch)
                        {
                            case '@':
                                if (result.Namespaces == null)
                                    result.Namespaces = new List<Name>();
                                Name currentName = new Name();
                                currentName.Tag = tag.ToString();
                                currentName.TemplateParameters = templateParams;
                                result.Namespaces.Add(currentName);
                                tag = new StringBuilder();
                                templateParams = null;
                                break;
                            case '%':
                                if (tag.Length == 0)
                                {
                                    templateParams = new List<TemplateParam>();
                                    state = 1;
                                }
                                else
                                {
                                    throw new ApplicationException("Invalid character in state 0:" + ch);
                                }
                                break;
                            case '$':
                                if (tag.Length == 0)
                                    state = 12; // special name
                                else
                                    state = 14; // parameters
                                break;
                            default:
                                tag.Append(ch);
                                break;
                        }
                        break;
                    case 1:
                        switch (ch)
                        {
                            case '$':
                                state = 2;
                                break;
                            default:
                                tag.Append(ch);
                                break;
                        }
                        break;
                    case 2:
                        switch (ch)
                        {
                            case 'i':
                                state = 9;
                                break;
                            case 'u':
                                //unsigned = true;
                                break;
                            case 't':
                                templParamUdtNameLen = 0;
                                state = 8;
                                break;
                            case '%':
                                state = 0;
                                break;
                            default:
                                throw new ApplicationException("Invalid character while in state 2: " + ch + ", in string: " + strname);
                        }
                        break;
                    case 5: // constant in template
                        if (char.IsDigit(ch))
                        {
                            constTemplParam.Append(ch);
                        }
                        else
                        {
                            templateParams.Add(new ImmTemplParam(constTemplParam.ToString()));
                            state = 11;
                            i--;
                        }
                        break;
                    case 8:
                        if (char.IsDigit(ch))
                        {
                            templParamUdtNameLen *= 10;
                            templParamUdtNameLen += (int)char.GetNumericValue(ch);
                        }
                        else
                        {
                            //if (strname.Length < i + templParamUdtNameLen)
                            //	throw new ApplicationException("Invalid type name length: " + templParamUdtNameLen.ToString() + ", exceed name length: " + strname.Length.ToString());
                            TagTemplParam tagpar = new TagTemplParam();
                            tagpar.Tag = TranslateUdtNameV1(strname.Substring(i, templParamUdtNameLen));
                            templateParams.Add(tagpar);
                            i += templParamUdtNameLen - 1;
                            state = 11;
                        }
                        break;
                    case 9:
                        switch (ch)
                        {
                            case 'c':
                                //param = 2;
                                state = 10;
                                break;
                            case 'u':
                                //unsigned = true;
                                break;
                            //default: ;
                            //	throw new ApplicationException("Invalid character while in state 2: " + ch + ", in string: " + strname);
                        }
                        break;
                    case 10:
                        switch (ch)
                        {
                            case '$':
                                state = 5;
                                constTemplParam = new StringBuilder();
                                break;
                            default:
                                throw new ApplicationException("Invalid character while in state " + state.ToString() + ": " + ch + ", in string: " + strname);
                        }
                        break;
                    case 11:
                        switch (ch)
                        {
                            case '$':
                                state = 2;
                                break;
                            case '%':
                                state = 2;
                                i--;
                                break;
                        }
                        break;
                    case 12:
                        if (ch == 'b')
                        {
                            state = 13; // special name
                            specialName = new StringBuilder();
                        }
                        else
                        {
                            throw new ApplicationException("Invalid character in state 9: " + ch);
                        }
                        break;
                    case 13:
                        if (ch == '$')
                        {
                            result.IsSpecial = true;
                            result.SpecialName = StrToSpcName(specialName.ToString());
                            specialName = null;
                            state = 14;
                            break;
                        }
                        else
                        {
                            specialName.Append(ch);
                        }
                        break;
                    case 14:
                        // ignoring parameters
                        break;
                    default:
                        Debug.Fail("Invalid state: " + state.ToString());
                        break;
                }
            }
            if (state != 0 && state != 14)
                throw new ApplicationException("Unexpected end of name while in state: " + state.ToString());
            result.Tag = tag.ToString();
            result.TemplateParameters = templateParams;
            return result;
        }
Beispiel #2
0
        private static Name TranslateUdtNameV2(string strname)
        {
            Name result = new Name();
            StringBuilder tag = new StringBuilder();
            List<TemplateParam> templateParams = null;
            int state = 0;
            StringBuilder constTemplParam = null;
            StringBuilder specialName = null;
            TemplateParam templParam = null;

            int i = 0;
            for (; i < strname.Length; i++)
            {
                char ch = strname[i];
                switch (state)
                {
                    case 0:
                        switch (ch)
                        {
                            case '@':
                                if (result.Namespaces == null)
                                    result.Namespaces = new List<Name>();
                                Name currentName = new Name();
                                currentName.Tag = tag.ToString();
                                currentName.TemplateParameters = templateParams;
                                result.Namespaces.Add(currentName);
                                tag = new StringBuilder();
                                templateParams = null;
                                break;
                            case '%':
                                templateParams = new List<TemplateParam>();
                                state = 1;
                                break;
                            case '$':
                                if (tag.Length == 0)
                                    state = 9;
                                else
                                    state = 11; // parameters
                                break;
                            default:
                                tag.Append(ch);
                                break;
                        }
                        break;
                    case 1:
                        switch (ch)
                        {
                            case '$':
                                state = 2;
                                break;
                            default:
                                tag.Append(ch);
                                break;
                        }
                        break;
                    case 2:
                        switch (ch)
                        {
                            case 't':
                                state = 12;
                                break;
                            case '%':
                                state = 0;
                                break;
                            default:
                                templParam = ParseParam(strname, ref i);
                                if (templParam != null)
                                    state = 3;
                                else
                                    throw new ApplicationException("Invalid character while in state 2: " + ch + ", in string: " + strname);
                                break;
                        }
                        break;
                    case 3: // template param type grabbed
                        switch (ch)
                        {
                            case '$':
                                state = 4;
                                break;
                            default:
                                state = 2;
                                i--;
                                templateParams.Add(templParam);
                                break;
                        }
                        break;
                    case 4: // begin constant in template
                        switch (ch)
                        {
                            case 'i': // numeric constant
                                state = 5;
                                constTemplParam = new StringBuilder();
                                break;
                            case 'e': // reference to static variable
                                i++;
                                templateParams.Add(ParseTemplVarRefParam(templParam, strname, ref i));
                                state = 2;
                                break;
                            case 'g':
                                state = 5;
                                constTemplParam = new StringBuilder();
                                break;
                            default:
                                throw new ApplicationException("Invalid character while in state 4: " + ch + ", in string: " + strname);
                        }
                        break;
                    case 5: // constant in template
                        if (ch == '$')
                        {
                            templateParams.Add(new ImmTemplParam(constTemplParam.ToString()));
                            state = 2;
                        }
                        else
                        {
                            constTemplParam.Append(ch);
                        }
                        break;
                    case 9:
                        if (ch == 'b')
                        {
                            state = 10; // special name
                            specialName = new StringBuilder();
                        }
                        else
                        {
                            throw new ApplicationException("Invalid character in state 9: " + ch);
                        }
                        break;
                    case 10:
                        if (ch == '$')
                        {
                            result.IsSpecial = true;
                            result.SpecialName = StrToSpcName(specialName.ToString());
                            specialName = null;
                            state = 11;
                            break;
                        }
                        else
                        {
                            specialName.Append(ch);
                        }
                        break;
                    case 11:
                        // ignoring parameters
                        break;
                    case 12:
                        templateParams.Add(templateParams[(int)char.GetNumericValue(ch) - 1]);
                        state = 2;
                        break;
                    default:
                        Debug.Fail("Invalid state: " + state.ToString());
                        break;
                }
            }
            if (state != 0 && state != 11)
                throw new ApplicationException("Unexpected end of name while in state: " + state.ToString());
            result.Tag = tag.ToString();
            result.TemplateParameters = templateParams;
            return result;
        }
Beispiel #3
0
 public static Name Parse(string strname)
 {
     if (strname[0] == '@')
     {
         strname = strname.Substring(1);
         Name result;
         try
         {
             result = TranslateUdtNameV2(strname);
         }
         catch (Exception exv2)
         {
             try
             {
                 result = TranslateUdtNameV1(strname);
             }
             catch (Exception exv1)
             {
                 if (strname.Length >= 253)
                 {
                     Console.WriteLine("Warning: Name cannot be parsed: " + strname + " possible reason - incomplete");
                     Name name = new Name();
                     name.Tag = strname;
                     return name;
                 }
                 else
                 {
                     Console.WriteLine("Warning: Name cannot be parsed: " + strname);
                     //Console.WriteLine("  Error v2: " + exv2.Message);
                     //Console.WriteLine("  Error v1: " + exv1.Message);
                     Name name = new Name();
                     name.Tag = strname;
                     return name;
                 }
             }
         }
         return result;
     }
     else
     {
         Name name = new Name();
         name.Tag = strname;
         return name;
     }
 }