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
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
/* vim:syntax=ragel 
 */ 
 
%%{ 
    machine JSON5_mapping; 
    alphtype int; 
    include JSON5D "json5_defaults.rl"; 
    getkey ((int)INDEX_PCHARP(str, fpc)); 
 
    action parse_value { 
        state->level++; 
        p = _parse_JSON5(str, fpc, pe, state); 
        state->level--; 
 
        if (state->flags&JSON5_ERROR) { 
            if (validate) { 
                pop_2_elems(); /* pop mapping and key */ 
            } 
            return p; 
        } else if (validate) { 
            mapping_insert(m, &(Pike_sp[-2]), &(Pike_sp[-1])); 
            pop_2_elems(); 
        } 
 
        c++; 
        fexec p; 
    } 
 
    action parse_key { 
        state->level++; 
        if (state->flags&JSON5_UTF8) 
            p = _parse_JSON5_string_utf8(str, fpc, pe, state); 
        else 
            p = _parse_JSON5_string(str, fpc, pe, state); 
        state->level--; 
 
        if (state->flags&JSON5_ERROR) { 
            if (validate) { 
                pop_stack(); /* pop mapping */ 
            } 
            return p; 
        } 
 
        c++; 
        fexec p; 
    } 
 
    action identifier_start { 
      identifier_start = fpc; 
    } 
  
    action identifier_end { 
      ptrdiff_t len = fpc - identifier_start; 
      if (validate) { 
       init_string_builder(&s, 0); 
       string_builder_append(&s, ADD_PCHARP(str, identifier_start), len); 
        push_string(finish_string_builder(&s)); 
      } 
    } 
 
    main := '{' . myspace* . ( 
                        start: ( 
                            '}' -> final | 
                            identifier >identifier_start %identifier_end . myspace* . ':' -> value | 
                            ('"'|'\'') >parse_key . myspace* . ':' -> value 
                        ), 
                        value: ( 
                            myspace* . value_start >parse_value . myspace* -> repeat 
                        ), 
                        repeat: ( 
                            ',' . myspace* -> start | 
                            '}' -> final 
                        ) 
                       ) %*{ fpc--; fbreak; }; 
}%% 
 
static ptrdiff_t _parse_JSON5_mapping(PCHARP str, ptrdiff_t p, ptrdiff_t pe, struct parser_state *state) { 
    /* GCC complains about a being used uninitialized. This is clearly wrong, so 
     * lets silence this warning */ 
    struct mapping *m = m; 
    int cs; 
    int c = 0; 
    struct string_builder s; 
    ptrdiff_t eof = pe; 
    ptrdiff_t identifier_start; 
 
    const int validate = !(state->flags&JSON5_VALIDATE); 
 
    %% write data; 
 
    /* Check stacks since we have uncontrolled recursion here. */ 
    check_stack (10); 
    check_c_stack (1024); 
 
    if (validate) { 
        m = debug_allocate_mapping(5); 
        push_mapping(m); 
    } 
 
    %% write init; 
    %% write exec; 
    if (cs >= JSON5_mapping_first_final) { 
        return p; 
    } 
    state->flags |= JSON5_ERROR; 
    if (validate) { 
        if (c & 1) pop_2_elems(); /* pop key and mapping */ 
        else pop_stack(); 
    } 
 
    return p; 
}