aboutsummaryrefslogtreecommitdiff
path: root/Lua/README.md
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-09-25 00:20:21 -0700
committerBruce Hill <bruce@bruce-hill.com>2021-09-25 00:20:21 -0700
commit43911ca583478231000828183e4f7175bfa8227b (patch)
tree487aa87793fb1a1b3a7ec502f4d4e47c03c0e286 /Lua/README.md
parent9e8310705d72ac6d0b2b71b8a072d30a496f1ae8 (diff)
Updated docs
Diffstat (limited to 'Lua/README.md')
-rw-r--r--Lua/README.md26
1 files changed, 23 insertions, 3 deletions
diff --git a/Lua/README.md b/Lua/README.md
index f58e952..25ccf5f 100644
--- a/Lua/README.md
+++ b/Lua/README.md
@@ -6,11 +6,31 @@ roughly equivalent in usefulness to LPEG, but with a smaller codebase (roughly
## API
+The Lua `bp` bindings provide the following methods:
+
+```lua
+bp.match(text, pattern, [start_index]) --> match, match_start, match_length / nil
+bp.replace(text, pattern, replacement, [start_index]) --> text_with_replacements, num_replacements
+```
+
+Match objects returned by `bp.match()` are tables whose `__tostring` will
+return the text of the match. Additionally, match objects store the text of the
+match at index `0`, and any captures stored as match objects with a key
+corresponding to the capture's identifier (e.g. `@"a" @foo="bc"` will be
+encoded as `{[0]="abc", [1]="a", foo={[0]="bc"}}`. If multiple captures within
+a match share the same identifier, it is unspecified which captured match will
+be stored at that key, so it's best to be unambiguous.
+
+All methods will raise an error with a descriptive message if the given pattern
+has a syntax error.
+
+## Example Usage
+
```lua
local bp = require("bp")
local m, i, len = bp.match("like finding a needle in a haystack", '"n" @Es=+`e "dle"')
---> {[0]="needle", Es={[0]="ee"}}
+--> {[0]="needle", Es={[0]="ee"}}, 16, 6
--> tostring(m) == "needle", tostring(m.Es) == "ee"
-local replaced = bp.match("like finding a needle in a haystack", '"n" +`e "dle"', "cat")
---> "like finding a cat in a haystack"
+local replaced, nreplacements = bp.match("like finding a needle in a haystack", '"n" +`e "dle"', "cat")
+--> "like finding a cat in a haystack", 1
```