Browse Source

wip on abstract pattern

Justin Donaldson 5 years ago
parent
commit
7951c7b333
4 changed files with 134 additions and 88 deletions
  1. 44 0
      std/lua/_peg/json.peg
  2. 54 0
      std/lua/lib/lpeg/Junk.txt
  3. 27 88
      std/lua/lib/lpeg/Pattern.hx
  4. 9 0
      std/lua/lib/lpeg/Re.hx

+ 44 - 0
std/lua/_peg/json.peg

@@ -0,0 +1,44 @@
+json            <- (object /  array) !.
+object          <- begin_object
+                        %member_list
+                   end_object
+member          <- {: string name_separator value :}
+array           <- begin_array
+                        {| (value (value_separator value)* )? |}
+                   end_array
+
+number          <- { "-" ? int frac ? exp ? } => tonumber
+exp             <- [Ee] [+-] ? [0-9]+
+frac            <- "." [0-9]+
+int             <- "0" / ( [1-9] [0-9]* )
+string          <- '"' char* -> {} => final_string '"'
+char            <- unescaped    => normal
+                / '\"'          => escape
+                / "\\"          => escape
+                / "\b"          => escape
+                / "\f"          => escape
+                / "\n"          => escape
+                / "\r"          => escape
+                / "\t"          => escape
+                / "\/"          => escape
+                / (
+                    "\u"
+                    { [0-9A-Fa-f]^4 }  => tou16
+                  )+  -> {}            => unicode
+
+unescaped       <- [^\"%c]
+value           <- "false"      => retfalse
+                /  "null"       => retnil
+                /  "true"       => rettrue
+                /  object
+                /  array
+                /  number
+                /  string
+
+begin_array     <- ws "[" ws
+end_array       <- ws "]" ws
+begin_object    <- ws "{" ws
+end_object      <- ws "}" ws
+name_separator  <- ws ":" ws
+value_separator <- ws "," ws
+ws              <- %WS*

+ 54 - 0
std/lua/lib/lpeg/Junk.txt

@@ -0,0 +1,54 @@
+// @:forward(match)
+// abstract AbstractPattern(Pattern){
+//     @:op(A | B)
+//     public static inline function either(p1:AbstractPattern, p2:AbstractPattern) : AbstractPattern {
+//         return untyped __lua__("{0} + {1}", p1, p2);
+//     }
+
+//     @:op(A - B)
+//     public static inline function negate(p1:AbstractPattern, p2:AbstractPattern) : AbstractPattern {
+//         return untyped __lua__("{0} - {1}", p1, p2);
+//     }
+
+//     @:op(~A)
+//     public static inline function exists(p1:AbstractPattern) : AbstractPattern {
+//         return untyped __lua__("#{0}", p1);
+//     }
+
+//     @:op(A + B)
+//     public static inline function concat(p1 : AbstractPattern, p2 : AbstractPattern) : AbstractPattern {
+//         return untyped __lua__("{0} * {1}", p1, p2);
+//     }
+
+//     @:op(A + B)
+//     public static inline function concatStringLhs(str : String, p : AbstractPattern) : AbstractPattern {
+//         return untyped __lua__("{0} * {1}", str, p);
+//     }
+
+//     @:op(A + B)
+//     public static inline function concatStringRhs(p : AbstractPattern, str : String) : AbstractPattern {
+//         return untyped __lua__("{0} * {1}", p, str);
+//     }
+
+//     @:op(A * B)
+//     public static inline function repeat(p : AbstractPattern, n : Int) : AbstractPattern {
+//         return untyped __lua__("{0}^{1}", p, n);
+//     }
+
+//     @:op(A >> B)
+//     public static inline function captureFunction(p : AbstractPattern, f:haxe.Constraints.Function) : AbstractPattern {
+//         return untyped __lua__("{0} / {1}", p, f);
+//     }
+
+
+//     @:op(A >> B)
+//     public static inline function capturePosition(p : AbstractPattern, n:Int) : AbstractPattern {
+//         return untyped __lua__("{0} / {1}", p, n);
+//     }
+
+//     @:op(A ^ B)
+//     public static inline function pow(p : AbstractPattern, n:Float) : AbstractPattern {
+//         return Meta.__pow(p, n);
+//     }
+
+// }

+ 27 - 88
std/lua/lib/lpeg/Pattern.hx

@@ -1,39 +1,39 @@
 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;
 
-typedef AnyPattern = PatternImpl<Dynamic>;
-
-abstract Pattern<This:PatternImpl<This>>(PatternImpl<This>) {
-    public function new(value : PatternImpl<This>)  {
-        this = value;
+@:luaRequire("lpeg")
+extern class PatternMetatable {
+    static public function __add(p1:Pattern, p2:AbstractPattern) : AbstractPattern;
+    static public function __mul(p1:Pattern, n:Int) : AbstractPattern;
+    static public function __pow(p1:Pattern, n:Int) : AbstractPattern;
+    static public function __div(p1:Pattern, n:Dynamic) : AbstractPattern;
+    static inline function __init__() : Void {
+        untyped PatternMetatable = __lua__("getmetatable(require('lpeg').P(1))");
     }
-    @: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);}
+@:forward(match)
+abstract AbstractPattern(Pattern){
+    @:op(A + B)
+    public inline function add(p:AbstractPattern) : AbstractPattern  return M.__add(this, p);
 
-    @: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);}
+    @:op(A * B)
+    public inline function mul(n:Int) : AbstractPattern  return M.__pow(this, n);
 
-    public inline function len() : This { return untyped __lua_length__(this);}
-    public inline function match(subject:String, ?init:Int) : Int {
-        return this.match(subject, init);
-    }
+    @:op(A / B)
+    public inline function divf(f:Function) : AbstractPattern  return M.__div(this, f);
 
+    public inline function len() : AbstractPattern { return untyped __lua_length__(this);}
 }
 
+
 @:luaRequire("lpeg")
-extern class PatternImpl<This:PatternImpl<This>> {
+extern class Pattern {
     /**
       Converts the given value into a proper pattern, according to the following rules:
         * If the argument is a pattern, it is returned unmodified.
@@ -44,8 +44,6 @@ extern class PatternImpl<This:PatternImpl<This>> {
         * If the argument is a table, it is interpreted as a grammar (see Grammars).
         * 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 : 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).
@@ -54,69 +52,10 @@ extern class PatternImpl<This:PatternImpl<This>> {
 
       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 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;
 }
 
-extern class BeforePattern extends PatternImpl<BeforePattern> {
-    @:native("B")
-    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<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>) {
-       this = Table.fromMap(rules);
-       this[0] = name;
-    }
-}

+ 9 - 0
std/lua/lib/lpeg/Re.hx

@@ -0,0 +1,9 @@
+package lua.lib.lpeg;
+
+import lua.lib.lpeg.Pattern;
+
+@:luaRequire("re")
+extern class Re {
+    public static function compile(str : String, ?def : Dynamic) : AbstractPattern;
+}
+