UNPKG

4.27 kBJavaScriptView Raw
1(function (Prism) {
2
3 /**
4 * Replaces all placeholders "<<n>>" of given pattern with the n-th replacement (zero based).
5 *
6 * Note: This is a simple text based replacement. Be careful when using backreferences!
7 *
8 * @param {string} pattern the given pattern.
9 * @param {string[]} replacements a list of replacement which can be inserted into the given pattern.
10 * @returns {string} the pattern with all placeholders replaced with their corresponding replacements.
11 * @example replace(/a<<0>>a/.source, [/b+/.source]) === /a(?:b+)a/.source
12 */
13 function replace(pattern, replacements) {
14 return pattern.replace(/<<(\d+)>>/g, function (m, index) {
15 return '(?:' + replacements[+index] + ')';
16 });
17 }
18 /**
19 * @param {string} pattern
20 * @param {string[]} replacements
21 * @param {string} [flags]
22 * @returns {RegExp}
23 */
24 function re(pattern, replacements, flags) {
25 return RegExp(replace(pattern, replacements), flags || '');
26 }
27
28 /**
29 * Creates a nested pattern where all occurrences of the string `<<self>>` are replaced with the pattern itself.
30 *
31 * @param {string} pattern
32 * @param {number} depthLog2
33 * @returns {string}
34 */
35 function nested(pattern, depthLog2) {
36 for (var i = 0; i < depthLog2; i++) {
37 pattern = pattern.replace(/<<self>>/g, function () { return '(?:' + pattern + ')'; });
38 }
39 return pattern.replace(/<<self>>/g, '[^\\s\\S]');
40 }
41
42 // https://docs.microsoft.com/en-us/azure/quantum/user-guide/language/typesystem/
43 // https://github.com/microsoft/qsharp-language/tree/main/Specifications/Language/5_Grammar
44 var keywordKinds = {
45 // keywords which represent a return or variable type
46 type: 'Adj BigInt Bool Ctl Double false Int One Pauli PauliI PauliX PauliY PauliZ Qubit Range Result String true Unit Zero',
47 // all other keywords
48 other: 'Adjoint adjoint apply as auto body borrow borrowing Controlled controlled distribute elif else fail fixup for function if in internal intrinsic invert is let mutable namespace new newtype open operation repeat return self set until use using while within'
49 };
50 // keywords
51 function keywordsToPattern(words) {
52 return '\\b(?:' + words.trim().replace(/ /g, '|') + ')\\b';
53 }
54 var keywords = RegExp(keywordsToPattern(keywordKinds.type + ' ' + keywordKinds.other));
55
56 // types
57 var identifier = /\b[A-Za-z_]\w*\b/.source;
58 var qualifiedName = replace(/<<0>>(?:\s*\.\s*<<0>>)*/.source, [identifier]);
59
60 var typeInside = {
61 'keyword': keywords,
62 'punctuation': /[<>()?,.:[\]]/
63 };
64
65 // strings
66 var regularString = /"(?:\\.|[^\\"])*"/.source;
67
68 Prism.languages.qsharp = Prism.languages.extend('clike', {
69 'comment': /\/\/.*/,
70 'string': [
71 {
72 pattern: re(/(^|[^$\\])<<0>>/.source, [regularString]),
73 lookbehind: true,
74 greedy: true
75 }
76 ],
77 'class-name': [
78 {
79 // open Microsoft.Quantum.Canon;
80 // open Microsoft.Quantum.Canon as CN;
81 pattern: re(/(\b(?:as|open)\s+)<<0>>(?=\s*(?:;|as\b))/.source, [qualifiedName]),
82 lookbehind: true,
83 inside: typeInside
84 },
85 {
86 // namespace Quantum.App1;
87 pattern: re(/(\bnamespace\s+)<<0>>(?=\s*\{)/.source, [qualifiedName]),
88 lookbehind: true,
89 inside: typeInside
90 },
91 ],
92 'keyword': keywords,
93 'number': /(?:\b0(?:x[\da-f]+|b[01]+|o[0-7]+)|(?:\B\.\d+|\b\d+(?:\.\d*)?)(?:e[-+]?\d+)?)l?\b/i,
94 'operator': /\band=|\bor=|\band\b|\bnot\b|\bor\b|<[-=]|[-=]>|>>>=?|<<<=?|\^\^\^=?|\|\|\|=?|&&&=?|w\/=?|~~~|[*\/+\-^=!%]=?/,
95 'punctuation': /::|[{}[\];(),.:]/
96 });
97
98 Prism.languages.insertBefore('qsharp', 'number', {
99 'range': {
100 pattern: /\.\./,
101 alias: 'operator'
102 }
103 });
104
105 // single line
106 var interpolationExpr = nested(replace(/\{(?:[^"{}]|<<0>>|<<self>>)*\}/.source, [regularString]), 2);
107
108 Prism.languages.insertBefore('qsharp', 'string', {
109 'interpolation-string': {
110 pattern: re(/\$"(?:\\.|<<0>>|[^\\"{])*"/.source, [interpolationExpr]),
111 greedy: true,
112 inside: {
113 'interpolation': {
114 pattern: re(/((?:^|[^\\])(?:\\\\)*)<<0>>/.source, [interpolationExpr]),
115 lookbehind: true,
116 inside: {
117 'punctuation': /^\{|\}$/,
118 'expression': {
119 pattern: /[\s\S]+/,
120 alias: 'language-qsharp',
121 inside: Prism.languages.qsharp
122 }
123 }
124 },
125 'string': /[\s\S]+/
126 }
127 }
128 });
129
130}(Prism));
131
132Prism.languages.qs = Prism.languages.qsharp;