Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Resolving Symbolic References in a CodeDOM (Part 7)

, 2 Dec 2012 CDDL
Resolving symbolic references in a CodeDOM.
Nova.0.6.exe.zip
Mono.Cecil.dll
Nova.CLI.exe
Nova.CodeDOM.dll
Nova.Examples.exe
Nova.Studio.exe
Nova.Test.exe
Nova.UI.dll
Nova.0.6.zip
Nova.CLI
Properties
Nova.CodeDOM
CodeDOM
Annotations
Base
Comments
Base
DocComments
CodeRef
Base
List
Name
Base
Other
Simple
CompilerDirectives
Base
Conditionals
Base
Messages
Base
Pragmas
Base
Symbols
Base
Base
Interfaces
Expressions
AnonymousMethods
Base
Operators
Base
Binary
Arithmetic
Base
Assignment
Base
Bitwise
Base
Conditional
Relational
Base
Shift
Base
Other
Base
Unary
Base
Other
References
Base
GotoTargets
Base
Methods
Namespaces
Other
Properties
Types
Base
Variables
Base
Projects
Assemblies
Namespaces
References
Base
Statements
Base
Conditionals
Base
Exceptions
Generics
Constraints
Base
Iterators
Base
Jumps
Loops
Methods
OperatorDecls
Miscellaneous
Namespaces
Properties
Base
Events
Types
Base
Variables
Base
Parsing
Base
Properties
Rendering
Resolving
Utilities
Mono.Cecil
Reflection
Nova.Examples
Properties
Nova.Studio
Images
About.png
Configuration.png
EditCopy.png
EditCut.png
EditDelete.png
EditPaste.png
EditRedo.png
EditUndo.png
Error.png
Exit.png
FileNew.png
FileOpen.png
FileSave.png
FileSaveAll.png
FileSaveAs.png
Find.png
Help.png
Info.png
Logo.png
Options.png
Print.png
PrintPreview.png
Properties.png
Todo.png
Warning.png
Objects.ico
Properties
Settings.settings
Nova.Test
Properties
Nova.UI
CodeDOM
Annotations
Base
Comments
Base
DocComments
CodeRef
Base
List
Name
Base
Other
Simple
CompilerDirectives
Base
Conditionals
Base
Messages
Base
Pragmas
Base
Symbols
Base
Base
Expressions
AnonymousMethods
Base
Operators
Base
Binary
Arithmetic
Base
Assignment
Base
Bitwise
Base
Conditional
Relational
Base
Shift
Base
Other
Base
Unary
Base
Other
References
Base
GotoTargets
Base
Methods
Namespaces
Other
Properties
Types
Base
Variables
Base
Projects
Namespaces
References
Base
Statements
Base
Conditionals
Base
Exceptions
Generics
Constraints
Base
Iterators
Base
Jumps
Loops
Methods
OperatorDecls
Miscellaneous
Namespaces
Properties
Base
Events
Types
Base
Variables
Base
Properties
Resolving
Utilties
// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;

using Nova.Parsing;
using Nova.Rendering;

namespace Nova.CodeDOM
{
    /// <summary>
    /// The action of a <see cref="PragmaWarningDirective"/>.
    /// </summary>
    public enum PragmaWarningAction { Invalid, Disable, Restore }

    /// <summary>
    /// Used to turn warnings on or off.
    /// </summary>
    public class PragmaWarningDirective : PragmaDirective
    {
        #region /* FIELDS */

        protected PragmaWarningAction _pragmaWarningAction;
        protected List<int> _warningNumbers;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a <see cref="PragmaWarningDirective"/> with the specified action.
        /// </summary>
        public PragmaWarningDirective(PragmaWarningAction pragmaWarningAction)
        {
            _pragmaWarningAction = pragmaWarningAction;
        }

