Skip to content

Commit 005c430

Browse files
committed
Add CodeMirror langauge definition
1 parent 99a7eac commit 005c430

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,17 @@ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
384384
defer cancel()
385385
values, err := eval.Evaluate(source, input, eval.WithContext(ctx))
386386
```
387+
388+
## Editor Support
389+
390+
A CodeMirror 6 language definition for Scanspec syntax highlighting is available at [`contrib/codemirror/scanspec.js`](contrib/codemirror/scanspec.js).
391+
392+
``` javascript
393+
import { scanspec } from "./scanspec.js"
394+
import { EditorView, basicSetup } from "codemirror"
395+
396+
new EditorView({
397+
extensions: [basicSetup, scanspec],
398+
parent: document.getElementById("editor"),
399+
})
400+
```

contrib/codemirror/scanspec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { StreamLanguage } from "@codemirror/language"
2+
3+
export const scanspec = StreamLanguage.define({
4+
token(stream, state) {
5+
// Comments
6+
if (stream.match("#")) {
7+
stream.skipToEnd()
8+
return "comment"
9+
}
10+
11+
// Strings
12+
if (stream.match('"')) {
13+
while (!stream.eol()) {
14+
if (stream.next() === "\\") stream.next()
15+
else if (stream.current().endsWith('"') && stream.current().length > 1) break
16+
}
17+
return "string"
18+
}
19+
20+
// Numbers (float before int)
21+
if (stream.match(/\d+\.\d*/)) return "number"
22+
if (stream.match(/\d+/)) return "number"
23+
24+
// Operators
25+
if (stream.match(/\*\*|==|!=|<=|>=|&&|\|\|/)) return "operator"
26+
if (stream.match(/[+\-*/<>=!]/)) return "operator"
27+
28+
// Keywords, types, and identifiers
29+
if (stream.match(/[a-zA-Z_][a-zA-Z0-9_]*/)) {
30+
const word = stream.current()
31+
if (/^(check|else|end|eof|eol|for|if|scan|scanln|var)$/.test(word))
32+
return "keyword"
33+
if (/^(bool|int|int64|float32|float64|string)$/.test(word))
34+
return "typeName"
35+
return "variableName"
36+
}
37+
38+
// Skip whitespace and punctuation
39+
stream.next()
40+
return null
41+
},
42+
})

0 commit comments

Comments
 (0)