Browse Source

[lua] add more lpeg grammars and operators

Justin Donaldson 5 years ago
parent
commit
616dff5dbe

+ 9 - 0
std/lua/lib/cjson/Cjson.hx

@@ -0,0 +1,9 @@
+package lua.lib.cjson;
+
+@:luaRequire("cjson")
+extern class Cjson {
+    public static function encode(obj:Dynamic) : String;
+    public static function decode(str:String) : Dynamic;
+    public static function decode_array_with_array_mt(?enabled:Bool) : Void;
+}
+

+ 4 - 3
std/lua/lib/lpeg/Grammar.hx

@@ -1,6 +1,7 @@
-
-abstract Grammar<Table<String, String>>{
-    public funtion new(name:String, rules : StringMap<String>) {
+package lua.lib.lpeg;
+import haxe.ds.StringMap;
+abstract Grammar(Table<String,String>){
+    public function new(name:String, rules : StringMap<String>) {
        this = Table.fromMap(rules);
        this[0] = name;
     }

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

@@ -1,5 +1,23 @@
+package lua.lib.lpeg;
 
 @:luaRequire("lpeg")
 extern class Lpeg {
-    static function match
+    public static function match(pattern : Pattern, subject:String, ?init:Int) : Matched;
+
+    /**
+      If the given value is a pattern, returns the string "pattern". Otherwise returns nil.
+     **/
+    public static function type(value : Dynamic) : String;
+    /**
+      Returns a string with the running version of LPeg.
+     **/
+    public static function version() : String;
+
+    /**
+      Sets a limit for the size of the backtrack stack used by LPeg to track calls and choices. (The default limit is 400.) Most well-written patterns need little backtrack levels and therefore you seldom need to change this limit; before changing it you should try to rewrite your pattern to avoid the need for extra space. Nevertheless, a few useful patterns may overflow. Also, with recursive grammars, subjects with deep recursion may also need larger limits.
+     **/
+    public static function setmaxstack(max:Int) : Void;
+
+    public static function locale(arg:Table<String,Pattern>) : Table<String,Pattern>;
+
 }

+ 1 - 1
std/lua/lib/lpeg/Matched.hx

@@ -1,4 +1,4 @@
-
+package lua.lib.lpeg;
 abstract Matched(Int) {
     inline public function new(d:Dynamic) {
         this = d;

+ 62 - 38
std/lua/lib/lpeg/Pattern.hx

@@ -1,28 +1,25 @@
-package lpeg;
+package lua.lib.lpeg;
 import haxe.extern.EitherType;
+import haxe.extern.Rest;
 import lua.Table;
 import haxe.Constraints.Function;
 
-@:luaRequire("lpeg.P")
+
+
+@:luaRequire("lpeg")
 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.
-
-     * If the argument is a string, it is translated to a pattern that matches the string literally.
-
-     * If the argument is a non-negative number n, the result is a pattern that matches exactly n characters.
-
-     * 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).
-
-     * 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.
-
-     * 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.
+        * If the argument is a pattern, it is returned unmodified.
+        * If the argument is a string, it is translated to a pattern that matches the string literally.
+        * If the argument is a non-negative number n, the result is a pattern that matches exactly n characters.
+        * 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).
+        * 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.
+        * 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.
      **/
-    public function new(value : PatternArgument);
+    @:native("P")
+    public function new(value : PatternArgument) : 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).
 
@@ -30,32 +27,59 @@ 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:PatternCapture, ?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) : PatternCapture)
-    public function match(subject:String, ?init:Int) : Matched;
+    @: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 times(times:Int) : Pattern {
+        return untyped __lua__("{}^{}", this, time);
+    }
 
-    /**
-      If the given value is a pattern, returns the string "pattern". Otherwise returns nil.
-     **/
-    public static function type(value : Dynamic) : String;
-    /**
-      Returns a string with the running version of LPeg.
-     **/
-    public static function version() : String;
-
-    /**
-      Sets a limit for the size of the backtrack stack used by LPeg to track calls and choices. (The default limit is 400.) Most well-written patterns need little backtrack levels and therefore you seldom need to change this limit; before changing it you should try to rewrite your pattern to avoid the need for extra space. Nevertheless, a few useful patterns may overflow. Also, with recursive grammars, subjects with deep recursion may also need larger limits.
-     **/
-    public static function setmaxstack(max:Int) : Void;
+}
 
+extern class BeforePattern extends Pattern {
+    @:native("B")
+    public function new(value : PatternArgument) : Void;
 }
 
-extern class PatternCapture extend Pattern {}
+extern class RangePattern extends Pattern {
+    @:native("R")
+    public function new(args:Rest<String>) : Void;
+}
 
-typedef PatternArgument = EitherType<Pattern, EitherType<String, EitherType<Int, EitherType<Bool, EitherType<AnyTable, Function>>>>>;
+extern class StringPattern extends Pattern {
+    @:native("S")
+    public function new(arg:String) : Void;
+}
 
+extern class VariablePattern extends Pattern {
+    @:native("V")
+    public function new(arg:String) : Void;
+}
 
+extern class CapturePattern extends Pattern {
+}
 
+typedef PatternArgument = EitherType<Pattern, EitherType<String, EitherType<Int, EitherType<Bool, EitherType<AnyTable, Function>>>>>;