Skip to content

Commit 7e89b55

Browse files
authored
Merge pull request #4 from qsharp-community/modernize
added sample extension and added Iterable
2 parents f98e48e + dec08cc commit 7e89b55

10 files changed

Lines changed: 183 additions & 3 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,6 @@ out/
106106

107107
# macOS
108108
.DS_Store
109+
110+
# generated copy for the grammar test extension (see build.js)
111+
test-extension/qsharp.tmLanguage.json

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Test grammar (Extension Development Host)",
6+
"type": "extensionHost",
7+
"request": "launch",
8+
"args": [
9+
"--disable-extension=quantum.qsharp-lang-vscode",
10+
"--extensionDevelopmentPath=${workspaceFolder}/test-extension",
11+
"${workspaceFolder}/test-extension/sample.qs"
12+
]
13+
}
14+
]
15+
}

build.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ function build() {
3131
);
3232

3333
console.log("Built grammars/qsharp.tmLanguage and grammars/qsharp.tmLanguage.json");
34+
35+
// Keep a copy inside the test extension (gitignored) so its manifest can
36+
// reference a grammar within its own folder — VS Code warns otherwise.
37+
const testExtDir = path.join(__dirname, "test-extension");
38+
if (fs.existsSync(testExtDir)) {
39+
fs.writeFileSync(
40+
path.join(testExtDir, "qsharp.tmLanguage.json"),
41+
JSON.stringify(grammar, null, "\t") + "\n"
42+
);
43+
console.log("Refreshed test-extension/qsharp.tmLanguage.json");
44+
}
3445
}
3546

3647
build();

grammars/qsharp.tmLanguage

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@
718718
<key>name</key>
719719
<string>support.type.constraint.qsharp</string>
720720
<key>match</key>
721-
<string>\b(Eq|Add|Sub|Mul|Div|Mod|Signed|Ord|Show|Integral|Exp)\b</string>
721+
<string>\b(Eq|Add|Sub|Mul|Div|Mod|Signed|Ord|Show|Integral|Iterable|Exp)\b</string>
722722
</dict>
723723
<dict>
724724
<key>name</key>

grammars/qsharp.tmLanguage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@
477477
},
478478
{
479479
"name": "support.type.constraint.qsharp",
480-
"match": "\\b(Eq|Add|Sub|Mul|Div|Mod|Signed|Ord|Show|Integral|Exp)\\b"
480+
"match": "\\b(Eq|Add|Sub|Mul|Div|Mod|Signed|Ord|Show|Integral|Iterable|Exp)\\b"
481481
},
482482
{
483483
"name": "punctuation.squarebracket.open.qsharp",

src/qsharp.tmLanguage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ repository:
259259
- name: keyword.operator.qsharp
260260
match: \+
261261
- name: support.type.constraint.qsharp
262-
match: \b(Eq|Add|Sub|Mul|Div|Mod|Signed|Ord|Show|Integral|Exp)\b
262+
match: \b(Eq|Add|Sub|Mul|Div|Mod|Signed|Ord|Show|Integral|Iterable|Exp)\b
263263
- name: punctuation.squarebracket.open.qsharp
264264
match: \[
265265
- name: punctuation.squarebracket.close.qsharp

test-extension/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Grammar test extension
2+
3+
A throwaway VS Code extension used to manually test the Q# TextMate grammar in a
4+
real editor, isolated from the QDK extension (no language server, so no semantic
5+
highlighting paints over the grammar).
6+
7+
It contributes the `qsharp` language for `.qs` files. Its grammar
8+
(`qsharp.tmLanguage.json`) is generated into this folder by `npm run build`
9+
(gitignored) so the manifest can reference a file inside its own folder — VS Code
10+
warns about grammar paths that escape the extension directory.
11+
12+
## Usage
13+
14+
1. Build the grammar so `grammars/qsharp.tmLanguage.json` is current:
15+
16+
```bash
17+
npm run build
18+
```
19+
20+
2. Open the **repository root** in VS Code and press **F5** (the
21+
`Test grammar (Extension Development Host)` launch config). This opens an
22+
Extension Development Host with the official QDK extension disabled and
23+
[`sample.qs`](./sample.qs) loaded.
24+
25+
3. Verify tokenization with **`Developer: Inspect Editor Tokens and Scopes`**
26+
(Command Palette) — hover a token to see its `source.qsharp` scopes. This is
27+
the authoritative check; colors are just the active theme's scope mapping.
28+
29+
4. After editing `src/qsharp.tmLanguage.yml`, re-run `npm run build` and reload
30+
the host window (`Cmd/Ctrl+R`) to pick up the new grammar.
31+
32+
## Notes
33+
34+
- The launch config disables `quantum.qsharp-lang-vscode` only inside the
35+
development host — your normal editor is untouched.
36+
- If you prefer not to use F5, symlink this folder into your extensions dir
37+
(`ln -s "$PWD/test-extension" ~/.vscode/extensions/qsharp-tmlanguage-test`),
38+
disable the QDK extension, and reload.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"comments": {
3+
"lineComment": "//"
4+
},
5+
"brackets": [
6+
["[", "]"],
7+
["(", ")"],
8+
["{", "}"]
9+
],
10+
"autoClosingPairs": [
11+
["[", "]"],
12+
["(", ")"],
13+
["{", "}"],
14+
{ "open": "\"", "close": "\"", "notIn": ["string"] }
15+
],
16+
"surroundingPairs": [
17+
["[", "]"],
18+
["(", ")"],
19+
["{", "}"],
20+
["\"", "\""]
21+
]
22+
}

test-extension/package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "qsharp-tmlanguage-test-extension",
3+
"displayName": "Q# TextMate grammar (dev)",
4+
"description": "Loads the locally-built Q# grammar for manual testing in an Extension Development Host.",
5+
"publisher": "local",
6+
"version": "0.0.0",
7+
"private": true,
8+
"engines": {
9+
"vscode": "^1.75.0"
10+
},
11+
"contributes": {
12+
"languages": [
13+
{
14+
"id": "qsharp",
15+
"aliases": ["Q#", "qsharp"],
16+
"extensions": [".qs"],
17+
"configuration": "./language-configuration.json"
18+
}
19+
],
20+
"grammars": [
21+
{
22+
"language": "qsharp",
23+
"scopeName": "source.qsharp",
24+
"path": "./qsharp.tmLanguage.json"
25+
}
26+
]
27+
}
28+
}

test-extension/sample.qs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import Std.Diagnostics.*;
2+
import Std.Math.PI as Pi;
3+
4+
/// A doc comment describing a struct.
5+
struct Complex { Re : Double, Im : Double }
6+
7+
@EntryPoint()
8+
operation Main() : Result[] {
9+
// literals: decimal, hex, binary, octal, BigInt, float, separators
10+
let ints = [42, 0xFF, 0b1010, 0o52, 1_000];
11+
let big = 42L;
12+
let approx = 1.0e-3;
13+
14+
// qubit allocation (single + tuple), gates, measurement, reset
15+
use qs = Qubit[3];
16+
use (control, target) = (Qubit(), Qubit());
17+
for i in 0..Length(qs) - 1 {
18+
H(qs[i]);
19+
}
20+
Controlled X([control], target);
21+
Rx(Pi() / 4.0, target);
22+
23+
// structs: construction, copy-and-update, field access
24+
let c = new Complex { Re = 1.0, Im = 0.0 };
25+
let c2 = new Complex { ...c, Im = 2.0 };
26+
let magnitudeSquared = c2.Re * c2.Re + c2.Im * c2.Im;
27+
28+
// operators: comparison, bitwise, ternary, ranges
29+
let flags = (5 &&& 3) ||| (1 <<< 2);
30+
let sign = magnitudeSquared > 0.0 ? One | Zero;
31+
let slice = ints[1..2];
32+
33+
// lambdas, partial application, string interpolation
34+
let addOne = x -> x + 1;
35+
let applyX = q => X(q);
36+
Message($"c2 = {c2.Re} + {c2.Im}i, sign = {sign}");
37+
38+
// control flow: if / elif / else, within-apply, mutation without `set`
39+
mutable counter = 0;
40+
counter += 1;
41+
if counter == 1 {
42+
Message("one");
43+
} elif counter == 2 {
44+
Message("two");
45+
} else {
46+
Message("many");
47+
}
48+
49+
within {
50+
H(qs[0]);
51+
} apply {
52+
Z(qs[0]);
53+
}
54+
55+
ResetAll(qs);
56+
return MeasureEachZ(qs);
57+
}
58+
59+
function Sum2<'T : Add>(a : 'T, b : 'T) : 'T {
60+
a + b
61+
}
62+
63+
export Main, Sum2, Complex;

0 commit comments

Comments
 (0)