microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.23.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/pip/qsharp/.data/qsharp_codemirror.js

95lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4// This file provides CodeMirror syntax highlighting for Q# magic cells
5// in classic Jupyter Notebooks. It does nothing in other (Jupyter Notebook 7,
6// VS Code, Azure Notebooks, etc.) environments.
7
8// Detect the prerequisites and do nothing if they don't exist.
9if (window.require && window.CodeMirror && window.Jupyter) {
10 // The simple mode plugin for CodeMirror is not loaded by default, so require it.
11 window.require(["codemirror/addon/mode/simple"], function defineMode() {
12 let rules = [
13 {
14 token: "comment",
15 regex: /(\/\/).*/,
16 beginWord: false,
17 },
18 {
19 token: "string",
20 regex: String.raw`^\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)`,
21 beginWord: false,
22 },
23 {
24 token: "keyword",
25 regex: String.raw`(namespace|open|as|operation|function|body|adjoint|newtype|controlled|internal)\b`,
26 beginWord: true,
27 },
28 {
29 token: "keyword",
30 regex: String.raw`(if|elif|else|repeat|until|fixup|for|in|return|fail|within|apply)\b`,
31 beginWord: true,
32 },
33 {
34 token: "keyword",
35 regex: String.raw`(Adjoint|Controlled|Adj|Ctl|is|self|auto|distribute|invert|intrinsic)\b`,
36 beginWord: true,
37 },
38 {
39 token: "keyword",
40 regex: String.raw`(let|set|use|borrow|mutable)\b`,
41 beginWord: true,
42 },
43 {
44 token: "operatorKeyword",
45 regex: String.raw`(not|and|or)\b|(w/)`,
46 beginWord: true,
47 },
48 {
49 token: "operatorKeyword",
50 regex: String.raw`(=)|(!)|(<)|(>)|(\+)|(-)|(\*)|(/)|(\^)|(%)|(\|)|(&&&)|(~~~)|(\.\.\.)|(\.\.)|(\?)`,
51 beginWord: false,
52 },
53 {
54 token: "meta",
55 regex: String.raw`(Int|BigInt|Double|Bool|Qubit|Pauli|Result|Range|String|Unit)\b`,
56 beginWord: true,
57 },
58 {
59 token: "atom",
60 regex: String.raw`(true|false|Pauli(I|X|Y|Z)|One|Zero)\b`,
61 beginWord: true,
62 },
63 ];
64 let simpleRules = [];
65 for (let rule of rules) {
66 simpleRules.push({
67 token: rule.token,
68 regex: new RegExp(rule.regex, "g"),
69 sol: rule.beginWord,
70 });
71 if (rule.beginWord) {
72 // Need an additional rule due to the fact that CodeMirror simple mode doesn't work with ^ token
73 simpleRules.push({
74 token: rule.token,
75 regex: new RegExp(String.raw`\W` + rule.regex, "g"),
76 sol: false,
77 });
78 }
79 }
80
81 // Register the mode defined above with CodeMirror
82 window.CodeMirror.defineSimpleMode("qsharp", { start: simpleRules });
83 window.CodeMirror.defineMIME("text/x-qsharp", "qsharp");
84
85 // Tell Jupyter to associate %%qsharp magic cells with the qsharp mode
86 window.Jupyter.CodeCell.options_default.highlight_modes["qsharp"] = {
87 reg: [/^%%qsharp/],
88 };
89
90 // Force re-highlighting of all cells the first time this code runs
91 for (const cell of window.Jupyter.notebook.get_cells()) {
92 cell.auto_highlight();
93 }
94 });
95}
96