1 | // Copyright (c) 2011-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.Collections.Generic; |
---|
21 | using System.Linq; |
---|
22 | |
---|
23 | namespace ICSharpCode.NRefactory.PatternMatching |
---|
24 | { |
---|
25 | /// <summary> |
---|
26 | /// Represents the result of a pattern matching operation. |
---|
27 | /// </summary> |
---|
28 | public struct Match |
---|
29 | { |
---|
30 | // TODO: maybe we should add an implicit Match->bool conversion? (implicit operator bool(Match m) { return m != null; }) |
---|
31 | |
---|
32 | List<KeyValuePair<string, INode>> results; |
---|
33 | |
---|
34 | public bool Success { |
---|
35 | get { return results != null; } |
---|
36 | } |
---|
37 | |
---|
38 | internal static Match CreateNew() |
---|
39 | { |
---|
40 | Match m; |
---|
41 | m.results = new List<KeyValuePair<string, INode>>(); |
---|
42 | return m; |
---|
43 | } |
---|
44 | |
---|
45 | internal int CheckPoint() |
---|
46 | { |
---|
47 | return results.Count; |
---|
48 | } |
---|
49 | |
---|
50 | internal void RestoreCheckPoint(int checkPoint) |
---|
51 | { |
---|
52 | results.RemoveRange(checkPoint, results.Count - checkPoint); |
---|
53 | } |
---|
54 | |
---|
55 | public IEnumerable<INode> Get(string groupName) |
---|
56 | { |
---|
57 | if (results == null) |
---|
58 | yield break; |
---|
59 | foreach (var pair in results) { |
---|
60 | if (pair.Key == groupName) |
---|
61 | yield return pair.Value; |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | public IEnumerable<T> Get<T>(string groupName) where T : INode |
---|
66 | { |
---|
67 | if (results == null) |
---|
68 | yield break; |
---|
69 | foreach (var pair in results) { |
---|
70 | if (pair.Key == groupName) |
---|
71 | yield return (T)pair.Value; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | public bool Has(string groupName) |
---|
76 | { |
---|
77 | if (results == null) |
---|
78 | return false; |
---|
79 | foreach (var pair in results) { |
---|
80 | if (pair.Key == groupName) |
---|
81 | return true; |
---|
82 | } |
---|
83 | return false; |
---|
84 | } |
---|
85 | |
---|
86 | public void Add(string groupName, INode node) |
---|
87 | { |
---|
88 | if (groupName != null && node != null) { |
---|
89 | results.Add(new KeyValuePair<string, INode>(groupName, node)); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | internal void AddNull (string groupName) |
---|
94 | { |
---|
95 | if (groupName != null) { |
---|
96 | results.Add(new KeyValuePair<string, INode>(groupName, null)); |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|