-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathToken.cs
More file actions
158 lines (150 loc) · 5.54 KB
/
Token.cs
File metadata and controls
158 lines (150 loc) · 5.54 KB
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
using BytecodeApi.Extensions;
namespace BytecodeApi.Lexer;
/// <summary>
/// Represents a token that was parsed using a lexer.
/// </summary>
/// <typeparam name="TTokenType">The <see langword="enum" /> type of the token.</typeparam>
public sealed class Token<TTokenType> where TTokenType : struct, IConvertible
{
/// <summary>
/// Gets the one-based line number of this token.
/// </summary>
public int LineNumber { get; }
/// <summary>
/// Gets the type of this token.
/// </summary>
public TTokenType Type { get; }
/// <summary>
/// Gets a <see cref="string" /> with the value of this token.
/// </summary>
public string Value { get; }
/// <summary>
/// Initializes a new instance of the <see cref="Token{TTokenType}" /> class.
/// </summary>
/// <param name="lineNumber">The one-based line number of the token.</param>
/// <param name="type">The type of the token.</param>
/// <param name="value">A <see cref="string" /> with the value of the token.</param>
public Token(int lineNumber, TTokenType type, string value)
{
Check.ArgumentNull(value);
Check.ArgumentEx.StringNotEmpty(value);
LineNumber = lineNumber;
Type = type;
Value = value;
}
/// <summary>
/// Checks, if this token is of a given type.
/// </summary>
/// <param name="type">The type to check this token against.</param>
/// <returns>
/// <see langword="true" />, if this token is of the given type;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Is(TTokenType type)
{
return Type.Equals(type);
}
/// <summary>
/// Checks, if this token is of any of the given types.
/// </summary>
/// <param name="types">A collection of types to check this token against.</param>
/// <returns>
/// <see langword="true" />, if this token is of any of the given types;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Is(params TTokenType[] types)
{
Check.ArgumentNull(types);
Check.ArgumentEx.ArrayElementsRequired(types);
return types.Any(type => Type.Equals(type));
}
/// <summary>
/// Checks, if this token is of a given type and matches a given value.
/// </summary>
/// <param name="type">The type to check this token against.</param>
/// <param name="value">The value to match this token against.</param>
/// <returns>
/// <see langword="true" />, if this token is of the given type and matches the given value;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Is(TTokenType type, string value)
{
return Is(type, value, false);
}
/// <summary>
/// Checks, if this token is of a given type and matches a given value.
/// </summary>
/// <param name="type">The type to check this token against.</param>
/// <param name="value">The value to match this token against.</param>
/// <param name="ignoreCase"><see langword="true" /> to ignore character casing during value comparison.</param>
/// <returns>
/// <see langword="true" />, if this token is of the given type and matches the given value;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Is(TTokenType type, string value, bool ignoreCase)
{
return Type.Equals(type) && Value.Equals(value, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
}
/// <summary>
/// Checks, if this token matches a given value.
/// </summary>
/// <param name="value">The value to match this token against.</param>
/// <returns>
/// <see langword="true" />, if this token matches the given value;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Is(string value)
{
return Is(value, false);
}
/// <summary>
/// Checks, if this token matches a given value.
/// </summary>
/// <param name="value">The value to match this token against.</param>
/// <param name="ignoreCase"><see langword="true" /> to ignore character casing during value comparison.</param>
/// <returns>
/// <see langword="true" />, if this token matches the given value;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Is(string value, bool ignoreCase)
{
return Value.Equals(value, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
}
/// <summary>
/// Checks, if this token matches any of the given values.
/// </summary>
/// <param name="values">A collection of values to check this token against.</param>
/// <returns>
/// <see langword="true" />, if this token matches any of the given values;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Is(params string[] values)
{
return Is(values, false);
}
/// <summary>
/// Checks, if this token matches any of the given values.
/// </summary>
/// <param name="values">A collection of values to check this token against.</param>
/// <param name="ignoreCase"><see langword="true" /> to ignore character casing during value comparison.</param>
/// <returns>
/// <see langword="true" />, if this token matches any of the given values;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Is(string[] values, bool ignoreCase)
{
Check.ArgumentNull(values);
Check.ArgumentEx.ArrayElementsRequired(values);
return Value.EqualsAny(values, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
}
/// <summary>
/// Returns the value of this <see cref="Token{TTokenType}" />.
/// </summary>
/// <returns>
/// The value of this <see cref="Token{TTokenType}" />.
/// </returns>
public override string ToString()
{
return Value;
}
}