RepetitionParser.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /// Spart License (zlib/png)
  2. ///
  3. ///
  4. /// Copyright (c) 2003 Jonathan de Halleux
  5. ///
  6. /// This software is provided 'as-is', without any express or implied warranty.
  7. /// In no event will the authors be held liable for any damages arising from
  8. /// the use of this software.
  9. ///
  10. /// Permission is granted to anyone to use this software for any purpose,
  11. /// including commercial applications, and to alter it and redistribute it
  12. /// freely, subject to the following restrictions:
  13. ///
  14. /// 1. The origin of this software must not be misrepresented; you must not
  15. /// claim that you wrote the original software. If you use this software in a
  16. /// product, an acknowledgment in the product documentation would be
  17. /// appreciated but is not required.
  18. ///
  19. /// 2. Altered source versions must be plainly marked as such, and must not be
  20. /// misrepresented as being the original software.
  21. ///
  22. /// 3. This notice may not be removed or altered from any source distribution.
  23. ///
  24. /// Author: Jonathan de Halleuxnamespace Spart.Parsers.Composite
  25. namespace Spart.Parsers.Composite
  26. {
  27. using System;
  28. using Spart.Scanners;
  29. using Spart.Actions;
  30. using Spart.Parsers.NonTerminal;
  31. public class RepetitionParser : UnaryTerminalParser
  32. {
  33. private uint m_LowerBound;
  34. private uint m_UpperBound;
  35. public RepetitionParser(Parser parser, uint lowerBound, uint upperBound)
  36. :base(parser)
  37. {
  38. SetBounds(lowerBound,upperBound);
  39. }
  40. public uint LowerBound
  41. {
  42. get
  43. {
  44. return m_LowerBound;
  45. }
  46. }
  47. public uint UpperBound
  48. {
  49. get
  50. {
  51. return m_UpperBound;
  52. }
  53. }
  54. public void SetBounds(uint lb, uint ub)
  55. {
  56. if (ub < lb)
  57. throw new ArgumentException("lower bound must be smaller than upper bound");
  58. m_LowerBound = lb;
  59. m_UpperBound = ub;
  60. }
  61. public override ParserMatch ParseMain(IScanner scanner)
  62. {
  63. // save scanner state
  64. long offset = scanner.Offset;
  65. ParserMatch m=scanner.EmptyMatch;
  66. ParserMatch m_temp=null;
  67. // execution bound
  68. int count=0;
  69. // lower bound, minimum number of executions
  70. while(count < LowerBound && !scanner.AtEnd)
  71. {
  72. m_temp = Parser.Parse(scanner);
  73. // stop if not successful
  74. if (!m_temp.Success)
  75. break;
  76. // increment count and update full match
  77. ++count;
  78. m.Concat(m_temp);
  79. }
  80. if (count == LowerBound)
  81. {
  82. while(count < UpperBound && !scanner.AtEnd)
  83. {
  84. m_temp = Parser.Parse(scanner);
  85. // stop if not successful
  86. if (!m_temp.Success)
  87. break;
  88. // increment count
  89. ++count;
  90. m.Concat(m_temp);
  91. }
  92. }
  93. else
  94. {
  95. m=scanner.NoMatch;
  96. // restoring parser failed, rewind scanner
  97. scanner.Seek(offset);
  98. }
  99. return m;
  100. }
  101. public override Parser Clone()
  102. {
  103. RepetitionParser clone = base.Clone() as RepetitionParser;
  104. return clone;
  105. }
  106. }
  107. }