SymbolList.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Lextm.SharpSnmpLib.Mib
  6. {
  7. public class SymbolList : List<Symbol>
  8. {
  9. public class SymbolEnumerator : ISymbolEnumerator
  10. {
  11. private SymbolList _list = null;
  12. private int _index = -1;
  13. internal SymbolEnumerator(SymbolList list)
  14. {
  15. if (list == null)
  16. {
  17. throw new ArgumentNullException("lexer");
  18. }
  19. _list = list;
  20. }
  21. #region ISymbolEnumerator Member
  22. public bool PutBack(Symbol item)
  23. {
  24. if ((_index < 0) || (_index >= _list.Count) || (item != _list[_index]))
  25. {
  26. throw new ArgumentException(@"wrong last symbol", "last");
  27. //return false;
  28. }
  29. _index--;
  30. return true;
  31. }
  32. #endregion
  33. #region IEnumerator<Symbol> Member
  34. public Symbol Current
  35. {
  36. get
  37. {
  38. if ((_index >= 0) && (_index <= _list.Count))
  39. {
  40. return _list[_index];
  41. }
  42. return null;
  43. }
  44. }
  45. #endregion
  46. #region IDisposable Member
  47. public void Dispose()
  48. {
  49. }
  50. #endregion
  51. #region IEnumerator Member
  52. object System.Collections.IEnumerator.Current
  53. {
  54. get { return this.Current; }
  55. }
  56. public bool MoveNext()
  57. {
  58. _index++;
  59. return (_index >= 0) && (_index < _list.Count);
  60. }
  61. public void Reset()
  62. {
  63. _index = -1;
  64. }
  65. #endregion
  66. }
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="SymbolList"/> class.
  69. /// </summary>
  70. public SymbolList()
  71. {
  72. }
  73. public ISymbolEnumerator GetSymbolEnumerator()
  74. {
  75. return new SymbolEnumerator(this);
  76. }
  77. public string Join(string separator)
  78. {
  79. if (separator == null)
  80. separator = "";
  81. StringBuilder result = new StringBuilder();
  82. foreach (Symbol s in this)
  83. {
  84. result.Append(s);
  85. result.Append(separator);
  86. }
  87. if (result.Length > 0)
  88. {
  89. result.Length -= separator.Length;
  90. }
  91. return result.ToString();
  92. }
  93. }
  94. public static class SymbolEnumeratorExtension
  95. {
  96. public static Symbol NextSymbol(this IEnumerator<Symbol> enumerator)
  97. {
  98. if (enumerator.MoveNext())
  99. {
  100. return enumerator.Current;
  101. }
  102. return null;
  103. }
  104. public static Symbol NextNonEOLSymbol(this IEnumerator<Symbol> enumerator)
  105. {
  106. while (enumerator.MoveNext())
  107. {
  108. if (enumerator.Current != Symbol.EOL)
  109. {
  110. return enumerator.Current;
  111. }
  112. }
  113. return null;
  114. }
  115. }
  116. }