char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
c96f608d9
char/aliceai-t5-moe
1#include "tests.h" 2 3void test_python_dict_parser (testing & t ) { 4// Test parsing a simple Python dict object with single quotes 5t .test ("simple Python dict object parsing" , [](testing & t ) { 6auto parser = build_peg_parser ([](common_peg_parser_builder & p ) {return p .python_value (); }); 7 8 std::string input = "{'name': 'test', 'value': 42, 'flag': True}" ; 9common_peg_parse_context ctx (input ); 10 11auto result = parser .parse (ctx ); 12 13t .assert_equal ("result_is_success" , true,result .success ()); 14t .assert_equal ("result_end" ,input .size (),result .end ); 15 }); 16 17// Test parsing a Python array with mixed types 18t .test ("Python array with mixed types" , [](testing & t ) { 19auto parser = build_peg_parser ([](common_peg_parser_builder & p ) {return p .python_value (); }); 20 21 std::string input = "[1, 'hello', True, None, 3.14]" ; 22common_peg_parse_context ctx (input ); 23 24auto result = parser .parse (ctx ); 25 26t .assert_equal ("result_is_success" , true,result .success ()); 27t .assert_equal ("result_end" ,input .size (),result .end ); 28 }); 29 30// Test parsing nested Python dict with objects and arrays 31t .test ("nested Python dict with objects and arrays" , [](testing & t ) { 32auto parser = build_peg_parser ([](common_peg_parser_builder & p ) {return p .python_value (); }); 33 34 std::string input = 35"{'users': [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}], 'count': 2, 'metadata': {'version': '1.0', 'tags': ['admin', 'user']}}" ; 36common_peg_parse_context ctx (input ); 37 38auto result = parser .parse (ctx ); 39 40t .assert_equal ("result_is_success" , true,result .success ()); 41t .assert_equal ("result_end" ,input .size (),result .end ); 42 }); 43 44// Test parsing Python dict with escaped single quotes 45t .test ("Python dict with escaped single quotes" , [](testing & t ) { 46auto parser = build_peg_parser ([](common_peg_parser_builder & p ) {return p .python_value (); }); 47 48 std::string input = "{'message': 'It\\'s working!'}" ; 49common_peg_parse_context ctx (input ); 50 51auto result = parser .parse (ctx ); 52 53t .assert_equal ("result_is_success" , true,result .success ()); 54t .assert_equal ("result_end" ,input .size (),result .end ); 55 }); 56 57// Test parsing Python dict with double quotes inside single quotes 58t .test ("Python dict with double quotes inside single quotes" , [](testing & t ) { 59auto parser = build_peg_parser ([](common_peg_parser_builder & p ) {return p .python_value (); }); 60 61 std::string input = "{'quote': 'He said \"Hello\"'}" ; 62common_peg_parse_context ctx (input ); 63 64auto result = parser .parse (ctx ); 65 66t .assert_equal ("result_is_success" , true,result .success ()); 67t .assert_equal ("result_end" ,input .size (),result .end ); 68 }); 69 70// Test the example from the requirements 71t .test ("complex Python dict example from requirements" , [](testing & t ) { 72auto parser = build_peg_parser ([](common_peg_parser_builder & p ) {return p .python_value (); }); 73 74 std::string input = "{ 'obj' : { 'something': 1, 'other \"something\"' : 'foo\\'s bar' } }" ; 75common_peg_parse_context ctx (input ); 76 77auto result = parser .parse (ctx ); 78 79t .assert_equal ("result_is_success" , true,result .success ()); 80t .assert_equal ("result_end" ,input .size (),result .end ); 81 }); 82 83// Test need_more_input() parsing - incomplete object 84t .test ("need_more_input() parsing - incomplete object" , [](testing & t ) { 85auto parser = build_peg_parser ([](common_peg_parser_builder & p ) {return p .python_value (); }); 86 87 std::string input = "{'name': 'test', 'value': " ; 88common_peg_parse_context ctx (input ,COMMON_PEG_PARSE_FLAG_LENIENT ); 89 90auto result = parser .parse (ctx ); 91 92t .assert_equal ("result_is_need_more_input" , true,result .need_more_input ()); 93 }); 94 95// Test need_more_input() parsing - incomplete single-quoted string 96t .test ("need_more_input() parsing - incomplete single-quoted string" , [](testing & t ) { 97auto parser = build_peg_parser ([](common_peg_parser_builder & p ) {return p .python_value (); }); 98 99 std::string input = "{'name': 'test" ; 100common_peg_parse_context ctx (input ,COMMON_PEG_PARSE_FLAG_LENIENT ); 101 102auto result = parser .parse (ctx ); 103 104t .assert_equal ("result_is_need_more_input" , true,result .need_more_input ()); 105 }); 106 107// Test unicode in Python dict strings 108t .test ("unicode in Python dict strings" , [](testing & t ) { 109auto parser = build_peg_parser ([](common_peg_parser_builder & p ) {return p .python_value (); }); 110 111 std::string input = "{'message': 'Hello, δΈη!'}" ; 112common_peg_parse_context ctx (input ); 113 114auto result = parser .parse (ctx ); 115 116t .assert_equal ("result_is_success" , true,result .success ()); 117t .assert_equal ("result_end" ,input .size (),result .end ); 118 }); 119 120// Test Python dict with unicode escapes 121t .test ("Python dict with unicode escapes" , [](testing & t ) { 122auto parser = build_peg_parser ([](common_peg_parser_builder & p ) {return p .python_value (); }); 123 124 std::string input = "{'unicode': 'Hello\\u0041'}" ; 125common_peg_parse_context ctx (input ); 126 127auto result = parser .parse (ctx ); 128 129t .assert_equal ("result_is_success" , true,result .success ()); 130t .assert_equal ("result_end" ,input .size (),result .end ); 131 }); 132 133// Test that Python parser accepts double-quoted strings too 134t .test ("Python parser accepts double-quoted strings" , [](testing & t ) { 135auto parser = build_peg_parser ([](common_peg_parser_builder & p ) {return p .python_value (); }); 136 137 std::string input = "{\"name\": \"test\"}" ; 138common_peg_parse_context ctx (input ); 139 140auto result = parser .parse (ctx ); 141 142t .assert_equal ("result_is_success" , true,result .success ()); 143t .assert_equal ("result_end" ,input .size (),result .end ); 144 }); 145 146// Test Python parser with mixed quote styles 147t .test ("Python parser with mixed quote styles" , [](testing & t ) { 148auto parser = build_peg_parser ([](common_peg_parser_builder & p ) {return p .python_value (); }); 149 150 std::string input = "{\"name\": 'test', 'value': \"hello\"}" ; 151common_peg_parse_context ctx (input ); 152 153auto result = parser .parse (ctx ); 154 155t .assert_equal ("result_is_success" , true,result .success ()); 156t .assert_equal ("result_end" ,input .size (),result .end ); 157 }); 158 159// Test Python True/False/None 160t .test ("Python True/False/None" , [](testing & t ) { 161auto parser = build_peg_parser ([](common_peg_parser_builder & p ) {return p .python_value (); }); 162 163t .test ("True" , [& ](testing & t ) { 164 std::string input = "True" ; 165common_peg_parse_context ctx (input ); 166auto result = parser .parse (ctx ); 167t .assert_true ("success" ,result .success ()); 168t .assert_equal ("end" ,input .size (),result .end ); 169 }); 170 171t .test ("False" , [& ](testing & t ) { 172 std::string input = "False" ; 173common_peg_parse_context ctx (input ); 174auto result = parser .parse (ctx ); 175t .assert_true ("success" ,result .success ()); 176t .assert_equal ("end" ,input .size (),result .end ); 177 }); 178 179t .test ("None" , [& ](testing & t ) { 180 std::string input = "None" ; 181common_peg_parse_context ctx (input ); 182auto result = parser .parse (ctx ); 183t .assert_true ("success" ,result .success ()); 184t .assert_equal ("end" ,input .size (),result .end ); 185 }); 186 187t .test ("rejects JSON-style true/false/null" , [& ](testing & t ) { 188for (const auto & kw : {"true" ,"false" ,"null" }) { 189 std::string input = kw ; 190common_peg_parse_context ctx (input ); 191auto result = parser .parse (ctx ); 192t .assert_true (std::string ("rejects " )+ kw ,result .fail ()); 193 } 194 }); 195 }); 196 197// Test single-quoted string content parser directly 198t .test ("single-quoted string content parser" , [](testing & t ) { 199auto parser = build_peg_parser ([](common_peg_parser_builder & p ) { 200return p .sequence ({p .literal ("'" ),p .string_content ('\'' ),p .literal ("'" ),p .space () }); 201 }); 202 203t .test ("simple string" , [& ](testing & t ) { 204 std::string input = "'hello'" ; 205common_peg_parse_context ctx (input ); 206 207auto result = parser .parse (ctx ); 208t .assert_true ("success" ,result .success ()); 209t .assert_equal ("end" ,input .size (),result .end ); 210 }); 211 212t .test ("string with escaped single quote" , [& ](testing & t ) { 213 std::string input = "'it\\'s'" ; 214common_peg_parse_context ctx (input ); 215 216auto result = parser .parse (ctx ); 217t .assert_true ("success" ,result .success ()); 218t .assert_equal ("end" ,input .size (),result .end ); 219 }); 220 221t .test ("string with double quotes" , [& ](testing & t ) { 222 std::string input = "'say \"hello\"'" ; 223common_peg_parse_context ctx (input ); 224 225auto result = parser .parse (ctx ); 226t .assert_true ("success" ,result .success ()); 227t .assert_equal ("end" ,input .size (),result .end ); 228 }); 229 230t .test ("incomplete string" , [& ](testing & t ) { 231 std::string input = "'hello" ; 232common_peg_parse_context ctx (input ,COMMON_PEG_PARSE_FLAG_LENIENT ); 233 234auto result = parser .parse (ctx ); 235t .assert_true ("need_more_input" ,result .need_more_input ()); 236 }); 237 }); 238 239// Test json() with pre-registered flexible json-string rule (python dict support) 240t .test ("json() parser with flexible json-string rule" , [](testing & t ) { 241t .test ("json() rejects single quotes by default" , [& ](testing & t ) { 242auto parser = build_peg_parser ([](common_peg_parser_builder & p ) { 243return p .json (); 244 }); 245 246 std::string input = "{'name': 'test'}" ; 247common_peg_parse_context ctx (input ); 248 249auto result = parser .parse (ctx ); 250t .assert_true ("fail" ,result .fail ()); 251 }); 252 253t .test ("json() accepts single quotes with pre-registered flexible json-string rule" , [& ](testing & t ) { 254auto parser = build_peg_parser ([](common_peg_parser_builder & p ) { 255// Pre-register json-string rule with both quote styles 256p .rule ("json-string" , [& ]() { 257return p .choice ({p .double_quoted_string (),p .single_quoted_string () }); 258 }); 259return p .json (); 260 }); 261 262 std::string input = "{'name': 'test'}" ; 263common_peg_parse_context ctx (input ); 264 265auto result = parser .parse (ctx ); 266t .assert_true ("success" ,result .success ()); 267t .assert_equal ("end" ,input .size (),result .end ); 268 }); 269 270t .test ("json() still accepts double quotes with flexible json-string rule" , [& ](testing & t ) { 271auto parser = build_peg_parser ([](common_peg_parser_builder & p ) { 272p .rule ("json-string" , [& ]() { 273return p .choice ({p .double_quoted_string (),p .single_quoted_string () }); 274 }); 275return p .json (); 276 }); 277 278 std::string input = "{\"name\": \"test\"}" ; 279common_peg_parse_context ctx (input ); 280 281auto result = parser .parse (ctx ); 282t .assert_true ("success" ,result .success ()); 283t .assert_equal ("end" ,input .size (),result .end ); 284 }); 285 286t .test ("json() accepts mixed quote styles with flexible json-string rule" , [& ](testing & t ) { 287auto parser = build_peg_parser ([](common_peg_parser_builder & p ) { 288p .rule ("json-string" , [& ]() { 289return p .choice ({p .double_quoted_string (),p .single_quoted_string () }); 290 }); 291return p .json (); 292 }); 293 294 std::string input = "{\"name\": 'test', 'value': \"hello\"}" ; 295common_peg_parse_context ctx (input ); 296 297auto result = parser .parse (ctx ); 298t .assert_true ("success" ,result .success ()); 299t .assert_equal ("end" ,input .size (),result .end ); 300 }); 301 302t .test ("complex nested structure with flexible json-string rule" , [& ](testing & t ) { 303auto parser = build_peg_parser ([](common_peg_parser_builder & p ) { 304p .rule ("json-string" , [& ]() { 305return p .choice ({p .double_quoted_string (),p .single_quoted_string () }); 306 }); 307return p .json (); 308 }); 309 310 std::string input = "{ 'obj' : { 'something': 1, 'other \"something\"' : 'foo\\'s bar' } }" ; 311common_peg_parse_context ctx (input ); 312 313auto result = parser .parse (ctx ); 314t .assert_true ("success" ,result .success ()); 315t .assert_equal ("end" ,input .size (),result .end ); 316 }); 317 }); 318}