2
0

Re.hx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C)2005-2018 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package python.lib;
  23. import python.Tuple;
  24. private abstract Choice <A,B>(Dynamic) {
  25. @:from public static inline function fromA <A,B>(x:A):Choice<A,B> return cast x;
  26. @:from public static inline function fromB <A,B>(x:B):Choice<A,B> return cast x;
  27. }
  28. typedef Pattern = Choice<String, Regex>;
  29. typedef Repl = Choice<String, MatchObject->String>;
  30. extern class MatchObject
  31. {
  32. public var pos(default, null):Int;
  33. public var endpos(default, null):Int;
  34. public var lastindex(default, null):Int;
  35. public var lastgroup(default, null):Int;
  36. public var re(default, null):Regex;
  37. public var string(default, null):String;
  38. public function expand(template:String):String;
  39. @:overload(function (x:String):String {})
  40. public function group(?i:Int = 0):String;
  41. public function groups(defaultVal:String = null):Tuple<String>;
  42. public function groupdict(defaultVal:Dict<String, String> = null):Dict<String, String>;
  43. @:overload(function (x:String):Int {})
  44. public function start (?i:Int = 0):Int;
  45. @:overload(function (x:String):Int {})
  46. public function end (?i:Int = 0):Int;
  47. public function span (?i:Int):Tuple2<Int, Int>;
  48. public inline function groupById(s:String):String {
  49. return group(s);
  50. }
  51. public inline function startById(s:String):Int {
  52. return start(s);
  53. }
  54. public inline function endById(s:String):Int {
  55. return end(s);
  56. }
  57. }
  58. private class RegexHelper {
  59. public static function findallDynamic(r:Regex, string:String, ?pos:Int, ?endpos:Int):Array<Dynamic>
  60. {
  61. if (endpos == null) {
  62. if (pos == null) {
  63. return python.Syntax.field(r, "findall")(string);
  64. } else {
  65. return python.Syntax.field(r, "findall")(string, pos);
  66. }
  67. } else {
  68. return python.Syntax.field(r, "findall")(string, pos, endpos);
  69. }
  70. }
  71. }
  72. extern class Regex
  73. {
  74. public function search(string:String, pos:Int = 0, ?endpos:Int):Null<MatchObject>;
  75. public function match(string:String, pos:Int = 0, ?endpos:Int):Null<MatchObject>;
  76. public function split(string:String, maxsplit:Int=0):Array<String>;
  77. public inline function findallString(string:String, ?pos:Int, ?endpos:Int):Array<String>
  78. {
  79. return cast this.findallDynamic(string, pos, endpos);
  80. }
  81. public inline function findallDynamic(string:String, ?pos:Int, ?endpos:Int):Array<Dynamic>
  82. {
  83. return RegexHelper.findallDynamic(this, string, pos, endpos);
  84. }
  85. public inline function findallTuple(string:String, ?pos:Int, ?endpos:Int):Array<Tuple<String>> {
  86. return cast this.findallDynamic(string, pos, endpos);
  87. }
  88. public inline function findallArray(string:String, ?pos:Int, ?endpos:Int):Array<Array<String>>
  89. {
  90. return findallTuple(string, pos, endpos).map(function (t) return t.toArray());
  91. }
  92. public function finditer(string:String, ?pos:Int, ?endpos:Int):NativeIterator<MatchObject>;
  93. public function sub(repl:Repl, string:String, count:Int=0):String;
  94. public function subn(repl:Repl, string:String, count:Int=0):String;
  95. public var flags(default, null):Int;
  96. public var groups(default, null):Int;
  97. public var groupindex(default, null):Dict<String, Int>;
  98. public var pattern(default, null):String;
  99. }
  100. @:pythonImport("re")
  101. extern class Re
  102. {
  103. public static var A:Int;
  104. public static var ASCII:Int;
  105. public static var DEBUG:Int;
  106. public static var I:Int;
  107. public static var IGNORECASE:Int;
  108. public static var L:Int;
  109. public static var LOCALE:Int;
  110. public static var M:Int;
  111. public static var MULTILINE:Int;
  112. public static var S:Int;
  113. public static var DOTALL:Int;
  114. public static var X:Int;
  115. public static var VERBOSE:Int;
  116. public static var U:Int;
  117. public static var UNICODE:Int;
  118. public static function compile (pattern:String, ?flags:Int = 0):Regex;
  119. public static function match (pattern:Pattern, string:String, flags:Int = 0):Null<MatchObject>;
  120. public static function search (pattern:Pattern, string:String, flags:Int = 0):Null<MatchObject>;
  121. public static function split(pattern:Pattern, string:String, maxsplit:Int=0, flags:Int=0):Array<String>;
  122. public static inline function findallDynamic(pattern:Pattern, string:String, flags:Int=0):Array<Dynamic>
  123. {
  124. return python.Syntax.field(pattern, "findall")(string, flags);
  125. }
  126. public static inline function findallString(pattern:Pattern, string:String, flags:Int=0):Array<String>
  127. {
  128. return python.Syntax.field(pattern, "findall")(string, flags);
  129. }
  130. public static inline function findallTuple(pattern:Pattern, string:String, flags:Int=0):Array<Tuple<String>>
  131. {
  132. return python.Syntax.field(pattern, "findall")(string, flags);
  133. }
  134. public static inline function findallArray(pattern:Pattern, string:String, flags:Int=0):Array<Array<String>>
  135. {
  136. return findallTuple(pattern, string,flags).map(function (t) return t.toArray());
  137. }
  138. public static function finditer(pattern:Pattern, string:String, flags:Int=0):NativeIterator<MatchObject>;
  139. @:overload(function (pattern:Pattern, repl:String, string:String, ?count:Int=0, ?flags:Int=0):String {})
  140. public static function sub(pattern:Pattern, repl:MatchObject->String, string:String, ?count:Int=0, ?flags:Int=0):String;
  141. public static function subn(pattern:Pattern, repl:Repl, string:String, count:Int=0, flags:Int=0):String;
  142. public static function escape(string:String):String;
  143. public static function purge():Void;
  144. }