Пример #1
0
        public static LinkedList <string> Parse(string src)
        {
            LinkedList <string> result        = new LinkedList <string>();
            StringTokenState    gStringState  = null;
            ComentTokenState    gCommentState = null;
            int i = 0;

            while (i < src.Length)
            {
                if (gStringState == null && gCommentState == null)
                {
                    char c        = src.ElementAt(i);
                    bool isPreEnd = (i >= src.Length - 2);
                    char test     = ' ';
                    if (!isPreEnd)
                    {
                        test = src.ElementAt(i + 1);
                    }
                    if (c == '\"')
                    {
                        gStringState = new StringTokenState();
                        gStringState.push(c);
                    }
                    else if (c == '/' && !isPreEnd && (test == '/' || test == '*'))
                    {
                        gCommentState = new ComentTokenState();
                        gCommentState.push(c);
                    }
                    else
                    {
                        if (canNextToken(c))
                        {
                            result.AddLast(tempText);
                            tempText = "";
                            push(c);
                        }
                        else
                        {
                            push(c);
                        }
                    }
                }
                else if (gStringState != null)
                {
                    char c = src.ElementAt(i);
                    gStringState.push(c);
                    if (gStringState.isStringEnd())
                    {
                        result.AddLast(gStringState.textTemp);
                        gStringState = null;
                    }
                }
                else if (gCommentState != null)
                {
                    char c = src.ElementAt(i);
                    gCommentState.push(c);
                    if (gCommentState.isComentEnd())
                    {
                        result.AddLast(gCommentState.textTemp);
                        gCommentState = null;
                    }
                }
                i += 1;
            }
            return(result);
        }
Пример #2
0
        public string NextToken()
        {
            string result = null;


            while (!reader.EndOfStream)
            {
                if (result == null)
                {
                    result = "";
                }
                reader.DiscardBufferedData();
                reader.BaseStream.Seek(i, SeekOrigin.Begin);
                if (gStringState == null && gCommentState == null)
                {
                    char c = (char)reader.Read();
                    reader.BaseStream.Seek(i + 1, SeekOrigin.Begin);
                    bool isPreEnd = reader.EndOfStream;
                    char test     = ' ';
                    if (!isPreEnd)
                    {
                        test = (char)reader.Read();
                    }
                    if (c == '\"')
                    {
                        gStringState = new StringTokenState();
                        gStringState.push(c);
                    }
                    else if (c == '/' && !isPreEnd && (test == '/' || test == '*'))
                    {
                        gCommentState = new ComentTokenState();
                        gCommentState.push(c);
                    }
                    else
                    {
                        if (canNextToken(c))
                        {
                            result = tempText.ToString();
                            tempText.Clear();
                            push(c);
                            i++;
                            break;
                        }
                        else
                        {
                            push(c);
                        }
                    }
                }
                else if (gStringState != null)
                {
                    char c = (char)reader.Read();
                    gStringState.push(c);
                    if (gStringState.isStringEnd())
                    {
                        result       = gStringState.textTemp;
                        gStringState = null;
                        i++;
                        break;
                    }
                }
                else if (gCommentState != null)
                {
                    char c = (char)reader.Read();
                    gCommentState.push(c);
                    if (gCommentState.isComentEnd())
                    {
                        result        = gCommentState.textTemp;
                        gCommentState = null;
                        i++;
                        break;
                    }
                }
                i++;
            }

            return(result);
        }