Пример #1
0
    public void read()
    {
        List <CodeKeyword> codekeyStack = new List <CodeKeyword>();
        List <CodeSymbol>  symbolStack  = new List <CodeSymbol>();
        List <PBElement>   elementStack = new List <PBElement>();

        elementStack.Add(this);

        if (charArr == null)
        {
            charArr = code.ToCharArray();
        }

        PBElement curPBElement = null;

        int startIndex = 0;

        for (int i = 0; i < code.Length; i++)
        {
            startIndex = i;
            CodeSymbol curSymbol = FindNextSymbol(code, startIndex);
            int        Index     = curSymbol.Index;

            if (Index > 0)
            {
                //跳过注释
                if (curSymbol.Symbol == '/')
                {
                    if (charArr [curSymbol.Index + 1] == '*')
                    {
                        while (Index < charArr.Length)
                        {
                            curSymbol = FindNextSymbol(code, Index + 1, '/');
                            Index     = curSymbol.Index;

                            if (Index == -1)
                            {
                                Debug.LogErrorFormat("{0} not find '/' end", name);
                                return;
                            }
                            if (charArr [curSymbol.Index - 1] == '*')
                            {
                                break;
                            }
                        }

                        i = Index;

                        Debug.LogWarningFormat("{0} jump /**/ index {1} ", name, Index);

                        continue;
                    }
                }

                string key = code.Substring(startIndex, Index - startIndex);

                if (curPBElement != null && key.StartsWith("//"))
                {
                    //	curPBElement.Notes = key;
                }

                if (key == "")
                {
                }
                else
                {
                    //关键字压入
                    codekeyStack.Add(new CodeKeyword(startIndex, key));
                }

                if (key.Equals("import"))
                {
                    //PBFile pbFile = new PBFile ();
                    PBImportFile pushPBImportFile = new PBImportFile();
                    pushPBImportFile.startIndex = startIndex;

                    elementStack.Add(pushPBImportFile);
                    curPBElement = pushPBImportFile;
                }
                else
                if (key.Equals("package"))
                {
                    PBPackage pushPBPackage = new PBPackage();
                    pushPBPackage.startIndex = startIndex;

                    elementStack.Add(pushPBPackage);
                    curPBElement = pushPBPackage;
                }
                else
                if (key.Equals("required") || key.Equals("optional") || key.Equals("repeated"))
                {
                    PBAttribute pushPBAttribute = new PBAttribute();
                    pushPBAttribute.startIndex = startIndex;

                    elementStack.Add(pushPBAttribute);
                    curPBElement = pushPBAttribute;
                }
                else
                //enum压入
                if (key == "enum")
                {
                    PBEnum pushPBElement = new PBEnum();
                    pushPBElement.pbFile     = this;
                    pushPBElement.name       = key;
                    pushPBElement.startIndex = Index;

                    elementStack.Add(pushPBElement);

                    curPBElement = pushPBElement;
                }
                else
                //message压入
                if (key == "message")
                {
                    PBMessage pushPBMessage = new PBMessage();
                    pushPBMessage.pbFile     = this;
                    pushPBMessage.name       = key;
                    pushPBMessage.startIndex = Index;

                    elementStack.Add(pushPBMessage);

                    curPBElement = pushPBMessage;

                    if (codekeyStack.Count >= 2 && codekeyStack[codekeyStack.Count - 2].value.StartsWith("//"))
                    {
                        pushPBMessage.Notes = codekeyStack[codekeyStack.Count - 2].value;
                    }
                }

                /*
                 * if(curSymbol.Symbol == '='){
                 *      string pname  =  (string)codekeyStack[codekeyStack.Count-1];
                 *      Debug.LogWarning(" >> pname :)))))   -------->  " + pname);
                 * }else
                 */

                if (curSymbol.Symbol == '{')
                {
                    CodeKeyword ptype = codekeyStack[codekeyStack.Count - 2];
                    CodeKeyword pname = codekeyStack[codekeyStack.Count - 1];
                    //Debug.LogWarning(" >> Index :)))))   -------->  " + pname.Index + ", >> pname :)))))   -------->  " + pname.value );
                    //压入符号栈
                    symbolStack.Add(curSymbol);
                }
                else
                if (curSymbol.Symbol == '}')
                {
                    //块结束
                    CodeSymbol pushSymbol = symbolStack[symbolStack.Count - 1];
                    if (pushSymbol.Symbol == '{')
                    {
                        symbolStack.RemoveAt(symbolStack.Count - 1);
                        //弹出符号栈
                        PBElement popUpPBElement = elementStack[elementStack.Count - 1];
                        popUpPBElement.endIndex = Index;
                        popUpPBElement.cut(code);

                        elementStack.RemoveAt(elementStack.Count - 1);

                        if (elementStack.Count > 0)
                        {
                            elementStack[elementStack.Count - 1].addChild(popUpPBElement);
                        }
                    }
                    else
                    {
                        Debug.LogError("Symbol Error { ");
                    }
                }
                else
                if (curSymbol.Symbol == '[')
                {
                    symbolStack.Add(curSymbol);

                    PBKeyValue pushPBKeyValue = new PBKeyValue();
                    pushPBKeyValue.startIndex = Index;
                    elementStack.Add(pushPBKeyValue);
                }
                else
                if (curSymbol.Symbol == ']')
                {
                    CodeSymbol pushSymbol = symbolStack[symbolStack.Count - 1];
                    if (pushSymbol.Symbol == '[')
                    {
                        symbolStack.RemoveAt(symbolStack.Count - 1);
                        //弹出
                        PBElement popUpPBElement = elementStack[elementStack.Count - 1];
                        popUpPBElement.endIndex = Index;
                        popUpPBElement.cut(code);

                        elementStack.RemoveAt(elementStack.Count - 1);

                        if (elementStack.Count > 0)
                        {
                            elementStack[elementStack.Count - 1].addChild(popUpPBElement);
                        }
                    }
                    else
                    {
                        Debug.LogError("Symbol Error [ ");
                    }
                }
                else
                if (curSymbol.Symbol == ';')
                {
                    //弹出
                    PBElement popUpPBElement = elementStack[elementStack.Count - 1];

                    if (popUpPBElement.pb_type == PBElement.type_PBPackage)
                    {
                        popUpPBElement.endIndex = Index;
                        popUpPBElement.cut(code);

                        this.package = (popUpPBElement as PBPackage).pbPackageName;

                        elementStack.RemoveAt(elementStack.Count - 1);
                    }
                    else
                    if (popUpPBElement.pb_type == PBElement.type_PBImportFile)
                    {
                        popUpPBElement.endIndex = Index;
                        popUpPBElement.cut(code);

                        PBFile pbFile = new PBFile();
                        pbFile.name = (popUpPBElement as PBImportFile).pbFilePath;
                        pbFile.readFile(pbFile.name);
                        pbFile.read();

                        PBFileImportDic.Add(pbFile.name, pbFile);

                        elementStack.RemoveAt(elementStack.Count - 1);
                    }
                    else
                    //枚举 属性
                    if (popUpPBElement.pb_type == PBElement.type_PBEnum)
                    {
                        PBKeyValue PBKeyValue = new PBKeyValue();
                        PBKeyValue.key        = codekeyStack[codekeyStack.Count - 2].value;
                        PBKeyValue.value      = codekeyStack[codekeyStack.Count - 1].value;
                        PBKeyValue.startIndex = codekeyStack[codekeyStack.Count - 2].Index;
                        PBKeyValue.endIndex   = Index;
                        PBKeyValue.cut(code);

                        popUpPBElement.addChild(PBKeyValue);

                        curPBElement = PBKeyValue;

                        codekeyStack.RemoveAt(codekeyStack.Count - 1);
                        codekeyStack.RemoveAt(codekeyStack.Count - 1);

                        CodeSymbol nextSymbol = FindNextSymbol(code, Index + 1);
                        string     nextKey    = code.Substring(Index + 1, nextSymbol.Index - (Index + 1));

                        if (curPBElement != null && nextKey.StartsWith("//"))
                        {
                            curPBElement.Notes = nextKey;
                        }
                    }
                    else
                    //message 属性
                    if (popUpPBElement.pb_type == PBElement.type_PBAttribute)
                    {
                        popUpPBElement.endIndex = Index;
                        popUpPBElement.cut(code);

                        elementStack.RemoveAt(elementStack.Count - 1);

                        curPBElement = popUpPBElement;

                        if (elementStack.Count > 0)
                        {
                            elementStack[elementStack.Count - 1].addChild(popUpPBElement);
                        }

                        CodeSymbol nextSymbol = PBFile.FindNextSymbol(code, Index + 1);
                        string     nextKey    = code.Substring(Index + 1, nextSymbol.Index - (Index + 1));

                        if (curPBElement != null && nextKey.StartsWith("//"))
                        {
                            curPBElement.Notes = nextKey;
                        }
                    }
                }
            }
            i = Index;
        }
        //getAllChild(this);
    }