Welcome to little lamb

Code » qmdoc » commit c26d44b

Add some bit more specific parsing error codes

author Olivier Brunel
2022-12-26 23:16:45 UTC
committer Olivier Brunel
2022-12-26 23:16:45 UTC
parent b48ae90768587ad287c84f13f6745fa92eb4eedd

Add some bit more specific parsing error codes

main.c +60 -51

diff --git a/main.c b/main.c
index fb8b7ab..f1be0fa 100644
--- a/main.c
+++ b/main.c
@@ -79,6 +79,15 @@ struct ctx {
     } buf;
 };
 
+enum {
+    ERR_PARSER_ENTER_BLOCK  = -100,
+    ERR_PARSER_LEAVE_BLOCK  = -101,
+    ERR_PARSER_ENTER_SPAN   = -102,
+    ERR_PARSER_LEAVE_SPAN   = -103,
+    ERR_PARSER_TEXT         = -104,
+    ERR_PARSER_BUFFERED     = -105,
+};
+
 static int
 raw_text(struct ctx *ctx, const char *text, size_t size)
 {
@@ -196,24 +205,24 @@ enter_block(MD_BLOCKTYPE type, void *details, void *ctx_)
                         || !escape_text(ctx, str_title(ctx->cur_page),
                                         strlen(str_title(ctx->cur_page)))
                         || !raw_str(ctx, "</title>"))
-                    return ERR_IO;
+                    return ERR_PARSER_ENTER_BLOCK;
                 if (ctx->options & OPT_INLINE_CSS) {
                     for (int i = 0; i < NB_CSS; ++i) {
                         if (!raw_str(ctx, "<style>")
                                 || !raw_str(ctx, ctx->sa_names.s + ctx->css[i].offset)
                                 || !raw_str(ctx, "</style>"))
-                            return ERR_IO;
+                            return ERR_PARSER_ENTER_BLOCK;
                     }
                 } else if (!(ctx->options & OPT_NO_CSS)) {
                     for (int i = 0; i < NB_CSS; ++i) {
                         if (!raw_str(ctx, "<link rel=\"stylesheet\" href=\"")
                                 || !escape_text(ctx, ctx->css[i].file, strlen(ctx->css[i].file))
                                 || !raw_str(ctx, "\">"))
-                        return ERR_IO;
+                        return ERR_PARSER_ENTER_BLOCK;
                     }
                 }
                 if (!raw_str(ctx, "</head><body><header><nav><ul>"))
-                        return ERR_IO;
+                        return ERR_PARSER_ENTER_BLOCK;
 
                 for (int i = 0; i < ctx->nb_pages; ++i) {
                     if (!raw_str(ctx, "<li><a href=\"")
@@ -221,39 +230,39 @@ enter_block(MD_BLOCKTYPE type, void *details, void *ctx_)
                                 || !raw_str(ctx, "\">")
                                 || !escape_text(ctx, str_title(i), strlen(str_title(i)))
                                 || !raw_str(ctx, "</a></li>"))
-                        return ERR_IO;
+                        return ERR_PARSER_ENTER_BLOCK;
                 }
 #undef str_title
 #undef str_file
 
                 if (!raw_str(ctx, "</ul></nav></header><main>"))
-                        return ERR_IO;
+                        return ERR_PARSER_ENTER_BLOCK;
             }
             break;
 
         case MD_BLOCK_QUOTE:
             if (!raw_str(ctx, "<blockquote>"))
-                return ERR_IO;
+                return ERR_PARSER_ENTER_BLOCK;
             break;
 
         case MD_BLOCK_UL:
             if (!raw_str(ctx, "<ul>"))
-                return ERR_IO;
+                return ERR_PARSER_ENTER_BLOCK;
             break;
 
         case MD_BLOCK_OL:
             if (!raw_str(ctx, "<ol>"))
-                return ERR_IO;
+                return ERR_PARSER_ENTER_BLOCK;
             break;
 
         case MD_BLOCK_LI:
             if (!raw_str(ctx, "<li>"))
