Example #1
0
 /// <summary>
 /// Construct the annotation with the given range
 /// </summary>
 /// <param name="Directive">The type of this directive</param>
 /// <param name="Location">The start location</param>
 /// <param name="EndLocation">The end location</param>
 /// <param name="Tokens">List of tokens</param>
 public PreprocessorMarkup(PreprocessorMarkupType Directive, TextLocation Location, TextLocation EndLocation, List <Token> Tokens)
 {
     this.Type        = Directive;
     this.Location    = Location;
     this.EndLocation = EndLocation;
     this.Tokens      = Tokens;
 }
Example #2
0
        /// <summary>
        /// Create markup for the given text buffer
        /// </summary>
        /// <param name="Reader">Reader for token objects</param>
        /// <returns>Array of markup objects which split up the given text buffer</returns>
        public static PreprocessorMarkup[] ParseArray(TextBuffer Text)
        {
            TokenReader Reader = new TokenReader(Text, TextLocation.Origin);

            List <PreprocessorMarkup> Markup = new List <PreprocessorMarkup>();

            if (Reader.MoveNext())
            {
                bool bMoveNext = true;
                while (bMoveNext)
                {
                    TextLocation StartLocation = Reader.TokenWhitespaceLocation;
                    if (Reader.Current.Text == "#")
                    {
                        // Create the appropriate markup object for the directive
                        PreprocessorMarkupType Type = PreprocessorMarkupType.OtherDirective;
                        if (Reader.MoveNext())
                        {
                            switch (Reader.Current.Text)
                            {
                            case "include":
                                Type = PreprocessorMarkupType.Include;
                                break;

                            case "define":
                                Type = PreprocessorMarkupType.Define;
                                break;

                            case "undef":
                                Type = PreprocessorMarkupType.Undef;
                                break;

                            case "if":
                                Type = PreprocessorMarkupType.If;
                                break;

                            case "ifdef":
                                Type = PreprocessorMarkupType.Ifdef;
                                break;

                            case "ifndef":
                                Type = PreprocessorMarkupType.Ifndef;
                                break;

                            case "elif":
                                Type = PreprocessorMarkupType.Elif;
                                break;

                            case "else":
                                Type = PreprocessorMarkupType.Else;
                                break;

                            case "endif":
                                Type = PreprocessorMarkupType.Endif;
                                break;

                            case "pragma":
                                Type = PreprocessorMarkupType.Pragma;
                                break;

                            case "error":
                                Type = PreprocessorMarkupType.Error;
                                break;

                            case "\n":
                                Type = PreprocessorMarkupType.Empty;
                                break;
                            }
                        }

                        // Create the token list
                        List <Token> Tokens = new List <Token>();
                        if (Type == PreprocessorMarkupType.OtherDirective)
                        {
                            Tokens.Add(Reader.Current);
                        }

                        // Read the first token
                        if (Type == PreprocessorMarkupType.Empty)
                        {
                            bMoveNext = true;
                        }
                        else if (Type == PreprocessorMarkupType.Include)
                        {
                            bMoveNext = Reader.MoveNext(TokenReaderContext.IncludeDirective);
                        }
                        else if (Type == PreprocessorMarkupType.Error)
                        {
                            bMoveNext = Reader.MoveNext(TokenReaderContext.TokenString);
                        }
                        else
                        {
                            bMoveNext = Reader.MoveNext();
                        }

                        // Read the rest of the tokens
                        while (bMoveNext && Reader.Current.Text != "\n")
                        {
                            Tokens.Add(Reader.Current);
                            bMoveNext = Reader.MoveNext();
                        }

                        // Create the markup
                        Markup.Add(new PreprocessorMarkup(Type, StartLocation, Reader.TokenEndLocation, Tokens));

                        // Move to the next token
                        bMoveNext = Reader.MoveNext();
                    }
                    else if (Reader.Current.Text != "\n")
                    {
                        // Skip over as many parser tokens as possible before the next directive (or EOF)
                        bMoveNext = Reader.MoveToNextLine();
                        while (bMoveNext && Reader.Current.Text != "#")
                        {
                            bMoveNext = Reader.MoveToNextLine();
                        }

                        // Create the new fragment
                        PreprocessorMarkupType Type = IsIncludeMarkup(Text, StartLocation, Reader.TokenLocation)? PreprocessorMarkupType.IncludeMarkup : PreprocessorMarkupType.Text;
                        Markup.Add(new PreprocessorMarkup(Type, StartLocation, Reader.TokenLocation, null));
                    }
                    else
                    {
                        // Skip the empty line
                        bMoveNext = Reader.MoveNext();
                    }
                }
            }
            return(Markup.ToArray());
        }