示例#1
0
        /// <summary>
        /// Pushes a region onto the region stack.
        /// </summary>
        /// <param name="region">The region to add.</param>
        public void PushRegion(RegionDirective region)
        {
            Param.AssertNotNull(region, "region");

            this.regions.Push(region);
            if (region.IsGeneratedCodeRegion)
            {
                this.IncrementGeneratedCodeBlocks();
            }
        }
示例#2
0
        /// <summary>
        /// Pops a region from the region stack.
        /// </summary>
        /// <returns>Returns the popped region.</returns>
        public RegionDirective PopRegion()
        {
            if (this.regions.Count > 0)
            {
                RegionDirective region = this.regions.Pop();
                if (region != null && region.IsGeneratedCodeRegion)
                {
                    this.DecrementGeneratedCodeBlocks();
                }

                return(region);
            }

            return(null);
        }
示例#3
0
        /// <summary>
        /// Pushes a region onto the region stack.
        /// </summary>
        /// <param name="region">The region to add.</param>
        public void PushRegion(RegionDirective region)
        {
            Param.AssertNotNull(region, "region");

            this.regions.Push(region);
            if (region.IsGeneratedCodeRegion)
            {
                this.IncrementGeneratedCodeBlocks();
            }
        }
示例#4
0
        /// <summary>
        /// Checks a region.
        /// </summary>
        /// <param name="region">The region to check.</param>
        /// <param name="parentElement">The parent element.</param>
        /// <param name="settings">The current settings.</param>
        private void CheckRegion(RegionDirective region, Element parentElement, Settings settings)
        {
            Param.AssertNotNull(region, "region");
            Param.AssertNotNull(parentElement, "parentElement");
            Param.AssertNotNull(settings, "settings");

            if (settings.DoNotUseRegions)
            {
                if (!region.Generated && !region.IsGeneratedCodeRegion)
                {
                    // There should not be any regions in the code.
                    this.AddViolation(parentElement, region.LineNumber, Rules.DoNotUseRegions);
                }
            }
        }
示例#5
0
        /// <summary>
        /// Gets and converts preprocessor directive.
        /// </summary>
        /// <param name="preprocessorSymbol">The preprocessor symbol.</param>
        /// <param name="generated">Indicates whether the preprocessor directive lies within a block of generated code.</param>
        /// <returns>Returns the preprocessor directive.</returns>
        private PreprocessorDirective PeekPreprocessorDirective(Symbol preprocessorSymbol, bool generated)
        {
            Param.AssertNotNull(preprocessorSymbol, "preprocessorSymbol");
            Param.Ignore(generated);

            CsLanguageService.Debug.Assert(preprocessorSymbol != null && preprocessorSymbol.SymbolType == SymbolType.PreprocessorDirective, "Expected a preprocessor directive");

            // Get the type of the preprocessor directive.
            int bodyIndex;
            string type = CsLanguageService.GetPreprocessorDirectiveType(preprocessorSymbol, out bodyIndex);
            if (type == null)
            {
                throw new SyntaxException(this.document, preprocessorSymbol.LineNumber);
            }

            // Create the correct preprocessor object type.
            PreprocessorDirective preprocessor = null;
            if (type == "region")
            {
                var region = new RegionDirective(this.document, preprocessorSymbol.Text, preprocessorSymbol.Location, generated);
                this.symbols.PushRegion(region);
                preprocessor = region;
            }
            else if (type == "endregion")
            {
                var endregion = new EndRegionDirective(this.document, preprocessorSymbol.Text, preprocessorSymbol.Location, generated);
                RegionDirective startregion = this.symbols.PopRegion();

                if (startregion == null)
                {
                    throw new SyntaxException(this.document, preprocessorSymbol.LineNumber);
                }

                startregion.Partner = endregion;
                endregion.Partner = startregion;

                preprocessor = endregion;
            }
            else if (type == "if")
            {
                preprocessor = this.GetConditionalCompilationDirective(preprocessorSymbol, PreprocessorType.If, bodyIndex);
            }
            else if (type == "elif")
            {
                preprocessor = this.GetConditionalCompilationDirective(preprocessorSymbol, PreprocessorType.Elif, bodyIndex);
            }
            else if (type == "else")
            {
                preprocessor = this.GetConditionalCompilationDirective(preprocessorSymbol, PreprocessorType.Else, bodyIndex);
            }
            else if (type == "endif")
            {
                preprocessor = this.GetConditionalCompilationDirective(preprocessorSymbol, PreprocessorType.Endif, bodyIndex);
            }
            else if (type == "pragma")
            {
                preprocessor = new PragmaDirective(this.document, preprocessorSymbol.Text, preprocessorSymbol.Location, generated);
            }
            else if (type == "define")
            {
                preprocessor = new DefineDirective(this.document, preprocessorSymbol.Text, preprocessorSymbol.Location, generated);
            }
            else if (type == "undef")
            {
                preprocessor = new UndefDirective(this.document, preprocessorSymbol.Text, preprocessorSymbol.Location, generated);
            }
            else if (type == "line")
            {
                preprocessor = new LineDirective(this.document, preprocessorSymbol.Text, preprocessorSymbol.Location, generated);
            }
            else if (type == "error")
            {
                preprocessor = new ErrorDirective(this.document, preprocessorSymbol.Text, preprocessorSymbol.Location, generated);
            }
            else if (type == "warning")
            {
                preprocessor = new WarningDirective(this.document, preprocessorSymbol.Text, preprocessorSymbol.Location, generated);
            }
            else
            {
                throw this.CreateSyntaxException();                
            }

            return preprocessor;
        }
示例#6
0
        /// <summary>
        /// Updates the regions, generated code status, and locations of items in the document.
        /// </summary>
        internal void Update()
        {
            int lineNumber      = 1;
            int indexOnLine     = 0;
            int indexInDocument = 0;

            int generatedCount = 0;
            Stack <RegionDirective> regionStack = new Stack <RegionDirective>();

            // If the document is marked as generated, then treat everything in the document as generated.
            if (this.Generated)
            {
                ++generatedCount;
            }

            for (LexicalElement lex = this.FindFirstLexicalElement(); lex != null; lex = lex.FindNextLexicalElement())
            {
                if (!lex.Parent.Is(CodeUnitType.LexicalElement))
                {
                    lex.Location = CalculateLocation(lex, ref lineNumber, ref indexOnLine, ref indexInDocument);
                }

                if (lex.Children.LexicalElementCount > 0)
                {
                    SetLexicalElementChildLocations(lex);
                }

                lex.Generated = generatedCount > 0;

                if (lex.Is(PreprocessorType.Region))
                {
                    RegionDirective region = (RegionDirective)lex;
                    regionStack.Push(region);

                    if (region.IsGeneratedCodeRegion)
                    {
                        ++generatedCount;
                    }
                }
                else if (lex.Is(PreprocessorType.EndRegion))
                {
                    EndRegionDirective endRegion = (EndRegionDirective)lex;

                    if (regionStack.Count == 0)
                    {
                        throw new SyntaxException(this.Document, endRegion.LineNumber, Strings.NoMatchingRegion);
                    }

                    RegionDirective region = regionStack.Pop();

                    endRegion.Partner = region;
                    region.Partner    = endRegion;

                    if (region.IsGeneratedCodeRegion)
                    {
                        --generatedCount;
                    }
                }
            }

            if (regionStack.Count > 0)
            {
                throw new SyntaxException(this.Document, this.LineNumber, Strings.NoMatchingEndregion);
            }
        }