Skip to content

Commit 27e1eba

Browse files
committed
clean up annotations
1 parent 4371f16 commit 27e1eba

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

LuaCATS/jsregexp/core.lua

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,51 @@
22

33
local jsregexp = {}
44

5+
---
56
---Compile a regular expression. Throws if an error occurs.
7+
---
8+
---Example:
69
---```lua
710
--- local re = jsregexp.compile("\\w+", "g")
811
---```
12+
---
913
---@param re string
1014
---@param flags? string
1115
---@return JSRegExp.RegExp RegExp
1216
function jsregexp.compile(re, flags) end
1317

18+
---
1419
---Safely compile a regular expression.
20+
---
21+
---Example:
1522
---```lua
1623
--- local re, err = jsregexp.compile_safe("\\w+", "g")
1724
--- if not re then
1825
--- print(err)
1926
--- end
2027
---```
28+
---
2129
---@param re string
2230
---@param flags? string
2331
---@return JSRegExp.RegExp? RegExp
2432
---@return string? Error
2533
function jsregexp.compile_safe(re, flags) end
2634

35+
---
2736
---Convert a lua utf8 lua string to a utf16 js string. For internal use.
37+
---
2838
---@param str string
2939
---@return JSRegExp.JSString
3040
function jsregexp.to_jsstring(str) end
3141

42+
---
3243
---UTF-16 representation of a string. For internal use.
44+
---
3345
---@class JSRegExp.JSString
3446

47+
---
3548
---A compiled JavaScript regular expression object.
49+
---
3650
---@class JSRegExp.RegExp
3751
---@field last_index integer the position at wchich the next match will be searched in re:exec or re:test (see notes below)
3852
---@field source string the regexp string
@@ -46,30 +60,41 @@ function jsregexp.to_jsstring(str) end
4660
---@field unicode boolean is the unicode flag set?
4761
local re = {}
4862

63+
---
4964
---Execute the regular expression against a string.
65+
---
66+
---Example:
5067
---```lua
5168
--- local re = jsregexp.compile("\\w+", "g")
5269
--- local match = re:exec("Hello World")
5370
--- if match then
5471
--- print(match) -- Hello
5572
--- end
5673
---```
74+
---
5775
---@param string string|JSRegExp.JSString
5876
---@return JSRegExp.Match?
5977
function re:exec(string) end
6078

79+
---
6180
---Test the regular expression against a string.
81+
---
82+
---Example:
6283
---```lua
6384
--- local re = jsregexp.compile("\\w+", "g")
6485
--- if re:test("Hello World") then
6586
--- print("Matched!")
6687
--- end
6788
---```
89+
---
6890
---@param string string|JSRegExp.JSString
6991
---@return boolean
7092
function re:test(string) end
7193

94+
---
7295
---Returns a list of all matches or nil if no match.
96+
---
97+
---Example:
7398
---```lua
7499
--- local re = jsregexp.compile("\\w+", "g")
75100
--- local matches = re:match("Hello World")
@@ -79,63 +104,84 @@ function re:test(string) end
79104
--- end
80105
--- end
81106
---```
107+
---
82108
---@param string string
83109
---@return JSRegExp.Match[]?
84110
function re:match(string) end
85111

112+
---
86113
---Returns a closure that repeatedly calls re:exec, to be used in for-loops.
114+
---
115+
---Example:
87116
---```lua
88117
--- local re = jsregexp.compile("\\w+", "g")
89118
--- for match in re:match_all("Hello World") do
90119
--- print(match)
91120
--- end
92121
---```
122+
---
93123
---@param string string
94124
---@return fun():JSRegExp.Match
95125
function re:match_all(string) end
96126

127+
---
97128
---Returns a list of all matches.
129+
---
130+
---Example:
98131
---```lua
99132
--- local re = jsregexp.compile("\\w+", "g")
100133
--- local matches = re:match("Hello World")
101134
--- for _, match in ipairs(matches) do
102135
--- print(match)
103136
--- end
104137
---```
138+
---
105139
---@param string string
106140
---@return JSRegExp.Match[]
107141
function re:match_all_list(string) end
108142

143+
---
109144
---Returns the 1-based index of the first match of re in str, or -1 if no match
145+
---
146+
---Example:
110147
---```lua
111148
--- local re = jsregexp.compile("Wo\\w+")
112149
--- local idx = re:match("Hello World")
113150
--- if idx > 0 then
114151
--- print("Matched at index " .. idx)
115152
--- end
116153
---```
154+
---
117155
---@param string string
118156
---@return integer
119157
function re:search(string) end
120158

159+
---
121160
---Splits str at re, at most `limit` times
161+
---
162+
---Example:
122163
---```lua
123164
--- local re = jsregexp.compile("\\s+")
124165
--- local res = re:split("Hello World")
125166
--- for _, str in ipairs(res) do
126167
--- print(str)
127168
--- end
128169
---```
170+
---
129171
---@param string string
130172
---@param limit? integer
131173
---@return string[]
132174
function re:split(string, limit) end
133175

176+
---
134177
---Relplace the first match (all matches, if global) of re in str by replacement.
178+
---
179+
---Example:
135180
---```lua
136181
--- local re = jsregexp.compile("\\w+")
137182
--- local res = re:replace("Hello World", "Hey")
138183
--- print(res) -- Hey World
184+
---
139185
--- local res2 = re:replace("Hello World", function(match, str)
140186
--- if match[0] == "Hello" then
141187
--- return "Hey"
@@ -147,16 +193,21 @@ function re:split(string, limit) end
147193
--- end)
148194
--- print(res2) -- Hey World
149195
---```
196+
---
150197
---@param string string
151198
---@param replacement (fun(match: JSRegExp.Match, str: string):string)|string
152199
---@return string
153200
function re:replace(string, replacement) end
154201

202+
---
155203
---Relplace all occurances of re in str by replacement.
204+
---
205+
---Example:
156206
---```lua
157207
--- local re = jsregexp.compile("\\w+", "g")
158208
--- local res = re:replace_all("Hello World", "Hey")
159209
--- print(res) -- Hey Hey
210+
---
160211
--- local res2 = re:replace_all("Hello World", function(match, str)
161212
--- if match[0] == "Hello" then
162213
--- return "Hey"
@@ -168,6 +219,7 @@ function re:replace(string, replacement) end
168219
--- end)
169220
--- print(res2) -- Hey Joe
170221
---```
222+
---
171223
---@param string string
172224
---@param replacement (fun(match: JSRegExp.Match, str: string):string)|string
173225
---@return string

0 commit comments

Comments
 (0)