ParseOptions.cs 6.6 KB
Newer Older
1 2 3 4
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
O
Omar Tawfik 已提交
5
using System.Collections.Immutable;
6
using System.ComponentModel;
7 8 9 10 11 12 13 14 15 16
using System.Linq;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis
{
    /// <summary>
    /// Represents parse options common to C# and VB.
    /// </summary>
    public abstract class ParseOptions
    {
O
Omar Tawfik 已提交
17 18
        private readonly Lazy<ImmutableArray<Diagnostic>> _lazyErrors;

19 20 21 22 23
        /// <summary>
        /// Specifies whether to parse as regular code files, script files or interactive code.
        /// </summary>
        public SourceCodeKind Kind { get; protected set; }

O
Omar Tawfik 已提交
24 25 26 27 28 29
        /// <summary>
        /// Gets the specified source code kind, which is the value that was specified in
        /// the call to the constructor, or modified using the <see cref="WithKind(SourceCodeKind)"/> method.
        /// </summary>
        public SourceCodeKind SpecifiedKind { get; protected set; }

30 31 32 33 34 35 36 37
        /// <summary>
        /// Gets a value indicating whether the documentation comments are parsed.
        /// </summary>
        /// <value><c>true</c> if documentation comments are parsed, <c>false</c> otherwise.</value>
        public DocumentationMode DocumentationMode { get; protected set; }

        internal ParseOptions(SourceCodeKind kind, DocumentationMode documentationMode)
        {
O
Omar Tawfik 已提交
38 39
            this.SpecifiedKind = kind;
            this.Kind = kind.MapSpecifiedToEffectiveKind();
40
            this.DocumentationMode = documentationMode;
O
Omar Tawfik 已提交
41 42 43 44 45 46 47

            _lazyErrors = new Lazy<ImmutableArray<Diagnostic>>(() =>
            {
                var builder = ArrayBuilder<Diagnostic>.GetInstance();
                ValidateOptions(builder);
                return builder.ToImmutableAndFree();
            });
48 49
        }

50 51 52 53 54
        /// <summary>
        /// Gets the source language ("C#" or "Visual Basic").
        /// </summary>
        public abstract string Language { get; }

O
Omar Tawfik 已提交
55 56 57 58 59 60 61 62
        /// <summary>
        /// Errors collection related to an incompatible set of parse options
        /// </summary>
        public ImmutableArray<Diagnostic> Errors
        {
            get { return _lazyErrors.Value; }
        }

63 64 65
        /// <summary>
        /// Creates a new options instance with the specified source code kind.
        /// </summary>
C
Charles Stoner 已提交
66
        public ParseOptions WithKind(SourceCodeKind kind)
67 68 69 70
        {
            return CommonWithKind(kind);
        }

O
Omar Tawfik 已提交
71 72 73 74 75 76 77 78 79 80
        /// <summary>
        /// Performs validation of options compatibilities and generates diagnostics if needed
        /// </summary>
        internal abstract void ValidateOptions(ArrayBuilder<Diagnostic> builder);

        internal void ValidateOptions(ArrayBuilder<Diagnostic> builder, CommonMessageProvider messageProvider)
        {
            // Validate SpecifiedKind not Kind, to catch deprecated specified kinds:
            if (!SpecifiedKind.IsValid())
            {
O
Omar Tawfik 已提交
81
                builder.Add(messageProvider.CreateDiagnostic(messageProvider.WRN_BadSourceCodeKind, Location.None, SpecifiedKind.ToString()));
O
Omar Tawfik 已提交
82 83 84 85
            }

            if (!DocumentationMode.IsValid())
            {
O
Omar Tawfik 已提交
86
                builder.Add(messageProvider.CreateDiagnostic(messageProvider.ERR_BadDocumentationMode, Location.None, DocumentationMode.ToString()));
O
Omar Tawfik 已提交
87 88 89
            }
        }

90 91
        // It was supposed to be a protected implementation detail. 
        // The "pattern" we have for these is the public With* method is the only public callable one, 
92
        // and that forwards to the protected Common* like all the other methods in the class. 
93
        [EditorBrowsable(EditorBrowsableState.Never)]
C
Charles Stoner 已提交
94
        public abstract ParseOptions CommonWithKind(SourceCodeKind kind);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

        /// <summary>
        /// Creates a new options instance with the specified documentation mode.
        /// </summary>
        public ParseOptions WithDocumentationMode(DocumentationMode documentationMode)
        {
            return CommonWithDocumentationMode(documentationMode);
        }

        protected abstract ParseOptions CommonWithDocumentationMode(DocumentationMode documentationMode);

        /// <summary>
        /// Enable some experimental language features for testing.
        /// </summary>
        public ParseOptions WithFeatures(IEnumerable<KeyValuePair<string, string>> features)
        {
            return CommonWithFeatures(features);
        }

        protected abstract ParseOptions CommonWithFeatures(IEnumerable<KeyValuePair<string, string>> features);

        /// <summary>
        /// Returns the experimental features.
        /// </summary>
        public abstract IReadOnlyDictionary<string, string> Features
        {
            get;
        }

        /// <summary>
        /// Names of defined preprocessor symbols.
        /// </summary>
        public abstract IEnumerable<string> PreprocessorSymbolNames { get; }

        public abstract override bool Equals(object obj);

        protected bool EqualsHelper(ParseOptions other)
        {
            if (object.ReferenceEquals(other, null))
            {
                return false;
            }

            return
O
Omar Tawfik 已提交
139
                this.SpecifiedKind == other.SpecifiedKind &&
140 141 142 143 144 145 146 147 148 149
                this.DocumentationMode == other.DocumentationMode &&
                this.Features.SequenceEqual(other.Features) &&
                (this.PreprocessorSymbolNames == null ? other.PreprocessorSymbolNames == null : this.PreprocessorSymbolNames.SequenceEqual(other.PreprocessorSymbolNames, StringComparer.Ordinal));
        }

        public abstract override int GetHashCode();

        protected int GetHashCodeHelper()
        {
            return
O
Omar Tawfik 已提交
150
                Hash.Combine((int)this.SpecifiedKind,
151 152 153 154 155
                Hash.Combine((int)this.DocumentationMode,
                Hash.Combine(HashFeatures(this.Features),
                Hash.Combine(Hash.CombineValues(this.PreprocessorSymbolNames, StringComparer.Ordinal), 0))));
        }

156
        private static int HashFeatures(IReadOnlyDictionary<string, string> features)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        {
            int value = 0;
            foreach (var kv in features)
            {
                value = Hash.Combine(kv.Key.GetHashCode(),
                        Hash.Combine(kv.Value.GetHashCode(), value));
            }

            return value;
        }

        public static bool operator ==(ParseOptions left, ParseOptions right)
        {
            return object.Equals(left, right);
        }

        public static bool operator !=(ParseOptions left, ParseOptions right)
        {
            return !object.Equals(left, right);
        }
    }
}