CSharpParseOptions.cs 7.6 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 54 55 56 57 58 59
#if !SCRIPTING
            if (kind != SourceCodeKind.Regular)
            {
                throw new NotSupportedException(kind.ToString());
            }
#endif

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

72 73 74 75 76 77 78 79
        private CSharpParseOptions(CSharpParseOptions other) : this(
            languageVersion: other.LanguageVersion,
            documentationMode: other.DocumentationMode,
            kind: other.Kind,
            preprocessorSymbols: other.PreprocessorSymbols)
        {
        }

P
Pilchie 已提交
80 81 82 83 84
        // No validation
        internal CSharpParseOptions(
            LanguageVersion languageVersion,
            DocumentationMode documentationMode,
            SourceCodeKind kind,
85
            ImmutableArray<string> preprocessorSymbols)
P
Pilchie 已提交
86 87 88 89 90 91 92
            : base(kind, documentationMode)
        {
            Debug.Assert(!preprocessorSymbols.IsDefault);
            this.LanguageVersion = languageVersion;
            this.PreprocessorSymbols = preprocessorSymbols;
        }

93
        internal new CSharpParseOptions WithKind(SourceCodeKind kind)
P
Pilchie 已提交
94 95 96 97 98 99 100 101
        {
            if (kind == this.Kind)
            {
                return this;
            }

            if (!kind.IsValid())
            {
102
                throw new ArgumentOutOfRangeException(nameof(kind));
P
Pilchie 已提交
103 104
            }

105
            return new CSharpParseOptions(this) { Kind = kind };
P
Pilchie 已提交
106 107 108 109 110 111 112 113 114 115 116
        }

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

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

120
            return new CSharpParseOptions(this) { LanguageVersion = version };
P
Pilchie 已提交
121 122 123 124 125 126 127 128 129
        }

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

        public CSharpParseOptions WithPreprocessorSymbols(params string[] preprocessorSymbols)
        {
130
            return WithPreprocessorSymbols(ImmutableArray.Create(preprocessorSymbols));
P
Pilchie 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144
        }

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

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

145
            return new CSharpParseOptions(this) { PreprocessorSymbols = symbols };
P
Pilchie 已提交
146 147 148 149 150 151 152 153 154 155 156
        }

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

            if (!documentationMode.IsValid())
            {
157
                throw new ArgumentOutOfRangeException(nameof(documentationMode));
P
Pilchie 已提交
158 159
            }

160 161 162
            return new CSharpParseOptions(this) { DocumentationMode = documentationMode };
        }

163
        internal override ParseOptions CommonWithKind(SourceCodeKind kind)
164 165 166 167 168 169 170
        {
            return WithKind(kind);
        }

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

N
nmgafter 已提交
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 199 200 201 202 203 204 205
        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>();
            }
        }

206 207 208 209 210 211 212
        internal bool IsFeatureEnabled(MessageID feature)
        {
            LanguageVersion availableVersion = LanguageVersion;
            LanguageVersion requiredVersion = feature.RequiredVersion();
            return availableVersion >= requiredVersion;
        }

P
Pilchie 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        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));
        }
    }
240
}