        /// <summary>
        /// Create a <see cref="PragmaWarningDirective"/> with the specified action and warning numbers.
        /// </summary>
        public PragmaWarningDirective(PragmaWarningAction pragmaWarningAction, params int[] warningNumbers)
            : this(pragmaWarningAction)
        {
            CreateNumbers().AddRange(warningNumbers);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The associated <see cref="PragmaWarningAction"/>.
        /// </summary>
        public PragmaWarningAction PragmaWarningAction
        {
            get { return _pragmaWarningAction; }
            set { _pragmaWarningAction = value; }
        }

        /// <summary>
        /// The associated warning numbers.
        /// </summary>
        public List<int> WarningNumbers
        {
            get { return _warningNumbers; }
        }

        /// <summary>
        /// True if there are any warning numbers.
        /// </summary>
        public bool HasWarningNumbers
        {
            get { return (_warningNumbers != null && _warningNumbers.Count > 0); }
        }

        public override string PragmaType { get { return ParseToken; } }

        #endregion

        #region /* METHODS */

        /// <summary>
        /// Create the list of warning numbers, or return the existing one.
        /// </summary>
        public List<int> CreateNumbers()
        {
            if (_warningNumbers == null)
                _warningNumbers = new List<int>();
            return _warningNumbers;
        }

        /// <summary>
        /// Deep-clone the code object.
        /// </summary>
        public override CodeObject Clone()
        {
            PragmaWarningDirective clone = (PragmaWarningDirective)base.Clone();
            if (_warningNumbers != null && _warningNumbers.Count > 0)
                clone._warningNumbers = new List<int>(_warningNumbers);
            return clone;
        }

        #endregion

        #region /* PARSING */

        /// <summary>
        /// The token used to parse the code object.
        /// </summary>
        public new const string ParseToken = "warning";

        /// <summary>
        /// The token used to parse the 'disable' action.
        /// </summary>
        public const string ParseTokenDisable = "disable";

        /// <summary>
        /// The token used to parse the 'restore' action.
        /// </summary>
        public const string ParseTokenRestore = "restore";

        /// <summary>
        /// The token used to separate warning numbers.
        /// </summary>
        public const string ParseTokenSeparator = Expression.ParseTokenSeparator;

        internal static new void AddParsePoints()
        {
            AddPragmaParsePoint(ParseToken, Parse);
        }

        /// <summary>
        /// Parse a <see cref="PragmaWarningDirective"/>.
        /// </summary>
        public static new PragmaWarningDirective Parse(Parser parser, CodeObject parent, ParseFlags flags)
        {
            return new PragmaWarningDirective(parser, parent);
        }

        /// <summary>
        /// Parse a <see cref="PragmaWarningDirective"/>.
        /// </summary>
        public PragmaWarningDirective(Parser parser, CodeObject parent)
            : base(parser, parent)
        {
            parser.NextToken();  // Move past 'pragma'
            Token token = parser.NextTokenSameLine(false);  // Move past 'warning'
            if (token != null)
            {
                // Parse the warning action
                _pragmaWarningAction = ParseAction(parser.TokenText);
                if (_pragmaWarningAction != PragmaWarningAction.Invalid)
                {
                    token = parser.NextTokenSameLine(false);  // Move past the action

                    // Parse the list of warning numbers
                    while (token != null && token.IsNumeric)
                    {
                        int number;
                        if (!int.TryParse(token.Text, out number))
                        {
                            number = int.MaxValue;
                            parser.AttachMessage(this, "Integer value expected", token);
                        }
                        CreateNumbers().Add(number);
                        token = parser.NextTokenSameLine(false);  // Move past the number
                        if (token != null && token.Text == ParseTokenSeparator)
                            token = parser.NextTokenSameLine(false);  // Move past ','
                        else
                            break;
                    }
                }
            }
            MoveEOLComment(parser.LastToken);
        }

        protected static PragmaWarningAction ParseAction(string actionName)
        {
            PragmaWarningAction action;
            switch (actionName)
            {
                case ParseTokenDisable: action = PragmaWarningAction.Disable; break;
                case ParseTokenRestore: action = PragmaWarningAction.Restore; break;
                default:                action = PragmaWarningAction.Invalid; break;
            }
            return action;
        }

        #endregion

        #region /* RENDERING */

        /// <summary>
        /// Format a <see cref="PragmaWarningAction"/> as a string.
        /// </summary>
        public static string PragmaWarningActionToString(PragmaWarningAction pragmaWarningAction)
        {
            switch (pragmaWarningAction)
            {
                case PragmaWarningAction.Disable: return ParseTokenDisable;
                case PragmaWarningAction.Restore: return ParseTokenRestore;
            }
            return "";
        }

        protected override void AsTextArgument(CodeWriter writer, RenderFlags flags)
        {
            base.AsTextArgument(writer, flags);
            writer.Write(" " + PragmaWarningActionToString(_pragmaWarningAction));
            if (HasWarningNumbers)
            {
                writer.Write(" ");
                bool first = true;
                foreach (int number in _warningNumbers)
                {
                    if (!first)
                        writer.Write(ParseTokenSeparator + " ");
                    writer.Write(number.ToString());
                    first = false;
                }
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)

Share

About the Author

KenBeckett
Software Developer (Senior)
United States United States
I've been writing software since the late 70's, currently focusing mainly on C#.NET. I also like to travel around the world, and I own a Chocolate Factory (sadly, none of my employees are oompa loompas).

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 2 Dec 2012
Article Copyright 2012 by KenBeckett
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid