aboutsummaryrefslogtreecommitdiff
path: root/json.c
blob: 445f81b9418843d700ab44f7960f30f9f996b207 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// json.c - Code for printing JSON output of matches.
//

#include <stdio.h>

#include "json.h"

__attribute__((nonnull))
static int _json_match(const char *text, match_t *m, int comma, bool verbose);

//
// Helper function for json_match().
// `comma` is used to track whether a comma will need to be printed before the
// next object or not.
//
static int _json_match(const char *text, match_t *m, int comma, bool verbose)
{
    if (!verbose && m->pat->type != BP_TAGGED) {
        if (m->children) {
            for (int i = 0; m->children && m->children[i]; i++)
                comma |= _json_match(text, m->children[i], comma, verbose);
        }
        return comma;
    }

    if (comma) printf(",\n");
    comma = 0;
    printf("{");
    if (m->pat->type == BP_TAGGED) {
        printf("\"tag\":\"%.*s\"", (int)Match(m->pat, BP_TAGGED)->namelen, Match(m->pat, BP_TAGGED)->name);
        comma = 1;
    }
    if (verbose) {
        if (comma) printf(",");
        printf("\"rule\":\"");
        for (const char *c = m->pat->start; c < m->pat->end; c++) {
            switch (*c) {
            case '"': printf("\\\""); break;
            case '\\': printf("\\\\"); break;
            case '\t': printf("\\t"); break;
            case '\n': printf("↵"); break;
            default: printf("%c", *c); break;
            }
        }
        printf("\",");
        printf("\"range\":[%zd,%zd]", m->start - text, m->end - text);
        comma = 1;
    } else {
        if (comma) printf(",");
        printf("\"text\":\"");
        for (const char *c = m->start; c < m->end; c++) {
            switch (*c) {
            case '"': printf("\\\""); break;
            case '\\': printf("\\\\"); break;
            case '\t': printf("\\t"); break;
            case '\n': printf("↵"); break;
            default: printf("%c", *c); break;
            }
        }
        printf("\"");
        comma = 1;
    }

    if (m->children) {
        if (comma) printf(",");
        printf("\"children\":[");
        comma = 0;
        for (int i = 0; m->children && m->children[i]; i++)
            comma |= _json_match(text, m->children[i], comma, verbose);
        printf("]");
    }
    printf("}");
    return 1;
}

//
// Print a match object as a JSON object.
//
void json_match(const char *text, match_t *m, bool verbose)
{
    (void)_json_match(text, m, 0, verbose);
}

// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0