/src/lib/parse/mini_parser_buffer.e

http://github.com/tybor/Liberty · Specman e · 206 lines · 138 code · 27 blank · 41 comment · 2 complexity · 6601e69d2725c68ef72359043167de4f MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. class MINI_PARSER_BUFFER
  5. --
  6. -- Useful and efficient mini parsing buffer.
  7. -- From the user point of view, an object of this class can be considered
  8. -- as the combination of a STRING to be parsed with an extra INTEGER
  9. -- index to memorize the current position in this STRING.
  10. --
  11. insert
  12. STRING_HANDLER
  13. redefine
  14. default_create
  15. end
  16. create {ANY}
  17. default_create
  18. feature {ANY}
  19. initialize_with (string: ABSTRACT_STRING) is
  20. -- Initialize the `Current' buffer interning `string'.
  21. require
  22. string /= Void
  23. do
  24. storage := string.intern
  25. current_index := storage.lower
  26. last_error := Void
  27. ensure
  28. count = string.count
  29. last_error = Void
  30. end
  31. next is
  32. -- Move the `current_index' forward by one.
  33. require
  34. not end_reached
  35. do
  36. current_index := current_index + 1
  37. ensure
  38. current_index = 1 + old current_index
  39. end
  40. skip_separators is
  41. -- Skip all separators by using `is_separator' of class CHARACTER.
  42. local
  43. stop: BOOLEAN
  44. do
  45. from
  46. until
  47. stop
  48. loop
  49. if current_index > upper then
  50. stop := True
  51. elseif storage.item(current_index).is_separator then
  52. current_index := current_index + 1
  53. else
  54. stop := True
  55. end
  56. end
  57. end
  58. last_error: PARSE_ERROR
  59. -- Can be used to memorize an error message.
  60. set_last_error (error: like last_error) is
  61. do
  62. last_error := error
  63. ensure
  64. last_error = error
  65. end
  66. set_last_error_message (error_message: STRING) is
  67. do
  68. create last_error.make(current_index, error_message, last_error)
  69. ensure
  70. last_error.message = error_message
  71. end
  72. set_current_index (new_index: like current_index) is
  73. -- To be able to move (usually back) in the buffer
  74. require
  75. new_index.in_range(lower, upper + 1)
  76. do
  77. current_index := new_index
  78. ensure
  79. current_index = new_index
  80. end
  81. feature {ANY} -- Memo
  82. set_memory (a_memory: like memory) is
  83. require
  84. a_memory /= Void
  85. do
  86. memory := a_memory
  87. ensure
  88. memory = a_memory
  89. end
  90. memo: INTEGER is
  91. do
  92. Result := memory.memo(Current)
  93. end
  94. restore (a_memo: like memo) is
  95. do
  96. memory.restore(a_memo, Current)
  97. end
  98. memory: MINI_PARSER_MEMORY
  99. feature {ANY} -- Queries
  100. lower: INTEGER is
  101. do
  102. Result := storage.lower
  103. end
  104. upper: INTEGER is
  105. -- Maximum valid index in storage.
  106. do
  107. Result := storage.upper
  108. end
  109. count: INTEGER is
  110. -- The length of the `Current' buffer which is also the maximum valid index.
  111. do
  112. Result := storage.count
  113. end
  114. capacity: INTEGER is
  115. -- Of `storage'.
  116. do
  117. Result := storage.capacity
  118. end
  119. current_index: INTEGER
  120. -- Index of the current character.
  121. current_character: CHARACTER is
  122. -- The current character (the one at `current_index').
  123. require
  124. current_index.in_range(lower, upper + 1)
  125. do
  126. Result := storage.item(current_index)
  127. end
  128. end_reached: BOOLEAN is
  129. -- Is the end of the buffer reached?
  130. do
  131. Result := current_index > upper
  132. if Result then
  133. marked := True
  134. end
  135. ensure
  136. Result = (current_index > upper)
  137. Result implies marked
  138. end
  139. marked: BOOLEAN
  140. clear_mark is
  141. do
  142. marked := False
  143. ensure
  144. not marked
  145. end
  146. feature {}
  147. storage: FIXED_STRING
  148. -- The `storage' area to be parsed.
  149. default_create is
  150. do
  151. memory := default_memory
  152. end
  153. default_memory: DEFAULT_MINI_PARSER_MEMORY is
  154. once
  155. create Result
  156. end
  157. invariant
  158. memory /= Void
  159. end -- class MINI_PARSER_BUFFER
  160. --
  161. -- Copyright (c) 2009 by all the people cited in the AUTHORS file.
  162. --
  163. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  164. -- of this software and associated documentation files (the "Software"), to deal
  165. -- in the Software without restriction, including without limitation the rights
  166. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  167. -- copies of the Software, and to permit persons to whom the Software is
  168. -- furnished to do so, subject to the following conditions:
  169. --
  170. -- The above copyright notice and this permission notice shall be included in
  171. -- all copies or substantial portions of the Software.
  172. --
  173. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  174. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  175. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  176. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  177. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  178. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  179. -- THE SOFTWARE.