/src/lib/xml/internal/xml_parser_tools.e

http://github.com/tybor/Liberty · Specman e · 268 lines · 226 code · 20 blank · 22 comment · 21 complexity · 4e7836e31eef81570cc7df9e309dc075 MD5 · raw file

  1. -- See the Copyright notice at the end of this file.
  2. --
  3. deferred class XML_PARSER_TOOLS
  4. insert
  5. UNICODE_CHARACTERS
  6. feature {ANY}
  7. line: INTEGER
  8. do
  9. Result := buffer.line
  10. end
  11. column: INTEGER
  12. do
  13. Result := buffer.column
  14. end
  15. feature {}
  16. buffer: UNICODE_PARSER_BUFFER
  17. deferred
  18. end
  19. skip_blanks
  20. require
  21. buffer.is_connected
  22. do
  23. from
  24. until
  25. buffer.end_of_input or else not is_separator(current_character)
  26. loop
  27. buffer.next
  28. end
  29. end
  30. end_of_input: BOOLEAN
  31. require
  32. buffer.is_connected
  33. do
  34. Result := buffer.end_of_input
  35. end
  36. next
  37. require
  38. buffer.is_connected
  39. not end_of_input
  40. do
  41. buffer.next
  42. end
  43. previous
  44. require
  45. buffer.is_connected
  46. buffer.index > 0
  47. do
  48. buffer.previous
  49. end
  50. current_character: INTEGER
  51. require
  52. buffer.is_connected
  53. not end_of_input
  54. do
  55. Result := buffer.code
  56. end
  57. skip (character: CHARACTER): BOOLEAN
  58. require
  59. buffer.is_connected
  60. do
  61. if not end_of_input then
  62. skip_blanks
  63. if current_character = character.code then
  64. Result := True
  65. next
  66. end
  67. end
  68. end
  69. skip2 (char1, char2: CHARACTER): BOOLEAN
  70. require
  71. buffer.is_connected
  72. do
  73. if skip(char1) then
  74. if skip(char2) then
  75. Result := True
  76. else
  77. previous
  78. end
  79. end
  80. end
  81. skip_word (word: STRING): BOOLEAN
  82. require
  83. buffer.is_connected
  84. not word.is_empty
  85. local
  86. w: INTEGER; p: UNICODE_PARSER_POSITION
  87. do
  88. p := buffer.save_position
  89. skip_blanks
  90. from
  91. w := word.lower
  92. Result := True
  93. until
  94. not Result or else w > word.upper
  95. loop
  96. Result := skip(word.item(w))
  97. w := w + 1
  98. end
  99. if not Result then
  100. buffer.restore_position(p)
  101. end
  102. end
  103. is_identifier_start (unicode: INTEGER): BOOLEAN
  104. require
  105. buffer.is_connected
  106. do
  107. Result := is_letter(unicode)
  108. if not Result then
  109. inspect
  110. unicode
  111. when 95, 58 then -- '_', ':'
  112. Result := True
  113. else
  114. check not Result end
  115. end
  116. end
  117. end
  118. is_identifier_part (unicode: INTEGER): BOOLEAN
  119. require
  120. buffer.is_connected
  121. do
  122. Result := is_letter(unicode) or else is_digit(unicode) or else is_combining_char(unicode) or else is_extender(unicode)
  123. if not Result then
  124. inspect
  125. unicode
  126. when 95, 58, 45, 46 then -- '_', ':', '-', '.'
  127. Result := True
  128. else
  129. check not Result end
  130. end
  131. end
  132. end
  133. read_identifier: UNICODE_STRING
  134. require
  135. buffer.is_connected
  136. local
  137. done: BOOLEAN
  138. do
  139. skip_blanks
  140. from
  141. if end_of_input then
  142. done := True
  143. else
  144. Result := once U""
  145. Result.clear_count
  146. if is_identifier_start(current_character) then
  147. Result.extend(current_character)
  148. next
  149. else
  150. done := True
  151. end
  152. end
  153. until
  154. done
  155. loop
  156. if end_of_input then
  157. done := True
  158. else
  159. if is_identifier_part(current_character) then
  160. Result.extend(current_character)
  161. next
  162. else
  163. done := True
  164. end
  165. end
  166. end
  167. if Result.is_empty then
  168. Result := Void
  169. end
  170. end
  171. read_string: UNICODE_STRING
  172. require
  173. buffer.is_connected
  174. current_character = '%''.code or else current_character = '"'.code
  175. local
  176. done: BOOLEAN; limit: INTEGER; p: UNICODE_PARSER_POSITION
  177. do
  178. p := buffer.save_position
  179. from
  180. Result := once U""
  181. Result.clear_count
  182. limit := current_character
  183. next
  184. until
  185. done
  186. loop
  187. if end_of_input then
  188. Result := Void
  189. buffer.restore_position(p)
  190. done := True
  191. elseif current_character = limit then
  192. next
  193. done := True
  194. else
  195. Result.extend(current_character)
  196. next
  197. end
  198. end
  199. end
  200. read_identifier_as_string: STRING
  201. require
  202. buffer.is_connected
  203. local
  204. s: UNICODE_STRING
  205. do
  206. s := read_identifier
  207. if s /= Void then
  208. Result := once ""
  209. Result.clear_count
  210. s.utf8_encode_in(Result)
  211. end
  212. end
  213. read_string_as_string: STRING
  214. require
  215. buffer.is_connected
  216. local
  217. s: UNICODE_STRING
  218. do
  219. s := read_string
  220. if s /= Void then
  221. Result := once ""
  222. Result.clear_count
  223. s.utf8_encode_in(Result)
  224. end
  225. end
  226. invariant
  227. readable_url: (buffer /= Void and then buffer.is_connected) implies buffer.url.read
  228. end -- class XML_PARSER_TOOLS
  229. --
  230. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  231. --
  232. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  233. -- of this software and associated documentation files (the "Software"), to deal
  234. -- in the Software without restriction, including without limitation the rights
  235. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  236. -- copies of the Software, and to permit persons to whom the Software is
  237. -- furnished to do so, subject to the following conditions:
  238. --
  239. -- The above copyright notice and this permission notice shall be included in
  240. -- all copies or substantial portions of the Software.
  241. --
  242. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  243. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  244. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  245. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  246. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  247. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  248. -- THE SOFTWARE.