@exercism/codemirror-lang-cairo 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/LICENSE +22 -0
- package/README.md +102 -2
- package/dist/index.cjs +909 -0
- package/dist/index.d.cts +18 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +902 -0
- package/package.json +51 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2024 by Erik Schierboom <erik_schierboom@hotmail.com> and others
|
|
4
|
+
Copyright (C) 2018 by Marijn Haverbeke <marijn@haverbeke.berlin> and others (@lezer/rust and @codemirror/lang-rust, which this grammar and its completion are adapted from)
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
|
14
|
+
all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,103 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Cairo language support for CodeMirror
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
This package gives CodeMirror 6 a Lezer grammar for Cairo, the language of Starknet, with highlighting, indentation, folding and autocomplete.
|
|
6
|
+
Exercism's in-browser editor uses it for the Cairo track.
|
|
7
|
+
|
|
8
|
+
To keep the package working on the Exercism website, don't change the versions of these packages:
|
|
9
|
+
|
|
10
|
+
```json
|
|
11
|
+
"@codemirror/language": "^6.10.1",
|
|
12
|
+
"@lezer/highlight": "^1.2.0",
|
|
13
|
+
"@lezer/lr": "^1.4.0"
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
- NPM: `npm install @exercism/codemirror-lang-cairo`
|
|
19
|
+
- Yarn: `yarn add @exercism/codemirror-lang-cairo`
|
|
20
|
+
- Bun: `bun add @exercism/codemirror-lang-cairo`
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import { EditorView, basicSetup } from "codemirror";
|
|
26
|
+
import { indentUnit } from "@codemirror/language";
|
|
27
|
+
import { cairo } from "@exercism/codemirror-lang-cairo";
|
|
28
|
+
|
|
29
|
+
new EditorView({
|
|
30
|
+
extensions: [basicSetup, cairo(), indentUnit.of(" ")],
|
|
31
|
+
parent: document.body,
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Cairo code is indented with 4 spaces, which is what `scarb fmt` writes, so the example sets a 4-space indent unit.
|
|
36
|
+
|
|
37
|
+
## What the grammar reads
|
|
38
|
+
|
|
39
|
+
The grammar reads Cairo 2 as the current compiler accepts it.
|
|
40
|
+
That covers functions, structs, enums, traits, `impl Name of Trait`, impl aliases, modules, `use`, constants and type aliases.
|
|
41
|
+
It also reads generic parameters such as `+Drop<T>`, `-Copy<T>`, `impl Name: Trait<T>` and `+Iterator<I>[Item: T]`, the `@` snapshot and `*` desnap operators, `ref` parameters and arguments, named arguments such as `f(name: x)` and `f(:name)`, closures, `let ... else`, `if let` and `while let` with let chains, and short strings such as `'hello'` and `'a'_u8`.
|
|
42
|
+
|
|
43
|
+
Cairo's built-in macros, such as `array![]`, `println!` and `assert_eq!`, take expressions, so the grammar reads their arguments as expressions.
|
|
44
|
+
A user-defined macro that takes other tokens, such as `count!(a b c)`, will show an error.
|
|
45
|
+
The grammar reads `macro` declarations as a block of tokens.
|
|
46
|
+
|
|
47
|
+
## Completion
|
|
48
|
+
|
|
49
|
+
The completion offers:
|
|
50
|
+
|
|
51
|
+
- the keywords, and snippets for functions, methods, structs, enums, traits, impls, `#[generate_trait]` impls, `let`, `if`, `match`, the loops, `use`, `mod` and tests.
|
|
52
|
+
- the built-in macros, such as `array!`, `println!`, `assert!` and `assert_eq!`.
|
|
53
|
+
- the types, traits and functions in Cairo's prelude, such as `felt252`, `u256`, `ByteArray`, `ArrayTrait`, `Into`, `Some` and `assert`.
|
|
54
|
+
- the functions of a core library trait after its name, as in `ArrayTrait::` or `ArrayTrait::<u32>::`, and the variants after `Option::` and `Result::`.
|
|
55
|
+
- the items of a core library module after its path, as in `core::dict::` or `use core::fmt::{Display, `.
|
|
56
|
+
- common methods from the core library after a `.`, such as `len`, `span`, `append`, `into` and `unwrap`.
|
|
57
|
+
- the names declared in the file where they are in scope: functions, constants, structs, enums, traits, impls, modules, imported names, parameters, generic parameters and the variables bound by `let`, `for`, `match`, `if let` and `while let`.
|
|
58
|
+
- the fields and methods declared in the file after a `.`, the functions of an impl or trait declared in the file after its name or `Self::`, the variants of an enum after its name, and the file's items after `super::`.
|
|
59
|
+
|
|
60
|
+
It offers nothing inside strings, short strings and comments, or where a new name is being declared.
|
|
61
|
+
|
|
62
|
+
`src/corelib.ts` lists the names in the core library.
|
|
63
|
+
The file was generated from the `corelib/src` folder of https://github.com/starkware-libs/cairo. It holds names only.
|
|
64
|
+
|
|
65
|
+
## Credits
|
|
66
|
+
|
|
67
|
+
The grammar is adapted from [@lezer/rust](https://code.haverbeke.berlin/lezer/rust) by Marijn Haverbeke, which is released under the MIT licence.
|
|
68
|
+
The completion is adapted from Exercism's fork of [@codemirror/lang-rust](https://code.haverbeke.berlin/codemirror/lang-rust), also by Marijn Haverbeke and released under the MIT licence.
|
|
69
|
+
|
|
70
|
+
## Developing
|
|
71
|
+
|
|
72
|
+
Run `bun install` to install the dependencies.
|
|
73
|
+
Then run `bun run dev` to start a [Vite](https://vite.dev/) server (usually at http://localhost:5173/), which shows `index.html` with a sample program in the editor.
|
|
74
|
+
|
|
75
|
+
The source files are:
|
|
76
|
+
|
|
77
|
+
```text
|
|
78
|
+
src
|
|
79
|
+
├── index.ts (the language, highlighting, indentation and folding)
|
|
80
|
+
├── syntax.grammar (the Lezer grammar)
|
|
81
|
+
├── tokens.ts (the tokenizers for closure parameters and generic brackets)
|
|
82
|
+
├── complete.ts (the completion sources)
|
|
83
|
+
├── snippets.ts (the snippets)
|
|
84
|
+
└── corelib.ts (the core library names)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Testing
|
|
88
|
+
|
|
89
|
+
Run `bun run test`.
|
|
90
|
+
It builds the package and then runs the tests in `test/`.
|
|
91
|
+
The grammar tests in `test/cases` use the built parser, because the grammar needs the tokenizers in `src/tokens.ts`.
|
|
92
|
+
|
|
93
|
+
## Building
|
|
94
|
+
|
|
95
|
+
Run `bun run prepare` to build the package into `dist/`.
|
|
96
|
+
`bun install` runs the same command, so a fresh install also builds the package.
|
|
97
|
+
|
|
98
|
+
## Publishing
|
|
99
|
+
|
|
100
|
+
To publish a new version, set `version` in `package.json` and commit it.
|
|
101
|
+
Then push a tag made of `v` followed by that version, for example `v1.0.0`.
|
|
102
|
+
The tag starts the Publish workflow, which builds and tests the package and publishes it to npm.
|
|
103
|
+
If the tag differs from `version`, the workflow stops before it publishes.
|