source: node_modules/refractor/lang/qsharp.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.8 KB
RevLine 
[d24f17c]1'use strict'
2
3module.exports = qsharp
4qsharp.displayName = 'qsharp'
5qsharp.aliases = ['qs']
6function qsharp(Prism) {
7 ;(function (Prism) {
8 /**
9 * Replaces all placeholders "<<n>>" of given pattern with the n-th replacement (zero based).
10 *
11 * Note: This is a simple text based replacement. Be careful when using backreferences!
12 *
13 * @param {string} pattern the given pattern.
14 * @param {string[]} replacements a list of replacement which can be inserted into the given pattern.
15 * @returns {string} the pattern with all placeholders replaced with their corresponding replacements.
16 * @example replace(/a<<0>>a/.source, [/b+/.source]) === /a(?:b+)a/.source
17 */
18 function replace(pattern, replacements) {
19 return pattern.replace(/<<(\d+)>>/g, function (m, index) {
20 return '(?:' + replacements[+index] + ')'
21 })
22 }
23 /**
24 * @param {string} pattern
25 * @param {string[]} replacements
26 * @param {string} [flags]
27 * @returns {RegExp}
28 */
29 function re(pattern, replacements, flags) {
30 return RegExp(replace(pattern, replacements), flags || '')
31 }
32 /**
33 * Creates a nested pattern where all occurrences of the string `<<self>>` are replaced with the pattern itself.
34 *
35 * @param {string} pattern
36 * @param {number} depthLog2
37 * @returns {string}
38 */
39 function nested(pattern, depthLog2) {
40 for (var i = 0; i < depthLog2; i++) {
41 pattern = pattern.replace(/<<self>>/g, function () {
42 return '(?:' + pattern + ')'
43 })
44 }
45 return pattern.replace(/<<self>>/g, '[^\\s\\S]')
46 } // https://docs.microsoft.com/en-us/azure/quantum/user-guide/language/typesystem/
47 // https://github.com/microsoft/qsharp-language/tree/main/Specifications/Language/5_Grammar
48 var keywordKinds = {
49 // keywords which represent a return or variable type
50 type: 'Adj BigInt Bool Ctl Double false Int One Pauli PauliI PauliX PauliY PauliZ Qubit Range Result String true Unit Zero',
51 // all other keywords
52 other:
53 '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'
54 } // keywords
55 function keywordsToPattern(words) {
56 return '\\b(?:' + words.trim().replace(/ /g, '|') + ')\\b'
57 }
58 var keywords = RegExp(
59 keywordsToPattern(keywordKinds.type + ' ' + keywordKinds.other)
60 ) // types
61 var identifier = /\b[A-Za-z_]\w*\b/.source
62 var qualifiedName = replace(/<<0>>(?:\s*\.\s*<<0>>)*/.source, [identifier])
63 var typeInside = {
64 keyword: keywords,
65 punctuation: /[<>()?,.:[\]]/
66 } // strings
67 var regularString = /"(?:\\.|[^\\"])*"/.source
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, [
82 qualifiedName
83 ]),
84 lookbehind: true,
85 inside: typeInside
86 },
87 {
88 // namespace Quantum.App1;
89 pattern: re(/(\bnamespace\s+)<<0>>(?=\s*\{)/.source, [qualifiedName]),
90 lookbehind: true,
91 inside: typeInside
92 }
93 ],
94 keyword: keywords,
95 number:
96 /(?:\b0(?:x[\da-f]+|b[01]+|o[0-7]+)|(?:\B\.\d+|\b\d+(?:\.\d*)?)(?:e[-+]?\d+)?)l?\b/i,
97 operator:
98 /\band=|\bor=|\band\b|\bnot\b|\bor\b|<[-=]|[-=]>|>>>=?|<<<=?|\^\^\^=?|\|\|\|=?|&&&=?|w\/=?|~~~|[*\/+\-^=!%]=?/,
99 punctuation: /::|[{}[\];(),.:]/
100 })
101 Prism.languages.insertBefore('qsharp', 'number', {
102 range: {
103 pattern: /\.\./,
104 alias: 'operator'
105 }
106 }) // single line
107 var interpolationExpr = nested(
108 replace(/\{(?:[^"{}]|<<0>>|<<self>>)*\}/.source, [regularString]),
109 2
110 )
111 Prism.languages.insertBefore('qsharp', 'string', {
112 'interpolation-string': {
113 pattern: re(/\$"(?:\\.|<<0>>|[^\\"{])*"/.source, [interpolationExpr]),
114 greedy: true,
115 inside: {
116 interpolation: {
117 pattern: re(/((?:^|[^\\])(?:\\\\)*)<<0>>/.source, [
118 interpolationExpr
119 ]),
120 lookbehind: true,
121 inside: {
122 punctuation: /^\{|\}$/,
123 expression: {
124 pattern: /[\s\S]+/,
125 alias: 'language-qsharp',
126 inside: Prism.languages.qsharp
127 }
128 }
129 },
130 string: /[\s\S]+/
131 }
132 }
133 })
134 })(Prism)
135 Prism.languages.qs = Prism.languages.qsharp
136}
Note: See TracBrowser for help on using the repository browser.