Пример #1
0
 /// <summary>
 /// Create a <see cref="SyntaxToken"/> from one or more <see cref="LexicalToken"/>.
 /// </summary>
 private static SyntaxToken ProduceSyntaxToken(Source <LexicalToken> source, int start, int length, string text)
 {
     if (length == 1)
     {
         return(SyntaxToken.From(source.Peek(start)));
     }
     else
     {
         // use the trivia form the first token and the text supplied (instead of concatenating the same text from the tokens)
         return(SyntaxToken.Identifier(source.Peek(start).Trivia, text));
     }
 }
Пример #2
0
        /// <summary>
        /// Matches one or more lexical tokens to the corresponding text.
        /// </summary>
        private static int MatchesText(Source <LexicalToken> source, int start, string text)
        {
            // consume all lexical tokens with combined text that matches the specified text
            int textOffset = 0;
            int i          = 0;

            for (; !source.IsEnd(start + i); i++)
            {
                var token = source.Peek(start + i);

                // only first token can have trivia
                if (i > 0 && token.Trivia.Length > 0)
                {
                    break;
                }

                // token has more text than remaining
                if (token.Text.Length > text.Length - textOffset)
                {
                    break;
                }

                // text parts must match exactly
                if (string.Compare(token.Text, 0, text, textOffset, token.Text.Length) != 0)
                {
                    break;
                }

                textOffset += token.Text.Length;

                if (textOffset == text.Length)
                {
                    return(i + 1);
                }
            }

            return(-(i + 1));
        }