Datasets:

Modalities:
Text
Formats:
text
Size:
< 1K
ArXiv:
Libraries:
Datasets
File size: 13,056 Bytes
2517be1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "tests.h"

void test_python_dict_parser(testing &t) {
    // Test parsing a simple Python dict object with single quotes
    t.test("simple Python dict object parsing", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.python_value(); });

        std::string    input = "{'name': 'test', 'value': 42, 'flag': True}";
        common_peg_parse_context ctx(input);

        auto result = parser.parse(ctx);

        t.assert_equal("result_is_success", true, result.success());
        t.assert_equal("result_end", input.size(), result.end);
    });

    // Test parsing a Python array with mixed types
    t.test("Python array with mixed types", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.python_value(); });

        std::string    input = "[1, 'hello', True, None, 3.14]";
        common_peg_parse_context ctx(input);

        auto result = parser.parse(ctx);

        t.assert_equal("result_is_success", true, result.success());
        t.assert_equal("result_end", input.size(), result.end);
    });

    // Test parsing nested Python dict with objects and arrays
    t.test("nested Python dict with objects and arrays", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.python_value(); });

        std::string input =
            "{'users': [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}], 'count': 2, 'metadata': {'version': '1.0', 'tags': ['admin', 'user']}}";
        common_peg_parse_context ctx(input);

        auto result = parser.parse(ctx);

        t.assert_equal("result_is_success", true, result.success());
        t.assert_equal("result_end", input.size(), result.end);
    });

    // Test parsing Python dict with escaped single quotes
    t.test("Python dict with escaped single quotes", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.python_value(); });

        std::string    input = "{'message': 'It\\'s working!'}";
        common_peg_parse_context ctx(input);

        auto result = parser.parse(ctx);

        t.assert_equal("result_is_success", true, result.success());
        t.assert_equal("result_end", input.size(), result.end);
    });

    // Test parsing Python dict with double quotes inside single quotes
    t.test("Python dict with double quotes inside single quotes", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.python_value(); });

        std::string    input = "{'quote': 'He said \"Hello\"'}";
        common_peg_parse_context ctx(input);

        auto result = parser.parse(ctx);

        t.assert_equal("result_is_success", true, result.success());
        t.assert_equal("result_end", input.size(), result.end);
    });

    // Test the example from the requirements
    t.test("complex Python dict example from requirements", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.python_value(); });

        std::string    input = "{ 'obj' : { 'something': 1, 'other \"something\"' : 'foo\\'s bar' } }";
        common_peg_parse_context ctx(input);

        auto result = parser.parse(ctx);

        t.assert_equal("result_is_success", true, result.success());
        t.assert_equal("result_end", input.size(), result.end);
    });

    // Test need_more_input() parsing - incomplete object
    t.test("need_more_input() parsing - incomplete object", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.python_value(); });

        std::string    input = "{'name': 'test', 'value': ";
        common_peg_parse_context ctx(input, COMMON_PEG_PARSE_FLAG_LENIENT);

        auto result = parser.parse(ctx);

        t.assert_equal("result_is_need_more_input", true, result.need_more_input());
    });

    // Test need_more_input() parsing - incomplete single-quoted string
    t.test("need_more_input() parsing - incomplete single-quoted string", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.python_value(); });

        std::string    input = "{'name': 'test";
        common_peg_parse_context ctx(input, COMMON_PEG_PARSE_FLAG_LENIENT);

        auto result = parser.parse(ctx);

        t.assert_equal("result_is_need_more_input", true, result.need_more_input());
    });

    // Test unicode in Python dict strings
    t.test("unicode in Python dict strings", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.python_value(); });

        std::string    input = "{'message': 'Hello, 世界!'}";
        common_peg_parse_context ctx(input);

        auto result = parser.parse(ctx);

        t.assert_equal("result_is_success", true, result.success());
        t.assert_equal("result_end", input.size(), result.end);
    });

    // Test Python dict with unicode escapes
    t.test("Python dict with unicode escapes", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.python_value(); });

        std::string    input = "{'unicode': 'Hello\\u0041'}";
        common_peg_parse_context ctx(input);

        auto result = parser.parse(ctx);

        t.assert_equal("result_is_success", true, result.success());
        t.assert_equal("result_end", input.size(), result.end);
    });

    // Test that Python parser accepts double-quoted strings too
    t.test("Python parser accepts double-quoted strings", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.python_value(); });

        std::string    input = "{\"name\": \"test\"}";
        common_peg_parse_context ctx(input);

        auto result = parser.parse(ctx);

        t.assert_equal("result_is_success", true, result.success());
        t.assert_equal("result_end", input.size(), result.end);
    });

    // Test Python parser with mixed quote styles
    t.test("Python parser with mixed quote styles", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.python_value(); });

        std::string    input = "{\"name\": 'test', 'value': \"hello\"}";
        common_peg_parse_context ctx(input);

        auto result = parser.parse(ctx);

        t.assert_equal("result_is_success", true, result.success());
        t.assert_equal("result_end", input.size(), result.end);
    });

    // Test Python True/False/None
    t.test("Python True/False/None", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.python_value(); });

        t.test("True", [&](testing &t) {
            std::string input = "True";
            common_peg_parse_context ctx(input);
            auto result = parser.parse(ctx);
            t.assert_true("success", result.success());
            t.assert_equal("end", input.size(), result.end);
        });

        t.test("False", [&](testing &t) {
            std::string input = "False";
            common_peg_parse_context ctx(input);
            auto result = parser.parse(ctx);
            t.assert_true("success", result.success());
            t.assert_equal("end", input.size(), result.end);
        });

        t.test("None", [&](testing &t) {
            std::string input = "None";
            common_peg_parse_context ctx(input);
            auto result = parser.parse(ctx);
            t.assert_true("success", result.success());
            t.assert_equal("end", input.size(), result.end);
        });

        t.test("rejects JSON-style true/false/null", [&](testing &t) {
            for (const auto & kw : {"true", "false", "null"}) {
                std::string input = kw;
                common_peg_parse_context ctx(input);
                auto result = parser.parse(ctx);
                t.assert_true(std::string("rejects ") + kw, result.fail());
            }
        });
    });

    // Test single-quoted string content parser directly
    t.test("single-quoted string content parser", [](testing &t) {
        auto parser = build_peg_parser([](common_peg_parser_builder & p) {
            return p.sequence({ p.literal("'"), p.string_content('\''), p.literal("'"), p.space() });
        });

        t.test("simple string", [&](testing &t) {
            std::string input = "'hello'";
            common_peg_parse_context ctx(input);

            auto result = parser.parse(ctx);
            t.assert_true("success", result.success());
            t.assert_equal("end", input.size(), result.end);
        });

        t.test("string with escaped single quote", [&](testing &t) {
            std::string input = "'it\\'s'";
            common_peg_parse_context ctx(input);

            auto result = parser.parse(ctx);
            t.assert_true("success", result.success());
            t.assert_equal("end", input.size(), result.end);
        });

        t.test("string with double quotes", [&](testing &t) {
            std::string input = "'say \"hello\"'";
            common_peg_parse_context ctx(input);

            auto result = parser.parse(ctx);
            t.assert_true("success", result.success());
            t.assert_equal("end", input.size(), result.end);
        });

        t.test("incomplete string", [&](testing &t) {
            std::string input = "'hello";
            common_peg_parse_context ctx(input, COMMON_PEG_PARSE_FLAG_LENIENT);

            auto result = parser.parse(ctx);
            t.assert_true("need_more_input", result.need_more_input());
        });
    });

    // Test json() with pre-registered flexible json-string rule (python dict support)
    t.test("json() parser with flexible json-string rule", [](testing &t) {
        t.test("json() rejects single quotes by default", [&](testing &t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) {
                return p.json();
            });

            std::string input = "{'name': 'test'}";
            common_peg_parse_context ctx(input);

            auto result = parser.parse(ctx);
            t.assert_true("fail", result.fail());
        });

        t.test("json() accepts single quotes with pre-registered flexible json-string rule", [&](testing &t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) {
                // Pre-register json-string rule with both quote styles
                p.rule("json-string", [&]() {
                    return p.choice({ p.double_quoted_string(), p.single_quoted_string() });
                });
                return p.json();
            });

            std::string input = "{'name': 'test'}";
            common_peg_parse_context ctx(input);

            auto result = parser.parse(ctx);
            t.assert_true("success", result.success());
            t.assert_equal("end", input.size(), result.end);
        });

        t.test("json() still accepts double quotes with flexible json-string rule", [&](testing &t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) {
                p.rule("json-string", [&]() {
                    return p.choice({ p.double_quoted_string(), p.single_quoted_string() });
                });
                return p.json();
            });

            std::string input = "{\"name\": \"test\"}";
            common_peg_parse_context ctx(input);

            auto result = parser.parse(ctx);
            t.assert_true("success", result.success());
            t.assert_equal("end", input.size(), result.end);
        });

        t.test("json() accepts mixed quote styles with flexible json-string rule", [&](testing &t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) {
                p.rule("json-string", [&]() {
                    return p.choice({ p.double_quoted_string(), p.single_quoted_string() });
                });
                return p.json();
            });

            std::string input = "{\"name\": 'test', 'value': \"hello\"}";
            common_peg_parse_context ctx(input);

            auto result = parser.parse(ctx);
            t.assert_true("success", result.success());
            t.assert_equal("end", input.size(), result.end);
        });

        t.test("complex nested structure with flexible json-string rule", [&](testing &t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) {
                p.rule("json-string", [&]() {
                    return p.choice({ p.double_quoted_string(), p.single_quoted_string() });
                });
                return p.json();
            });

            std::string input = "{ 'obj' : { 'something': 1, 'other \"something\"' : 'foo\\'s bar' } }";
            common_peg_parse_context ctx(input);

            auto result = parser.parse(ctx);
            t.assert_true("success", result.success());
            t.assert_equal("end", input.size(), result.end);
        });
    });
}