aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua/pat2pcre.lua
diff options
context:
space:
mode:
authorHadriel Kaplan <hadrielk@yahoo.com>2014-02-22 00:44:00 -0500
committerAnders Broman <a.broman58@gmail.com>2014-03-10 07:11:12 +0000
commitea46cdc4748d84d5026a7703b25e427f8170833e (patch)
tree97c581cda8563f0b91996fd685c9dc6fb42eeba5 /test/lua/pat2pcre.lua
parent81c0091c0ad593c5ded0af4622cf10c19cb18ae7 (diff)
Add GLib's regex library into Lua
While Lua's built-in pattern support is ok for simple things, many people end up wanting a real regex engine. Since Wireshark already includes the GLib Regex library (a wrapper for PCRE), it makes sense to expose that library to Lua scripts. This has been done using Lrexlib, one of the most popular regex bindings for Lua. Lrexlib didn't support binding GLib's Regex in particular - it does for PCRE but GLib is a different API - so I've done that. A fairly thorough testsuite came along with that, which has been incorporated into the wireshark wslua testuites as well in this commit. Change-Id: I05811d1edf7af8d7c9f4f081de6850f31c0717c7 Reviewed-on: https://code.wireshark.org/review/332 Reviewed-by: Anders Broman <a.broman58@gmail.com>
Diffstat (limited to 'test/lua/pat2pcre.lua')
-rwxr-xr-xtest/lua/pat2pcre.lua87
1 files changed, 87 insertions, 0 deletions
diff --git a/test/lua/pat2pcre.lua b/test/lua/pat2pcre.lua
new file mode 100755
index 0000000000..2d60a443b2
--- /dev/null
+++ b/test/lua/pat2pcre.lua
@@ -0,0 +1,87 @@
+-- See Copyright Notice in the file lrexlib.h
+
+-- Convert Lua regex pattern to its PCRE equivalent.
+
+local t_esc = {
+ a = "[:alpha:]",
+ A = "[:^alpha:]",
+ c = "[:cntrl:]",
+ C = "[:^cntrl:]",
+ d = "[:digit:]",
+ D = "[:^digit:]",
+ l = "[:lower:]",
+ L = "[:^lower:]",
+ p = "[:punct:]",
+ P = "[:^punct:]",
+ s = "[:space:]",
+ S = "[:^space:]",
+ u = "[:upper:]",
+ U = "[:^upper:]",
+ w = "[:alnum:]",
+ W = "[:^alnum:]",
+ x = "[:xdigit:]",
+ X = "[:^xdigit:]",
+ z = "\\x00",
+ Z = "\\x01-\\xFF",
+}
+
+local function rep_normal (ch)
+ assert (ch ~= "b", "\"%b\" subpattern is not supported")
+ assert (ch ~= "0", "invalid capture index")
+ local v = t_esc[ch]
+ return v and ("[" .. v .. "]") or ("\\" .. ch)
+end
+
+local function rep_charclass (ch)
+ return t_esc[ch] or ("\\" .. ch)
+end
+
+function pat2pcre (s)
+ local ind = 0
+
+ local function getc ()
+ ind = ind + 1
+ return string.sub (s, ind, ind)
+ end
+
+ local function getnum ()
+ local num = string.match (s, "^\\(%d%d?%d?)", ind)
+ if num then
+ ind = ind + #num
+ return string.format ("\\x%02X", num)
+ end
+ end
+
+ local out, state = "", "normal"
+ while ind < #s do
+ local ch = getc ()
+ if state == "normal" then
+ if ch == "%" then
+ out = out .. rep_normal (getc ())
+ elseif ch == "-" then
+ out = out .. "*?"
+ elseif ch == "." then
+ out = out .. "\\C"
+ elseif ch == "[" then
+ out = out .. ch
+ state = "charclass"
+ else
+ local num = getnum ()
+ out = num and (out .. num) or (out .. ch)
+ end
+ elseif state == "charclass" then
+ if ch == "%" then
+ out = out .. rep_charclass (getc ())
+ elseif ch == "]" then
+ out = out .. ch
+ state = "normal"
+ else
+ local num = getnum ()
+ out = num and (out .. num) or (out .. ch)
+ end
+ end
+ end
+ return out
+end
+
+return pat2pcre