Skip to content

Commit

Permalink
tidy up stringmatcher.lua take 2
Browse files Browse the repository at this point in the history
  • Loading branch information
hansonchar committed Jun 18, 2024
1 parent 5f1b1b4 commit 9b71565
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions doc/generic/pgf/lib/stringmatcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ local C, P, V = lpeg.C, lpeg.P, lpeg.V

local loc = lpeg.locale()
local SP = loc.space ^ 0 -- spaces

-- Combine all patterns into one general pattern
-- local general_str = single_quoted + double_quoted + multiline

local string_matcher =
P {
"str",
single_quoted = SP * "'" * C(((1 - P "'") + "\\'") ^ 0) * "'",
double_quoted = SP * '"' * C(((1 - P '"') + '\\"') ^ 0) * '"',
multiline = SP * "[[" * C((1 - (SP * P "]]")) ^ 0) * SP * "]]",
str = V "single_quoted" + V "double_quoted" + V "multiline"
}

if not UNIT_TESTING then
return string_matcher
end

-- Unit tests and debugging

-- local I = function(tag)
-- return lpeg.P(
-- function()
Expand All @@ -13,7 +32,7 @@ local SP = loc.space ^ 0 -- spaces
-- )
-- end

if UNIT_TESTING then
do
-- Define patterns for single-quoted and double-quoted strings
local single_quoted = SP * "'" * C(((1 - P "'") + "\\'") ^ 0) * "'"

Expand All @@ -38,22 +57,6 @@ if UNIT_TESTING then
]=]))
end

-- Combine all patterns into one general pattern
-- local general_str = single_quoted + double_quoted + multiline

local string_matcher =
P {
"str",
single_quoted = SP * "'" * C(((1 - P "'") + "\\'") ^ 0) * "'",
double_quoted = SP * '"' * C(((1 - P '"') + '\\"') ^ 0) * '"',
multiline = SP * "[[" * C((1 - (SP * P "]]")) ^ 0) * SP * "]]",
str = V "single_quoted" + V "double_quoted" + V "multiline"
}

if not UNIT_TESTING then
return string_matcher
end

-- Test strings
local test_strings = {
"'single quoted string'",
Expand Down

0 comments on commit 9b71565

Please sign in to comment.