CSharpParseOptions.cs 8.3 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5 6 7 8 9 10 11 12 13

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp
{
    /// <summary>
    /// This class stores several source parsing related options and offers access to their values.
    /// </summary>
14
    public sealed class CSharpParseOptions : ParseOptions, IEquatable<CSharpParseOptions>
P
Pilchie 已提交
15 16 17 18
    {
        /// <summary>
        /// The default parse options.
        /// </summary>
19
        public static CSharpParseOptions Default { get; } = new CSharpParseOptions();
P
Pilchie 已提交
20

21 22
        private ImmutableDictionary<string, string> _features;

P
Pilchie 已提交
23 24 25
        /// <summary>
        /// Gets the language version.
        /// </summary>
26
        public LanguageVersion LanguageVersion { get; private set; }
P
Pilchie 已提交
27

28
        internal ImmutableArray<string> PreprocessorSymbols { get; private set; }
P
Pilchie 已提交
29 30 31 32 33 34 35 36 37 38

        /// <summary>
        /// Gets the names of defined preprocessor symbols.
        /// </summary>
        public override IEnumerable<string> PreprocessorSymbolNames
        {
            get { return PreprocessorSymbols; }
        }

        public CSharpParseOptions(
39
            LanguageVersion languageVersion = LanguageVersion.Latest,
P
Pilchie 已提交
40 41
            DocumentationMode documentationMode = DocumentationMode.Parse,
            SourceCodeKind kind = SourceCodeKind.Regular,
42
            IEnumerable<string> preprocessorSymbols = null)
43
            : this(languageVersion.MapLatestToVersion(), documentationMode, kind, preprocessorSymbols.ToImmutableArrayOrEmpty())
P
Pilchie 已提交
44
        {
N
Neal Gafter 已提交
45
            // We test the mapped value, LanguageVersion, rather than the parameter, languageVersion,
46
            // which has not had "Latest" mapped to the latest version yet.
47
            if (!LanguageVersion.IsValid())
P
Pilchie 已提交
48
            {
49
                throw new ArgumentOutOfRangeException(nameof(languageVersion));
P
Pilchie 已提交
50 51 52 53
            }

            if (!kind.IsValid())
            {
54
                throw new ArgumentOutOfRangeException(nameof(kind));
P
Pilchie 已提交
55 56
            }

57
            if (preprocessorSymbols != null)
P
Pilchie 已提交
58 59 60 61 62 63 64 65 66 67 68
            {
                foreach (var preprocessorSymbol in preprocessorSymbols)
                {
                    if (!SyntaxFacts.IsValidIdentifier(preprocessorSymbol))
                    {
                        throw new ArgumentException("preprocessorSymbols");
                    }
                }
            }
        }

69 70 71 72 73 74 75 76 77 78 79 80 81
        internal CSharpParseOptions(
            LanguageVersion languageVersion,
            DocumentationMode documentationMode,
            SourceCodeKind kind,
            IEnumerable<string> preprocessorSymbols,
            ImmutableDictionary<string, string> features)
            : this(languageVersion, documentationMode, kind, preprocessorSymbols)
        {
            if (features == null)
            {
                throw new ArgumentNullException(nameof(features));
            }

J
Jared Parsons 已提交
82
            _features = features;
83 84
        }

85 86 87 88
        private CSharpParseOptions(CSharpParseOptions other) : this(
            languageVersion: other.LanguageVersion,
            documentationMode: other.DocumentationMode,
            kind: other.Kind,
A
Andy Gocke 已提交
89 90
            preprocessorSymbols: other.PreprocessorSymbols,
            features: other.Features.ToImmutableDictionary())
91 92 93
        {
        }

P
Pilchie 已提交
94 95 96 97 98
        // No validation
        internal CSharpParseOptions(
            LanguageVersion languageVersion,
            DocumentationMode documentationMode,
            SourceCodeKind kind,
99
            ImmutableArray<string> preprocessorSymbols)
P
Pilchie 已提交
100 101 102 103 104
            : base(kind, documentationMode)
        {
            Debug.Assert(!preprocessorSymbols.IsDefault);
            this.LanguageVersion = languageVersion;
            this.PreprocessorSymbols = preprocessorSymbols;
J
Jared Parsons 已提交
105
            _features = ImmutableDictionary<string, string>.Empty;
P
Pilchie 已提交
106 107
        }

C
Charles Stoner 已提交
108
        public new CSharpParseOptions WithKind(SourceCodeKind kind)
P
Pilchie 已提交
109 110 111 112 113 114 115 116
        {
            if (kind == this.Kind)
            {
                return this;
            }

            if (!kind.IsValid())
            {
117
                throw new ArgumentOutOfRangeException(nameof(kind));
P
Pilchie 已提交
118 119
            }

120
            return new CSharpParseOptions(this) { Kind = kind };
P
Pilchie 已提交
121 122 123 124
        }

        public CSharpParseOptions WithLanguageVersion(LanguageVersion version)
        {
125 126
            version = version.MapLatestToVersion();

P
Pilchie 已提交
127 128 129 130 131 132 133
            if (version == this.LanguageVersion)
            {
                return this;
            }

            if (!version.IsValid())
            {
134
                throw new ArgumentOutOfRangeException(nameof(version));
P
Pilchie 已提交
135 136
            }

137
            return new CSharpParseOptions(this) { LanguageVersion = version };
P
Pilchie 已提交
138 139 140 141 142 143 144 145 146
        }

        public CSharpParseOptions WithPreprocessorSymbols(IEnumerable<string> preprocessorSymbols)
        {
            return WithPreprocessorSymbols(preprocessorSymbols.AsImmutableOrNull());
        }

        public CSharpParseOptions WithPreprocessorSymbols(params string[] preprocessorSymbols)
        {
147
            return WithPreprocessorSymbols(ImmutableArray.Create(preprocessorSymbols));
P
Pilchie 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161
        }

        public CSharpParseOptions WithPreprocessorSymbols(ImmutableArray<string> symbols)
        {
            if (symbols.IsDefault)
            {
                symbols = ImmutableArray<string>.Empty;
            }

            if (symbols.Equals(this.PreprocessorSymbols))
            {
                return this;
            }

162
            return new CSharpParseOptions(this) { PreprocessorSymbols = symbols };
P
Pilchie 已提交
163 164 165 166 167 168 169 170 171 172 173
        }

        public new CSharpParseOptions WithDocumentationMode(DocumentationMode documentationMode)
        {
            if (documentationMode == this.DocumentationMode)
            {
                return this;
            }

            if (!documentationMode.IsValid())
            {
174
                throw new ArgumentOutOfRangeException(nameof(documentationMode));
P
Pilchie 已提交
175 176
            }

177 178 179
            return new CSharpParseOptions(this) { DocumentationMode = documentationMode };
        }

C
Charles Stoner 已提交
180
        public override ParseOptions CommonWithKind(SourceCodeKind kind)
181 182 183 184 185 186 187
        {
            return WithKind(kind);
        }

        protected override ParseOptions CommonWithDocumentationMode(DocumentationMode documentationMode)
        {
            return WithDocumentationMode(documentationMode);
P
Pilchie 已提交
188 189
        }

N
nmgafter 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        protected override ParseOptions CommonWithFeatures(IEnumerable<KeyValuePair<string, string>> features)
        {
            return WithFeatures(features);
        }

        /// <summary>
        /// Enable some experimental language features for testing.
        /// </summary>
        public new CSharpParseOptions WithFeatures(IEnumerable<KeyValuePair<string, string>> features)
        {
            if (features == null)
            {
                throw new ArgumentNullException(nameof(features));
            }

205
            return new CSharpParseOptions(this) { _features = features.ToImmutableDictionary(StringComparer.OrdinalIgnoreCase) };
N
nmgafter 已提交
206 207 208 209 210 211
        }

        public override IReadOnlyDictionary<string, string> Features
        {
            get
            {
212
                return _features;
N
nmgafter 已提交
213 214 215
            }
        }

216 217
        internal bool IsFeatureEnabled(MessageID feature)
        {
218 219 220 221 222
            string featureFlag = feature.RequiredFeature();
            if (featureFlag != null)
            {
                return Features.ContainsKey(featureFlag);
            }
223 224 225 226 227
            LanguageVersion availableVersion = LanguageVersion;
            LanguageVersion requiredVersion = feature.RequiredVersion();
            return availableVersion >= requiredVersion;
        }

P
Pilchie 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
        public override bool Equals(object obj)
        {
            return this.Equals(obj as CSharpParseOptions);
        }

        public bool Equals(CSharpParseOptions other)
        {
            if (object.ReferenceEquals(this, other))
            {
                return true;
            }

            if (!base.EqualsHelper(other))
            {
                return false;
            }

            return this.LanguageVersion == other.LanguageVersion;
        }

        public override int GetHashCode()
        {
            return
                Hash.Combine(base.GetHashCodeHelper(),
                Hash.Combine((int)this.LanguageVersion, 0));
        }
    }
255
}