소스 검색

[lua] more lpeg components

Justin Donaldson 5 년 전
부모
커밋
db40d96ee0
3개의 변경된 파일52개의 추가작업 그리고 35개의 파일을 삭제
  1. 7 0
      std/lua/lib/lpeg/Grammar.hx
  2. 24 0
      std/lua/lib/lpeg/Matched.hx
  3. 21 35
      std/lua/lib/lpeg/Pattern.hx

+ 7 - 0
std/lua/lib/lpeg/Grammar.hx

@@ -0,0 +1,7 @@
+
+abstract Grammar<Table<String, String>>{
+    public funtion new(name:String, rules : StringMap<String>) {
+       this = Table.fromMap(rules);
+       this[0] = name;
+    }
+}

+ 24 - 0
std/lua/lib/lpeg/Matched.hx

@@ -0,0 +1,24 @@
+
+abstract Matched(Int) {
+    inline public function new(d:Dynamic) {
+        this = d;
+    }
+    inline public function matched() : Bool {
+        return Std.isOfType(this, Int) || Lua.next(cast this) != null;
+    }
+    inline public function captures() : Table<String,String> {
+        if (Std.isOfType(this,Table)) {
+            return cast this;
+        } else {
+            return Table.create();
+        }
+    }
+    inline public function position() : Int {
+        if (Std.isOfType(this, Int)) {
+            return this;
+        } else {
+            return null;
+        }
+    }
+}
+

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

@@ -6,30 +6,35 @@ import haxe.Constraints.Function;
 @:luaRequire("lpeg.P")
 extern class Pattern {
     /**
-        Converts the given value into a proper pattern, according to the following rules:
+      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 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 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 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 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 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 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 function, returns a pattern equivalent to a match-time capture over the empty string.
+     **/
     public function new(value : PatternArgument);
     /**
-        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).
+      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.
+      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;
+      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;
 
     /**
@@ -42,34 +47,15 @@ extern class Pattern {
     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.
+      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 PatternCapture extend Pattern {}
+
 typedef PatternArgument = EitherType<Pattern, EitherType<String, EitherType<Int, EitherType<Bool, EitherType<AnyTable, Function>>>>>;
 
-abstract Matched(Int) {
-    inline public function new(d:Dynamic) {
-        this = d;
-    }
-    inline public function matched() : Bool {
-        return Std.isOfType(this, Int) || Lua.next(cast this) != null;
-    }
-    inline public function captures() : Table<String,String> {
-        if (Std.isOfType(this,Table)) {
-            return cast this;
-        } else {
-            return Table.create();
-        }
-    }
-    inline public function position() : Int {
-        if (Std.isOfType(this, Int)) {
-            return this;
-        } else {
-            return null;
-        }
-    }
-}
+