Re.hx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C)2005-2019 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>
  26. return cast x;
  27. @:from public static inline function fromB<A, B>(x:B):Choice<A, B>
  28. return cast x;
  29. }
  30. typedef Pattern = Choice<String, Regex>;
  31. typedef Repl = Choice<String, MatchObject->String>;
  32. extern class MatchObject {
  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. 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. public function search(string:String, pos:Int = 0, ?endpos:Int):Null<MatchObject>;
  74. public function match(string:String, pos:Int = 0, ?endpos:Int):Null<MatchObject>;
  75. public function split(string:String, maxsplit:Int = 0):Array<String>;
  76. public inline function findallString(string:String, ?pos:Int, ?endpos:Int):Array<String> {
  77. return cast this.findallDynamic(string, pos, endpos);
  78. }
  79. public inline function findallDynamic(string:String, ?pos:Int, ?endpos:Int):Array<Dynamic> {
  80. return RegexHelper.findallDynamic(this, string, pos, endpos);
  81. }
  82. public inline function findallTuple(string:String, ?pos:Int, ?endpos:Int):Array<Tuple<String>> {
  83. return cast this.findallDynamic(string, pos, endpos);
  84. }
  85. public inline function findallArray(string:String, ?pos:Int, ?endpos:Int):Array<Array<String>> {
  86. return findallTuple(string, pos, endpos).map(function(t) return t.toArray());
  87. }
  88. public function finditer(string:String, ?pos:Int, ?endpos:Int):NativeIterator<MatchObject>;
  89. public function sub(repl:Repl, string:String, count:Int = 0):String;
  90. public function subn(repl:Repl, string:String, count:Int = 0):String;
  91. public var flags(default, null):Int;
  92. public var groups(default, null):Int;
  93. public var groupindex(default, null):Dict<String, Int>;
  94. public var pattern(default, null):String;
  95. }
  96. @:pythonImport("re")
  97. extern class Re {
  98. public static var A:Int;
  99. public static var ASCII:Int;
  100. public static var DEBUG:Int;
  101. public static var I:Int;
  102. public static var IGNORECASE:Int;
  103. public static var L:Int;
  104. public static var LOCALE:Int;
  105. public static var M:Int;
  106. public static var MULTILINE:Int;
  107. public static var S:Int;
  108. public static var DOTALL:Int;
  109. public static var X:Int;
  110. public static var VERBOSE:Int;
  111. public static var U:Int;
  112. public static var UNICODE:Int;
  113. public static function compile(pattern:String, ?flags:Int = 0):Regex;
  114. public static function match(pattern:Pattern, string:String, flags:Int = 0):Null<MatchObject>;
  115. public static function search(pattern:Pattern, string:String, flags:Int = 0):Null<MatchObject>;
  116. public static function split(pattern:Pattern, string:String, maxsplit:Int = 0, flags:Int = 0):Array<String>;
  117. public static inline function findallDynamic(pattern:Pattern, string:String, flags:Int = 0):Array<Dynamic> {
  118. return python.Syntax.field(pattern, "findall")(string, flags);
  119. }
  120. public static inline function findallString(pattern:Pattern, string:String, flags:Int = 0):Array<String> {
  121. return python.Syntax.field(pattern, "findall")(string, flags);
  122. }
  123. public static inline function findallTuple(pattern:Pattern, string:String, flags:Int = 0):Array<Tuple<String>> {
  124. return python.Syntax.field(pattern, "findall")(string, flags);
  125. }
  126. public static inline function findallArray(pattern:Pattern, string:String, flags:Int = 0):Array<Array<String>> {
  127. return findallTuple(pattern, string, flags).map(function(t) return t.toArray());
  128. }
  129. public static function finditer(pattern:Pattern, string:String, flags:Int = 0):NativeIterator<MatchObject>;
  130. @:overload(function(pattern:Pattern, repl:String, string:String, ?count:Int = 0, ?flags:Int = 0):String {})
  131. public static function sub(pattern:Pattern, repl:MatchObject->String, string:String, ?count:Int = 0, ?flags:Int = 0):String;
  132. public static function subn(pattern:Pattern, repl:Repl, string:String, count:Int = 0, flags:Int = 0):String;
  133. public static function escape(string:String):String;
  134. public static function purge():Void;
  135. }