Explorar o código

slightly less wonky Pattern class

Justin Donaldson %!s(int64=5) %!d(string=hai) anos
pai
achega
e01c25ee3e
Modificáronse 4 ficheiros con 92 adicións e 25 borrados
  1. 35 23
      std/lua/lib/lpeg/Pattern.hx
  2. 3 0
      std/lua/lib/lpeg/Predef.hx
  3. 5 2
      std/lua/lib/lpeg/Re.hx
  4. 49 0
      std/lua/lib/lpeg/Rehx.hx

+ 35 - 23
std/lua/lib/lpeg/Pattern.hx

@@ -1,48 +1,62 @@
 package lua.lib.lpeg;
-import lua.Table;
-import haxe.ds.StringMap;
-import haxe.extern.Rest;
 import haxe.Constraints.Function;
 import lua.lib.lpeg.Pattern.PatternMetatable as M;
 
+import haxe.extern.Rest;
+import haxe.extern.EitherType as E;
+import lua.Table;
+import haxe.Constraints.Function;
+
+typedef PatternArgument = E<Pattern, E<String, E<Int, E<Bool, E<Table<String,String>, Function>>>>>;
+
 @:luaRequire("lpeg")
 extern class PatternMetatable {
-    static public function __add(v1:Dynamic, v2:Dynamic) : AbstractPattern;
-    static public function __mul(v1:Dynamic, v2:Dynamic) : AbstractPattern;
-    static public function __pow(v1:Dynamic, v2:Dynamic) : AbstractPattern;
-    static public function __div(v1:Dynamic, v2:Dynamic) : AbstractPattern;
+    static public function __add(v1:Dynamic, v2:Dynamic) : Pattern;
+    static public function __mul(v1:Dynamic, v2:Dynamic) : Pattern;
+    static public function __pow(v1:Dynamic, v2:Dynamic) : Pattern;
+    static public function __div(v1:Dynamic, v2:Dynamic) : Pattern;
     static inline function __init__() : Void {
         untyped PatternMetatable = __lua__("getmetatable(require('lpeg').P(1))");
     }
 }
 
-
 @:forward(match)
-abstract AbstractPattern(Pattern){
+abstract Pattern(PatternImpl){
     @:op(A + B)
-    public static inline function add(p1:AbstractPattern, p2 : AbstractPattern) : AbstractPattern  return M.__mul(p1, p2);
+    public static inline function add(p1:Pattern, p2 : Pattern) : Pattern  return M.__mul(p1, p2);
 
     @:op(A + B)
-    public static inline function addString(p : AbstractPattern, str:String) : AbstractPattern  return M.__mul(p, str);
+    public static inline function addString(p : Pattern, str:String) : Pattern  return M.__mul(p, str);
 
     @:op(A + B)
-    public static inline function addStringPre(str : String, p : AbstractPattern) : AbstractPattern  return M.__mul(str, p);
+    public static inline function addStringPre(str : String, p : Pattern) : Pattern  return M.__mul(str, p);
+
+    @:op(A | B)
+    public static inline function or(p1:Pattern, p2:Pattern) : Pattern  return M.__add(p1, p2);
 
-    // @:op(A | B)
-    // public inline function or(p:AbstractPattern) : AbstractPattern  return M.__add(this, p);
+    @:op(A | B)
+    public static inline function orString(p:Pattern, s:String) : Pattern  return M.__add(p, s);
+
+    @:op(A | B)
+    public static inline function orStringPre(s:String, p:Pattern) : Pattern  return M.__add(s, p);
 
     @:op(A * B)
-    public static inline function mul(p: AbstractPattern, n:Int) : AbstractPattern  return M.__pow(p, n);
+    public static inline function mul(p: Pattern, n:Int) : Pattern  return M.__pow(p, n);
 
     @:op(A >> B)
-    public static inline function divf(p : AbstractPattern, f:Function) : AbstractPattern  return M.__div(p, f);
+    public static inline function divf(p : Pattern, f:Function) : Pattern  return M.__div(p, f);
+
+    public inline function len() : Pattern { return untyped __lua_length__(this);}
 
-    public inline function len() : AbstractPattern { return untyped __lua_length__(this);}
+    inline public static function P(p:PatternArgument) : Pattern  return PatternImpl.P(p);
+    inline public static function R(arg:String) : Pattern return PatternImpl.R(arg);
+    inline public static function Cf(p:Pattern, f : Function) : Pattern return PatternImpl.Cf(p, f);
 }
 
 
+
 @:luaRequire("lpeg")
-extern class Pattern {
+extern class PatternImpl {
     /**
       Converts the given value into a proper pattern, according to the following rules:
         * If the argument is a pattern, it is returned unmodified.
@@ -61,11 +75,9 @@ extern class Pattern {
 
       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;
      **/
-    public static function R(args:Rest<String>) : AbstractPattern;
-    public static function Cf(p:AbstractPattern, f : Function) : AbstractPattern;
+    public static function P(p:PatternArgument) : Pattern;
+    public static function R(args:Rest<String>) : Pattern;
+    public static function Cf(p:Pattern, f : Function) : Pattern;
     public function match(subject:String, ?init:Int) : Int;
-    public function __add(p2:AbstractPattern) : AbstractPattern;
-    public function __mul(n:Int) : AbstractPattern;
-    public function __div(n:Dynamic) : AbstractPattern;
 }
 

+ 3 - 0
std/lua/lib/lpeg/Predef.hx

@@ -0,0 +1,3 @@
+extern class Predef {
+    var a : Pattern;
+}

+ 5 - 2
std/lua/lib/lpeg/Re.hx

@@ -4,6 +4,9 @@ import lua.lib.lpeg.Pattern;
 
 @:luaRequire("re")
 extern class Re {
-    public static function compile(str : String, ?def : Dynamic) : AbstractPattern;
+    public static function compile(str : String, ?def : Dynamic) : Pattern;
+    public static function match(subject : String, pattern : Pattern, ?index : Int) : String;
+    public static function find(subject : String, pattern : Pattern, ?index : Int) : Int;
+    public static function gsub(subject : String, pattern : Pattern, replace : String) : String;
+    public static function updatelocale() : Void;
 }
-

+ 49 - 0
std/lua/lib/lpeg/Rehx.hx

@@ -0,0 +1,49 @@
+package lua.lib.lpeg;
+import lua.lib.lpeg.Pattern;
+
+class Rehx {
+    public static function compile(str : String, ?def : Dynamic) : Pattern {
+        return null;
+    }
+
+    static var I =  Pattern.P((s:String,i:Int)-> lua.Lua.print(i, s.substring(0,i)));
+
+    static function getDef(id:Dynamic, defs : Table<String, String>){
+        if (defs[id] != null) {
+            return defs[id];
+        } else {
+            throw ('undefined name: ${id}');
+            return null;
+        }
+    }
+
+    static function patError(s:String, i:Int){
+        var sub = if (s.length <  (i + 20)){
+            s.substring(i);
+        } else {
+            s.substring(i, i+20) + "...";
+        }
+        throw('pattern error near $sub');
+    }
+
+    static function mult(p : Pattern, n : Int){
+        var np = Pattern.P(true);
+        while (n >= 1){
+            if (n%2 >=1) np = np + p;
+            p = p + p;
+            n = Std.int(n/2);
+        }
+        return np;
+    }
+
+    static function equalCap(s:String, i:Int, c:String) : Int {
+        if (lua.Lua.type(c)  != "string") return null;
+        var e = c.length + i;
+        if (s.substring(i, e-1) == c) {
+            return e;
+        } else return null;
+
+    }
+
+
+}