@exercism/codemirror-lang-wast 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/LICENSE +21 -0
- package/README.md +55 -2
- package/dist/index.cjs +283 -0
- package/dist/index.d.cts +44 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +274 -0
- package/package.json +57 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
## 1.0.0
|
|
2
|
+
|
|
3
|
+
This is Exercism's first release of the package, published as
|
|
4
|
+
`@exercism/codemirror-lang-wast`. It adds completion of keywords, instructions,
|
|
5
|
+
types, snippets and the `$` names declared in the file, and marks declared names
|
|
6
|
+
and called functions in the grammar.
|
|
7
|
+
|
|
8
|
+
## 6.0.2 (2023-12-28)
|
|
9
|
+
|
|
10
|
+
### Bug fixes
|
|
11
|
+
|
|
12
|
+
Tag comments and strings as isolating for the purpose of bidirectional text.
|
|
13
|
+
|
|
14
|
+
## 6.0.1 (2022-10-24)
|
|
15
|
+
|
|
16
|
+
### Bug fixes
|
|
17
|
+
|
|
18
|
+
Make sure the language object has a name.
|
|
19
|
+
|
|
20
|
+
## 6.0.0 (2022-06-08)
|
|
21
|
+
|
|
22
|
+
### Breaking changes
|
|
23
|
+
|
|
24
|
+
Update dependencies to 6.0.0
|
|
25
|
+
|
|
26
|
+
## 0.20.0 (2022-04-20)
|
|
27
|
+
|
|
28
|
+
### Breaking changes
|
|
29
|
+
|
|
30
|
+
Update dependencies to 0.20.0
|
|
31
|
+
|
|
32
|
+
## 0.19.0 (2021-09-14)
|
|
33
|
+
|
|
34
|
+
### Breaking changes
|
|
35
|
+
|
|
36
|
+
First numbered release.
|
|
37
|
+
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2018-2021 by Marijn Haverbeke <marijn@haverbeke.berlin> and others
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,56 @@
|
|
|
1
|
-
|
|
1
|
+
<!-- NOTE: README.md is generated from src/README.md -->
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This is Exercism's fork of [@codemirror/lang-wast](https://code.haverbeke.berlin/codemirror/lang-wast). It is published as `@exercism/codemirror-lang-wast`, and our changes live on the `exercism` branch. The fork adds completion for keywords, instructions, types, snippets and the `$` names declared in the file. Its grammar marks declared names as `Definition` nodes and called functions as `Callee` nodes, so completion can find them.
|
|
4
|
+
|
|
5
|
+
To use it, install it from npm:
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install @exercism/codemirror-lang-wast
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
To build and test it from a checkout, run `npm install` (which also builds the package), then `npm test`.
|
|
12
|
+
|
|
13
|
+
# @codemirror/lang-wast [](https://www.npmjs.org/package/@codemirror/lang-wast)
|
|
14
|
+
|
|
15
|
+
[ [**WEBSITE**](https://codemirror.net/) | [**ISSUES**](https://code.haverbeke.berlin/codemirror/dev/issues) | [**FORUM**](https://discuss.codemirror.net/) | [**CHANGELOG**](https://code.haverbeke.berlin/codemirror/lang-wast/src/branch/main/CHANGELOG.md) ]
|
|
16
|
+
|
|
17
|
+
This package implements WebAssembly Text Format language support for
|
|
18
|
+
the [CodeMirror](https://codemirror.net/) code editor.
|
|
19
|
+
|
|
20
|
+
The [project page](https://codemirror.net/) has more information, a
|
|
21
|
+
number of [examples](https://codemirror.net/examples/) and the
|
|
22
|
+
[documentation](https://codemirror.net/docs/).
|
|
23
|
+
|
|
24
|
+
This code is released under an
|
|
25
|
+
[MIT license](https://code.haverbeke.berlin/codemirror/lang-wast/tree/main/LICENSE).
|
|
26
|
+
|
|
27
|
+
We aim to be an inclusive, welcoming community. To make that explicit,
|
|
28
|
+
we have a [code of
|
|
29
|
+
conduct](http://contributor-covenant.org/version/1/1/0/) that applies
|
|
30
|
+
to communication around the project.
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
import {EditorView, basicSetup} from "codemirror"
|
|
36
|
+
import {wast} from "@exercism/codemirror-lang-wast"
|
|
37
|
+
|
|
38
|
+
const view = new EditorView({
|
|
39
|
+
parent: document.body,
|
|
40
|
+
doc: `(call $log (i32.const 0) (i32.const 13))`,
|
|
41
|
+
extensions: [basicSetup, wast()]
|
|
42
|
+
})
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## API Reference
|
|
46
|
+
|
|
47
|
+
<dl>
|
|
48
|
+
<dt id="user-content-wast">
|
|
49
|
+
<code><strong><a href="#user-content-wast">wast</a></strong>() → <a href="https://codemirror.net/docs/ref#language.LanguageSupport">LanguageSupport</a></code></dt>
|
|
50
|
+
|
|
51
|
+
<dd></dd>
|
|
52
|
+
<dt id="user-content-wastlanguage">
|
|
53
|
+
<code><strong><a href="#user-content-wastlanguage">wastLanguage</a></strong>: <a href="https://codemirror.net/docs/ref#language.LRLanguage">LRLanguage</a></code></dt>
|
|
54
|
+
|
|
55
|
+
<dd></dd>
|
|
56
|
+
</dl>
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var language = require('@codemirror/language');
|
|
4
|
+
var highlight = require('@lezer/highlight');
|
|
5
|
+
var lr = require('@lezer/lr');
|
|
6
|
+
var autocomplete = require('@codemirror/autocomplete');
|
|
7
|
+
|
|
8
|
+
// This file was generated by lezer-generator. You probably shouldn't edit it.
|
|
9
|
+
const spec_Keyword = {__proto__:null,export:52, func:56, param:56, local:56, global:56, memory:56, table:56, type:56, data:56, elem:56, tag:56, block:56, loop:56, if:56, call:60, return_call:60, "ref.func":60, anyref:64, dataref:64, eqref:64, externref:64, i31ref:64, funcref:64, i8:64, i16:64, i32:64, i64:64, f32:64, f64:64, v128:64};
|
|
10
|
+
const parser = lr.LRParser.deserialize({
|
|
11
|
+
version: 14,
|
|
12
|
+
states: "%WQ]QPOOOOQO'#Cf'#CfOOQO'#Cg'#CgOwQPO'#CyOOQO'#Cj'#CjO!iQPO'#C`OOQO'#Cm'#CmOOQO'#Cu'#CuO!sQPO'#C}OOQO'#Ct'#CtOOQO'#Co'#CoQ]QPOOOOQO'#Ch'#ChOOQO,59e,59eOOQO'#Cc'#CcOOQO,58z,58zO#eQPO,58zO$WQPO,58zOOQO'#Cn'#CnOOQO,59i,59iOOQO-E6m-E6mOOQO1G.f1G.fO$_QPO'#CeOOQO'#Ci'#CiOOQO'#Cw'#CwOOQO'#Cp'#CpO$fQPO1G.fOOQO,59P,59PO$mQPO,59POOQO-E6n-E6nOOQO7+$Q7+$QOOQO1G.k1G.k",
|
|
13
|
+
stateData: "$t~OgOSPOSQOS~OTTOWVO_VO`VOlPOnQOoUOpSO~Oo[OTmXWmX_mX`mXemXlmXnmXpmXUmX~OU_Oj^O~P]OobOTqXWqX_qX`qXeqXlqXnqXpqXUqX~OUeO~P]OTfOWhO_hO`hOlPOnQOogOpSO~OUeO~P#lOUkO~P#lOUnO~P#lOUoO~P#lO",
|
|
14
|
+
goto: "$QrPPPPsPPyP|!S!`!j|!mPPs!y!|#WPPP#b#hP#nP#tPPP#hXVOTZ`RaTXhafjlWWOTZ`XhafjlaROTZ`afjlR]RWVOTZ`XhafjlRcWQZOQ`TTdZ`QjaQlfTmjlXYOTZ`XXOTZ`XiafjlWXOTZ`Xhafjl",
|
|
15
|
+
nodeNames: "⚠ LineComment BlockComment Module App ( ) Keyword Keyword App Keyword Keyword Callee Identifier Type Number String Identifier Definition",
|
|
16
|
+
maxTerm: 33,
|
|
17
|
+
nodeProps: [
|
|
18
|
+
["isolate", -3,1,2,16,""],
|
|
19
|
+
["group", -6,4,8,14,15,16,17,"Expression"],
|
|
20
|
+
["closedBy", 5,")"],
|
|
21
|
+
["openedBy", 6,"("]
|
|
22
|
+
],
|
|
23
|
+
skippedNodes: [0,1,2],
|
|
24
|
+
repeatNodeCount: 2,
|
|
25
|
+
tokenData: "0o~R^XY}YZ}]^}pq}rs!Stu#pxy'Uyz(e{|(j}!O(j!Q!R(s!R![*p!]!^.^#T#o.{~!SOg~~!VVOr!Srs!ls#O!S#O#P!q#P;'S!S;'S;=`#j<%lO!S~!qO`~~!tRO;'S!S;'S;=`!};=`O!S~#QWOr!Srs!ls#O!S#O#P!q#P;'S!S;'S;=`#j;=`<%l!S<%lO!S~#mP;=`<%l!S~#siqr%bst%btu%buv%bvw%bwx%bz{%b{|%b}!O%b!O!P%b!P!Q%b!Q![%b![!]%b!^!_%b!_!`%b!`!a%b!a!b%b!b!c%b!c!}%b#Q#R%b#R#S%b#S#T%b#T#o%b#p#q%b#r#s%b~%gio~qr%bst%btu%buv%bvw%bwx%bz{%b{|%b}!O%b!O!P%b!P!Q%b!Q![%b![!]%b!^!_%b!_!`%b!`!a%b!a!b%b!b!c%b!c!}%b#Q#R%b#R#S%b#S#T%b#T#o%b#p#q%b#r#s%b~'ZPT~!]!^'^~'aTO!]'^!]!^'p!^;'S'^;'S;=`(_<%lO'^~'sVOy'^yz(Yz!]'^!]!^'p!^;'S'^;'S;=`(_<%lO'^~(_OQ~~(bP;=`<%l'^~(jOU~~(mQ!Q!R(s!R![*p~(xU_~!O!P)[!Q![*p!g!h){#R#S+U#X#Y){#l#m+[~)aR_~!Q![)j!g!h){#X#Y){~)oS_~!Q![)j!g!h){#R#S*j#X#Y){~*OR{|*X}!O*X!Q![*_~*[P!Q![*_~*dQ_~!Q![*_#R#S*X~*mP!Q![)j~*uT_~!O!P)[!Q![*p!g!h){#R#S+U#X#Y){~+XP!Q![*p~+_R!Q![+h!c!i+h#T#Z+h~+mV_~!O!P,S!Q![+h!c!i+h!r!s-P#R#S+[#T#Z+h#d#e-P~,XT_~!Q![,h!c!i,h!r!s-P#T#Z,h#d#e-P~,mU_~!Q![,h!c!i,h!r!s-P#R#S.Q#T#Z,h#d#e-P~-ST{|-c}!O-c!Q![-o!c!i-o#T#Z-o~-fR!Q![-o!c!i-o#T#Z-o~-tS_~!Q![-o!c!i-o#R#S-c#T#Z-o~.TR!Q![,h!c!i,h#T#Z,h~.aP!]!^.d~.iSP~OY.dZ;'S.d;'S;=`.u<%lO.d~.xP;=`<%l.d~/QiW~qr.{st.{tu.{uv.{vw.{wx.{z{.{{|.{}!O.{!O!P.{!P!Q.{!Q![.{![!].{!^!_.{!_!`.{!`!a.{!a!b.{!b!c.{!c!}.{#Q#R.{#R#S.{#S#T.{#T#o.{#p#q.{#r#s.{",
|
|
26
|
+
tokenizers: [0],
|
|
27
|
+
topRules: {"Module":[0,3]},
|
|
28
|
+
specialized: [{term: 8, get: (value) => spec_Keyword[value] || -1}],
|
|
29
|
+
tokenPrec: 0
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const numericOps = {
|
|
33
|
+
int: "const clz ctz popcnt add sub mul div_s div_u rem_s rem_u and or xor shl shr_s shr_u rotl rotr " +
|
|
34
|
+
"eqz eq ne lt_s lt_u gt_s gt_u le_s le_u ge_s ge_u load load8_s load8_u load16_s load16_u " +
|
|
35
|
+
"store store8 store16 extend8_s extend16_s trunc_f32_s trunc_f32_u trunc_f64_s trunc_f64_u " +
|
|
36
|
+
"trunc_sat_f32_s trunc_sat_f32_u trunc_sat_f64_s trunc_sat_f64_u",
|
|
37
|
+
i32: "wrap_i64 reinterpret_f32",
|
|
38
|
+
i64: "load32_s load32_u store32 extend32_s extend_i32_s extend_i32_u reinterpret_f64",
|
|
39
|
+
float: "const abs neg ceil floor trunc nearest sqrt add sub mul div min max copysign " +
|
|
40
|
+
"eq ne lt gt le ge load store convert_i32_s convert_i32_u convert_i64_s convert_i64_u",
|
|
41
|
+
f32: "demote_f64 reinterpret_i32",
|
|
42
|
+
f64: "promote_f32 reinterpret_i64"
|
|
43
|
+
};
|
|
44
|
+
function prefixed(type, ops) {
|
|
45
|
+
return ops.split(" ").map(op => type + "." + op);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
The names of the WebAssembly instructions, such as `i32.add` and
|
|
49
|
+
`local.get`.
|
|
50
|
+
*/
|
|
51
|
+
const instructions = [
|
|
52
|
+
..."unreachable nop block loop if then else end br br_if br_table return call call_indirect " +
|
|
53
|
+
"return_call return_call_indirect drop select".split(" "),
|
|
54
|
+
..."local.get local.set local.tee global.get global.set".split(" "),
|
|
55
|
+
..."memory.size memory.grow memory.fill memory.copy memory.init data.drop".split(" "),
|
|
56
|
+
..."table.get table.set table.size table.grow table.fill table.copy table.init elem.drop".split(" "),
|
|
57
|
+
..."ref.null ref.is_null ref.func".split(" "),
|
|
58
|
+
...prefixed("i32", numericOps.int + " " + numericOps.i32),
|
|
59
|
+
...prefixed("i64", numericOps.int + " " + numericOps.i64),
|
|
60
|
+
...prefixed("f32", numericOps.float + " " + numericOps.f32),
|
|
61
|
+
...prefixed("f64", numericOps.float + " " + numericOps.f64)
|
|
62
|
+
];
|
|
63
|
+
/**
|
|
64
|
+
The keywords that start a module field or a part of one, such as
|
|
65
|
+
`func`, `param` and `export`.
|
|
66
|
+
*/
|
|
67
|
+
const keywords = "module func param result local global mut export import memory table data elem start type offset item declare"
|
|
68
|
+
.split(" ");
|
|
69
|
+
/**
|
|
70
|
+
The value and reference types.
|
|
71
|
+
*/
|
|
72
|
+
const types = "i32 i64 f32 f64 v128 funcref externref".split(" ");
|
|
73
|
+
// Snippets are written without their outer parentheses, which the
|
|
74
|
+
// completion source adds when the word isn't already after a `(`.
|
|
75
|
+
const snippetTemplates = [
|
|
76
|
+
{ template: "module\n\t${}\n", label: "module", detail: "definition" },
|
|
77
|
+
{ template: "func $${name} (param $${param} ${i32}) (result ${i32})\n\t${}\n", label: "func", detail: "definition" },
|
|
78
|
+
{ template: "func (export \"${name}\") (param $${param} ${i32}) (result ${i32})\n\t${}\n",
|
|
79
|
+
label: "func", detail: "exported definition" },
|
|
80
|
+
{ template: "export \"${name}\" (func $${name})", label: "export", detail: "function" },
|
|
81
|
+
{ template: "import \"${module}\" \"${name}\" (func $${name} (param ${i32}))", label: "import", detail: "function" },
|
|
82
|
+
{ template: "param $${name} ${i32}", label: "param", detail: "definition" },
|
|
83
|
+
{ template: "local $${name} ${i32}", label: "local", detail: "definition" },
|
|
84
|
+
{ template: "global $${name} (mut ${i32}) (${i32}.const ${0})", label: "global", detail: "mutable definition" },
|
|
85
|
+
{ template: "memory (export \"${mem}\") ${1}", label: "memory", detail: "exported definition" },
|
|
86
|
+
{ template: "block $${block}\n\t${}\n", label: "block", detail: "labelled" },
|
|
87
|
+
{ template: "loop $${loop}\n\t${}\n\t(br_if $${loop} ${condition})\n", label: "loop", detail: "with br_if" },
|
|
88
|
+
{ template: "if (${condition})\n\t(then\n\t\t${}\n\t)\n", label: "if", detail: "block" },
|
|
89
|
+
{ template: "if (${condition})\n\t(then\n\t\t${}\n\t)\n\t(else\n\t\t${}\n\t)\n", label: "if", detail: "/ else block" }
|
|
90
|
+
];
|
|
91
|
+
/**
|
|
92
|
+
Snippets for common WebAssembly constructs, such as modules,
|
|
93
|
+
functions, loops and blocks.
|
|
94
|
+
*/
|
|
95
|
+
const snippets = snippetTemplates.map(s => autocomplete.snippetCompletion(s.template, s));
|
|
96
|
+
const word = /^[a-z][\w.]*$/;
|
|
97
|
+
const dontComplete = ["String", "LineComment", "BlockComment", "Identifier", "Definition", "Callee"];
|
|
98
|
+
function snippetSource(context) {
|
|
99
|
+
let before = context.matchBefore(/[a-z][\w.]*$/);
|
|
100
|
+
if (!before && !context.explicit)
|
|
101
|
+
return null;
|
|
102
|
+
let from = before ? before.from : context.pos;
|
|
103
|
+
let afterParen = context.state.sliceDoc(from - 1, from) == "(";
|
|
104
|
+
let options = snippetTemplates.map(s => {
|
|
105
|
+
let template = afterParen ? s.template : "(" + s.template + ")";
|
|
106
|
+
return autocomplete.snippetCompletion(template, { label: s.label, detail: s.detail, type: "keyword" });
|
|
107
|
+
});
|
|
108
|
+
return { from, options, validFor: word };
|
|
109
|
+
}
|
|
110
|
+
const keywordCompletions = [
|
|
111
|
+
...keywords.map(label => ({ label, type: "keyword" })),
|
|
112
|
+
...instructions.filter(i => keywords.indexOf(i) < 0).map(label => ({ label, type: "function", detail: "instruction" })),
|
|
113
|
+
...types.map(label => ({ label, type: "type" }))
|
|
114
|
+
];
|
|
115
|
+
/**
|
|
116
|
+
Completes keywords, instructions, types and snippets, everywhere
|
|
117
|
+
except in strings, comments and `$` names.
|
|
118
|
+
*/
|
|
119
|
+
const keywordCompletionSource = autocomplete.ifNotIn(dontComplete, context => {
|
|
120
|
+
let list = autocomplete.completeFromList(keywordCompletions)(context);
|
|
121
|
+
let snips = snippetSource(context);
|
|
122
|
+
if (!list)
|
|
123
|
+
return snips;
|
|
124
|
+
if (!snips)
|
|
125
|
+
return list;
|
|
126
|
+
return Object.assign(Object.assign({}, list), { options: list.options.concat(snips.options) });
|
|
127
|
+
});
|
|
128
|
+
const kindOfKeyword = {
|
|
129
|
+
func: "func", param: "local", local: "local", global: "global",
|
|
130
|
+
block: "label", loop: "label", if: "label",
|
|
131
|
+
type: "type", memory: "memory", table: "table", data: "data", elem: "elem", tag: "tag"
|
|
132
|
+
};
|
|
133
|
+
const kindWanted = {
|
|
134
|
+
"local.get": "local", "local.set": "local", "local.tee": "local",
|
|
135
|
+
"global.get": "global", "global.set": "global",
|
|
136
|
+
call: "func", return_call: "func", "ref.func": "func",
|
|
137
|
+
br: "label", br_if: "label", br_table: "label",
|
|
138
|
+
type: "type"
|
|
139
|
+
};
|
|
140
|
+
const completionType = {
|
|
141
|
+
func: "function", local: "variable", global: "variable", label: "namespace", type: "type"
|
|
142
|
+
};
|
|
143
|
+
function keywordOf(state, app) {
|
|
144
|
+
var _a;
|
|
145
|
+
let first = (_a = app.firstChild) === null || _a === void 0 ? void 0 : _a.nextSibling;
|
|
146
|
+
return first && first.name == "Keyword" ? state.sliceDoc(first.from, first.to) : "";
|
|
147
|
+
}
|
|
148
|
+
function enclosing(state, node, keyword) {
|
|
149
|
+
for (let cur = node.parent; cur; cur = cur.parent)
|
|
150
|
+
if (cur.name == "App" && keywordOf(state, cur) == keyword)
|
|
151
|
+
return cur;
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
// For each scope, keyed by the start of the function or folded block
|
|
155
|
+
// that holds it, the names declared in it. Module-wide names, such as
|
|
156
|
+
// functions and globals, are under -1. Parameters, locals and flat `loop $l ... end` labels belong to their
|
|
157
|
+
// function. A folded `(block $l ...)` label belongs to the block.
|
|
158
|
+
const scopeCache = new WeakMap();
|
|
159
|
+
function declaredNames(state, tree) {
|
|
160
|
+
let cached = scopeCache.get(tree);
|
|
161
|
+
if (cached)
|
|
162
|
+
return cached;
|
|
163
|
+
let scopes = new Map();
|
|
164
|
+
tree.iterate({
|
|
165
|
+
enter(ref) {
|
|
166
|
+
if (ref.name != "Definition")
|
|
167
|
+
return;
|
|
168
|
+
let node = ref.node, kw = node.prevSibling;
|
|
169
|
+
let keyword = kw ? state.sliceDoc(kw.from, kw.to) : "";
|
|
170
|
+
let kind = kindOfKeyword[keyword];
|
|
171
|
+
if (!kind)
|
|
172
|
+
return;
|
|
173
|
+
let scope = null;
|
|
174
|
+
if (kind == "local") {
|
|
175
|
+
scope = enclosing(state, node, "func");
|
|
176
|
+
}
|
|
177
|
+
else if (kind == "label") {
|
|
178
|
+
let parent = node.parent;
|
|
179
|
+
scope = parent && parent.name == "App" && keywordOf(state, parent) == keyword ? parent
|
|
180
|
+
: enclosing(state, node, "func");
|
|
181
|
+
}
|
|
182
|
+
let at = scope ? scope.from : -1;
|
|
183
|
+
let list = scopes.get(at);
|
|
184
|
+
if (!list)
|
|
185
|
+
scopes.set(at, list = []);
|
|
186
|
+
let name = state.sliceDoc(node.from, node.to);
|
|
187
|
+
if (!list.some(d => d.name == name && d.kind == kind))
|
|
188
|
+
list.push({ name, kind, detail: keyword });
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
scopeCache.set(tree, scopes);
|
|
192
|
+
return scopes;
|
|
193
|
+
}
|
|
194
|
+
const nameBefore = /\$[0-9A-Za-z!#$%&'*+\-./:<=>?@^_`|~]*$/;
|
|
195
|
+
const keywordBeforeName = /([a-z][\w.]*)\s+$/;
|
|
196
|
+
/**
|
|
197
|
+
Completes the `$` names declared in the file (functions, parameters,
|
|
198
|
+
locals, globals, labels and so on) that are in scope at the cursor.
|
|
199
|
+
After an instruction such as `local.get` or `call`, it offers only
|
|
200
|
+
names of the kind the instruction takes.
|
|
201
|
+
*/
|
|
202
|
+
const localCompletionSource = context => {
|
|
203
|
+
let before = context.matchBefore(nameBefore);
|
|
204
|
+
if (!before)
|
|
205
|
+
return null;
|
|
206
|
+
let tree = language.syntaxTree(context.state);
|
|
207
|
+
let inner = tree.resolveInner(context.pos, -1);
|
|
208
|
+
if (["String", "LineComment", "BlockComment", "Definition"].indexOf(inner.name) > -1)
|
|
209
|
+
return null;
|
|
210
|
+
let lineStart = context.state.doc.lineAt(before.from).from;
|
|
211
|
+
let prev = keywordBeforeName.exec(context.state.sliceDoc(lineStart, before.from));
|
|
212
|
+
let keyword = prev ? prev[1] : "";
|
|
213
|
+
let inExport = !!enclosing(context.state, inner, "export");
|
|
214
|
+
// Right after a declaring keyword, such as in `(param $`, the user is
|
|
215
|
+
// writing a new name. In `(export "f" (func $` and `(type $`, the
|
|
216
|
+
// name refers to one declared elsewhere.
|
|
217
|
+
if (kindOfKeyword[keyword] && !inExport && keyword != "type")
|
|
218
|
+
return null;
|
|
219
|
+
let wanted = kindWanted[keyword] || (inExport ? kindOfKeyword[keyword] : undefined);
|
|
220
|
+
let scopes = declaredNames(context.state, tree);
|
|
221
|
+
let options = [];
|
|
222
|
+
let add = (at) => {
|
|
223
|
+
for (let d of scopes.get(at) || [])
|
|
224
|
+
if (!wanted || d.kind == wanted)
|
|
225
|
+
options.push({ label: d.name, type: completionType[d.kind] || "constant", detail: d.detail });
|
|
226
|
+
};
|
|
227
|
+
add(-1);
|
|
228
|
+
for (let cur = inner; cur; cur = cur.parent)
|
|
229
|
+
if (cur.name == "App" && scopes.has(cur.from))
|
|
230
|
+
add(cur.from);
|
|
231
|
+
return { from: before.from, options, validFor: nameBefore };
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const wastLanguage = language.LRLanguage.define({
|
|
235
|
+
name: "wast",
|
|
236
|
+
parser: parser.configure({
|
|
237
|
+
props: [
|
|
238
|
+
language.indentNodeProp.add({
|
|
239
|
+
App: language.delimitedIndent({ closing: ")", align: false })
|
|
240
|
+
}),
|
|
241
|
+
language.foldNodeProp.add({
|
|
242
|
+
App: language.foldInside,
|
|
243
|
+
BlockComment(tree) { return { from: tree.from + 2, to: tree.to - 2 }; }
|
|
244
|
+
}),
|
|
245
|
+
highlight.styleTags({
|
|
246
|
+
Keyword: highlight.tags.keyword,
|
|
247
|
+
Type: highlight.tags.typeName,
|
|
248
|
+
Number: highlight.tags.number,
|
|
249
|
+
String: highlight.tags.string,
|
|
250
|
+
Identifier: highlight.tags.variableName,
|
|
251
|
+
Definition: highlight.tags.definition(highlight.tags.variableName),
|
|
252
|
+
Callee: highlight.tags.function(highlight.tags.variableName),
|
|
253
|
+
LineComment: highlight.tags.lineComment,
|
|
254
|
+
BlockComment: highlight.tags.blockComment,
|
|
255
|
+
"( )": highlight.tags.paren
|
|
256
|
+
})
|
|
257
|
+
]
|
|
258
|
+
}),
|
|
259
|
+
languageData: {
|
|
260
|
+
commentTokens: { line: ";;", block: { open: "(;", close: ";)" } },
|
|
261
|
+
closeBrackets: { brackets: ["(", '"'] }
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
/**
|
|
265
|
+
WebAssembly text format support. Includes completion for
|
|
266
|
+
keywords, instructions, types, snippets and the `$` names declared
|
|
267
|
+
in the file.
|
|
268
|
+
*/
|
|
269
|
+
function wast() {
|
|
270
|
+
return new language.LanguageSupport(wastLanguage, [
|
|
271
|
+
wastLanguage.data.of({ autocomplete: keywordCompletionSource }),
|
|
272
|
+
wastLanguage.data.of({ autocomplete: localCompletionSource })
|
|
273
|
+
]);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
exports.instructions = instructions;
|
|
277
|
+
exports.keywordCompletionSource = keywordCompletionSource;
|
|
278
|
+
exports.keywords = keywords;
|
|
279
|
+
exports.localCompletionSource = localCompletionSource;
|
|
280
|
+
exports.snippets = snippets;
|
|
281
|
+
exports.types = types;
|
|
282
|
+
exports.wast = wast;
|
|
283
|
+
exports.wastLanguage = wastLanguage;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { LanguageSupport, LRLanguage } from '@codemirror/language';
|
|
2
|
+
import { CompletionSource, Completion } from '@codemirror/autocomplete';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
The names of the WebAssembly instructions, such as `i32.add` and
|
|
6
|
+
`local.get`.
|
|
7
|
+
*/
|
|
8
|
+
declare const instructions: readonly string[];
|
|
9
|
+
/**
|
|
10
|
+
The keywords that start a module field or a part of one, such as
|
|
11
|
+
`func`, `param` and `export`.
|
|
12
|
+
*/
|
|
13
|
+
declare const keywords: readonly string[];
|
|
14
|
+
/**
|
|
15
|
+
The value and reference types.
|
|
16
|
+
*/
|
|
17
|
+
declare const types: readonly string[];
|
|
18
|
+
/**
|
|
19
|
+
Snippets for common WebAssembly constructs, such as modules,
|
|
20
|
+
functions, loops and blocks.
|
|
21
|
+
*/
|
|
22
|
+
declare const snippets: readonly Completion[];
|
|
23
|
+
/**
|
|
24
|
+
Completes keywords, instructions, types and snippets, everywhere
|
|
25
|
+
except in strings, comments and `$` names.
|
|
26
|
+
*/
|
|
27
|
+
declare const keywordCompletionSource: CompletionSource;
|
|
28
|
+
/**
|
|
29
|
+
Completes the `$` names declared in the file (functions, parameters,
|
|
30
|
+
locals, globals, labels and so on) that are in scope at the cursor.
|
|
31
|
+
After an instruction such as `local.get` or `call`, it offers only
|
|
32
|
+
names of the kind the instruction takes.
|
|
33
|
+
*/
|
|
34
|
+
declare const localCompletionSource: CompletionSource;
|
|
35
|
+
|
|
36
|
+
declare const wastLanguage: LRLanguage;
|
|
37
|
+
/**
|
|
38
|
+
WebAssembly text format support. Includes completion for
|
|
39
|
+
keywords, instructions, types, snippets and the `$` names declared
|
|
40
|
+
in the file.
|
|
41
|
+
*/
|
|
42
|
+
declare function wast(): LanguageSupport;
|
|
43
|
+
|
|
44
|
+
export { instructions, keywordCompletionSource, keywords, localCompletionSource, snippets, types, wast, wastLanguage };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { LanguageSupport, LRLanguage } from '@codemirror/language';
|
|
2
|
+
import { CompletionSource, Completion } from '@codemirror/autocomplete';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
The names of the WebAssembly instructions, such as `i32.add` and
|
|
6
|
+
`local.get`.
|
|
7
|
+
*/
|
|
8
|
+
declare const instructions: readonly string[];
|
|
9
|
+
/**
|
|
10
|
+
The keywords that start a module field or a part of one, such as
|
|
11
|
+
`func`, `param` and `export`.
|
|
12
|
+
*/
|
|
13
|
+
declare const keywords: readonly string[];
|
|
14
|
+
/**
|
|
15
|
+
The value and reference types.
|
|
16
|
+
*/
|
|
17
|
+
declare const types: readonly string[];
|
|
18
|
+
/**
|
|
19
|
+
Snippets for common WebAssembly constructs, such as modules,
|
|
20
|
+
functions, loops and blocks.
|
|
21
|
+
*/
|
|
22
|
+
declare const snippets: readonly Completion[];
|
|
23
|
+
/**
|
|
24
|
+
Completes keywords, instructions, types and snippets, everywhere
|
|
25
|
+
except in strings, comments and `$` names.
|
|
26
|
+
*/
|
|
27
|
+
declare const keywordCompletionSource: CompletionSource;
|
|
28
|
+
/**
|
|
29
|
+
Completes the `$` names declared in the file (functions, parameters,
|
|
30
|
+
locals, globals, labels and so on) that are in scope at the cursor.
|
|
31
|
+
After an instruction such as `local.get` or `call`, it offers only
|
|
32
|
+
names of the kind the instruction takes.
|
|
33
|
+
*/
|
|
34
|
+
declare const localCompletionSource: CompletionSource;
|
|
35
|
+
|
|
36
|
+
declare const wastLanguage: LRLanguage;
|
|
37
|
+
/**
|
|
38
|
+
WebAssembly text format support. Includes completion for
|
|
39
|
+
keywords, instructions, types, snippets and the `$` names declared
|
|
40
|
+
in the file.
|
|
41
|
+
*/
|
|
42
|
+
declare function wast(): LanguageSupport;
|
|
43
|
+
|
|
44
|
+
export { instructions, keywordCompletionSource, keywords, localCompletionSource, snippets, types, wast, wastLanguage };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { syntaxTree, LRLanguage, indentNodeProp, delimitedIndent, foldNodeProp, foldInside, LanguageSupport } from '@codemirror/language';
|
|
2
|
+
import { styleTags, tags } from '@lezer/highlight';
|
|
3
|
+
import { LRParser } from '@lezer/lr';
|
|
4
|
+
import { snippetCompletion, ifNotIn, completeFromList } from '@codemirror/autocomplete';
|
|
5
|
+
|
|
6
|
+
// This file was generated by lezer-generator. You probably shouldn't edit it.
|
|
7
|
+
const spec_Keyword = {__proto__:null,export:52, func:56, param:56, local:56, global:56, memory:56, table:56, type:56, data:56, elem:56, tag:56, block:56, loop:56, if:56, call:60, return_call:60, "ref.func":60, anyref:64, dataref:64, eqref:64, externref:64, i31ref:64, funcref:64, i8:64, i16:64, i32:64, i64:64, f32:64, f64:64, v128:64};
|
|
8
|
+
const parser = /*@__PURE__*/LRParser.deserialize({
|
|
9
|
+
version: 14,
|
|
10
|
+
states: "%WQ]QPOOOOQO'#Cf'#CfOOQO'#Cg'#CgOwQPO'#CyOOQO'#Cj'#CjO!iQPO'#C`OOQO'#Cm'#CmOOQO'#Cu'#CuO!sQPO'#C}OOQO'#Ct'#CtOOQO'#Co'#CoQ]QPOOOOQO'#Ch'#ChOOQO,59e,59eOOQO'#Cc'#CcOOQO,58z,58zO#eQPO,58zO$WQPO,58zOOQO'#Cn'#CnOOQO,59i,59iOOQO-E6m-E6mOOQO1G.f1G.fO$_QPO'#CeOOQO'#Ci'#CiOOQO'#Cw'#CwOOQO'#Cp'#CpO$fQPO1G.fOOQO,59P,59PO$mQPO,59POOQO-E6n-E6nOOQO7+$Q7+$QOOQO1G.k1G.k",
|
|
11
|
+
stateData: "$t~OgOSPOSQOS~OTTOWVO_VO`VOlPOnQOoUOpSO~Oo[OTmXWmX_mX`mXemXlmXnmXpmXUmX~OU_Oj^O~P]OobOTqXWqX_qX`qXeqXlqXnqXpqXUqX~OUeO~P]OTfOWhO_hO`hOlPOnQOogOpSO~OUeO~P#lOUkO~P#lOUnO~P#lOUoO~P#lO",
|
|
12
|
+
goto: "$QrPPPPsPPyP|!S!`!j|!mPPs!y!|#WPPP#b#hP#nP#tPPP#hXVOTZ`RaTXhafjlWWOTZ`XhafjlaROTZ`afjlR]RWVOTZ`XhafjlRcWQZOQ`TTdZ`QjaQlfTmjlXYOTZ`XXOTZ`XiafjlWXOTZ`Xhafjl",
|
|
13
|
+
nodeNames: "⚠ LineComment BlockComment Module App ( ) Keyword Keyword App Keyword Keyword Callee Identifier Type Number String Identifier Definition",
|
|
14
|
+
maxTerm: 33,
|
|
15
|
+
nodeProps: [
|
|
16
|
+
["isolate", -3,1,2,16,""],
|
|
17
|
+
["group", -6,4,8,14,15,16,17,"Expression"],
|
|
18
|
+
["closedBy", 5,")"],
|
|
19
|
+
["openedBy", 6,"("]
|
|
20
|
+
],
|
|
21
|
+
skippedNodes: [0,1,2],
|
|
22
|
+
repeatNodeCount: 2,
|
|
23
|
+
tokenData: "0o~R^XY}YZ}]^}pq}rs!Stu#pxy'Uyz(e{|(j}!O(j!Q!R(s!R![*p!]!^.^#T#o.{~!SOg~~!VVOr!Srs!ls#O!S#O#P!q#P;'S!S;'S;=`#j<%lO!S~!qO`~~!tRO;'S!S;'S;=`!};=`O!S~#QWOr!Srs!ls#O!S#O#P!q#P;'S!S;'S;=`#j;=`<%l!S<%lO!S~#mP;=`<%l!S~#siqr%bst%btu%buv%bvw%bwx%bz{%b{|%b}!O%b!O!P%b!P!Q%b!Q![%b![!]%b!^!_%b!_!`%b!`!a%b!a!b%b!b!c%b!c!}%b#Q#R%b#R#S%b#S#T%b#T#o%b#p#q%b#r#s%b~%gio~qr%bst%btu%buv%bvw%bwx%bz{%b{|%b}!O%b!O!P%b!P!Q%b!Q![%b![!]%b!^!_%b!_!`%b!`!a%b!a!b%b!b!c%b!c!}%b#Q#R%b#R#S%b#S#T%b#T#o%b#p#q%b#r#s%b~'ZPT~!]!^'^~'aTO!]'^!]!^'p!^;'S'^;'S;=`(_<%lO'^~'sVOy'^yz(Yz!]'^!]!^'p!^;'S'^;'S;=`(_<%lO'^~(_OQ~~(bP;=`<%l'^~(jOU~~(mQ!Q!R(s!R![*p~(xU_~!O!P)[!Q![*p!g!h){#R#S+U#X#Y){#l#m+[~)aR_~!Q![)j!g!h){#X#Y){~)oS_~!Q![)j!g!h){#R#S*j#X#Y){~*OR{|*X}!O*X!Q![*_~*[P!Q![*_~*dQ_~!Q![*_#R#S*X~*mP!Q![)j~*uT_~!O!P)[!Q![*p!g!h){#R#S+U#X#Y){~+XP!Q![*p~+_R!Q![+h!c!i+h#T#Z+h~+mV_~!O!P,S!Q![+h!c!i+h!r!s-P#R#S+[#T#Z+h#d#e-P~,XT_~!Q![,h!c!i,h!r!s-P#T#Z,h#d#e-P~,mU_~!Q![,h!c!i,h!r!s-P#R#S.Q#T#Z,h#d#e-P~-ST{|-c}!O-c!Q![-o!c!i-o#T#Z-o~-fR!Q![-o!c!i-o#T#Z-o~-tS_~!Q![-o!c!i-o#R#S-c#T#Z-o~.TR!Q![,h!c!i,h#T#Z,h~.aP!]!^.d~.iSP~OY.dZ;'S.d;'S;=`.u<%lO.d~.xP;=`<%l.d~/QiW~qr.{st.{tu.{uv.{vw.{wx.{z{.{{|.{}!O.{!O!P.{!P!Q.{!Q![.{![!].{!^!_.{!_!`.{!`!a.{!a!b.{!b!c.{!c!}.{#Q#R.{#R#S.{#S#T.{#T#o.{#p#q.{#r#s.{",
|
|
24
|
+
tokenizers: [0],
|
|
25
|
+
topRules: {"Module":[0,3]},
|
|
26
|
+
specialized: [{term: 8, get: (value) => spec_Keyword[value] || -1}],
|
|
27
|
+
tokenPrec: 0
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const numericOps = {
|
|
31
|
+
int: "const clz ctz popcnt add sub mul div_s div_u rem_s rem_u and or xor shl shr_s shr_u rotl rotr " +
|
|
32
|
+
"eqz eq ne lt_s lt_u gt_s gt_u le_s le_u ge_s ge_u load load8_s load8_u load16_s load16_u " +
|
|
33
|
+
"store store8 store16 extend8_s extend16_s trunc_f32_s trunc_f32_u trunc_f64_s trunc_f64_u " +
|
|
34
|
+
"trunc_sat_f32_s trunc_sat_f32_u trunc_sat_f64_s trunc_sat_f64_u",
|
|
35
|
+
i32: "wrap_i64 reinterpret_f32",
|
|
36
|
+
i64: "load32_s load32_u store32 extend32_s extend_i32_s extend_i32_u reinterpret_f64",
|
|
37
|
+
float: "const abs neg ceil floor trunc nearest sqrt add sub mul div min max copysign " +
|
|
38
|
+
"eq ne lt gt le ge load store convert_i32_s convert_i32_u convert_i64_s convert_i64_u",
|
|
39
|
+
f32: "demote_f64 reinterpret_i32",
|
|
40
|
+
f64: "promote_f32 reinterpret_i64"
|
|
41
|
+
};
|
|
42
|
+
function prefixed(type, ops) {
|
|
43
|
+
return ops.split(" ").map(op => type + "." + op);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
The names of the WebAssembly instructions, such as `i32.add` and
|
|
47
|
+
`local.get`.
|
|
48
|
+
*/
|
|
49
|
+
const instructions = [
|
|
50
|
+
..."unreachable nop block loop if then else end br br_if br_table return call call_indirect " +
|
|
51
|
+
/*@__PURE__*/"return_call return_call_indirect drop select".split(" "),
|
|
52
|
+
.../*@__PURE__*/"local.get local.set local.tee global.get global.set".split(" "),
|
|
53
|
+
.../*@__PURE__*/"memory.size memory.grow memory.fill memory.copy memory.init data.drop".split(" "),
|
|
54
|
+
.../*@__PURE__*/"table.get table.set table.size table.grow table.fill table.copy table.init elem.drop".split(" "),
|
|
55
|
+
.../*@__PURE__*/"ref.null ref.is_null ref.func".split(" "),
|
|
56
|
+
.../*@__PURE__*/prefixed("i32", numericOps.int + " " + numericOps.i32),
|
|
57
|
+
.../*@__PURE__*/prefixed("i64", numericOps.int + " " + numericOps.i64),
|
|
58
|
+
.../*@__PURE__*/prefixed("f32", numericOps.float + " " + numericOps.f32),
|
|
59
|
+
.../*@__PURE__*/prefixed("f64", numericOps.float + " " + numericOps.f64)
|
|
60
|
+
];
|
|
61
|
+
/**
|
|
62
|
+
The keywords that start a module field or a part of one, such as
|
|
63
|
+
`func`, `param` and `export`.
|
|
64
|
+
*/
|
|
65
|
+
const keywords = /*@__PURE__*/"module func param result local global mut export import memory table data elem start type offset item declare"
|
|
66
|
+
.split(" ");
|
|
67
|
+
/**
|
|
68
|
+
The value and reference types.
|
|
69
|
+
*/
|
|
70
|
+
const types = /*@__PURE__*/"i32 i64 f32 f64 v128 funcref externref".split(" ");
|
|
71
|
+
// Snippets are written without their outer parentheses, which the
|
|
72
|
+
// completion source adds when the word isn't already after a `(`.
|
|
73
|
+
const snippetTemplates = [
|
|
74
|
+
{ template: "module\n\t${}\n", label: "module", detail: "definition" },
|
|
75
|
+
{ template: "func $${name} (param $${param} ${i32}) (result ${i32})\n\t${}\n", label: "func", detail: "definition" },
|
|
76
|
+
{ template: "func (export \"${name}\") (param $${param} ${i32}) (result ${i32})\n\t${}\n",
|
|
77
|
+
label: "func", detail: "exported definition" },
|
|
78
|
+
{ template: "export \"${name}\" (func $${name})", label: "export", detail: "function" },
|
|
79
|
+
{ template: "import \"${module}\" \"${name}\" (func $${name} (param ${i32}))", label: "import", detail: "function" },
|
|
80
|
+
{ template: "param $${name} ${i32}", label: "param", detail: "definition" },
|
|
81
|
+
{ template: "local $${name} ${i32}", label: "local", detail: "definition" },
|
|
82
|
+
{ template: "global $${name} (mut ${i32}) (${i32}.const ${0})", label: "global", detail: "mutable definition" },
|
|
83
|
+
{ template: "memory (export \"${mem}\") ${1}", label: "memory", detail: "exported definition" },
|
|
84
|
+
{ template: "block $${block}\n\t${}\n", label: "block", detail: "labelled" },
|
|
85
|
+
{ template: "loop $${loop}\n\t${}\n\t(br_if $${loop} ${condition})\n", label: "loop", detail: "with br_if" },
|
|
86
|
+
{ template: "if (${condition})\n\t(then\n\t\t${}\n\t)\n", label: "if", detail: "block" },
|
|
87
|
+
{ template: "if (${condition})\n\t(then\n\t\t${}\n\t)\n\t(else\n\t\t${}\n\t)\n", label: "if", detail: "/ else block" }
|
|
88
|
+
];
|
|
89
|
+
/**
|
|
90
|
+
Snippets for common WebAssembly constructs, such as modules,
|
|
91
|
+
functions, loops and blocks.
|
|
92
|
+
*/
|
|
93
|
+
const snippets = /*@__PURE__*/snippetTemplates.map(s => snippetCompletion(s.template, s));
|
|
94
|
+
const word = /^[a-z][\w.]*$/;
|
|
95
|
+
const dontComplete = ["String", "LineComment", "BlockComment", "Identifier", "Definition", "Callee"];
|
|
96
|
+
function snippetSource(context) {
|
|
97
|
+
let before = context.matchBefore(/[a-z][\w.]*$/);
|
|
98
|
+
if (!before && !context.explicit)
|
|
99
|
+
return null;
|
|
100
|
+
let from = before ? before.from : context.pos;
|
|
101
|
+
let afterParen = context.state.sliceDoc(from - 1, from) == "(";
|
|
102
|
+
let options = snippetTemplates.map(s => {
|
|
103
|
+
let template = afterParen ? s.template : "(" + s.template + ")";
|
|
104
|
+
return snippetCompletion(template, { label: s.label, detail: s.detail, type: "keyword" });
|
|
105
|
+
});
|
|
106
|
+
return { from, options, validFor: word };
|
|
107
|
+
}
|
|
108
|
+
const keywordCompletions = [
|
|
109
|
+
.../*@__PURE__*/keywords.map(label => ({ label, type: "keyword" })),
|
|
110
|
+
.../*@__PURE__*/instructions.filter(i => keywords.indexOf(i) < 0).map(label => ({ label, type: "function", detail: "instruction" })),
|
|
111
|
+
.../*@__PURE__*/types.map(label => ({ label, type: "type" }))
|
|
112
|
+
];
|
|
113
|
+
/**
|
|
114
|
+
Completes keywords, instructions, types and snippets, everywhere
|
|
115
|
+
except in strings, comments and `$` names.
|
|
116
|
+
*/
|
|
117
|
+
const keywordCompletionSource = /*@__PURE__*/ifNotIn(dontComplete, context => {
|
|
118
|
+
let list = completeFromList(keywordCompletions)(context);
|
|
119
|
+
let snips = snippetSource(context);
|
|
120
|
+
if (!list)
|
|
121
|
+
return snips;
|
|
122
|
+
if (!snips)
|
|
123
|
+
return list;
|
|
124
|
+
return Object.assign(Object.assign({}, list), { options: list.options.concat(snips.options) });
|
|
125
|
+
});
|
|
126
|
+
const kindOfKeyword = {
|
|
127
|
+
func: "func", param: "local", local: "local", global: "global",
|
|
128
|
+
block: "label", loop: "label", if: "label",
|
|
129
|
+
type: "type", memory: "memory", table: "table", data: "data", elem: "elem", tag: "tag"
|
|
130
|
+
};
|
|
131
|
+
const kindWanted = {
|
|
132
|
+
"local.get": "local", "local.set": "local", "local.tee": "local",
|
|
133
|
+
"global.get": "global", "global.set": "global",
|
|
134
|
+
call: "func", return_call: "func", "ref.func": "func",
|
|
135
|
+
br: "label", br_if: "label", br_table: "label",
|
|
136
|
+
type: "type"
|
|
137
|
+
};
|
|
138
|
+
const completionType = {
|
|
139
|
+
func: "function", local: "variable", global: "variable", label: "namespace", type: "type"
|
|
140
|
+
};
|
|
141
|
+
function keywordOf(state, app) {
|
|
142
|
+
var _a;
|
|
143
|
+
let first = (_a = app.firstChild) === null || _a === void 0 ? void 0 : _a.nextSibling;
|
|
144
|
+
return first && first.name == "Keyword" ? state.sliceDoc(first.from, first.to) : "";
|
|
145
|
+
}
|
|
146
|
+
function enclosing(state, node, keyword) {
|
|
147
|
+
for (let cur = node.parent; cur; cur = cur.parent)
|
|
148
|
+
if (cur.name == "App" && keywordOf(state, cur) == keyword)
|
|
149
|
+
return cur;
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
// For each scope, keyed by the start of the function or folded block
|
|
153
|
+
// that holds it, the names declared in it. Module-wide names, such as
|
|
154
|
+
// functions and globals, are under -1. Parameters, locals and flat `loop $l ... end` labels belong to their
|
|
155
|
+
// function. A folded `(block $l ...)` label belongs to the block.
|
|
156
|
+
const scopeCache = /*@__PURE__*/new WeakMap();
|
|
157
|
+
function declaredNames(state, tree) {
|
|
158
|
+
let cached = scopeCache.get(tree);
|
|
159
|
+
if (cached)
|
|
160
|
+
return cached;
|
|
161
|
+
let scopes = new Map();
|
|
162
|
+
tree.iterate({
|
|
163
|
+
enter(ref) {
|
|
164
|
+
if (ref.name != "Definition")
|
|
165
|
+
return;
|
|
166
|
+
let node = ref.node, kw = node.prevSibling;
|
|
167
|
+
let keyword = kw ? state.sliceDoc(kw.from, kw.to) : "";
|
|
168
|
+
let kind = kindOfKeyword[keyword];
|
|
169
|
+
if (!kind)
|
|
170
|
+
return;
|
|
171
|
+
let scope = null;
|
|
172
|
+
if (kind == "local") {
|
|
173
|
+
scope = enclosing(state, node, "func");
|
|
174
|
+
}
|
|
175
|
+
else if (kind == "label") {
|
|
176
|
+
let parent = node.parent;
|
|
177
|
+
scope = parent && parent.name == "App" && keywordOf(state, parent) == keyword ? parent
|
|
178
|
+
: enclosing(state, node, "func");
|
|
179
|
+
}
|
|
180
|
+
let at = scope ? scope.from : -1;
|
|
181
|
+
let list = scopes.get(at);
|
|
182
|
+
if (!list)
|
|
183
|
+
scopes.set(at, list = []);
|
|
184
|
+
let name = state.sliceDoc(node.from, node.to);
|
|
185
|
+
if (!list.some(d => d.name == name && d.kind == kind))
|
|
186
|
+
list.push({ name, kind, detail: keyword });
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
scopeCache.set(tree, scopes);
|
|
190
|
+
return scopes;
|
|
191
|
+
}
|
|
192
|
+
const nameBefore = /\$[0-9A-Za-z!#$%&'*+\-./:<=>?@^_`|~]*$/;
|
|
193
|
+
const keywordBeforeName = /([a-z][\w.]*)\s+$/;
|
|
194
|
+
/**
|
|
195
|
+
Completes the `$` names declared in the file (functions, parameters,
|
|
196
|
+
locals, globals, labels and so on) that are in scope at the cursor.
|
|
197
|
+
After an instruction such as `local.get` or `call`, it offers only
|
|
198
|
+
names of the kind the instruction takes.
|
|
199
|
+
*/
|
|
200
|
+
const localCompletionSource = context => {
|
|
201
|
+
let before = context.matchBefore(nameBefore);
|
|
202
|
+
if (!before)
|
|
203
|
+
return null;
|
|
204
|
+
let tree = syntaxTree(context.state);
|
|
205
|
+
let inner = tree.resolveInner(context.pos, -1);
|
|
206
|
+
if (["String", "LineComment", "BlockComment", "Definition"].indexOf(inner.name) > -1)
|
|
207
|
+
return null;
|
|
208
|
+
let lineStart = context.state.doc.lineAt(before.from).from;
|
|
209
|
+
let prev = keywordBeforeName.exec(context.state.sliceDoc(lineStart, before.from));
|
|
210
|
+
let keyword = prev ? prev[1] : "";
|
|
211
|
+
let inExport = !!enclosing(context.state, inner, "export");
|
|
212
|
+
// Right after a declaring keyword, such as in `(param $`, the user is
|
|
213
|
+
// writing a new name. In `(export "f" (func $` and `(type $`, the
|
|
214
|
+
// name refers to one declared elsewhere.
|
|
215
|
+
if (kindOfKeyword[keyword] && !inExport && keyword != "type")
|
|
216
|
+
return null;
|
|
217
|
+
let wanted = kindWanted[keyword] || (inExport ? kindOfKeyword[keyword] : undefined);
|
|
218
|
+
let scopes = declaredNames(context.state, tree);
|
|
219
|
+
let options = [];
|
|
220
|
+
let add = (at) => {
|
|
221
|
+
for (let d of scopes.get(at) || [])
|
|
222
|
+
if (!wanted || d.kind == wanted)
|
|
223
|
+
options.push({ label: d.name, type: completionType[d.kind] || "constant", detail: d.detail });
|
|
224
|
+
};
|
|
225
|
+
add(-1);
|
|
226
|
+
for (let cur = inner; cur; cur = cur.parent)
|
|
227
|
+
if (cur.name == "App" && scopes.has(cur.from))
|
|
228
|
+
add(cur.from);
|
|
229
|
+
return { from: before.from, options, validFor: nameBefore };
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const wastLanguage = /*@__PURE__*/LRLanguage.define({
|
|
233
|
+
name: "wast",
|
|
234
|
+
parser: /*@__PURE__*/parser.configure({
|
|
235
|
+
props: [
|
|
236
|
+
/*@__PURE__*/indentNodeProp.add({
|
|
237
|
+
App: /*@__PURE__*/delimitedIndent({ closing: ")", align: false })
|
|
238
|
+
}),
|
|
239
|
+
/*@__PURE__*/foldNodeProp.add({
|
|
240
|
+
App: foldInside,
|
|
241
|
+
BlockComment(tree) { return { from: tree.from + 2, to: tree.to - 2 }; }
|
|
242
|
+
}),
|
|
243
|
+
/*@__PURE__*/styleTags({
|
|
244
|
+
Keyword: tags.keyword,
|
|
245
|
+
Type: tags.typeName,
|
|
246
|
+
Number: tags.number,
|
|
247
|
+
String: tags.string,
|
|
248
|
+
Identifier: tags.variableName,
|
|
249
|
+
Definition: /*@__PURE__*/tags.definition(tags.variableName),
|
|
250
|
+
Callee: /*@__PURE__*/tags.function(tags.variableName),
|
|
251
|
+
LineComment: tags.lineComment,
|
|
252
|
+
BlockComment: tags.blockComment,
|
|
253
|
+
"( )": tags.paren
|
|
254
|
+
})
|
|
255
|
+
]
|
|
256
|
+
}),
|
|
257
|
+
languageData: {
|
|
258
|
+
commentTokens: { line: ";;", block: { open: "(;", close: ";)" } },
|
|
259
|
+
closeBrackets: { brackets: ["(", '"'] }
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
/**
|
|
263
|
+
WebAssembly text format support. Includes completion for
|
|
264
|
+
keywords, instructions, types, snippets and the `$` names declared
|
|
265
|
+
in the file.
|
|
266
|
+
*/
|
|
267
|
+
function wast() {
|
|
268
|
+
return new LanguageSupport(wastLanguage, [
|
|
269
|
+
wastLanguage.data.of({ autocomplete: keywordCompletionSource }),
|
|
270
|
+
wastLanguage.data.of({ autocomplete: localCompletionSource })
|
|
271
|
+
]);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export { instructions, keywordCompletionSource, keywords, localCompletionSource, snippets, types, wast, wastLanguage };
|
package/package.json
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exercism/codemirror-lang-wast",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "WebAssembly Text Format support for the CodeMirror code editor",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "cm-runtests",
|
|
7
|
+
"prepare": "cm-buildhelper src/wast.ts"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"editor",
|
|
11
|
+
"code",
|
|
12
|
+
"wast"
|
|
13
|
+
],
|
|
14
|
+
"author": {
|
|
15
|
+
"name": "Marijn Haverbeke",
|
|
16
|
+
"email": "marijn@haverbeke.berlin",
|
|
17
|
+
"url": "http://marijnhaverbeke.nl"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "dist/index.cjs",
|
|
21
|
+
"exports": {
|
|
22
|
+
"import": "./dist/index.js",
|
|
23
|
+
"require": "./dist/index.cjs"
|
|
24
|
+
},
|
|
25
|
+
"types": "dist/index.d.ts",
|
|
26
|
+
"module": "dist/index.js",
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@codemirror/autocomplete": "^6.0.0",
|
|
31
|
+
"@lezer/highlight": "^1.0.0",
|
|
32
|
+
"@codemirror/language": "^6.0.0",
|
|
33
|
+
"@lezer/common": "^1.2.0",
|
|
34
|
+
"@lezer/lr": "^1.0.0",
|
|
35
|
+
"@codemirror/state": "^6.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@codemirror/buildhelper": "^1.0.0",
|
|
39
|
+
"@lezer/generator": "^1.0.0",
|
|
40
|
+
"ist": "^1.1.7"
|
|
41
|
+
},
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/exercism/codemirror-lang-wast.git"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"dist",
|
|
48
|
+
"LICENSE",
|
|
49
|
+
"README.md",
|
|
50
|
+
"CHANGELOG.md"
|
|
51
|
+
],
|
|
52
|
+
"homepage": "https://github.com/exercism/codemirror-lang-wast#readme",
|
|
53
|
+
"bugs": {
|
|
54
|
+
"url": "https://github.com/exercism/codemirror-lang-wast/issues"
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
58
|
+
}
|
|
59
|
+
}
|