aravagarwal/CodeCloakPII
0
1================================================================================2Matching specific values3================================================================================4 5match command.split():6 case ["quit"]:7 print("Goodbye!")8 quit_game()9 case ["look"]:10 current_room.describe()11 case ["get", obj]:12 character.get(obj, current_room)13 case ["go", direction]:14 current_room = current_room.neighbor(direction)15 # The rest of your commands go here16 17--------------------------------------------------------------------------------18 19(module20 (match_statement21 (call22 (attribute23 (identifier)24 (identifier))25 (argument_list))26 (block27 (case_clause28 (case_pattern29 (list_pattern30 (case_pattern31 (string32 (string_start)33 (string_content)34 (string_end)))))35 (block36 (expression_statement37 (call38 (identifier)39 (argument_list40 (string41 (string_start)42 (string_content)43 (string_end)))))44 (expression_statement45 (call46 (identifier)47 (argument_list)))))48 (case_clause49 (case_pattern50 (list_pattern51 (case_pattern52 (string53 (string_start)54 (string_content)55 (string_end)))))56 (block57 (expression_statement58 (call59 (attribute60 (identifier)61 (identifier))62 (argument_list)))))63 (case_clause64 (case_pattern65 (list_pattern66 (case_pattern67 (string68 (string_start)69 (string_content)70 (string_end)))71 (case_pattern72 (dotted_name73 (identifier)))))74 (block75 (expression_statement76 (call77 (attribute78 (identifier)79 (identifier))80 (argument_list81 (identifier)82 (identifier))))))83 (case_clause84 (case_pattern85 (list_pattern86 (case_pattern87 (string88 (string_start)89 (string_content)90 (string_end)))91 (case_pattern92 (dotted_name93 (identifier)))))94 (block95 (expression_statement96 (assignment97 (identifier)98 (call99 (attribute100 (identifier)101 (identifier))102 (argument_list103 (identifier)))))))104 (comment))))105 106================================================================================107Matching multiple values108================================================================================109 110match command.split():111 case ["drop", *objects]:112 for obj in objects:113 character.drop(obj, current_room)114 115--------------------------------------------------------------------------------116 117(module118 (match_statement119 (call120 (attribute121 (identifier)122 (identifier))123 (argument_list))124 (block125 (case_clause126 (case_pattern127 (list_pattern128 (case_pattern129 (string130 (string_start)131 (string_content)132 (string_end)))133 (case_pattern134 (splat_pattern135 (identifier)))))136 (block137 (for_statement138 (identifier)139 (identifier)140 (block141 (expression_statement142 (call143 (attribute144 (identifier)145 (identifier))146 (argument_list147 (identifier)148 (identifier)))))))))))149 150================================================================================151Adding a wild card152================================================================================153 154match command.split():155# ^ conditional156 case ["quit"]: ... # Code omitted for brevity157 case ["go", direction]: pass158 case ["drop", *objects]: pass159 case _:160 print(f"Sorry, I couldn't understand {command!r}")161 162--------------------------------------------------------------------------------163 164(module165 (match_statement166 (call167 (attribute168 (identifier)169 (identifier))170 (argument_list))171 (block172 (comment)173 (case_clause174 (case_pattern175 (list_pattern176 (case_pattern177 (string178 (string_start)179 (string_content)180 (string_end)))))181 (block182 (expression_statement183 (ellipsis))184 (comment)))185 (case_clause186 (case_pattern187 (list_pattern188 (case_pattern189 (string190 (string_start)191 (string_content)192 (string_end)))193 (case_pattern194 (dotted_name195 (identifier)))))196 (block197 (pass_statement)))198 (case_clause199 (case_pattern200 (list_pattern201 (case_pattern202 (string203 (string_start)204 (string_content)205 (string_end)))206 (case_pattern207 (splat_pattern208 (identifier)))))209 (block210 (pass_statement)))211 (case_clause212 (case_pattern)213 (block214 (expression_statement215 (call216 (identifier)217 (argument_list218 (string219 (string_start)220 (string_content)221 (interpolation222 (identifier)223 (type_conversion))224 (string_end))))))))))225 226================================================================================227Or patterns228================================================================================229 230match command.split():231 case ["north"] | ["go", "north"]:232 current_room = current_room.neighbor("north")233 case ["get", obj] | ["pick", "up", obj] | ["pick", obj, "up"]:234 pass235 236--------------------------------------------------------------------------------237 238(module239 (match_statement240 (call241 (attribute242 (identifier)243 (identifier))244 (argument_list))245 (block246 (case_clause247 (case_pattern248 (union_pattern249 (list_pattern250 (case_pattern251 (string252 (string_start)253 (string_content)254 (string_end))))255 (list_pattern256 (case_pattern257 (string258 (string_start)259 (string_content)260 (string_end)))261 (case_pattern262 (string263 (string_start)264 (string_content)265 (string_end))))))266 (block267 (expression_statement268 (assignment269 (identifier)270 (call271 (attribute272 (identifier)273 (identifier))274 (argument_list275 (string276 (string_start)277 (string_content)278 (string_end))))))))279 (case_clause280 (case_pattern281 (union_pattern282 (list_pattern283 (case_pattern284 (string285 (string_start)286 (string_content)287 (string_end)))288 (case_pattern289 (dotted_name290 (identifier))))291 (list_pattern292 (case_pattern293 (string294 (string_start)295 (string_content)296 (string_end)))297 (case_pattern298 (string299 (string_start)300 (string_content)301 (string_end)))302 (case_pattern303 (dotted_name304 (identifier))))305 (list_pattern306 (case_pattern307 (string308 (string_start)309 (string_content)310 (string_end)))311 (case_pattern312 (dotted_name313 (identifier)))314 (case_pattern315 (string316 (string_start)317 (string_content)318 (string_end))))))319 (block320 (pass_statement))))))321 322================================================================================323As patterns324================================================================================325 326match command.split():327 case ["go", ("north" | "south" | "east" | "west") as direction]:328 current_room = current_room.neighbor(direction)329 330--------------------------------------------------------------------------------331 332(module333 (match_statement334 (call335 (attribute336 (identifier)337 (identifier))338 (argument_list))339 (block340 (case_clause341 (case_pattern342 (list_pattern343 (case_pattern344 (string345 (string_start)346 (string_content)347 (string_end)))348 (case_pattern349 (as_pattern350 (case_pattern351 (tuple_pattern352 (case_pattern353 (union_pattern354 (string355 (string_start)356 (string_content)357 (string_end))358 (string359 (string_start)360 (string_content)361 (string_end))362 (string363 (string_start)364 (string_content)365 (string_end))366 (string367 (string_start)368 (string_content)369 (string_end))))))370 (identifier)))))371 (block372 (expression_statement373 (assignment374 (identifier)375 (call376 (attribute377 (identifier)378 (identifier))379 (argument_list380 (identifier))))))))))381 382================================================================================383Actually not match384================================================================================385 386match = 2387match, a = 2, 3388match: int = secret389x, match = 2, "hey, what's up?"390*match, last = [1, 2, 3]391def foo(**match): pass392 393--------------------------------------------------------------------------------394 395(module396 (expression_statement397 (assignment398 (identifier)399 (integer)))400 (expression_statement401 (assignment402 (pattern_list403 (identifier)404 (identifier))405 (expression_list406 (integer)407 (integer))))408 (expression_statement409 (assignment410 (identifier)411 (type412 (identifier))413 (identifier)))414 (expression_statement415 (assignment416 (pattern_list417 (identifier)418 (identifier))419 (expression_list420 (integer)421 (string422 (string_start)423 (string_content)424 (string_end)))))425 (expression_statement426 (assignment427 (pattern_list428 (list_splat_pattern429 (identifier))430 (identifier))431 (list432 (integer)433 (integer)434 (integer))))435 (function_definition436 (identifier)437 (parameters438 (dictionary_splat_pattern439 (identifier)))440 (block441 (pass_statement))))442 443================================================================================444Match is match but not pattern matching445================================================================================446 447a = [match]448match = [match]449 450--------------------------------------------------------------------------------451 452(module453 (expression_statement454 (assignment455 (identifier)456 (list457 (identifier))))458 (expression_statement459 (assignment460 (identifier)461 (list462 (identifier)))))463 464================================================================================465Match kwargs466================================================================================467 468field = call(match=r".*\.txt$")469 470--------------------------------------------------------------------------------471 472(module473 (expression_statement474 (assignment475 (identifier)476 (call477 (identifier)478 (argument_list479 (keyword_argument480 (identifier)481 (string482 (string_start)483 (string_content)484 (string_end))))))))485 486================================================================================487Match kwargs 2488================================================================================489 490field = match(match=match, match)491 492--------------------------------------------------------------------------------493 494(module495 (expression_statement496 (assignment497 (identifier)498 (call499 (identifier)500 (argument_list501 (keyword_argument502 (identifier)503 (identifier))504 (identifier))))))505 506================================================================================507Case used as identifier508================================================================================509 510a = [case]511case = [case]512just_in_case = call_me(case=True)513 514--------------------------------------------------------------------------------515 516(module517 (expression_statement518 (assignment519 (identifier)520 (list521 (identifier))))522 (expression_statement523 (assignment524 (identifier)525 (list526 (identifier))))527 (expression_statement528 (assignment529 (identifier)530 (call531 (identifier)532 (argument_list533 (keyword_argument534 (identifier)535 (true)))))))536 537================================================================================538If guards539================================================================================540 541match 0:542 case 0 if False:543 x = False544 case 0 if True:545 x = True546 547--------------------------------------------------------------------------------548 549(module550 (match_statement551 (integer)552 (block553 (case_clause554 (case_pattern555 (integer))556 (if_clause557 (false))558 (block559 (expression_statement560 (assignment561 (identifier)562 (false)))))563 (case_clause564 (case_pattern565 (integer))566 (if_clause567 (true))568 (block569 (expression_statement570 (assignment571 (identifier)572 (true))))))))573 574================================================================================575Literals576================================================================================577 578match xxx:579 case 3 | -3:580 pass581 case "something":582 pass583 case "something" "else":584 pass585 case 1.0 | -1.0:586 pass587 case True | False:588 pass589 case None:590 pass591 592--------------------------------------------------------------------------------593 594(module595 (match_statement596 (identifier)597 (block598 (case_clause599 (case_pattern600 (union_pattern601 (integer)602 (integer)))603 (block604 (pass_statement)))605 (case_clause606 (case_pattern607 (string608 (string_start)609 (string_content)610 (string_end)))611 (block612 (pass_statement)))613 (case_clause614 (case_pattern615 (concatenated_string616 (string617 (string_start)618 (string_content)619 (string_end))620 (string621 (string_start)622 (string_content)623 (string_end))))624 (block625 (pass_statement)))626 (case_clause627 (case_pattern628 (union_pattern629 (float)630 (float)))631 (block632 (pass_statement)))633 (case_clause634 (case_pattern635 (union_pattern636 (true)637 (false)))638 (block639 (pass_statement)))640 (case_clause641 (case_pattern642 (none))643 (block644 (pass_statement))))))645 646================================================================================647Comma separated cases648================================================================================649 650match (0, 1, 2):651 case 0,1:652 x = 0653 case 0, *x:654 x = 0655 656--------------------------------------------------------------------------------657 658(module659 (match_statement660 (tuple661 (integer)662 (integer)663 (integer))664 (block665 (case_clause666 (case_pattern667 (integer))668 (case_pattern669 (integer))670 (block671 (expression_statement672 (assignment673 (identifier)674 (integer)))))675 (case_clause676 (case_pattern677 (integer))678 (case_pattern679 (splat_pattern680 (identifier)))681 (block682 (expression_statement683 (assignment684 (identifier)685 (integer))))))))686 687================================================================================688Case terminating in comma689================================================================================690 691match x,:692 case *x,:693 y = 0694 695--------------------------------------------------------------------------------696 697(module698 (match_statement699 (identifier)700 (block701 (case_clause702 (case_pattern703 (splat_pattern704 (identifier)))705 (block706 (expression_statement707 (assignment708 (identifier)709 (integer))))))))710 711================================================================================712Multiple match patterns713================================================================================714 715match ..., ...:716 case a, b:717 return locals()718 719--------------------------------------------------------------------------------720 721(module722 (match_statement723 (ellipsis)724 (ellipsis)725 (block726 (case_clause727 (case_pattern728 (dotted_name729 (identifier)))730 (case_pattern731 (dotted_name732 (identifier)))733 (block734 (return_statement735 (call736 (identifier)737 (argument_list))))))))738 739================================================================================740Match match, case case741================================================================================742 743match = case = 0744match match:745 case case:746 x = 0747--------------------------------------------------------------------------------748 749(module750 (expression_statement751 (assignment752 (identifier)753 (assignment754 (identifier)755 (integer))))756 (match_statement757 (identifier)758 (block759 (case_clause760 (case_pattern761 (dotted_name762 (identifier)))763 (block764 (expression_statement765 (assignment766 (identifier)767 (integer))))))))768 769================================================================================770Walrus match (Issue #150)771================================================================================772 773if match := re.fullmatch(r"(-)?(\d+:)?\d?\d:\d\d(\.\d*)?", time, flags=re.ASCII):774 return 42775 776--------------------------------------------------------------------------------777 778(module779 (if_statement780 (named_expression781 (identifier)782 (call783 (attribute784 (identifier)785 (identifier))786 (argument_list787 (string788 (string_start)789 (string_content)790 (string_end))791 (identifier)792 (keyword_argument793 (identifier)794 (attribute795 (identifier)796 (identifier))))))797 (block798 (return_statement799 (integer)))))800 801================================================================================802Matching objects803================================================================================804 805match event.get():806 case Click(position=(x, y)):807 handle_click_at(x, y)808 case KeyPress(key_name="Q") | Quit():809 game.quit()810 case KeyPress(key_name="up arrow"):811 game.go_north()812 ...813 case KeyPress():814 pass # Ignore other keystrokes815 case other_event:816 raise ValueError(f"Unrecognized event: {other_event}")817 818--------------------------------------------------------------------------------819 820(module821 (match_statement822 (call823 (attribute824 (identifier)825 (identifier))826 (argument_list))827 (block828 (case_clause829 (case_pattern830 (class_pattern831 (dotted_name832 (identifier))833 (case_pattern834 (keyword_pattern835 (identifier)836 (tuple_pattern837 (case_pattern838 (dotted_name839 (identifier)))840 (case_pattern841 (dotted_name842 (identifier))))))))843 (block844 (expression_statement845 (call846 (identifier)847 (argument_list848 (identifier)849 (identifier))))))850 (case_clause851 (case_pattern852 (union_pattern853 (class_pattern854 (dotted_name855 (identifier))856 (case_pattern857 (keyword_pattern858 (identifier)859 (string860 (string_start)861 (string_content)862 (string_end)))))863 (class_pattern864 (dotted_name865 (identifier)))))866 (block867 (expression_statement868 (call869 (attribute870 (identifier)871 (identifier))872 (argument_list)))))873 (case_clause874 (case_pattern875 (class_pattern876 (dotted_name877 (identifier))878 (case_pattern879 (keyword_pattern880 (identifier)881 (string882 (string_start)883 (string_content)884 (string_end))))))885 (block886 (expression_statement887 (call888 (attribute889 (identifier)890 (identifier))891 (argument_list)))892 (expression_statement893 (ellipsis))))894 (case_clause895 (case_pattern896 (class_pattern897 (dotted_name898 (identifier))))899 (block900 (pass_statement)901 (comment)))902 (case_clause903 (case_pattern904 (dotted_name905 (identifier)))906 (block907 (raise_statement908 (call909 (identifier)910 (argument_list911 (string912 (string_start)913 (string_content)914 (interpolation915 (identifier))916 (string_end))))))))))917 918================================================================================919Positional arguments920================================================================================921 922match event.get():923 case Click((x, y)):924 handle_click_at(x, y)925 926--------------------------------------------------------------------------------927 928(module929 (match_statement930 (call931 (attribute932 (identifier)933 (identifier))934 (argument_list))935 (block936 (case_clause937 (case_pattern938 (class_pattern939 (dotted_name940 (identifier))941 (case_pattern942 (tuple_pattern943 (case_pattern944 (dotted_name945 (identifier)))946 (case_pattern947 (dotted_name948 (identifier)))))))949 (block950 (expression_statement951 (call952 (identifier)953 (argument_list954 (identifier)955 (identifier)))))))))956 957================================================================================958Constants and enums959================================================================================960 961match event.get():962 case Click((x, y), button=Button.LEFT): # This is a left click963 handle_click_at(x, y)964 case Click():965 pass # ignore other clicks966 967--------------------------------------------------------------------------------968 969(module970 (match_statement971 (call972 (attribute973 (identifier)974 (identifier))975 (argument_list))976 (block977 (case_clause978 (case_pattern979 (class_pattern980 (dotted_name981 (identifier))982 (case_pattern983 (tuple_pattern984 (case_pattern985 (dotted_name986 (identifier)))987 (case_pattern988 (dotted_name989 (identifier)))))990 (case_pattern991 (keyword_pattern992 (identifier)993 (dotted_name994 (identifier)995 (identifier))))))996 (comment)997 (block998 (expression_statement999 (call1000 (identifier)1001 (argument_list1002 (identifier)1003 (identifier))))))1004 (case_clause1005 (case_pattern1006 (class_pattern1007 (dotted_name1008 (identifier))))1009 (block1010 (pass_statement)1011 (comment))))))1012 1013================================================================================1014Dict mappings1015================================================================================1016 1017for action in actions:1018 match action:1019 case {"text": message, "color": c}:1020 ui.set_text_color(c)1021 ui.display(message)1022 case {"sleep": duration}:1023 ui.wait(duration)1024 case {"sound": url, "format": "ogg"}:1025 ui.play(url)1026 case {a.b: c}:1027 action()1028 case {"sound": _, "format": _}:1029 warning("Unsupported audio format")1030--------------------------------------------------------------------------------1031 1032(module1033 (for_statement1034 (identifier)1035 (identifier)1036 (block1037 (match_statement1038 (identifier)1039 (block1040 (case_clause1041 (case_pattern1042 (dict_pattern1043 (string1044 (string_start)1045 (string_content)1046 (string_end))1047 (case_pattern1048 (dotted_name1049 (identifier)))1050 (string1051 (string_start)1052 (string_content)1053 (string_end))1054 (case_pattern1055 (dotted_name1056 (identifier)))))1057 (block1058 (expression_statement1059 (call1060 (attribute1061 (identifier)1062 (identifier))1063 (argument_list1064 (identifier))))1065 (expression_statement1066 (call1067 (attribute1068 (identifier)1069 (identifier))1070 (argument_list1071 (identifier))))))1072 (case_clause1073 (case_pattern1074 (dict_pattern1075 (string1076 (string_start)1077 (string_content)1078 (string_end))1079 (case_pattern1080 (dotted_name1081 (identifier)))))1082 (block1083 (expression_statement1084 (call1085 (attribute1086 (identifier)1087 (identifier))1088 (argument_list1089 (identifier))))))1090 (case_clause1091 (case_pattern1092 (dict_pattern1093 (string1094 (string_start)1095 (string_content)1096 (string_end))1097 (case_pattern1098 (dotted_name1099 (identifier)))1100 (string1101 (string_start)1102 (string_content)1103 (string_end))1104 (case_pattern1105 (string1106 (string_start)1107 (string_content)1108 (string_end)))))1109 (block1110 (expression_statement1111 (call1112 (attribute1113 (identifier)1114 (identifier))1115 (argument_list1116 (identifier))))))1117 (case_clause1118 (case_pattern1119 (dict_pattern1120 (dotted_name1121 (identifier)1122 (identifier))1123 (case_pattern1124 (dotted_name1125 (identifier)))))1126 (block1127 (expression_statement1128 (call1129 (identifier)1130 (argument_list)))))1131 (case_clause1132 (case_pattern1133 (dict_pattern1134 (string1135 (string_start)1136 (string_content)1137 (string_end))1138 (case_pattern)1139 (string1140 (string_start)1141 (string_content)1142 (string_end))1143 (case_pattern)))1144 (block1145 (expression_statement1146 (call1147 (identifier)1148 (argument_list1149 (string1150 (string_start)1151 (string_content)1152 (string_end))))))))))))1153 1154================================================================================1155Builtin classes1156================================================================================1157 1158for action in actions:1159 match action:1160 case {"text": str(message), "color": str(c)}:1161 ui.set_text_color(c)1162 ui.display(message)1163 case {"sleep": float(duration)}:1164 ui.wait(duration)1165 case {"sound": str(url), "format": "ogg"}:1166 ui.play(url)1167 case {"sound": _, "format": _}:1168 warning("Unsupported audio format")1169 1170--------------------------------------------------------------------------------1171 1172(module1173 (for_statement1174 (identifier)1175 (identifier)1176 (block1177 (match_statement1178 (identifier)1179 (block1180 (case_clause1181 (case_pattern1182 (dict_pattern1183 (string1184 (string_start)1185 (string_content)1186 (string_end))1187 (case_pattern1188 (class_pattern1189 (dotted_name1190 (identifier))1191 (case_pattern1192 (dotted_name1193 (identifier)))))1194 (string1195 (string_start)1196 (string_content)1197 (string_end))1198 (case_pattern1199 (class_pattern1200 (dotted_name