-                return ERR_IO;
+                return ERR_PARSER_ENTER_BLOCK;
             break;
 
         case MD_BLOCK_HR:
             if (!raw_str(ctx, "<hr>"))
-                return ERR_IO;
+                return ERR_PARSER_ENTER_BLOCK;
             break;
 
         case MD_BLOCK_H:
@@ -262,7 +271,7 @@ enter_block(MD_BLOCKTYPE type, void *details, void *ctx_)
                 char buf[UINT32_FMT];
                 buf[uint32_fmt(buf, (uint32) d->level)] = '\0';
                 if (!raw_str(ctx, "<h") || !raw_str(ctx, buf) || !raw_str(ctx, ">"))
-                    return ERR_IO;
+                    return ERR_PARSER_ENTER_BLOCK;
             }
             break;
 
@@ -274,7 +283,7 @@ enter_block(MD_BLOCKTYPE type, void *details, void *ctx_)
                     return ERR_INVALID;
 
                 if (!d->info.text || !strncmp(d->info.text, "pre\n", 4)) {
-                    return (raw_str(ctx, "<pre>")) ? 0 : ERR_IO;
+                    return (raw_str(ctx, "<pre>")) ? 0 : ERR_PARSER_ENTER_BLOCK;
                 } else {
                     const char *t = d->info.text;
                     size_t l = d->info.size;
@@ -320,7 +329,7 @@ enter_block(MD_BLOCKTYPE type, void *details, void *ctx_)
                     if (ctx->code.flags & CODE_BUFFERED)
                         ctx->buf.state = BUF_WAITING;
                     else
-                        return (raw_str(ctx, "<pre>")) ? 0 : ERR_IO;
+                        return (raw_str(ctx, "<pre>")) ? 0 : ERR_PARSER_ENTER_BLOCK;
                 }
             }
             break;
@@ -330,7 +339,7 @@ enter_block(MD_BLOCKTYPE type, void *details, void *ctx_)
 
         case MD_BLOCK_P:
             if (!raw_str(ctx, "<p>"))
-                return ERR_IO;
+                return ERR_PARSER_ENTER_BLOCK;
             break;
 
         case MD_BLOCK_TABLE:
@@ -353,27 +362,27 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_)
     switch (type) {
         case MD_BLOCK_DOC:
             if (!raw_str(ctx, "</body></html>"))
-                return ERR_IO;
+                return ERR_PARSER_LEAVE_BLOCK;
             break;
 
         case MD_BLOCK_QUOTE:
             if (!raw_str(ctx, "</blockquote>"))
-                return ERR_IO;
+                return ERR_PARSER_LEAVE_BLOCK;
             break;
 
         case MD_BLOCK_UL:
             if (!raw_str(ctx, "</ul>"))
-                return ERR_IO;
+                return ERR_PARSER_LEAVE_BLOCK;
             break;
 
         case MD_BLOCK_OL:
             if (!raw_str(ctx, "</ol>"))
-                return ERR_IO;
+                return ERR_PARSER_LEAVE_BLOCK;
             break;
 
         case MD_BLOCK_LI:
             if (!raw_str(ctx, "</li>"))
-                return ERR_IO;
+                return ERR_PARSER_LEAVE_BLOCK;
             break;
 
         case MD_BLOCK_HR:
@@ -385,7 +394,7 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_)
                 char buf[UINT32_FMT];
                 buf[uint32_fmt(buf, (uint32) d->level)] = '\0';
                 if (!raw_str(ctx, "</") || !raw_str(ctx, buf) || !raw_str(ctx, ">"))
-                    return ERR_IO;
+                    return ERR_PARSER_LEAVE_BLOCK;
             }
             break;
 
@@ -394,7 +403,7 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_)
                 struct ctx *ctx = ctx_;
 
                 if (!(ctx->code.flags & CODE_BUFFERED)) {
-                    return (raw_str(ctx, "</pre>")) ? 0 : ERR_IO;
+                    return (raw_str(ctx, "</pre>")) ? 0 : ERR_PARSER_LEAVE_BLOCK;
                 } else {
                     const char *buf = ctx->sa_names.s + ctx->buf.salen;
                     size_t blen = ctx->sa_names.len - ctx->buf.salen;
@@ -406,7 +415,7 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_)
                         size_t l = blen;
 
                         if (!raw_str(ctx, "<pre class=\"lineno\">"))
