Re.hx 5.8 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. var pos(default, null):Int;
  34. var endpos(default, null):Int;
  35. var lastindex(default, null):Int;
  36. var lastgroup(default, null):Int;
  37. var re(default, null):Regex;
  38. var string(default, null):String;
  39. function expand(template:String):String;
  40. @:overload(function(x:String):String {})
  41. function group(?i:Int = 0):String;
  42. function groups(defaultVal:String = null):Tuple<String>;
  43. function groupdict(defaultVal:Dict<String, String> = null):Dict<String, String>;
  44. @:overload(function(x:String):Int {})
  45. function start(?i:Int = 0):Int;
  46. @:overload(function(x:String):Int {})
  47. function end(?i:Int = 0):Int;
  48. function span(?i:Int):Tuple2<Int, Int>;
  49. inline function groupById(s:String):String {
  50. return group(s);
  51. }
  52. inline function startById(s:String):Int {
  53. return start(s);
  54. }
  55. 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. function search(string:String, pos:Int = 0, ?endpos:Int):Null<MatchObject>;
  74. function match(string:String, pos:Int = 0, ?endpos:Int):Null<MatchObject>;
  75. function split(string:String, maxsplit:Int = 0):Array<String>;
  76. inline function findallString(string:String, ?pos:Int, ?endpos:Int):Array<String> {
  77. return cast this.findallDynamic(string, pos, endpos);
  78. }
  79. inline function findallDynamic(string:String, ?pos:Int, ?endpos:Int):Array<Dynamic> {
  80. return RegexHelper.findallDynamic(this, string, pos, endpos);
  81. }
  82. inline function findallTuple(string:String, ?pos:Int, ?endpos:Int):Array<Tuple<String>> {
  83. return cast this.findallDynamic(string, pos, endpos);
  84. }
  85. 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. function finditer(string:String, ?pos:Int, ?endpos:Int):NativeIterator<MatchObject>;
  89. function sub(repl:Repl, string:String, count:Int = 0):String;
  90. function subn(repl:Repl, string:String, count:Int = 0):String;
  91. var flags(default, null):Int;
  92. var groups(default, null):Int;
  93. var groupindex(default, null):Dict<String, Int>;
  94. var pattern(default, null):String;
  95. }
  96. @:pythonImport("re")
  97. extern class Re {
  98. static var A:Int;
  99. static var ASCII:Int;
  100. static var DEBUG:Int;
  101. static var I:Int;
  102. static var IGNORECASE:Int;
  103. static var L:Int;
  104. static var LOCALE:Int;
  105. static var M:Int;
  106. static var MULTILINE:Int;
  107. static var S:Int;
  108. static var DOTALL:Int;
  109. static var X:Int;
  110. static var VERBOSE:Int;
  111. static var U:Int;
  112. static var UNICODE:Int;
  113. static function compile(pattern:String, ?flags:Int = 0):Regex;
  114. static function match(pattern:Pattern, string:String, flags:Int = 0):Null<MatchObject>;
  115. static function search(pattern:Pattern, string:String, flags:Int = 0):Null<MatchObject>;
  116. static function split(pattern:Pattern, string:String, maxsplit:Int = 0, flags:Int = 0):Array<String>;
  117. static inline function findallDynamic(pattern:Pattern, string:String, flags:Int = 0):Array<Dynamic> {
  118. return python.Syntax.field(pattern, "findall")(string, flags);
  119. }
  120. static inline function findallString(pattern:Pattern, string:String, flags:Int = 0):Array<String> {
  121. return python.Syntax.field(pattern, "findall")(string, flags);
  122. }
  123. 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. 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. 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. static function sub(pattern:Pattern, repl:MatchObject->String, string:String, ?count:Int = 0, ?flags:Int = 0):String;
  132. static function subn(pattern:Pattern, repl:Repl, string:String, count:Int = 0, flags:Int = 0):String;
  133. static function escape(string:String):String;
  134. static function purge():Void;
  135. }