UNPKG

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