/
ParseOptions.cs
179 lines (150 loc) · 6.62 KB
/
ParseOptions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// 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;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Linq;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// Represents parse options common to C# and VB.
/// </summary>
public abstract class ParseOptions
{
private readonly Lazy<ImmutableArray<Diagnostic>> _lazyErrors;
/// <summary>
/// Specifies whether to parse as regular code files, script files or interactive code.
/// </summary>
public SourceCodeKind Kind { get; protected set; }
/// <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; }
/// <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)
{
this.SpecifiedKind = kind;
this.Kind = kind.MapSpecifiedToEffectiveKind();
this.DocumentationMode = documentationMode;
_lazyErrors = new Lazy<ImmutableArray<Diagnostic>>(() =>
{
var builder = ArrayBuilder<Diagnostic>.GetInstance();
ValidateOptions(builder);
return builder.ToImmutableAndFree();
});
}
/// <summary>
/// Gets the source language ("C#" or "Visual Basic").
/// </summary>
public abstract string Language { get; }
/// <summary>
/// Errors collection related to an incompatible set of parse options
/// </summary>
public ImmutableArray<Diagnostic> Errors
{
get { return _lazyErrors.Value; }
}
/// <summary>
/// Creates a new options instance with the specified source code kind.
/// </summary>
public ParseOptions WithKind(SourceCodeKind kind)
{
return CommonWithKind(kind);
}
/// <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())
{
builder.Add(messageProvider.CreateDiagnostic(messageProvider.ERR_BadSourceCodeKind, Location.None, SpecifiedKind.ToString()));
}
if (!DocumentationMode.IsValid())
{
builder.Add(messageProvider.CreateDiagnostic(messageProvider.ERR_BadDocumentationMode, Location.None, DocumentationMode.ToString()));
}
}
// 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,
// and that forwards to the protected Common* like all the other methods in the class.
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract ParseOptions CommonWithKind(SourceCodeKind kind);
/// <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
this.SpecifiedKind == other.SpecifiedKind &&
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
Hash.Combine((int)this.SpecifiedKind,
Hash.Combine((int)this.DocumentationMode,
Hash.Combine(HashFeatures(this.Features),
Hash.Combine(Hash.CombineValues(this.PreprocessorSymbolNames, StringComparer.Ordinal), 0))));
}
private static int HashFeatures(IReadOnlyDictionary<string, string> features)
{
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);
}
}
}