예제 #1
0
        /// <summary>
        /// Analyzes the input text as C# code. This method doesn't return anything.
        /// Callers may retrieve the result of this process by querying the FormattedCode property.
        ///   Passing null results in raising an exception.
        /// </summary>
        /// <param name="csharp">The text to be analyzed.</param>
        protected void Parse(string csharp)
        {
            TokenClassifier   classifier;
            ConcreteToken     csToken;
            ClassificationTag tag;
            Lexer             lexer;
            StringBuilder     text;
            int tokenIndex;

            UiExceptionHelper.CheckNotNull(csharp, "csharp");

            csharp = PreProcess(csharp);

            lexer = new Lexer();
            lexer.Parse(csharp);

            classifier = new TokenClassifier();
            text       = new StringBuilder();
            csToken    = null;
            tokenIndex = 0;

            // loop through each token in the text
            while (lexer.Next())
            {
                // classify the current token
                tag = classifier.Classify(lexer.CurrentToken);

                // if the tag cannot be merged with current csToken
                // we flush csToken into _info and build a new instance
                // from the current tag.
                if (csToken == null ||
                    !csToken.CanMerge(_info.LineArray.Count, tag))
                {
                    _flushToken(csToken, _info);
                    csToken = new ConcreteToken(
                        lexer.CurrentToken.Text, tag,
                        lexer.CurrentToken.IndexStart,
                        _info.LineArray.Count);
                }

                // append token's text into text
                text.Append(lexer.CurrentToken.Text);

                // handle newline character. Appends tokenIndex to LineArray
                // and set tokenIndex to the start of the newline.
                if (lexer.CurrentToken.Text == "\n")
                {
                    _info.LineArray.Add(tokenIndex);
                    tokenIndex = _info.IndexArray.Count + 1;
                }
            }

            // flush terminal token
            _flushToken(csToken, _info);

            if (csToken != null &&
                _info.LineArray.Count == 0)
            {
                _info.LineArray.Add(tokenIndex);
            }

            _info.Text = csharp;

            return;
        }
        public void Test_Classify_As_Keyword()
        {
            TokenClassifier classifier;
            ClassificationTag result;
            string error;
            Lexer lexer;

            lexer = new Lexer();
            lexer.Parse(
                "abstract event new struct as explicit null switch " +
                "base extern object this bool false operator throw " +
                "break finally out true byte fixed override try case " +
                "float params typeof catch for private uint char " +
                "foreach protected ulong checked goto public unchecked " +
                "class if readonly unsafe const implicit ref ushort " +
                "continue in return using decimal int sbyte virtual " +
                "default interface sealed volatile delegate internal " +
                "short void do is sizeof while double lock stackalloc " +
                "else long static enum namespace string get set region " +
                "endregion ");

            classifier = new TokenClassifier();

            while (lexer.Next())
            {
                if (lexer.CurrentToken.Text.Trim() == "")
                    continue;

                result = classifier.Classify(lexer.CurrentToken);

                error = String.Format("Classification: [{0}] was expected for token [{1}] but [{2}] was returned.",
                    ClassificationTag.Keyword,
                    lexer.CurrentToken,
                    result);

                Assert.That(
                    result,
                    Is.EqualTo(ClassificationTag.Keyword),
                    error);
            }

            return;
        }
예제 #3
0
        /// <summary>
        /// Analyzes the input text as C# code. This method doesn't return anything.
        /// Callers may retrieve the result of this process by querying the FormattedCode property.
        ///   Passing null results in raising an exception.
        /// </summary>
        /// <param name="csharp">The text to be analyzed.</param>
        protected void Parse(string csharp)
        {
            TokenClassifier classifier;
            ConcreteToken csToken;
            ClassificationTag tag;
            Lexer lexer;
            StringBuilder text;
            int tokenIndex;

            UiExceptionHelper.CheckNotNull(csharp, "csharp");

            csharp = PreProcess(csharp);

            lexer = new Lexer();
            lexer.Parse(csharp);

            classifier = new TokenClassifier();
            text = new StringBuilder();
            csToken = null;
            tokenIndex = 0;

            // loop through each token in the text
            while (lexer.Next())
            {
                // classify the current token 
                tag = classifier.Classify(lexer.CurrentToken);

                // if the tag cannot be merged with current csToken
                // we flush csToken into _info and build a new instance
                // from the current tag.
                if (csToken == null ||
                    !csToken.CanMerge(_info.LineArray.Count, tag))
                {
                    _flushToken(csToken, _info);
                    csToken = new ConcreteToken(
                        lexer.CurrentToken.Text, tag,
                        lexer.CurrentToken.IndexStart,
                        _info.LineArray.Count);
                }

                // append token's text into text
                text.Append(lexer.CurrentToken.Text);

                // handle newline character. Appends tokenIndex to LineArray
                // and set tokenIndex to the start of the newline.
                if (lexer.CurrentToken.Text == "\n")
                {
                    _info.LineArray.Add(tokenIndex);
                    tokenIndex = _info.IndexArray.Count + 1;
                }
            }

            // flush terminal token
            _flushToken(csToken, _info);

            if (csToken != null &&
                _info.LineArray.Count == 0)
                _info.LineArray.Add(tokenIndex);

            _info.Text = csharp;

            return;
        }