ParserMatch.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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
  25. namespace Spart.Parsers
  26. {
  27. using System;
  28. using Spart.Scanners;
  29. /// <summary>
  30. /// A parser match
  31. /// </summary>
  32. public class ParserMatch
  33. {
  34. private IScanner m_Scanner;
  35. private long m_Offset;
  36. private int m_Length;
  37. /// <summary>
  38. /// Builds a new match
  39. /// </summary>
  40. /// <param name="scanner"></param>
  41. /// <param name="offset"></param>
  42. /// <param name="length"></param>
  43. public ParserMatch(IScanner scanner, long offset, int length)
  44. {
  45. if (scanner == null)
  46. throw new ArgumentNullException("scanner");
  47. m_Scanner = scanner;
  48. m_Offset = offset;
  49. m_Length = length;
  50. }
  51. /// <summary>
  52. /// Scanner
  53. /// </summary>
  54. public IScanner Scanner
  55. {
  56. get
  57. {
  58. return m_Scanner;
  59. }
  60. }
  61. /// <summary>
  62. /// Offset
  63. /// </summary>
  64. public long Offset
  65. {
  66. get
  67. {
  68. return m_Offset;
  69. }
  70. }
  71. /// <summary>
  72. /// Length
  73. /// </summary>
  74. public int Length
  75. {
  76. get
  77. {
  78. return m_Length;
  79. }
  80. }
  81. /// <summary>
  82. /// Extracts the match value
  83. /// </summary>
  84. public String Value
  85. {
  86. get
  87. {
  88. if (Length<0)
  89. throw new Exception("no match");
  90. return Scanner.Substring(Offset, Length);
  91. }
  92. }
  93. /// <summary>
  94. /// True if match successfull
  95. /// </summary>
  96. public bool Success
  97. {
  98. get
  99. {
  100. return Length >= 0;
  101. }
  102. }
  103. /// <summary>
  104. /// True if match empty
  105. /// </summary>
  106. public bool Empty
  107. {
  108. get
  109. {
  110. if (Length<0)
  111. throw new Exception("no match");
  112. return Length == 0;
  113. }
  114. }
  115. /// <summary>
  116. /// Concatenates match with m
  117. /// </summary>
  118. /// <param name="m"></param>
  119. public void Concat(ParserMatch m)
  120. {
  121. if(m==null)
  122. throw new ArgumentNullException("Cannot concatenate null match");
  123. if(!m.Success)
  124. throw new ArgumentException("Trying to concatenated non successful match");
  125. // if other is empty, return this
  126. if(m.Empty)
  127. return;
  128. if (m.Offset < Offset)
  129. {
  130. m_Offset = m.Offset;
  131. m_Length = m.Length;
  132. }
  133. else
  134. {
  135. m_Length = (int)(m.Offset-Offset) + m.Length;
  136. }
  137. }
  138. }
  139. }