-                            return ERR_IO;
+                            return ERR_PARSER_BUFFERED;
                         for (int n = ctx->code.from; s; ++n) {
                             const char *e = memchr(s, '\n', l);
                             if (!e) break;
@@ -415,13 +424,13 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_)
                             char buf[UINT32_FMT];
                             buf[uint32_fmt(buf, (uint32) n)] = '\0';
                             if (!raw_str(ctx, buf) || !raw_str(ctx, "\n"))
-                                return ERR_IO;
+                                return ERR_PARSER_BUFFERED;
                         }
                         if (!raw_str(ctx, "</pre><code>"))
-                            return ERR_IO;
+                            return ERR_PARSER_BUFFERED;
                     } else {
                         if (!raw_str(ctx, "<pre>"))
-                            return ERR_IO;
+                            return ERR_PARSER_BUFFERED;
                     }
 
                     int r;
@@ -429,13 +438,13 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_)
                         r = highlight_escape_text(ctx, buf, blen);
                     else
                         r = escape_text(ctx, buf, blen);
-                    if (!r) return ERR_IO;
+                    if (!r) return ERR_PARSER_BUFFERED;
 
                     if (ctx->code.flags & CODE_LINES)
                         r = raw_str(ctx, "</code>");
                     else
                         r = raw_str(ctx, "</pre>");
-                    if (!r) return ERR_IO;
+                    if (!r) return ERR_PARSER_BUFFERED;
 
                     ctx->code.flags = 0;
                 }
@@ -447,7 +456,7 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_)
 
         case MD_BLOCK_P:
             if (!raw_str(ctx, "</p>"))
-                return ERR_IO;
+                return ERR_PARSER_LEAVE_BLOCK;
             break;
 
         case MD_BLOCK_TABLE:
@@ -470,12 +479,12 @@ enter_span(MD_SPANTYPE type, void *details, void *ctx_)
     switch (type) {
         case MD_SPAN_EM:
             if (!raw_str(ctx, "<em>"))
-                return ERR_IO;
+                return ERR_PARSER_ENTER_SPAN;
             break;
 
         case MD_SPAN_STRONG:
             if (!raw_str(ctx, "<strong>"))
-                return ERR_IO;
+                return ERR_PARSER_ENTER_SPAN;
             break;
 
         case MD_SPAN_A:
@@ -483,14 +492,14 @@ enter_span(MD_SPANTYPE type, void *details, void *ctx_)
                 MD_SPAN_A_DETAIL *d = details;
 
                 if (!raw_str(ctx, "<a href=\"") || !attribute(ctx, &d->href))
-                    return ERR_IO;
+                    return ERR_PARSER_ENTER_SPAN;
 
                 if (d->title.text
                         && (!raw_str(ctx, "\" title=\"") || !attribute(ctx, &d->title)))
-                    return ERR_IO;
+                    return ERR_PARSER_ENTER_SPAN;
 
                 if (!raw_str(ctx, "\">"))
-                    return ERR_IO;
+                    return ERR_PARSER_ENTER_SPAN;
             }
             break;
 
@@ -500,12 +509,12 @@ enter_span(MD_SPANTYPE type, void *details, void *ctx_)
 
         case MD_SPAN_CODE:
             if (!raw_str(ctx, "<tt>"))
-                return ERR_IO;
+                return ERR_PARSER_ENTER_SPAN;
             break;
 
         case MD_SPAN_DEL:
             if (!raw_str(ctx, "<s>"))
-                return ERR_IO;
+                return ERR_PARSER_ENTER_SPAN;
             break;
 
         case MD_SPAN_LATEXMATH:
