Pattern.hx 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package lua.lib.lpeg;
  2. import lua.lib.lpeg.Pattern.PatternMetatable as M;
  3. import haxe.extern.Rest;
  4. import haxe.extern.EitherType as E;
  5. import lua.Table;
  6. import haxe.Constraints.Function;
  7. typedef PatternArgument = E<Pattern, E<String, E<Int, E<Bool, E<Table<String,String>, Function>>>>>;
  8. @:luaRequire("lpeg")
  9. extern class PatternMetatable {
  10. static public function __add(v1:Dynamic, v2:Dynamic) : Pattern;
  11. static public function __mul(v1:Dynamic, v2:Dynamic) : Pattern;
  12. static public function __pow(v1:Dynamic, v2:Dynamic) : Pattern;
  13. static public function __div(v1:Dynamic, v2:Dynamic) : Pattern;
  14. static inline function __init__() : Void {
  15. untyped PatternMetatable = __lua__("getmetatable(require('lpeg').P(1))");
  16. }
  17. }
  18. @:forward(match)
  19. abstract Pattern(PatternImpl){
  20. @:op(A + B)
  21. public static inline function add(p1:Pattern, p2 : Pattern) : Pattern return M.__mul(p1, p2);
  22. @:op(A + B)
  23. public static inline function addString(p : Pattern, str:String) : Pattern return M.__mul(p, str);
  24. @:op(A + B)
  25. public static inline function addStringPre(str : String, p : Pattern) : Pattern return M.__mul(str, p);
  26. @:op(A | B)
  27. public static inline function or(p1:Pattern, p2:Pattern) : Pattern return M.__add(p1, p2);
  28. @:op(A | B)
  29. public static inline function orString(p:Pattern, s:String) : Pattern return M.__add(p, s);
  30. @:op(A | B)
  31. public static inline function orStringPre(s:String, p:Pattern) : Pattern return M.__add(s, p);
  32. @:op(A * B)
  33. public static inline function mul(p: Pattern, n:Int) : Pattern return M.__pow(p, n);
  34. @:op(A >> B)
  35. public static inline function divf(p : Pattern, f:Function) : Pattern return M.__div(p, f);
  36. @:op(A >> B)
  37. public static inline function divi(p : Pattern, i:Int) : Pattern return M.__div(p, i);
  38. public inline function len() : Pattern { return untyped __lua_length__(this);}
  39. inline public static function P(p:PatternArgument) : Pattern return PatternImpl.P(p);
  40. inline public static function R(arg:String) : Pattern return PatternImpl.R(arg);
  41. inline public static function Cf(p:Pattern, f : Function) : Pattern return PatternImpl.Cf(p, f);
  42. inline public static function Ct(p:Pattern) : Table<Dynamic, String> return PatternImpl.Ct(p);
  43. inline public static function Cc(val:Dynamic) : Pattern return PatternImpl.Cc(val);
  44. inline public static function Ca(p:Pattern) : Pattern {
  45. return Cf(Cc([]) + p, (acc:Array<String>, v : Dynamic)-> {
  46. acc.push(v);
  47. return acc;
  48. });
  49. }
  50. }
  51. @:luaRequire("lpeg")
  52. extern class PatternImpl {
  53. /**
  54. Converts the given value into a proper pattern, according to the following rules:
  55. * If the argument is a pattern, it is returned unmodified.
  56. * If the argument is a string, it is translated to a pattern that matches the string literally.
  57. * If the argument is a non-negative number n, the result is a pattern that matches exactly n characters.
  58. * If the argument is a negative number -n, the result is a pattern that succeeds only if the input string has less than n characters left: lpeg.P(-n) is equivalent to -lpeg.P(n) (see the unary minus operation).
  59. * If the argument is a boolean, the result is a pattern that always succeeds or always fails (according to the boolean value), without consuming any input.
  60. * If the argument is a table, it is interpreted as a grammar (see Grammars).
  61. * If the argument is a function, returns a pattern equivalent to a match-time capture over the empty string.
  62. **/
  63. /**
  64. The matching function. It attempts to match the given pattern against the subject string. If the match succeeds, returns the index in the subject of the first character after the match, or the captured values (if the pattern captured any value).
  65. An optional numeric argument init makes the match start at that position in the subject string. As usual in Lua libraries, a negative value counts from the end.
  66. Unlike typical pattern-matching functions, match works only in anchored mode; that is, it tries to match the pattern with a prefix of the given subject string (at position init), not with an arbitrary substring of the subject. So, if we want to find a pattern anywhere in a string, we must either write a loop in Lua or write a pattern that matches anywhere. This second approach is easy and quite efficient;
  67. **/
  68. public static function P(p:PatternArgument) : Pattern;
  69. public static function R(args:Rest<String>) : Pattern;
  70. public static function Cf(p:Pattern, f : Function) : Pattern;
  71. public static function Cc(val:Rest<Dynamic>) : Pattern;
  72. public static function Ct(p:Pattern) : Table<String,String>;
  73. public function match(subject:String, ?init:Int) : Dynamic;
  74. }