Re.hx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C)2005-2015 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 TODO = Dynamic;
  29. typedef Pattern = Choice<String, Regex>;
  30. typedef Repl = Choice<String, MatchObject->String>;
  31. extern class MatchObject
  32. {
  33. public var pos(default, null):Int;
  34. public var endpos(default, null):Int;
  35. public var lastindex(default, null):Int;
  36. public var lastgroup(default, null):Int;
  37. public var re(default, null):Regex;
  38. public var string(default, null):String;
  39. public function expand(template:String):String;
  40. @:overload(function (x:String):String {})
  41. public function group(?i:Int = 0):String;
  42. public function groups(defaultVal:String = null):Tuple<String>;
  43. public function groupdict(defaultVal:Dict<String, String> = null):Dict<String, String>;
  44. @:overload(function (x:String):Int {})
  45. public function start (?i:Int = 0):Int;
  46. @:overload(function (x:String):Int {})
  47. public function end (?i:Int = 0):Int;
  48. public function span (?i:Int):Tuple2<Int, Int>;
  49. public inline function groupById(s:String):String {
  50. return group(s);
  51. }
  52. public inline function startById(s:String):Int {
  53. return start(s);
  54. }
  55. public inline function endById(s:String):Int {
  56. return end(s);
  57. }
  58. }
  59. private class RegexHelper {
  60. public static function findallDynamic(r:Regex, string:String, ?pos:Int, ?endpos:Int):Array<Dynamic>
  61. {
  62. if (endpos == null) {
  63. if (pos == null) {
  64. return python.Syntax.field(r, "findall")(string);
  65. } else {
  66. return python.Syntax.field(r, "findall")(string, pos);
  67. }
  68. } else {
  69. return python.Syntax.field(r, "findall")(string, pos, endpos);
  70. }
  71. }
  72. }
  73. extern class Regex
  74. {
  75. public function search(string:String, pos:Int = 0, ?endpos:Int):Null<MatchObject>;
  76. public function match(string:String, pos:Int = 0, ?endpos:Int):Null<MatchObject>;
  77. public function split(string:String, maxsplit:Int=0):Array<String>;
  78. public inline function findallString(string:String, ?pos:Int, ?endpos:Int):Array<String>
  79. {
  80. return cast this.findallDynamic(string, pos, endpos);
  81. }
  82. public inline function findallDynamic(string:String, ?pos:Int, ?endpos:Int):Array<Dynamic>
  83. {
  84. return RegexHelper.findallDynamic(this, string, pos, endpos);
  85. }
  86. public inline function findallTuple(string:String, ?pos:Int, ?endpos:Int):Array<Tuple<String>> {
  87. return cast this.findallDynamic(string, pos, endpos);
  88. }
  89. public inline function findallArray(string:String, ?pos:Int, ?endpos:Int):Array<Array<String>>
  90. {
  91. return findallTuple(string, pos, endpos).map(function (t) return t.toArray());
  92. }
  93. public function finditer(string:String, ?pos:Int, ?endpos:Int):NativeIterator<MatchObject>;
  94. public function sub(repl:Repl, string:String, count:Int=0):String;
  95. public function subn(repl:Repl, string:String, count:Int=0):String;
  96. public var flags(default, null):Int;
  97. public var groups(default, null):Int;
  98. public var groupindex(default, null):Dict<String, Int>;
  99. public var pattern(default, null):String;
  100. }
  101. @:pythonImport("re")
  102. extern class Re
  103. {
  104. public static var A:Int;
  105. public static var ASCII:Int;
  106. public static var DEBUG:Int;
  107. public static var I:Int;
  108. public static var IGNORECASE:Int;
  109. public static var L:Int;
  110. public static var LOCALE:Int;
  111. public static var M:Int;
  112. public static var MULTILINE:Int;
  113. public static var S:Int;
  114. public static var DOTALL:Int;
  115. public static var X:Int;
  116. public static var VERBOSE:Int;
  117. public static var U:Int;
  118. public static var UNICODE:Int;
  119. public static function compile (pattern:String, ?flags:Int = 0):Regex;
  120. public static function match (pattern:Pattern, string:String, flags:Int = 0):Null<MatchObject>;
  121. public static function search (pattern:Pattern, string:String, flags:Int = 0):Null<MatchObject>;
  122. public static function split(pattern:Pattern, string:String, maxsplit:Int=0, flags:Int=0):Array<String>;
  123. public static inline function findallDynamic(pattern:Pattern, string:String, flags:Int=0):Array<Dynamic>
  124. {
  125. return python.Syntax.field(pattern, "findall")(string, flags);
  126. }
  127. public static inline function findallString(pattern:Pattern, string:String, flags:Int=0):Array<String>
  128. {
  129. return python.Syntax.field(pattern, "findall")(string, flags);
  130. }
  131. public static inline function findallTuple(pattern:Pattern, string:String, flags:Int=0):Array<Tuple<String>>
  132. {
  133. return python.Syntax.field(pattern, "findall")(string, flags);
  134. }
  135. public static inline function findallArray(pattern:Pattern, string:String, flags:Int=0):Array<Array<String>>
  136. {
  137. return findallTuple(pattern, string,flags).map(function (t) return t.toArray());
  138. }
  139. public static function finditer(pattern:Pattern, string:String, flags:Int=0):NativeIterator<MatchObject>;
  140. @:overload(function (pattern:Pattern, repl:String, string:String, ?count:Int=0, ?flags:Int=0):String {})
  141. public static function sub(pattern:Pattern, repl:MatchObject->String, string:String, ?count:Int=0, ?flags:Int=0):String;
  142. public static function subn(pattern:Pattern, repl:Repl, string:String, count:Int=0, flags:Int=0):String;
  143. public static function escape(string:String):TODO;
  144. public static function purge():Void;
  145. }