コード例 #1
0
 /// <summary>
 /// Add a new token.
 /// </summary>
 /// <param name="token">
 /// The token.
 /// </param>
 private void AddNewToken(Token token)
 {
     Tokens.Add(token);
 }
コード例 #2
0
        /// <summary>
        /// Eat all comments from the matches, so we get a clean list wihtout any comments.
        /// Put the whole comment into one token.
        /// </summary>
        internal void EatComments()
        {
            for (int idx = 0; idx < Matches.Count; idx++)
            {
                var match = Matches[idx];

                // is it a line comment starting here
                if (match.Value == "//")
                {
                    var token = new Token(Line, match.Index, 0, string.Empty, CodeType.Comment);

                    // now search the end of this line
                    int i = idx;
                    for (; i < Matches.Count; i++)
                    {
                        if (Matches[i].Value == "\n")
                        {
                            break;
                        }
                    }

                    // is comment until EOF
                    if (i == Matches.Count)
                    {
                        token.Length = Matches[i - 1].Index + Matches[i - 1].Length - match.Index;
                    }
                    else
                    {
                        token.Length = Matches[i].Index - match.Index;
                    }

                    // set text from buffer
                    token.Text = buffer.Substring(token.Position, token.Length);
                    if (token.Text.EndsWith("\r"))
                    {
                        token.Length--;
                    }

                    // add token to list
                    AddNewToken(token);

                    // now remove all found comment token from the list
                    Matches.RemoveRange(idx, i - idx);
                }

                // is it a block comment starting here
                if (match.Value == "/*")
                {
                    var token = new Token(Line, match.Index, 0, string.Empty, CodeType.Comment);

                    // now search the */
                    int i = idx;
                    for (; i < Matches.Count; i++)
                    {
                        if (Matches[i].Value == "\n")
                        {
                            continue;
                        }

                        if (Matches[i].Value == "*/")
                        {
                            break;
                        }
                    }

                    // is comment until EOF
                    if (i == Matches.Count)
                    {
                        token.Length = Matches[i - 1].Index + Matches[i - 1].Length - match.Index;

                        // and error */ is missed
                        AddNewError(Matches[i - 1].Index + Matches[i - 1].Length, 1, "Missing */");
                    }
                    else
                    {
                        token.Length = Matches[i].Index + Matches[i].Length - match.Index;
                        i++;
                    }

                    // set text from buffer
                    token.Text = buffer.Substring(token.Position, token.Length);

                    // add token to list
                    AddNewToken(token);

                    // now remove all found comment token from the list
                    Matches.RemoveRange(idx, i - idx);
                }
            }
        }