-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Αdding support for Q# language #2804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f9c4fbe
adds support for Q# language
fedonman c2ae902
minor fixes
fedonman 219ac3f
updates operator regex
fedonman 59d39d1
updates keywords list
fedonman 679b405
updates strings regex
fedonman 771a652
updates operator regex
fedonman a89f8f3
updates numbers regex
fedonman 9345bcf
adds more test cases
fedonman 944ee5b
updates components/prism-qsharp.js with suggested changes
fedonman cf664ea
updates operator regex and adds test
fedonman 980cdbb
updates tests description
fedonman b1efdf1
Update components/prism-qsharp.js
fedonman 6aef3a8
updates operators regex
fedonman a0b7473
converts indentation to tabs
fedonman 23776a5
minor fixes
fedonman 7d21170
fixes missing keywordsToPattern() call
fedonman 236efb3
updates strings regex
fedonman 9ae9135
updates string interpolation
fedonman ab5796c
renames variables
fedonman 58521f4
Merge branch 'master' into master
fedonman d0d2f07
reruns tests
fedonman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| (function (Prism) { | ||
|
|
||
| /** | ||
| * Replaces all placeholders "<<n>>" of given pattern with the n-th replacement (zero based). | ||
| * | ||
| * Note: This is a simple text based replacement. Be careful when using backreferences! | ||
| * | ||
| * @param {string} pattern the given pattern. | ||
| * @param {string[]} replacements a list of replacement which can be inserted into the given pattern. | ||
| * @returns {string} the pattern with all placeholders replaced with their corresponding replacements. | ||
| * @example replace(/a<<0>>a/.source, [/b+/.source]) === /a(?:b+)a/.source | ||
| */ | ||
| function replace(pattern, replacements) { | ||
| return pattern.replace(/<<(\d+)>>/g, function (m, index) { | ||
| return '(?:' + replacements[+index] + ')'; | ||
| }); | ||
| } | ||
| /** | ||
| * @param {string} pattern | ||
| * @param {string[]} replacements | ||
| * @param {string} [flags] | ||
| * @returns {RegExp} | ||
| */ | ||
| function re(pattern, replacements, flags) { | ||
| return RegExp(replace(pattern, replacements), flags || ''); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a nested pattern where all occurrences of the string `<<self>>` are replaced with the pattern itself. | ||
| * | ||
| * @param {string} pattern | ||
| * @param {number} depthLog2 | ||
| * @returns {string} | ||
| */ | ||
| function nested(pattern, depthLog2) { | ||
| for (var i = 0; i < depthLog2; i++) { | ||
| pattern = pattern.replace(/<<self>>/g, function () { return '(?:' + pattern + ')'; }); | ||
| } | ||
| return pattern.replace(/<<self>>/g, '[^\\s\\S]'); | ||
| } | ||
|
|
||
| // https://docs.microsoft.com/en-us/azure/quantum/user-guide/language/typesystem/ | ||
| // https://github.com/microsoft/qsharp-language/tree/main/Specifications/Language/5_Grammar | ||
| var keywordKinds = { | ||
| // keywords which represent a return or variable type | ||
| type: 'Adj BigInt Bool Ctl Double false Int One Pauli PauliI PauliX PauliY PauliZ Qubit Range Result String true Unit Zero', | ||
| // all other keywords | ||
| 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' | ||
| } | ||
| // keywords | ||
| function keywordsToPattern(words) { | ||
| return '\\b(?:' + words.trim().replace(/ /g, '|') + ')\\b'; | ||
| } | ||
| var keywords = RegExp(keywordsToPattern(keywordKinds.type + ' ' + keywordKinds.other)); | ||
|
|
||
| // types | ||
| var identifier = /\b[A-Za-z_]\w*\b/.source; | ||
| var qualifiedName = replace(/<<0>>(?:\s*\.\s*<<0>>)*/.source, [identifier]); | ||
|
|
||
| var typeInside = { | ||
| 'keyword': keywords, | ||
| 'punctuation': /[<>()?,.:[\]]/ | ||
| }; | ||
|
|
||
| // strings | ||
| var regularString = /"(?:\\.|[^\\"])*"/.source; | ||
|
|
||
| Prism.languages.qsharp = Prism.languages.extend('clike', { | ||
| 'comment': /\/\/.*/, | ||
| 'string': [ | ||
| { | ||
| pattern: re(/(^|[^$\\])<<0>>/.source, [regularString]), | ||
| lookbehind: true, | ||
| greedy: true | ||
| } | ||
| ], | ||
| 'class-name': [ | ||
| { | ||
| // open Microsoft.Quantum.Canon; | ||
| // open Microsoft.Quantum.Canon as CN; | ||
| pattern: re(/(\b(?:as|open)\s+)<<0>>(?=\s*(?:;|as\b))/.source, [qualifiedName]), | ||
| lookbehind: true, | ||
| inside: typeInside | ||
| }, | ||
| { | ||
| // namespace Quantum.App1; | ||
| pattern: re(/(\bnamespace\s+)<<0>>(?=\s*{)/.source, [qualifiedName]), | ||
| lookbehind: true, | ||
| inside: typeInside | ||
| }, | ||
| ], | ||
| 'keyword': keywords, | ||
| 'number': /(?:\b0(?:x[\da-f]+|b[01]+|o[0-7]+)|(?:\B\.\d+|\b\d+(?:\.\d*)?)(?:e[-+]?\d+)?)l?\b/i, | ||
| 'operator': /\band=|\bor=|\band\b|\bor\b|\bnot\b|<[-=]|[-=]>|>>>=?|<<<=?|\^\^\^=?|\|\|\|=?|&&&=?|w\/=?|~~~|[*\/+\-^=!%]=?/, | ||
| 'punctuation': /::|[{}[\];(),.:]/ | ||
| }); | ||
|
|
||
| Prism.languages.insertBefore('qsharp', 'number', { | ||
| 'range': { | ||
| pattern: /\.\./, | ||
| alias: 'operator' | ||
| } | ||
| }); | ||
|
|
||
| // single line | ||
| var interpolationExpr = nested(replace(/\{(?:[^"{}]|<<0>>|<<self>>)*\}/.source, [regularString]), 2); | ||
|
|
||
| Prism.languages.insertBefore('qsharp', 'string', { | ||
| 'interpolation-string': { | ||
| pattern: re(/\$"(?:\\.|<<0>>|[^\\"{])*"/.source, [interpolationExpr]), | ||
| greedy: true, | ||
| inside: { | ||
| 'interpolation': { | ||
| pattern: re(/((?:^|[^\\])(?:\\\\)*)<<0>>/.source, [interpolationExpr]), | ||
| lookbehind: true, | ||
| inside: { | ||
| 'punctuation': /^\{|\}$/, | ||
| 'expression': { | ||
| pattern: /[\s\S]+/, | ||
| alias: 'language-qsharp', | ||
| inside: Prism.languages.qsharp | ||
| } | ||
| } | ||
| }, | ||
| 'string': /[\s\S]+/ | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| }(Prism)); | ||
|
|
||
| Prism.languages.qs = Prism.languages.qsharp; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <h2>Full example</h2> | ||
| <pre><code>namespace Bell { | ||
| open Microsoft.Quantum.Canon; | ||
| open Microsoft.Quantum.Intrinsic; | ||
|
|
||
| operation SetQubitState(desired : Result, target : Qubit) : Unit { | ||
| if desired != M(target) { | ||
| X(target); | ||
| } | ||
| } | ||
|
|
||
| @EntryPoint() | ||
| operation TestBellState(count : Int, initial : Result) : (Int, Int) { | ||
|
|
||
| mutable numOnes = 0; | ||
| use qubit = Qubit(); | ||
| for test in 1..count { | ||
| SetQubitState(initial, qubit); | ||
| let res = M(qubit); | ||
|
|
||
| // Count the number of ones we saw: | ||
| if res == One { | ||
| set numOnes += 1; | ||
| } | ||
| } | ||
|
|
||
| SetQubitState(Zero, qubit); | ||
|
|
||
| // Return number of times we saw a |0> and number of times we saw a |1> | ||
| Message("Test results (# of 0s, # of 1s): "); | ||
| return (count - numOnes, numOnes); | ||
| } | ||
| }</code></pre> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.