-
Notifications
You must be signed in to change notification settings - Fork 4k
/
ChildSyntaxList.Reversed.cs
163 lines (140 loc) · 5.49 KB
/
ChildSyntaxList.Reversed.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
public readonly partial struct ChildSyntaxList
{
public readonly partial struct Reversed : IEnumerable<SyntaxNodeOrToken>, IEquatable<Reversed>
{
private readonly SyntaxNode? _node;
private readonly int _count;
internal Reversed(SyntaxNode node, int count)
{
_node = node;
_count = count;
}
public Enumerator GetEnumerator()
{
Debug.Assert(_node is object);
return new Enumerator(_node, _count);
}
IEnumerator<SyntaxNodeOrToken> IEnumerable<SyntaxNodeOrToken>.GetEnumerator()
{
if (_node == null)
{
return SpecializedCollections.EmptyEnumerator<SyntaxNodeOrToken>();
}
return new EnumeratorImpl(_node, _count);
}
IEnumerator IEnumerable.GetEnumerator()
{
if (_node == null)
{
return SpecializedCollections.EmptyEnumerator<SyntaxNodeOrToken>();
}
return new EnumeratorImpl(_node, _count);
}
public override int GetHashCode()
{
return _node != null ? Hash.Combine(_node.GetHashCode(), _count) : 0;
}
public override bool Equals(object? obj)
{
return (obj is Reversed r) && Equals(r);
}
public bool Equals(Reversed other)
{
return _node == other._node
&& _count == other._count;
}
public struct Enumerator
{
private readonly SyntaxNode? _node;
private readonly int _count;
private int _childIndex;
internal Enumerator(SyntaxNode node, int count)
{
_node = node;
_count = count;
_childIndex = count;
}
[MemberNotNullWhen(true, nameof(_node))]
public bool MoveNext()
{
return --_childIndex >= 0;
}
public SyntaxNodeOrToken Current
{
get
{
Debug.Assert(_node is object);
return ItemInternal(_node, _childIndex);
}
}
public void Reset()
{
_childIndex = _count;
}
}
private class EnumeratorImpl : IEnumerator<SyntaxNodeOrToken>
{
private Enumerator _enumerator;
internal EnumeratorImpl(SyntaxNode node, int count)
{
_enumerator = new Enumerator(node, count);
}
/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
/// </summary>
/// <returns>
/// The element in the collection at the current position of the enumerator.
/// </returns>
public SyntaxNodeOrToken Current
{
get { return _enumerator.Current; }
}
/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
/// </summary>
/// <returns>
/// The element in the collection at the current position of the enumerator.
/// </returns>
object IEnumerator.Current
{
get { return _enumerator.Current; }
}
/// <summary>
/// Advances the enumerator to the next element of the collection.
/// </summary>
/// <returns>
/// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
/// </returns>
/// <exception cref="InvalidOperationException">The collection was modified after the enumerator was created. </exception>
public bool MoveNext()
{
return _enumerator.MoveNext();
}
/// <summary>
/// Sets the enumerator to its initial position, which is before the first element in the collection.
/// </summary>
/// <exception cref="InvalidOperationException">The collection was modified after the enumerator was created. </exception>
public void Reset()
{
_enumerator.Reset();
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{ }
}
}
}
}