@@ -515,7 +524,7 @@ enter_span(MD_SPANTYPE type, void *details, void *ctx_)
 
         case MD_SPAN_U:
             if (!raw_str(ctx, "<u>"))
-                return ERR_IO;
+                return ERR_PARSER_ENTER_SPAN;
             break;
     }
 
@@ -530,17 +539,17 @@ leave_span(MD_SPANTYPE type, void *details, void *ctx_)
     switch (type) {
         case MD_SPAN_EM:
             if (!raw_str(ctx, "</em>"))
-                return ERR_IO;
+                return ERR_PARSER_LEAVE_SPAN;
             break;
 
         case MD_SPAN_STRONG:
             if (!raw_str(ctx, "</strong>"))
-                return ERR_IO;
+                return ERR_PARSER_LEAVE_SPAN;
             break;
 
         case MD_SPAN_A:
             if (!raw_str(ctx, "</a>"))
-                return ERR_IO;
+                return ERR_PARSER_LEAVE_SPAN;
             break;
 
         case MD_SPAN_IMG:
@@ -549,12 +558,12 @@ leave_span(MD_SPANTYPE type, void *details, void *ctx_)
 
         case MD_SPAN_CODE:
             if (!raw_str(ctx, "</tt>"))
-                return ERR_IO;
+                return ERR_PARSER_LEAVE_SPAN;
             break;
 
         case MD_SPAN_DEL:
             if (!raw_str(ctx, "</s>"))
-                return ERR_IO;
+                return ERR_PARSER_LEAVE_SPAN;
             break;
 
         case MD_SPAN_LATEXMATH:
@@ -564,7 +573,7 @@ leave_span(MD_SPANTYPE type, void *details, void *ctx_)
 
         case MD_SPAN_U:
             if (!raw_str(ctx, "</u>"))
-                return ERR_IO;
+                return ERR_PARSER_LEAVE_SPAN;
             break;
     }
 
@@ -579,7 +588,7 @@ text(MD_TEXTTYPE type, const MD_CHAR *text, MD_SIZE size, void *ctx_)
     switch (type) {
         case MD_TEXT_NORMAL:
             if (!escape_text(ctx, text, size))
-                return ERR_IO;
+                return ERR_PARSER_TEXT;
             break;
 
         case MD_TEXT_NULLCHAR:
@@ -587,25 +596,25 @@ text(MD_TEXTTYPE type, const MD_CHAR *text, MD_SIZE size, void *ctx_)
 
         case MD_TEXT_BR:
             if (!raw_str(ctx, "<br>"))
-                return ERR_IO;
+                return ERR_PARSER_TEXT;
             break;
 
         case MD_TEXT_SOFTBR:
             if (!raw_str(ctx, "\n"))
-                return ERR_IO;
+                return ERR_PARSER_TEXT;
             break;
 
         case MD_TEXT_ENTITY:
             if (!escape_text(ctx, text, size))
-                return ERR_IO;
+                return ERR_PARSER_TEXT;
             break;
 
         case MD_TEXT_CODE:
             if (ctx->code.flags & CODE_BUFFERED) {
                 if (!raw_text(ctx, text, size))
-                    return ERR_IO;
+                    return ERR_PARSER_TEXT;
             } else if (!escape_text(ctx, text, size)) {
-                return ERR_IO;
+                return ERR_PARSER_TEXT;
             }
             break;
 
@@ -633,12 +642,12 @@ text(MD_TEXTTYPE type, const MD_CHAR *text, MD_SIZE size, void *ctx_)
             for (i = 0, n = sizeof(tags) / sizeof(*tags); i < n; ++i) {
                 if (size == tags[i].len && !strncmp(text, tags[i].name, tags[i].len)) {
                     if (!raw_str(ctx, (tags[i].repl) ? tags[i].repl : tags[i].name))
-                        return ERR_IO;
+                        return ERR_PARSER_TEXT;
                     break;
                 }
             }
             if (i == n && !escape_text(ctx, text, size))
-                return ERR_IO;
+                return ERR_PARSER_TEXT;
             break;
 
         case MD_TEXT_LATEXMATH: