StringScanner.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 Halleux
  25. namespace Spart.Scanners
  26. {
  27. using System;
  28. using System.IO;
  29. using Spart.Parsers;
  30. /// <summary>
  31. /// Scanner acting on a string.
  32. /// <seealso cref="IScanner"/>
  33. /// </summary>
  34. public class StringScanner : IScanner
  35. {
  36. private String m_InputString;
  37. private long m_Offset;
  38. private IFilter m_Filter;
  39. private Parser m_SkipParser;
  40. private bool m_IsSkipping;
  41. /// <summary>
  42. /// Creates a scanner on the string.
  43. /// </summary>
  44. /// <param name="inputString">Input string</param>
  45. /// <exception cref="ArgumentNullException">input string is null</exception>
  46. public StringScanner(String inputString) : this(inputString, Prims.Epsilon, 0)
  47. {
  48. }
  49. /// <summary>
  50. /// Creates a scanner on the string at a specified offset
  51. /// </summary>
  52. /// <param name="inputString">Input string</param>
  53. /// <exception cref="ArgumentNullException">input string is null</exception>
  54. /// <exception cref="ArgumentException">offset if out of range</exception>
  55. public StringScanner(String inputString, long offset) : this(inputString, Prims.Epsilon, offset)
  56. {
  57. }
  58. public StringScanner(String inputString, Parser skipParser) : this(inputString, skipParser, 0)
  59. {
  60. }
  61. public StringScanner(String inputString, Parser skipParser, long offset)
  62. {
  63. if (inputString == null)
  64. throw new ArgumentNullException("inputString is null");
  65. if (skipParser == null)
  66. throw new ArgumentNullException("skipParser is null");
  67. m_InputString = inputString;
  68. Offset = 0;
  69. Filter = null;
  70. IsSkipping = true;
  71. SkipParser = skipParser;
  72. }
  73. /// <summary>
  74. /// the input string
  75. /// </summary>
  76. public String InputString
  77. {
  78. get
  79. {
  80. return m_InputString;
  81. }
  82. }
  83. /// <summary>
  84. /// Current offset
  85. /// </summary>
  86. public long Offset
  87. {
  88. get
  89. {
  90. return GetSkipOffset();
  91. }
  92. set
  93. {
  94. if (value < 0 || value > InputString.Length)
  95. throw new ArgumentOutOfRangeException("offset out of bounds");
  96. m_Offset = value;
  97. m_Offset = GetSkipOffset();
  98. }
  99. }
  100. /// <summary>
  101. /// true if at the end of the string
  102. /// </summary>
  103. public bool AtEnd
  104. {
  105. get
  106. {
  107. return GetSkipOffset() == InputString.Length;
  108. }
  109. }
  110. /// <summary>
  111. /// Advance the cursor once
  112. /// </summary>
  113. /// <returns>true if not at end</returns>
  114. /// <exception cref="Exception">If called while AtEnd is true</exception>
  115. public bool Read()
  116. {
  117. if (AtEnd)
  118. throw new Exception("Scanner already at end");
  119. long newOffset = GetSkipOffset();
  120. m_Offset = newOffset + 1;
  121. return !AtEnd;
  122. }
  123. /// <summary>
  124. /// Current character
  125. /// </summary>
  126. /// <returns>character at cursor position</returns>
  127. public char Peek()
  128. {
  129. long newOffset = GetSkipOffset();
  130. if (Filter==null)
  131. return InputString[(int)newOffset];
  132. else
  133. return Filter.Filter(InputString[(int)newOffset]);
  134. }
  135. private long GetSkipOffset()
  136. {
  137. if (!IsSkipping)
  138. {
  139. return m_Offset;
  140. }
  141. else
  142. {
  143. IsSkipping = false;
  144. long offset = m_Offset;
  145. ParserMatch match = SkipParser.Parse(this);
  146. Seek(offset);
  147. IsSkipping = true;
  148. return match.Success ? match.Offset + match.Length : m_Offset;
  149. }
  150. }
  151. /// <summary>
  152. /// Extracts a substring
  153. /// </summary>
  154. /// <param name="offset"></param>
  155. /// <param name="length"></param>
  156. /// <returns></returns>
  157. public String Substring(long offset, int length)
  158. {
  159. String s=InputString.Substring((int)offset,Math.Min(length, InputString.Length-(int)offset));
  160. if (Filter != null)
  161. s=Filter.Filter(s);
  162. return s;
  163. }
  164. /// <summary>
  165. /// Moves the cursor to the offset position
  166. /// </summary>
  167. /// <param name="offset"></param>
  168. public void Seek(long offset)
  169. {
  170. if (offset < 0 || offset > InputString.Length)
  171. throw new ArgumentOutOfRangeException("offset");
  172. Offset = offset;
  173. }
  174. /// <summary>
  175. /// Current filter
  176. /// </summary>
  177. public IFilter Filter
  178. {
  179. get
  180. {
  181. return m_Filter;
  182. }
  183. set
  184. {
  185. m_Filter = value;
  186. }
  187. }
  188. /// <summary>
  189. /// Failure match
  190. /// </summary>
  191. public ParserMatch NoMatch
  192. {
  193. get
  194. {
  195. return new ParserMatch(this,Offset,-1);
  196. }
  197. }
  198. /// <summary>
  199. /// Empty match
  200. /// </summary>
  201. public ParserMatch EmptyMatch
  202. {
  203. get
  204. {
  205. return new ParserMatch(this,GetSkipOffset(),0);
  206. }
  207. }
  208. /// <summary>
  209. /// Creates a successful match
  210. /// </summary>
  211. /// <param name="offset"></param>
  212. /// <param name="length"></param>
  213. /// <returns></returns>
  214. public ParserMatch CreateMatch(long offset, int length)
  215. {
  216. return new ParserMatch(this,offset,length);
  217. }
  218. public bool IsSkipping
  219. {
  220. get
  221. {
  222. return m_IsSkipping;
  223. }
  224. set
  225. {
  226. m_IsSkipping = value;
  227. }
  228. }
  229. public Parser SkipParser
  230. {
  231. get
  232. {
  233. return m_SkipParser;
  234. }
  235. set
  236. {
  237. m_SkipParser = value;
  238. }
  239. }
  240. }
  241. }