-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathParserResult.cs
219 lines (190 loc) · 7.66 KB
/
ParserResult.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
namespace CommandLine
{
public sealed class TypeInfo
{
private readonly Type current;
private readonly IEnumerable<Type> choices;
private TypeInfo(Type current, IEnumerable<Type> choices)
{
this.current = current;
this.choices = choices;
}
public Type Current
{
get { return this.current; }
}
public IEnumerable<Type> Choices
{
get { return this.choices; }
}
internal static TypeInfo Create(Type current)
{
return new TypeInfo(current, Enumerable.Empty<Type>());
}
internal static TypeInfo Create(Type current, IEnumerable<Type> choices)
{
return new TypeInfo(current, choices);
}
}
/// <summary>
/// Discriminator enumeration of <see cref="CommandLine.ParserResultType"/> derivates.
/// </summary>
public enum ParserResultType
{
/// <summary>
/// Value of <see cref="CommandLine.Parsed{T}"/> type.
/// </summary>
Parsed,
/// <summary>
/// Value of <see cref="CommandLine.NotParsed{T}"/> type.
/// </summary>
NotParsed
}
/// <summary>
/// Models a parser result. When inherited by <see cref="CommandLine.Parsed{T}"/>, it contains an instance of type <typeparamref name="T"/>
/// with parsed values.
/// When inherited by <see cref="CommandLine.NotParsed{T}"/>, it contains a sequence of <see cref="CommandLine.Error"/>.
/// </summary>
/// <typeparam name="T">The type with attributes that define the syntax of parsing rules.</typeparam>
public abstract class ParserResult<T>
{
private readonly ParserResultType tag;
private readonly TypeInfo typeInfo;
internal ParserResult(ParserResultType tag, TypeInfo typeInfo)
{
this.tag = tag;
this.typeInfo = typeInfo;
}
/// <summary>
/// Parser result type discriminator, defined as <see cref="CommandLine.ParserResultType"/> enumeration.
/// </summary>
public ParserResultType Tag
{
get { return this.tag; }
}
public TypeInfo TypeInfo
{
get { return typeInfo; }
}
}
/// <summary>
/// It contains an instance of type <typeparamref name="T"/> with parsed values.
/// </summary>
/// <typeparam name="T">The type with attributes that define the syntax of parsing rules.</typeparam>
public sealed class Parsed<T> : ParserResult<T>, IEquatable<Parsed<T>>
{
private readonly T value;
internal Parsed(T value, TypeInfo typeInfo)
: base(ParserResultType.Parsed, typeInfo)
{
this.value = value;
}
internal Parsed(T value)
: this(value, TypeInfo.Create(value.GetType()))
{
}
/// <summary>
/// Gets the instance with parsed values.
/// </summary>
public T Value
{
get { return value; }
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with the current <see cref="System.Object"/>.</param>
/// <returns><value>true</value> if the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>; otherwise, <value>false</value>.</returns>
public override bool Equals(object obj)
{
var other = obj as Parsed<T>;
if (other != null)
{
return Equals(other);
}
return base.Equals(obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <remarks>A hash code for the current <see cref="System.Object"/>.</remarks>
public override int GetHashCode()
{
return new { Tag, Value }.GetHashCode();
}
/// <summary>
/// Returns a value that indicates whether the current instance and a specified <see cref="CommandLine.Parsed{T}"/> have the same value.
/// </summary>
/// <param name="other">The <see cref="CommandLine.Parsed{T}"/> instance to compare.</param>
/// <returns><value>true</value> if this instance of <see cref="CommandLine.Parsed{T}"/> and <paramref name="other"/> have the same value; otherwise, <value>false</value>.</returns>
public bool Equals(Parsed<T> other)
{
if (other == null)
{
return false;
}
return this.Tag.Equals(other.Tag)
&& Value.Equals(other.Value);
}
}
/// <summary>
/// It contains a sequence of <see cref="CommandLine.Error"/>.
/// </summary>
/// <typeparam name="T">The type with attributes that define the syntax of parsing rules.</typeparam>
public sealed class NotParsed<T> : ParserResult<T>, IEquatable<NotParsed<T>>
{
private readonly IEnumerable<Error> errors;
internal NotParsed(TypeInfo typeInfo, IEnumerable<Error> errors)
: base(ParserResultType.NotParsed, typeInfo)
{
this.errors = errors;
}
/// <summary>
/// Gets the sequence of parsing errors.
/// </summary>
public IEnumerable<Error> Errors
{
get { return errors; }
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with the current <see cref="System.Object"/>.</param>
/// <returns><value>true</value> if the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>; otherwise, <value>false</value>.</returns>
public override bool Equals(object obj)
{
var other = obj as NotParsed<T>;
if (other != null)
{
return Equals(other);
}
return base.Equals(obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <remarks>A hash code for the current <see cref="System.Object"/>.</remarks>
public override int GetHashCode()
{
return new { Tag, Errors }.GetHashCode();
}
/// <summary>
/// Returns a value that indicates whether the current instance and a specified <see cref="CommandLine.NotParsed{T}"/> have the same value.
/// </summary>
/// <param name="other">The <see cref="CommandLine.NotParsed{T}"/> instance to compare.</param>
/// <returns><value>true</value> if this instance of <see cref="CommandLine.NotParsed{T}"/> and <paramref name="other"/> have the same value; otherwise, <value>false</value>.</returns>
public bool Equals(NotParsed<T> other)
{
if (other == null)
{
return false;
}
return this.Tag.Equals(other.Tag)
&& Errors.SequenceEqual(other.Errors);
}
}
}