Click here to Skip to main content
15,896,111 members
Articles / Programming Languages / C#

Resolving Symbolic References in a CodeDOM (Part 7)

Rate me:
Please Sign up or sign in to vote.
4.75/5 (6 votes)
2 Dec 2012CDDL12 min read 19.4K   510   14  
Resolving symbolic references in a CodeDOM.
// 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)


Written By
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).

Comments and Discussions