1 | (function (Prism) {
|
---|
2 |
|
---|
3 | function literal(str) {
|
---|
4 | return function () { return str; };
|
---|
5 | }
|
---|
6 |
|
---|
7 | 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/;
|
---|
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: /\/\/[/!].*/,
|
---|
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 | 'char': {
|
---|
55 | // characters 'a', '\n', '\xFF', '\u{10FFFF}'
|
---|
56 | pattern: /(^|[^\\])'(?:[^'\\\r\n]|[\uD800-\uDFFF]{2}|\\(?:.|x[a-fA-F\d]{2}|u\{[a-fA-F\d]{1,6}\}))'/,
|
---|
57 | lookbehind: true,
|
---|
58 | greedy: true
|
---|
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-type': {
|
---|
84 | 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/,
|
---|
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));
|
---|