source: node_modules/refractor/lang/zig.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.3 KB
Line 
1'use strict'
2
3module.exports = zig
4zig.displayName = 'zig'
5zig.aliases = []
6function zig(Prism) {
7 ;(function (Prism) {
8 function literal(str) {
9 return function () {
10 return str
11 }
12 }
13 var keyword =
14 /\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/
15 var IDENTIFIER = '\\b(?!' + keyword.source + ')(?!\\d)\\w+\\b'
16 var ALIGN = /align\s*\((?:[^()]|\([^()]*\))*\)/.source
17 var PREFIX_TYPE_OP =
18 /(?:\?|\bpromise->|(?:\[[^[\]]*\]|\*(?!\*)|\*\*)(?:\s*<ALIGN>|\s*const\b|\s*volatile\b|\s*allowzero\b)*)/.source.replace(
19 /<ALIGN>/g,
20 literal(ALIGN)
21 )
22 var SUFFIX_EXPR =
23 /(?:\bpromise\b|(?:\berror\.)?<ID>(?:\.<ID>)*(?!\s+<ID>))/.source.replace(
24 /<ID>/g,
25 literal(IDENTIFIER)
26 )
27 var TYPE =
28 '(?!\\s)(?:!?\\s*(?:' + PREFIX_TYPE_OP + '\\s*)*' + SUFFIX_EXPR + ')+'
29 /*
30 * A simplified grammar for Zig compile time type literals:
31 *
32 * TypeExpr = ( "!"? PREFIX_TYPE_OP* SUFFIX_EXPR )+
33 *
34 * SUFFIX_EXPR = ( \b "promise" \b | ( \b "error" "." )? IDENTIFIER ( "." IDENTIFIER )* (?! \s+ IDENTIFIER ) )
35 *
36 * PREFIX_TYPE_OP = "?"
37 * | \b "promise" "->"
38 * | ( "[" [^\[\]]* "]" | "*" | "**" ) ( ALIGN | "const" \b | "volatile" \b | "allowzero" \b )*
39 *
40 * ALIGN = "align" "(" ( [^()] | "(" [^()]* ")" )* ")"
41 *
42 * IDENTIFIER = \b (?! KEYWORD ) [a-zA-Z_] \w* \b
43 *
44 */
45 Prism.languages.zig = {
46 comment: [
47 {
48 pattern: /\/\/[/!].*/,
49 alias: 'doc-comment'
50 },
51 /\/{2}.*/
52 ],
53 string: [
54 {
55 // "string" and c"string"
56 pattern: /(^|[^\\@])c?"(?:[^"\\\r\n]|\\.)*"/,
57 lookbehind: true,
58 greedy: true
59 },
60 {
61 // multiline strings and c-strings
62 pattern: /([\r\n])([ \t]+c?\\{2}).*(?:(?:\r\n?|\n)\2.*)*/,
63 lookbehind: true,
64 greedy: true
65 }
66 ],
67 char: {
68 // characters 'a', '\n', '\xFF', '\u{10FFFF}'
69 pattern:
70 /(^|[^\\])'(?:[^'\\\r\n]|[\uD800-\uDFFF]{2}|\\(?:.|x[a-fA-F\d]{2}|u\{[a-fA-F\d]{1,6}\}))'/,
71 lookbehind: true,
72 greedy: true
73 },
74 builtin: /\B@(?!\d)\w+(?=\s*\()/,
75 label: {
76 pattern:
77 /(\b(?:break|continue)\s*:\s*)\w+\b|\b(?!\d)\w+\b(?=\s*:\s*(?:\{|while\b))/,
78 lookbehind: true
79 },
80 'class-name': [
81 // const Foo = struct {};
82 /\b(?!\d)\w+(?=\s*=\s*(?:(?:extern|packed)\s+)?(?:enum|struct|union)\s*[({])/,
83 {
84 // const x: i32 = 9;
85 // var x: Bar;
86 // fn foo(x: bool, y: f32) void {}
87 pattern: RegExp(
88 /(:\s*)<TYPE>(?=\s*(?:<ALIGN>\s*)?[=;,)])|<TYPE>(?=\s*(?:<ALIGN>\s*)?\{)/.source
89 .replace(/<TYPE>/g, literal(TYPE))
90 .replace(/<ALIGN>/g, literal(ALIGN))
91 ),
92 lookbehind: true,
93 inside: null // see below
94 },
95 {
96 // extern fn foo(x: f64) f64; (optional alignment)
97 pattern: RegExp(
98 /(\)\s*)<TYPE>(?=\s*(?:<ALIGN>\s*)?;)/.source
99 .replace(/<TYPE>/g, literal(TYPE))
100 .replace(/<ALIGN>/g, literal(ALIGN))
101 ),
102 lookbehind: true,
103 inside: null // see below
104 }
105 ],
106 'builtin-type': {
107 pattern:
108 /\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/,
109 alias: 'keyword'
110 },
111 keyword: keyword,
112 function: /\b(?!\d)\w+(?=\s*\()/,
113 number:
114 /\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/,
115 boolean: /\b(?:false|true)\b/,
116 operator:
117 /\.[*?]|\.{2,3}|[-=]>|\*\*|\+\+|\|\||(?:<<|>>|[-+*]%|[-+*/%^&|<>!=])=?|[?~]/,
118 punctuation: /[.:,;(){}[\]]/
119 }
120 Prism.languages.zig['class-name'].forEach(function (obj) {
121 if (obj.inside === null) {
122 obj.inside = Prism.languages.zig
123 }
124 })
125 })(Prism)
126}
Note: See TracBrowser for help on using the repository browser.