@particle-academy/fancy-code 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -660,6 +660,377 @@ var tokenizePhp = (source) => {
660
660
  return tokens;
661
661
  };
662
662
 
663
+ // src/engine/tokenizers/python.ts
664
+ var KEYWORDS2 = /* @__PURE__ */ new Set([
665
+ "and",
666
+ "as",
667
+ "assert",
668
+ "async",
669
+ "await",
670
+ "break",
671
+ "class",
672
+ "continue",
673
+ "def",
674
+ "del",
675
+ "elif",
676
+ "else",
677
+ "except",
678
+ "finally",
679
+ "for",
680
+ "from",
681
+ "global",
682
+ "if",
683
+ "import",
684
+ "in",
685
+ "is",
686
+ "lambda",
687
+ "nonlocal",
688
+ "not",
689
+ "or",
690
+ "pass",
691
+ "raise",
692
+ "return",
693
+ "try",
694
+ "while",
695
+ "with",
696
+ "yield",
697
+ "True",
698
+ "False",
699
+ "None"
700
+ ]);
701
+ var TYPES = /* @__PURE__ */ new Set([
702
+ "int",
703
+ "str",
704
+ "float",
705
+ "bool",
706
+ "list",
707
+ "dict",
708
+ "tuple",
709
+ "set",
710
+ "bytes",
711
+ "bytearray",
712
+ "memoryview",
713
+ "range",
714
+ "frozenset",
715
+ "complex",
716
+ "type",
717
+ "object",
718
+ "property",
719
+ "classmethod",
720
+ "staticmethod",
721
+ "Any",
722
+ "Optional",
723
+ "Union",
724
+ "Callable",
725
+ "List",
726
+ "Dict",
727
+ "Tuple",
728
+ "Set",
729
+ "Sequence",
730
+ "Mapping",
731
+ "Iterator",
732
+ "Generator",
733
+ "Coroutine"
734
+ ]);
735
+ var tokenizePython = (source) => {
736
+ const tokens = [];
737
+ const len = source.length;
738
+ let i = 0;
739
+ while (i < len) {
740
+ const ch = source[i];
741
+ if (ch === " " || ch === " " || ch === "\n" || ch === "\r") {
742
+ i++;
743
+ continue;
744
+ }
745
+ if (ch === "#") {
746
+ const pos = i;
747
+ i++;
748
+ while (i < len && source[i] !== "\n") i++;
749
+ tokens.push({ type: "comment", start: pos, end: i });
750
+ continue;
751
+ }
752
+ if (ch === "@" && i + 1 < len && /[a-zA-Z_]/.test(source[i + 1])) {
753
+ const pos = i;
754
+ i++;
755
+ while (i < len && /[a-zA-Z0-9_.]/.test(source[i])) i++;
756
+ tokens.push({ type: "keyword", start: pos, end: i });
757
+ continue;
758
+ }
759
+ if (ch === '"' || ch === "'" || (ch === "f" || ch === "r" || ch === "b" || ch === "F" || ch === "R" || ch === "B") && (source[i + 1] === '"' || source[i + 1] === "'")) {
760
+ const pos = i;
761
+ if (ch !== '"' && ch !== "'") i++;
762
+ const quote = source[i];
763
+ if (source[i + 1] === quote && source[i + 2] === quote) {
764
+ const triple = quote + quote + quote;
765
+ i += 3;
766
+ while (i < len && source.slice(i, i + 3) !== triple) {
767
+ if (source[i] === "\\") i++;
768
+ i++;
769
+ }
770
+ i += 3;
771
+ tokens.push({ type: "string", start: pos, end: i });
772
+ continue;
773
+ }
774
+ i++;
775
+ while (i < len && source[i] !== quote && source[i] !== "\n") {
776
+ if (source[i] === "\\") i++;
777
+ i++;
778
+ }
779
+ if (i < len && source[i] === quote) i++;
780
+ tokens.push({ type: "string", start: pos, end: i });
781
+ continue;
782
+ }
783
+ if (ch >= "0" && ch <= "9" || ch === "." && i + 1 < len && source[i + 1] >= "0" && source[i + 1] <= "9") {
784
+ const pos = i;
785
+ if (ch === "0" && (source[i + 1] === "x" || source[i + 1] === "X")) {
786
+ i += 2;
787
+ while (i < len && /[0-9a-fA-F_]/.test(source[i])) i++;
788
+ } else if (ch === "0" && (source[i + 1] === "b" || source[i + 1] === "B")) {
789
+ i += 2;
790
+ while (i < len && /[01_]/.test(source[i])) i++;
791
+ } else if (ch === "0" && (source[i + 1] === "o" || source[i + 1] === "O")) {
792
+ i += 2;
793
+ while (i < len && /[0-7_]/.test(source[i])) i++;
794
+ } else {
795
+ while (i < len && /[0-9_.]/.test(source[i])) i++;
796
+ if (i < len && (source[i] === "e" || source[i] === "E")) {
797
+ i++;
798
+ if (i < len && (source[i] === "+" || source[i] === "-")) i++;
799
+ while (i < len && source[i] >= "0" && source[i] <= "9") i++;
800
+ }
801
+ }
802
+ if (i < len && source[i] === "j") i++;
803
+ tokens.push({ type: "number", start: pos, end: i });
804
+ continue;
805
+ }
806
+ if (/[a-zA-Z_]/.test(ch)) {
807
+ const pos = i;
808
+ i++;
809
+ while (i < len && /[a-zA-Z0-9_]/.test(source[i])) i++;
810
+ const word = source.slice(pos, i);
811
+ let j = i;
812
+ while (j < len && (source[j] === " " || source[j] === " ")) j++;
813
+ if (KEYWORDS2.has(word)) {
814
+ tokens.push({ type: "keyword", start: pos, end: i });
815
+ } else if (TYPES.has(word)) {
816
+ tokens.push({ type: "type", start: pos, end: i });
817
+ } else if (source[j] === "(") {
818
+ tokens.push({ type: "function", start: pos, end: i });
819
+ } else if (word[0] >= "A" && word[0] <= "Z") {
820
+ tokens.push({ type: "type", start: pos, end: i });
821
+ } else {
822
+ tokens.push({ type: "variable", start: pos, end: i });
823
+ }
824
+ continue;
825
+ }
826
+ if ("+-*/%=<>!&|^~:@".includes(ch)) {
827
+ const pos = i;
828
+ i++;
829
+ while (i < len && "+-*/%=<>!&|^~:".includes(source[i])) i++;
830
+ tokens.push({ type: "operator", start: pos, end: i });
831
+ continue;
832
+ }
833
+ if ("()[]{},.;\\".includes(ch)) {
834
+ tokens.push({ type: "punctuation", start: i, end: i + 1 });
835
+ i++;
836
+ continue;
837
+ }
838
+ tokens.push({ type: "plain", start: i, end: i + 1 });
839
+ i++;
840
+ }
841
+ return tokens;
842
+ };
843
+
844
+ // src/engine/tokenizers/go.ts
845
+ var KEYWORDS3 = /* @__PURE__ */ new Set([
846
+ "break",
847
+ "case",
848
+ "chan",
849
+ "const",
850
+ "continue",
851
+ "default",
852
+ "defer",
853
+ "else",
854
+ "fallthrough",
855
+ "for",
856
+ "func",
857
+ "go",
858
+ "goto",
859
+ "if",
860
+ "import",
861
+ "interface",
862
+ "map",
863
+ "package",
864
+ "range",
865
+ "return",
866
+ "select",
867
+ "struct",
868
+ "switch",
869
+ "type",
870
+ "var",
871
+ "nil",
872
+ "true",
873
+ "false",
874
+ "iota"
875
+ ]);
876
+ var TYPES2 = /* @__PURE__ */ new Set([
877
+ "bool",
878
+ "byte",
879
+ "complex64",
880
+ "complex128",
881
+ "error",
882
+ "float32",
883
+ "float64",
884
+ "int",
885
+ "int8",
886
+ "int16",
887
+ "int32",
888
+ "int64",
889
+ "rune",
890
+ "string",
891
+ "uint",
892
+ "uint8",
893
+ "uint16",
894
+ "uint32",
895
+ "uint64",
896
+ "uintptr",
897
+ "any"
898
+ ]);
899
+ var BUILTINS = /* @__PURE__ */ new Set([
900
+ "make",
901
+ "len",
902
+ "cap",
903
+ "new",
904
+ "append",
905
+ "copy",
906
+ "close",
907
+ "delete",
908
+ "complex",
909
+ "real",
910
+ "imag",
911
+ "panic",
912
+ "recover",
913
+ "print",
914
+ "println"
915
+ ]);
916
+ var tokenizeGo = (source) => {
917
+ const tokens = [];
918
+ const len = source.length;
919
+ let i = 0;
920
+ while (i < len) {
921
+ const ch = source[i];
922
+ if (ch === " " || ch === " " || ch === "\n" || ch === "\r") {
923
+ i++;
924
+ continue;
925
+ }
926
+ if (ch === "/" && source[i + 1] === "/") {
927
+ const pos = i;
928
+ i += 2;
929
+ while (i < len && source[i] !== "\n") i++;
930
+ tokens.push({ type: "comment", start: pos, end: i });
931
+ continue;
932
+ }
933
+ if (ch === "/" && source[i + 1] === "*") {
934
+ const pos = i;
935
+ i += 2;
936
+ while (i < len && !(source[i] === "*" && source[i + 1] === "/")) i++;
937
+ i += 2;
938
+ tokens.push({ type: "comment", start: pos, end: i });
939
+ continue;
940
+ }
941
+ if (ch === "`") {
942
+ const pos = i;
943
+ i++;
944
+ while (i < len && source[i] !== "`") i++;
945
+ i++;
946
+ tokens.push({ type: "string", start: pos, end: i });
947
+ continue;
948
+ }
949
+ if (ch === '"') {
950
+ const pos = i;
951
+ i++;
952
+ while (i < len && source[i] !== '"' && source[i] !== "\n") {
953
+ if (source[i] === "\\") i++;
954
+ i++;
955
+ }
956
+ if (i < len && source[i] === '"') i++;
957
+ tokens.push({ type: "string", start: pos, end: i });
958
+ continue;
959
+ }
960
+ if (ch === "'") {
961
+ const pos = i;
962
+ i++;
963
+ while (i < len && source[i] !== "'" && source[i] !== "\n") {
964
+ if (source[i] === "\\") i++;
965
+ i++;
966
+ }
967
+ if (i < len && source[i] === "'") i++;
968
+ tokens.push({ type: "string", start: pos, end: i });
969
+ continue;
970
+ }
971
+ if (ch >= "0" && ch <= "9" || ch === "." && i + 1 < len && source[i + 1] >= "0" && source[i + 1] <= "9") {
972
+ const pos = i;
973
+ if (ch === "0" && (source[i + 1] === "x" || source[i + 1] === "X")) {
974
+ i += 2;
975
+ while (i < len && /[0-9a-fA-F_]/.test(source[i])) i++;
976
+ } else if (ch === "0" && (source[i + 1] === "b" || source[i + 1] === "B")) {
977
+ i += 2;
978
+ while (i < len && /[01_]/.test(source[i])) i++;
979
+ } else if (ch === "0" && (source[i + 1] === "o" || source[i + 1] === "O")) {
980
+ i += 2;
981
+ while (i < len && /[0-7_]/.test(source[i])) i++;
982
+ } else {
983
+ while (i < len && /[0-9_.]/.test(source[i])) i++;
984
+ if (i < len && (source[i] === "e" || source[i] === "E")) {
985
+ i++;
986
+ if (i < len && (source[i] === "+" || source[i] === "-")) i++;
987
+ while (i < len && source[i] >= "0" && source[i] <= "9") i++;
988
+ }
989
+ }
990
+ if (i < len && source[i] === "i") i++;
991
+ tokens.push({ type: "number", start: pos, end: i });
992
+ continue;
993
+ }
994
+ if (/[a-zA-Z_]/.test(ch)) {
995
+ const pos = i;
996
+ i++;
997
+ while (i < len && /[a-zA-Z0-9_]/.test(source[i])) i++;
998
+ const word = source.slice(pos, i);
999
+ let j = i;
1000
+ while (j < len && (source[j] === " " || source[j] === " ")) j++;
1001
+ if (KEYWORDS3.has(word)) {
1002
+ tokens.push({ type: "keyword", start: pos, end: i });
1003
+ } else if (TYPES2.has(word)) {
1004
+ tokens.push({ type: "type", start: pos, end: i });
1005
+ } else if (BUILTINS.has(word) && source[j] === "(") {
1006
+ tokens.push({ type: "function", start: pos, end: i });
1007
+ } else if (source[j] === "(") {
1008
+ tokens.push({ type: "function", start: pos, end: i });
1009
+ } else if (word[0] >= "A" && word[0] <= "Z") {
1010
+ tokens.push({ type: "type", start: pos, end: i });
1011
+ } else {
1012
+ tokens.push({ type: "variable", start: pos, end: i });
1013
+ }
1014
+ continue;
1015
+ }
1016
+ if ("+-*/%=<>!&|^~:".includes(ch)) {
1017
+ const pos = i;
1018
+ i++;
1019
+ while (i < len && "+-*/%=<>!&|^~:".includes(source[i])) i++;
1020
+ tokens.push({ type: "operator", start: pos, end: i });
1021
+ continue;
1022
+ }
1023
+ if ("(){}[];,.".includes(ch)) {
1024
+ tokens.push({ type: "punctuation", start: i, end: i + 1 });
1025
+ i++;
1026
+ continue;
1027
+ }
1028
+ tokens.push({ type: "plain", start: i, end: i + 1 });
1029
+ i++;
1030
+ }
1031
+ return tokens;
1032
+ };
1033
+
663
1034
  // src/languages/builtin.ts
664
1035
  registerLanguage({
665
1036
  name: "JavaScript",
@@ -682,6 +1053,16 @@ registerLanguage({
682
1053
  aliases: ["php"],
683
1054
  tokenize: tokenizePhp
684
1055
  });
1056
+ registerLanguage({
1057
+ name: "Python",
1058
+ aliases: ["py", "python"],
1059
+ tokenize: tokenizePython
1060
+ });
1061
+ registerLanguage({
1062
+ name: "Go",
1063
+ aliases: ["go", "golang"],
1064
+ tokenize: tokenizeGo
1065
+ });
685
1066
  var iconBtnClass = "inline-flex items-center justify-center rounded-md p-1 text-zinc-500 transition-colors hover:bg-zinc-100 hover:text-zinc-700 dark:text-zinc-400 dark:hover:bg-zinc-800 dark:hover:text-zinc-200";
686
1067
  function LanguageSelector() {
687
1068
  const { language, setLanguage } = useCodeEditor();