コード例 #1
0
ファイル: MemberChain.cs プロジェクト: kth0729/intellua
        public static MemberChain ParseBackward(IntelluaSource source, int pos = -1)
        {
            const string seperator = ".:";
            const string lbracket  = "([{";
            const string rbracket  = ")]}";
            const string operators = "=+-*/;";

            Byte[] str = source.RawText;
            if (pos < 0)
            {
                pos = source.getRawPos() - 1;
            }
            PaserState state = PaserState.searchWordEnd;

            MemberChain rst       = new MemberChain();
            int         wordStart = pos;
            int         wordEnd   = pos;

            int bracketLevel = 0;

            bool isFuncion = false;

            while (pos >= 0 && pos < str.Length)
            {
                if (str[pos] > 127)
                {
                    pos--;
                    continue;
                }
                char c         = Convert.ToChar(str[pos]);
                bool isComment = Parser.isComment(source, pos);
                bool isString  = Parser.isString(source, pos);

                switch (state)
                {
                case PaserState.searchWordStart:
                    if (isString)
                    {
                        return(rst);
                    }
                    if (!char.IsLetterOrDigit(c) || isComment || pos == 0)
                    {
                        wordStart = pos + 1;
                        if (pos == 0 && char.IsLetterOrDigit(c))
                        {
                            wordStart = 0;
                        }
                        Byte[] bword = SubArray(str, wordStart, wordEnd - wordStart + 1);
                        string word  = Encoding.UTF8.GetString(bword);   //str.Substring(wordStart, wordEnd - wordStart + 1);
                        //word.Trim();
                        {
                            //int p = source.getRawPos(wordStart);
                            rst.Elements.Insert(0, new Word(word, isFuncion, wordStart));
                            isFuncion    = false;
                            rst.StartPos = pos;
                        }
                        state = PaserState.searchSeperator;
                    }
                    else
                    {
                        pos--;
                    }

                    break;

                case PaserState.searchWordEnd:
                    if (isComment)
                    {
                        pos--;
                        break;
                    }
                    if (isString)
                    {
                        return(rst);
                    }
                    if (operators.Contains(c))
                    {
                        return(rst);
                    }

                    if (seperator.Contains(c))
                    {
                        if (rst.Elements.Count == 0)
                        {
                            //int p = source.getRawPos(pos);
                            rst.Elements.Add(new Word("", false, pos));
                        }
                    }

                    if (rbracket.Contains(c))
                    {
                        if (rst.Elements.Count == 0)
                        {
                            //int p = source.getRawPos(pos);
                            rst.Elements.Add(new Word("", false, pos));
                        }
                        state = PaserState.searchBracket;
                        break;
                    }
                    if (char.IsLetterOrDigit(c))
                    {
                        wordEnd = pos;
                        if (rst.EndPos < 0)
                        {
                            rst.EndPos = pos;
                        }
                        state = PaserState.searchWordStart;
                    }
                    else
                    {
                        pos--;
                    }
                    break;

                case PaserState.searchSeperator:
                    if (isString)
                    {
                        return(rst);
                    }

                    if (char.IsWhiteSpace(c) || isComment)
                    {
                        pos--;
                    }
                    else if (seperator.Contains(c))
                    {
                        state = PaserState.searchWordEnd;
                        pos--;
                    }
                    else
                    {
                        //end
                        return(rst);
                    }
                    break;

                case PaserState.searchBracket:
                    if (!isComment && !isString)
                    {
                        if (rbracket.Contains(c))
                        {
                            bracketLevel++;
                        }
                        else if (lbracket.Contains(c))
                        {
                            bracketLevel--;
                            if (bracketLevel == 0)
                            {
                                if (c == '(')
                                {
                                    isFuncion = true;
                                }
                                state = PaserState.searchWordEnd;
                            }
                        }
                    }
                    pos--;
                    break;
                }
            }

            return(rst);
        }
コード例 #2
0
ファイル: MemberChain.cs プロジェクト: xiexin36/intellua
        public static MemberChain ParseFoward(IntelluaSource source, int pos)
        {
            const string seperator = ".:";
            const string lbracket  = "([{";
            const string rbracket  = ")]}";
            const string operators = "=+-*/;";

            Byte[] str = source.RawText;

            PaserState state = PaserState.searchWordStart;

            MemberChain rst       = new MemberChain();
            int         wordStart = pos;
            int         wordEnd   = pos;

            int bracketLevel = 0;

            while (pos < str.Length)
            {
                if (str[pos] > 127)
                {
                    pos++;
                    continue;
                }
                char c = Convert.ToChar(str[pos]);

                bool isComment = Parser.isComment(source, pos);
                bool isString  = Parser.isString(source, pos);

                switch (state)
                {
                case PaserState.searchWordEnd:
                    if (isString)
                    {
                        return(rst);
                    }
                    if (!Parser.isIndentifierChar(c) || isComment || pos == str.Length - 1)
                    {
                        wordEnd = pos;
                        string word;
                        if (pos == str.Length - 1)
                        {
                            Byte[] bword = SubArray(str, wordStart, wordEnd - wordStart + 1);
                            word = Encoding.UTF8.GetString(bword);
                        }
                        else
                        {
                            Byte[] bword = SubArray(str, wordStart, wordEnd - wordStart);
                            word = Encoding.UTF8.GetString(bword);
                            //word = str.Substring(wordStart, wordEnd - wordStart);
                        }
                        word.Trim();
                        {
                            //int p = wordStart;//source.getRawPos(wordStart);
                            rst.Elements.Add(new Word(word, false, wordStart));

                            rst.EndPos = pos;
                        }
                        state = PaserState.searchSeperator;
                    }
                    else
                    {
                        pos++;
                    }

                    break;

                case PaserState.searchWordStart:
                    if (isString)
                    {
                        return(rst);
                    }
                    if (operators.Contains(c))
                    {
                        return(rst);
                    }
                    if (isComment)
                    {
                        pos++;
                        break;
                    }

                    if (Parser.isIndentifierChar(c))
                    {
                        wordStart = pos;
                        if (rst.StartPos < 0)
                        {
                            rst.StartPos = pos;
                        }
                        state = PaserState.searchWordEnd;
                    }
                    else
                    {
                        pos++;
                    }
                    break;

                case PaserState.searchSeperator:
                    if (isString)
                    {
                        return(rst);
                    }
                    if (lbracket.Contains(c))
                    {
                        if (c == '(')
                        {
                            if (rst.Elements.Count > 0)
                            {
                                rst.Elements[rst.Elements.Count - 1].IsFunction = true;
                            }
                        }
                        state = PaserState.searchBracket;
                        break;
                    }
                    if (seperator.Contains(c))
                    {
                        state = PaserState.searchWordStart;
                        pos++;
                    }
                    else if (char.IsWhiteSpace(c) || isComment)
                    {
                        pos++;
                    }
                    else
                    {
                        //end
                        return(rst);
                    }
                    break;

                case PaserState.searchBracket:
                    if (!isComment && !isString)
                    {
                        if (lbracket.Contains(c))
                        {
                            bracketLevel++;
                        }
                        else if (rbracket.Contains(c))
                        {
                            bracketLevel--;
                            if (bracketLevel == 0)
                            {
                                state = PaserState.searchSeperator;
                            }
                        }
                    }
                    pos++;
                    break;
                }
            }

            return(rst);
        }