Check new prompts for proper bracing
John Smith

John Smith commited on 2023-88 06:24:37
Showing 1 changed files, with 15 additions and 0 deletions.


If one using the "a" option submits a prompt with an unmatched brace,
Memoire will warn the user about this, rather than blindly accepting
the bad prompt and potentially crashing.
... ...
@@ -167,6 +167,14 @@ local function histogram(list, min, max, width, func, scale)
167 167
     return s .. string.format("(%d out of range)\n", oorc)
168 168
 end
169 169
 
170
+--[[
171
+    returns a not-necessarily-meaningful number
172
+    iff `s` has mismathched curly-brackets
173
+]]
174
+local function mismatched(s)
175
+    return ("}" .. s):find("}[^{]+}") or (s .. "{"):find("{[^}]+{")
176
+end
177
+
170 178
 local actions = {
171 179
     a = {
172 180
         desc = "(a)dd card (from stdin)",
... ...
@@ -185,8 +193,15 @@ local actions = {
185 193
                     table.insert(nc, Prompt:new(pt, "", os.time(), 30, 0, 0, os.time(), config:get("StartingEase") or 2.5))
186 194
                     prctr = prctr + 1
187 195
                 end
196
+                if mismatched(pt) then
197
+                    io.write(string.format("Prompt %d:1 (\"%s\") contains mismatched braces\n", #deck + 1, pt))
198
+                end
199
+                if #nc >= 1 then
188 200
                     table.insert(deck, nc)
189 201
                     cardctr = cardctr + 1
202
+                else
203
+                    io.write(string.format("Prompt \"%s\" contains no answers (check braces)\n", pt))
204
+                end
190 205
             end
191 206
             io.write(string.format(
192 207
                 "Added %d cards (%d prompts)\n",
193 208