/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor.Highlighting/ScopeStack.cs

https://github.com/directhex/monodevelop · C# · 110 lines · 67 code · 13 blank · 30 comment · 2 complexity · 48dc23d4aacb1418670b701c03d0a4d0 MD5 · raw file

  1. //
  2. // ScopeStack.cs
  3. //
  4. // Author:
  5. // Mike Krüger <mikkrg@microsoft.com>
  6. //
  7. // Copyright (c) 2016 Microsoft Corporation
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Collections.Immutable;
  28. using System.Diagnostics.Contracts;
  29. using System.Collections.Generic;
  30. using System.Collections;
  31. namespace MonoDevelop.Ide.Editor.Highlighting
  32. {
  33. public sealed class ScopeStack : IEnumerable<string>
  34. {
  35. public static readonly ScopeStack Empty = new ScopeStack (null, ImmutableStack<string>.Empty, 0, null);
  36. public string FirstElement {
  37. get;
  38. private set;
  39. }
  40. public bool IsEmpty {
  41. get {
  42. return stack.IsEmpty;
  43. }
  44. }
  45. public int Count {
  46. get;
  47. private set;
  48. }
  49. ImmutableStack<string> stack;
  50. ScopeStack parent;
  51. public ScopeStack (string first)
  52. {
  53. FirstElement = first;
  54. stack = ImmutableStack<string>.Empty.Push (first);
  55. parent = ScopeStack.Empty;
  56. Count = 1;
  57. }
  58. // The reasoning for having a constructor which takes a parent is that we want to make allocations
  59. // only onthe less common operation - Push. Having the allocation of the parent ScopeStack happening
  60. // on Pop, rather than retaining the one on Push, would yield an allocation hot loop, given that:
  61. // We already allocate it once on push.
  62. // We pass the same scope stack multiple times.
  63. ScopeStack (string first, ImmutableStack<string> immutableStack, int count, ScopeStack parent)
  64. {
  65. this.FirstElement = first;
  66. this.stack = immutableStack;
  67. this.Count = count;
  68. this.parent = parent;
  69. }
  70. public ScopeStack Push (string item)
  71. {
  72. return new ScopeStack (FirstElement, stack.Push (item), Count + 1, this);
  73. }
  74. public ScopeStack Pop ()
  75. {
  76. if (parent == null)
  77. throw new InvalidOperationException ("ScopeStack is empty");
  78. return parent;
  79. }
  80. public string Peek ()
  81. {
  82. return stack.Peek ();
  83. }
  84. public ImmutableStack<string>.Enumerator GetEnumerator ()
  85. {
  86. return stack.GetEnumerator ();
  87. }
  88. IEnumerator<string> IEnumerable<string>.GetEnumerator ()
  89. {
  90. return ((IEnumerable<string>)stack).GetEnumerator ();
  91. }
  92. IEnumerator IEnumerable.GetEnumerator ()
  93. {
  94. return ((IEnumerable<string>)stack).GetEnumerator ();
  95. }
  96. }
  97. }