prism-zig.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. (function (Prism) {
  2. function literal(str) {
  3. return function () { return str; };
  4. }
  5. var keyword = /\b(?:align|allowzero|and|anyframe|anytype|asm|async|await|break|cancel|catch|comptime|const|continue|defer|else|enum|errdefer|error|export|extern|fn|for|if|inline|linksection|nakedcc|noalias|nosuspend|null|or|orelse|packed|promise|pub|resume|return|stdcallcc|struct|suspend|switch|test|threadlocal|try|undefined|union|unreachable|usingnamespace|var|volatile|while)\b/;
  6. var IDENTIFIER = '\\b(?!' + keyword.source + ')(?!\\d)\\w+\\b';
  7. var ALIGN = /align\s*\((?:[^()]|\([^()]*\))*\)/.source;
  8. var PREFIX_TYPE_OP = /(?:\?|\bpromise->|(?:\[[^[\]]*\]|\*(?!\*)|\*\*)(?:\s*<ALIGN>|\s*const\b|\s*volatile\b|\s*allowzero\b)*)/.source.replace(/<ALIGN>/g, literal(ALIGN));
  9. var SUFFIX_EXPR = /(?:\bpromise\b|(?:\berror\.)?<ID>(?:\.<ID>)*(?!\s+<ID>))/.source.replace(/<ID>/g, literal(IDENTIFIER));
  10. var TYPE = '(?!\\s)(?:!?\\s*(?:' + PREFIX_TYPE_OP + '\\s*)*' + SUFFIX_EXPR + ')+';
  11. /*
  12. * A simplified grammar for Zig compile time type literals:
  13. *
  14. * TypeExpr = ( "!"? PREFIX_TYPE_OP* SUFFIX_EXPR )+
  15. *
  16. * SUFFIX_EXPR = ( \b "promise" \b | ( \b "error" "." )? IDENTIFIER ( "." IDENTIFIER )* (?! \s+ IDENTIFIER ) )
  17. *
  18. * PREFIX_TYPE_OP = "?"
  19. * | \b "promise" "->"
  20. * | ( "[" [^\[\]]* "]" | "*" | "**" ) ( ALIGN | "const" \b | "volatile" \b | "allowzero" \b )*
  21. *
  22. * ALIGN = "align" "(" ( [^()] | "(" [^()]* ")" )* ")"
  23. *
  24. * IDENTIFIER = \b (?! KEYWORD ) [a-zA-Z_] \w* \b
  25. *
  26. */
  27. Prism.languages.zig = {
  28. 'comment': [
  29. {
  30. pattern: /\/\/[/!].*/,
  31. alias: 'doc-comment'
  32. },
  33. /\/{2}.*/
  34. ],
  35. 'string': [
  36. {
  37. // "string" and c"string"
  38. pattern: /(^|[^\\@])c?"(?:[^"\\\r\n]|\\.)*"/,
  39. lookbehind: true,
  40. greedy: true
  41. },
  42. {
  43. // multiline strings and c-strings
  44. pattern: /([\r\n])([ \t]+c?\\{2}).*(?:(?:\r\n?|\n)\2.*)*/,
  45. lookbehind: true,
  46. greedy: true
  47. }
  48. ],
  49. 'char': {
  50. // characters 'a', '\n', '\xFF', '\u{10FFFF}'
  51. pattern: /(^|[^\\])'(?:[^'\\\r\n]|[\uD800-\uDFFF]{2}|\\(?:.|x[a-fA-F\d]{2}|u\{[a-fA-F\d]{1,6}\}))'/,
  52. lookbehind: true,
  53. greedy: true
  54. },
  55. 'builtin': /\B@(?!\d)\w+(?=\s*\()/,
  56. 'label': {
  57. pattern: /(\b(?:break|continue)\s*:\s*)\w+\b|\b(?!\d)\w+\b(?=\s*:\s*(?:\{|while\b))/,
  58. lookbehind: true
  59. },
  60. 'class-name': [
  61. // const Foo = struct {};
  62. /\b(?!\d)\w+(?=\s*=\s*(?:(?:extern|packed)\s+)?(?:enum|struct|union)\s*[({])/,
  63. {
  64. // const x: i32 = 9;
  65. // var x: Bar;
  66. // fn foo(x: bool, y: f32) void {}
  67. pattern: RegExp(/(:\s*)<TYPE>(?=\s*(?:<ALIGN>\s*)?[=;,)])|<TYPE>(?=\s*(?:<ALIGN>\s*)?\{)/.source.replace(/<TYPE>/g, literal(TYPE)).replace(/<ALIGN>/g, literal(ALIGN))),
  68. lookbehind: true,
  69. inside: null // see below
  70. },
  71. {
  72. // extern fn foo(x: f64) f64; (optional alignment)
  73. pattern: RegExp(/(\)\s*)<TYPE>(?=\s*(?:<ALIGN>\s*)?;)/.source.replace(/<TYPE>/g, literal(TYPE)).replace(/<ALIGN>/g, literal(ALIGN))),
  74. lookbehind: true,
  75. inside: null // see below
  76. }
  77. ],
  78. 'builtin-type': {
  79. pattern: /\b(?:anyerror|bool|c_u?(?:int|long|longlong|short)|c_longdouble|c_void|comptime_(?:float|int)|f(?:16|32|64|128)|[iu](?:8|16|32|64|128|size)|noreturn|type|void)\b/,
  80. alias: 'keyword'
  81. },
  82. 'keyword': keyword,
  83. 'function': /\b(?!\d)\w+(?=\s*\()/,
  84. 'number': /\b(?:0b[01]+|0o[0-7]+|0x[a-fA-F\d]+(?:\.[a-fA-F\d]*)?(?:[pP][+-]?[a-fA-F\d]+)?|\d+(?:\.\d*)?(?:[eE][+-]?\d+)?)\b/,
  85. 'boolean': /\b(?:false|true)\b/,
  86. 'operator': /\.[*?]|\.{2,3}|[-=]>|\*\*|\+\+|\|\||(?:<<|>>|[-+*]%|[-+*/%^&|<>!=])=?|[?~]/,
  87. 'punctuation': /[.:,;(){}[\]]/
  88. };
  89. Prism.languages.zig['class-name'].forEach(function (obj) {
  90. if (obj.inside === null) {
  91. obj.inside = Prism.languages.zig;
  92. }
  93. });
  94. }(Prism));