Procházet zdrojové kódy

[lua] wip on jsonparser

Justin Donaldson před 5 roky
rodič
revize
16b45c48dc
2 změnil soubory, kde provedl 99 přidání a 90 odebrání
  1. 15 1
      std/lua/lib/lpeg/Lpeg.hx
  2. 84 89
      std/lua/lib/lpeg/Pattern.hx

+ 15 - 1
std/lua/lib/lpeg/Lpeg.hx

@@ -2,7 +2,21 @@ package lua.lib.lpeg;
 
 @:luaRequire("lpeg")
 extern class Lpeg {
-    public static function match(pattern : Pattern, subject:String, ?init:Int) : Matched;
+    /**
+      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).
+
+      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.
+
+      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;
+     **/
+
+    @:overload(function(subject:CapturePattern, ?init:Int) : Table<String,String> {})
+    @:overload(function(subject:Pattern, ?init:Int) : Int {})
+    @:overload(function(subject:Int, ?init:Int) : Int {})
+    @:overload(function(subject:Bool, ?init:Int) : Bool {})
+    @:overload(function(subject:Grammar, ?init:Int) : CapturePattern {})
+    public function match(pattern: Pattern, subject:String, ?init:Int) : Int;
+
 
     /**
       If the given value is a pattern, returns the string "pattern". Otherwise returns nil.

+ 84 - 89
std/lua/lib/lpeg/Pattern.hx

@@ -1,14 +1,39 @@
 package lua.lib.lpeg;
-import haxe.extern.EitherType;
-import haxe.extern.Rest;
 import lua.Table;
-import haxe.Constraints.Function;
 import haxe.ds.StringMap;
 
+typedef AnyPattern = PatternImpl<Dynamic>;
 
+abstract Pattern<This:PatternImpl<This>>(PatternImpl<This>) {
+    public function new(value : PatternImpl<This>)  {
+        this = value;
+    }
+    @:op(A + B)
+    public inline function add(p:AnyPattern) : This { return cast cast(this, Int) + cast(p, Int);}
+
+    @:op(A - B)
+    public inline function sub(p:AnyPattern) : This { return cast cast(this, Int) - cast(p, Int);}
+
+    @:op(A * B)
+    public inline function mul(p:AnyPattern) : This { return cast cast(this, Int) * cast(p, Int);}
+
+    @:op(A == B)
+    public inline function eq(p:AnyPattern) : This { return cast cast(this, Int) == cast(p, Int);}
+
+    @:op(A ^ B)
+    public inline function pow(n:Int) : This { return cast cast(this, Int) ^ n;}
+    @:op(-A)
+    public inline function unm() : This { return cast -cast(this, Int);}
+
+    public inline function len() : This { return untyped __lua_length__(this);}
+    public inline function match(subject:String, ?init:Int) : Int {
+        return this.match(subject, init);
+    }
+
+}
 
 @:luaRequire("lpeg")
-extern class Pattern {
+extern class PatternImpl<This:PatternImpl<This>> {
     /**
       Converts the given value into a proper pattern, according to the following rules:
         * If the argument is a pattern, it is returned unmodified.
@@ -20,7 +45,8 @@ extern class Pattern {
         * If the argument is a function, returns a pattern equivalent to a match-time capture over the empty string.
      **/
     @:native("P")
-    public function new(value : PatternArgument) : Void;
+    public function new(value : This) : Void;
+
     /**
       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).
 
@@ -28,96 +54,65 @@ 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;
      **/
-    @:overload(function(subject:CapturePattern, ?init:Int) : Table<String,String> {})
-    @:overload(function(subject:Pattern, ?init:Int) : Int {})
-    @:overload(function(subject:Int, ?init:Int) : Int {})
-    @:overload(function(subject:Bool, ?init:Int) : Bool {})
-    @:overload(function(subject:Grammar, ?init:Int) : CapturePattern {})
     public function match(subject:String, ?init:Int) : Int;
-
-    public inline function and() : Pattern {
-        return untyped __lua__("#{}", this);
-    }
-
-    public inline function invert() : Pattern {
-        return untyped __lua__("-{}", this);
-    }
-
-    public inline function either(p:Pattern) : Pattern {
-        return untyped __lua__("{} + {}", this, p);
-    }
-
-    public inline function not(p:Pattern) : Pattern {
-        return untyped __lua__("{} - {}", this, p);
-    }
-
-    public inline function next(p:Pattern) : Pattern {
-        return untyped __lua__("{} - {}", this, p);
-    }
-    public inline function repeat(times:Int) : Pattern {
-        return untyped __lua__("{}^{}", this, time);
-    }
-
 }
 
