/src/lib/regular_expression/regular_expression_builder.e

http://github.com/tybor/Liberty · Specman e · 291 lines · 190 code · 28 blank · 73 comment · 6 complexity · f0d7298fb11dcb83d088b25da02b6c68 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. expanded class REGULAR_EXPRESSION_BUILDER
  5. --
  6. -- Regular expression building from various dialects.
  7. -- Some REGULAR_EXPRESSION object is build from a string
  8. -- describing a pattern. A regular expression is a pattern
  9. -- that is matched against a subject string.
  10. --
  11. -- See tutorial/regular_expression for usage.
  12. --
  13. feature {ANY} -- Building REGULAR_EXPRESSION
  14. convert_posix_pattern (p: STRING): REGULAR_EXPRESSION
  15. -- Create some REGULAR_EXPRESSION from the pattern `p' according to POSIX syntax.
  16. -- If `p' is not a valid regular expression according to POSIX syntax, then `Result' is Void
  17. -- and `last_error_message' and `last_error_position' are set.
  18. require
  19. p /= Void
  20. do
  21. Result := convert_backtracking_pattern(p, posix_builder)
  22. ensure
  23. Result /= Void xor last_error_message /= Void
  24. initialized: Result /= Void implies not Result.last_match_succeeded
  25. substitution_cleared: Result /= Void implies not Result.substitution_pattern_ready
  26. end
  27. convert_perl_pattern (p: STRING): REGULAR_EXPRESSION
  28. -- Create some REGULAR_EXPRESSION from the pattern `p' according to Perl syntax.
  29. -- If `p' is not a valid regular expression according to Perl syntax, then `Result' is Void
  30. -- and `last_error_message' and `last_error_position' are set.
  31. require
  32. p /= Void
  33. do
  34. if has_extended_legibility then
  35. perl5_builder.set_extended_legibility
  36. else
  37. perl5_builder.set_no_extended_legibility
  38. end
  39. Result := convert_backtracking_pattern(p, perl5_builder)
  40. ensure
  41. Result /= Void xor last_error_message /= Void
  42. initialized: Result /= Void implies not Result.last_match_succeeded
  43. substitution_cleared: Result /= Void implies not Result.substitution_pattern_ready
  44. end
  45. convert_python_pattern (p: STRING): REGULAR_EXPRESSION
  46. -- Create some REGULAR_EXPRESSION from the pattern `p' according to Python syntax.
  47. -- If `p' is not a valid regular expression according to Python syntax, then `Result' is Void
  48. -- and `last_error_message' and `last_error_position' are set.
  49. require
  50. p /= Void
  51. do
  52. if has_extended_legibility then
  53. python_builder.set_extended_legibility
  54. else
  55. python_builder.set_no_extended_legibility
  56. end
  57. Result := convert_backtracking_pattern(p, python_builder)
  58. ensure
  59. Result /= Void xor last_error_message /= Void
  60. initialized: Result /= Void implies not Result.last_match_succeeded
  61. substitution_cleared: Result /= Void implies not Result.substitution_pattern_ready
  62. end
  63. feature {ANY} -- options
  64. is_case_insensitive: BOOLEAN
  65. -- Is the match case insensitive?
  66. -- Default is False
  67. is_case_sensitive: BOOLEAN
  68. -- Is the match case sensitive?
  69. -- Default is True
  70. do
  71. Result := not is_case_insensitive
  72. end
  73. set_case_sensitive
  74. -- Set the match as case sensitive.
  75. do
  76. is_case_insensitive := False
  77. ensure
  78. definition: is_case_insensitive = False and is_case_sensitive = True
  79. end
  80. set_case_insensitive
  81. -- Set the match as case insensitive.
  82. do
  83. is_case_insensitive := True
  84. ensure
  85. definition: is_case_insensitive = True and is_case_sensitive = False
  86. end
  87. does_any_match_newline: BOOLEAN
  88. -- Does the "any character" mark match a newline?
  89. -- Default is False
  90. set_any_match_newline
  91. -- The "any character" mark will match a newline.
  92. do
  93. does_any_match_newline := True
  94. ensure
  95. definition: does_any_match_newline = True
  96. end
  97. set_any_dont_match_newline
  98. -- The "any character" mark will not match a newline.
  99. do
  100. does_any_match_newline := False
  101. ensure
  102. definition: does_any_match_newline = False
  103. end
  104. does_match_line_boundary: BOOLEAN
  105. -- Does the begin/end marks match line boundary?
  106. -- Default is False
  107. does_match_text_boundary: BOOLEAN
  108. -- Does the begin/end marks match text boundary?
  109. -- Default is True
  110. do
  111. Result := not does_match_line_boundary
  112. ensure
  113. definition: Result = not does_match_line_boundary
  114. end
  115. set_match_line_boundary
  116. -- The begin/end marks will match line boundary.
  117. do
  118. does_match_line_boundary := True
  119. ensure
  120. definition: does_match_line_boundary = True and does_match_text_boundary = False
  121. end
  122. set_match_text_boundary
  123. -- The begin/end marks will match text boundary.
  124. do
  125. does_match_line_boundary := False
  126. ensure
  127. definition: does_match_line_boundary = False and does_match_text_boundary = True
  128. end
  129. has_extended_legibility: BOOLEAN
  130. -- Is the extended legibility active?
  131. has_extended_ligibility: BOOLEAN
  132. obsolete
  133. "Use `has_extended_legibility' instead."
  134. do
  135. Result := has_extended_legibility
  136. end
  137. set_extended_legibility
  138. -- Activate extended legibility.
  139. do
  140. has_extended_legibility := True
  141. ensure
  142. definition: has_extended_legibility = True
  143. end
  144. set_extended_ligibility
  145. obsolete
  146. "Use `set_extended_legibility' instead."
  147. do
  148. set_extended_legibility
  149. ensure
  150. definition: has_extended_legibility = True
  151. end
  152. set_no_extended_legibility
  153. -- Deactivate extended legibility.
  154. do
  155. has_extended_legibility := False
  156. ensure
  157. definition: has_extended_legibility = False
  158. end
  159. set_no_extended_ligibility
  160. obsolete
  161. "Use `set_no_extended_legibility' instead."
  162. do
  163. set_no_extended_legibility
  164. ensure
  165. definition: has_extended_legibility = False
  166. end
  167. set_default_options
  168. -- Set the default options
  169. do
  170. set_case_sensitive
  171. set_any_dont_match_newline
  172. set_match_text_boundary
  173. set_no_extended_legibility
  174. ensure
  175. is_case_sensitive
  176. not does_any_match_newline
  177. does_match_text_boundary
  178. not has_extended_legibility
  179. end
  180. feature {ANY} -- Error informations
  181. last_error_message: STRING
  182. -- Used to report error during last creation attempt.
  183. --
  184. -- See also `convert_perl_pattern', `convert_posix_pattern'.
  185. last_error_position: INTEGER
  186. -- Used to report error position during last creation attempt.
  187. --
  188. -- See also `convert_perl_pattern', `convert_posix_pattern'.
  189. feature {} -- Internal
  190. posix_builder: POSIX_REGULAR_EXPRESSION_BUILDER
  191. -- The builder for the POSIX syntax
  192. once
  193. create Result.make
  194. end
  195. perl5_builder: PERL5_REGULAR_EXPRESSION_BUILDER
  196. -- The builder for the PERL5 syntax
  197. once
  198. create Result.make
  199. end
  200. python_builder: PYTHON_REGULAR_EXPRESSION_BUILDER
  201. -- The builder for the Python syntax
  202. once
  203. create Result.make
  204. end
  205. convert_backtracking_pattern (p: STRING; builder: BACKTRACKING_REGULAR_EXPRESSION_BUILDER): BACKTRACKING_REGULAR_EXPRESSION
  206. -- Create some BACKTRACKING_REGULAR_EXPRESSION from the pattern `p' according to the syntax
  207. -- passed by the given 'builder'.
  208. -- If `p' is not a valid regular expression according the said syntax, then `Result' is Void
  209. -- and `last_error_message' and `last_error_message' are set.
  210. require
  211. p /= Void and builder /= Void
  212. do
  213. if is_case_insensitive then
  214. builder.set_case_insensitive
  215. else
  216. builder.set_case_sensitive
  217. end
  218. if does_any_match_newline then
  219. builder.set_any_match_newline
  220. else
  221. builder.set_any_dont_match_newline
  222. end
  223. if does_match_line_boundary then
  224. builder.set_match_line_boundary
  225. else
  226. builder.set_match_text_boundary
  227. end
  228. builder.set_expression(p)
  229. builder.parse
  230. if builder.has_result then
  231. create {BACKTRACKING_REGULAR_EXPRESSION} Result.make
  232. Result.set_pattern(builder.last_pattern)
  233. last_error_message := Void
  234. else
  235. last_error_position := builder.position
  236. last_error_message := builder.last_error.twin
  237. end
  238. ensure
  239. Result /= Void xor last_error_message /= Void
  240. initialized: Result /= Void implies not Result.last_match_succeeded
  241. substitution_cleared: Result /= Void implies not Result.substitution_pattern_ready
  242. end
  243. end -- class REGULAR_EXPRESSION_BUILDER
  244. --
  245. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  246. --
  247. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  248. -- of this software and associated documentation files (the "Software"), to deal
  249. -- in the Software without restriction, including without limitation the rights
  250. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  251. -- copies of the Software, and to permit persons to whom the Software is
  252. -- furnished to do so, subject to the following conditions:
  253. --
  254. -- The above copyright notice and this permission notice shall be included in
  255. -- all copies or substantial portions of the Software.
  256. --
  257. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  258. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  259. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  260. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  261. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  262. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  263. -- THE SOFTWARE.