CSharpParseOptions.cs 7.4 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 19 20 21 22 23
    {
        /// <summary>
        /// The default parse options.
        /// </summary>
        public static readonly CSharpParseOptions Default = new CSharpParseOptions();

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

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

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

        public CSharpParseOptions(
            LanguageVersion languageVersion = LanguageVersion.CSharp6,
            DocumentationMode documentationMode = DocumentationMode.Parse,
            SourceCodeKind kind = SourceCodeKind.Regular,
40 41
            IEnumerable<string> preprocessorSymbols = null)
            : this(languageVersion, documentationMode, kind, preprocessorSymbols.ToImmutableArrayOrEmpty())
P
Pilchie 已提交
42 43 44
        {
            if (!languageVersion.IsValid())
            {
45
                throw new ArgumentOutOfRangeException(nameof(languageVersion));
P
Pilchie 已提交
46 47 48 49
            }

            if (!kind.IsValid())
            {
50
                throw new ArgumentOutOfRangeException(nameof(kind));
P
Pilchie 已提交
51 52
            }

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

65 66 67 68 69 70 71 72
        private CSharpParseOptions(CSharpParseOptions other) : this(
            languageVersion: other.LanguageVersion,
            documentationMode: other.DocumentationMode,
            kind: other.Kind,
            preprocessorSymbols: other.PreprocessorSymbols)
        {
        }

P
Pilchie 已提交
73 74 75 76 77
        // No validation
        internal CSharpParseOptions(
            LanguageVersion languageVersion,
            DocumentationMode documentationMode,
            SourceCodeKind kind,
78
            ImmutableArray<string> preprocessorSymbols)
P
Pilchie 已提交
79 80 81 82 83 84 85
            : base(kind, documentationMode)
        {
            Debug.Assert(!preprocessorSymbols.IsDefault);
            this.LanguageVersion = languageVersion;
            this.PreprocessorSymbols = preprocessorSymbols;
        }

C
Charles Stoner 已提交
86
        public new CSharpParseOptions WithKind(SourceCodeKind kind)
P
Pilchie 已提交
87 88 89 90 91 92 93 94
        {
            if (kind == this.Kind)
            {
                return this;
            }

            if (!kind.IsValid())
            {
95
                throw new ArgumentOutOfRangeException(nameof(kind));
P
Pilchie 已提交
96 97
            }

98
            return new CSharpParseOptions(this) { Kind = kind };
P
Pilchie 已提交
99 100 101 102 103 104 105 106 107 108 109
        }

        public CSharpParseOptions WithLanguageVersion(LanguageVersion version)
        {
            if (version == this.LanguageVersion)
            {
                return this;
            }

            if (!version.IsValid())
            {
110
                throw new ArgumentOutOfRangeException(nameof(version));
P
Pilchie 已提交
111 112
            }

113
            return new CSharpParseOptions(this) { LanguageVersion = version };
P
Pilchie 已提交
114 115 116 117 118 119 120 121 122
        }

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

        public CSharpParseOptions WithPreprocessorSymbols(params string[] preprocessorSymbols)
        {
123
            return WithPreprocessorSymbols(ImmutableArray.Create(preprocessorSymbols));
P
Pilchie 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137
        }

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

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

138
            return new CSharpParseOptions(this) { PreprocessorSymbols = symbols };
P
Pilchie 已提交
139 140 141 142 143 144 145 146 147 148 149
        }

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

            if (!documentationMode.IsValid())
            {
150
                throw new ArgumentOutOfRangeException(nameof(documentationMode));
P
Pilchie 已提交
151 152
            }

153 154 155
            return new CSharpParseOptions(this) { DocumentationMode = documentationMode };
        }

C
Charles Stoner 已提交
156
        public override ParseOptions CommonWithKind(SourceCodeKind kind)
157 158 159 160 161 162 163
        {
            return WithKind(kind);
        }

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

N
nmgafter 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
        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));
            }

            // there are currently no parse options for experimental features
            if (System.Linq.Enumerable.Any(features))
            {
                throw new ArgumentException("Experimental features are not supported", nameof(features));
            }

            return this;
        }

        public override IReadOnlyDictionary<string, string> Features
        {
            get
            {
                // there are currently no parse options for experimental features
                return new Dictionary<string, string>();
            }
        }

199 200 201 202 203 204 205
        internal bool IsFeatureEnabled(MessageID feature)
        {
            LanguageVersion availableVersion = LanguageVersion;
            LanguageVersion requiredVersion = feature.RequiredVersion();
            return availableVersion >= requiredVersion;
        }

P
Pilchie 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        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));
        }
    }
233
}