-extern class BeforePattern extends Pattern {
+extern class BeforePattern extends PatternImpl<BeforePattern> {
     @:native("B")
-    public function new(value : PatternArgument);
+    public function new(value : AnyPattern);
 }
 
-extern class RangePattern extends Pattern {
-    @:native("R")
-    public function new(args:Rest<String>);
-}
-
-extern class StringPattern extends Pattern {
-    @:native("S")
-    public function new(arg:String);
-}
-
-extern class VariablePattern extends Pattern {
-    @:native("V")
-    public function new(arg:String);
-}
-
-extern class CapturePattern extends Pattern {
-    @:native("C")
-    public function new(pattern:CapturePattern);
-}
-
-extern class CaptureArgPattern extends Pattern {
-    @:native("Carg")
-    public function new(n:Int);
-}
-
-extern class CaptureBeforePattern extends Pattern {
-    @:native("Cb")
-    public function new(name:String);
-}
-
-extern class CaptureValuesPattern extends Pattern {
-    @:native("Cc")
-    public function new(values : String);
-}
-
-extern class CaptureFoldingPattern extends Pattern {
-    @:native("Cf")
-    public function new(pattern:Pattern, func: String->Dynamic);
-}
-
-extern class CaptureTaggedPattern extends Pattern {
-    @:native("Cg")
-    public function new(pattern:Pattern, ?name:String);
-}
-
-extern class CapturePosition extends Pattern {
-    @:native("Cp")
-    public function new();
-}
-
-
-
-typedef PatternArgument = EitherType<Pattern, EitherType<String, EitherType<Int, EitherType<Bool, EitherType<AnyTable, Function>>>>>;
+// extern class RangePattern extends Pattern {
+//     @:native("R")
+//     public function new(args:Rest<String>);
+// }
+
+// extern class StringPattern extends Pattern {
+//     @:native("S")
+//     public function new(arg:String);
+// }
+
+// extern class VariablePattern extends Pattern {
+//     @:native("V")
+//     public function new(arg:String);
+// }
+
+// extern class CapturePattern extends Pattern {
+//     @:native("C")
+//     public function new(pattern:CapturePattern);
+// }
+
+// extern class CaptureArgPattern extends Pattern {
+//     @:native("Carg")
+//     public function new(n:Int);
+// }
+
+// extern class CaptureBeforePattern extends Pattern {
+//     @:native("Cb")
+//     public function new(name:String);
+// }
+
+// extern class CaptureValuesPattern extends Pattern {
+//     @:native("Cc")
+//     public function new(values : String);
+// }
+
+// extern class CaptureFoldingPattern extends Pattern {
+//     @:native("Cf")
+//     public function new(pattern:Pattern, func: String->Dynamic);
+// }
+
+// extern class CaptureTaggedPattern extends Pattern {
+//     @:native("Cg")
+//     public function new(pattern:Pattern, ?name:String);
+// }
+
+// extern class CapturePosition extends Pattern {
+//     @:native("Cp")
+//     public function new();
+// }
+
+// typedef PatternArgument<Pattern> = EitherType<Pattern, EitherType<String, EitherType<Int, EitherType<Bool, EitherType<AnyTable, Function>>>>>;
 
 abstract Grammar(Table<String,String>) to Table<String,String>{
     public function new(name:String, rules : StringMap<String>) {