@mylasertools/kerfscript 0.4.0 → 0.6.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.
Files changed (68) hide show
  1. package/CHANGELOG.md +125 -1
  2. package/LANGUAGE.md +26 -11
  3. package/README.md +79 -5
  4. package/dist/ast.d.cts +13 -1
  5. package/dist/ast.d.ts +13 -1
  6. package/dist/cli.js +7509 -1629
  7. package/dist/diagnostics.d.cts +6 -0
  8. package/dist/diagnostics.d.ts +6 -0
  9. package/dist/evaluator.d.cts +12 -0
  10. package/dist/evaluator.d.ts +12 -0
  11. package/dist/format.d.cts +4 -0
  12. package/dist/format.d.ts +4 -0
  13. package/dist/highlight.cjs +151 -0
  14. package/dist/highlight.d.cts +13 -0
  15. package/dist/highlight.d.ts +13 -0
  16. package/dist/highlight.js +131 -0
  17. package/dist/import/dbg.d.cts +1 -0
  18. package/dist/import/dbg.d.ts +1 -0
  19. package/dist/import/index.d.cts +16 -0
  20. package/dist/import/index.d.ts +16 -0
  21. package/dist/import/shapes.d.cts +76 -0
  22. package/dist/import/shapes.d.ts +76 -0
  23. package/dist/import/svg.d.cts +48 -0
  24. package/dist/import/svg.d.ts +48 -0
  25. package/dist/import/xml.d.cts +12 -0
  26. package/dist/import/xml.d.ts +12 -0
  27. package/dist/index.cjs +391 -13
  28. package/dist/index.d.cts +7 -3
  29. package/dist/index.d.ts +7 -3
  30. package/dist/index.js +391 -13
  31. package/dist/language.cjs +417 -0
  32. package/dist/language.d.cts +143 -0
  33. package/dist/language.d.ts +143 -0
  34. package/dist/language.js +417 -0
  35. package/dist/lexer.d.cts +17 -0
  36. package/dist/lexer.d.ts +17 -0
  37. package/dist/libraries.cjs +380 -0
  38. package/dist/libraries.d.cts +112 -0
  39. package/dist/libraries.d.ts +112 -0
  40. package/dist/libraries.js +380 -0
  41. package/dist/lsp.d.cts +12 -0
  42. package/dist/lsp.d.ts +12 -0
  43. package/dist/paths.d.cts +1 -1
  44. package/dist/paths.d.ts +1 -1
  45. package/dist/signatures.cjs +675 -0
  46. package/dist/signatures.d.cts +20 -0
  47. package/dist/signatures.d.ts +20 -0
  48. package/dist/signatures.js +655 -0
  49. package/docs/cli.md +54 -2
  50. package/docs/desktop.md +5 -0
  51. package/docs/editors.md +121 -0
  52. package/docs/import.md +149 -0
  53. package/docs/javascript.md +177 -10
  54. package/docs/kerf.md +10 -0
  55. package/docs/library/box.svg +11 -0
  56. package/docs/library/calibration.svg +11 -0
  57. package/docs/library/dividers.svg +11 -0
  58. package/docs/library/enclosure.svg +11 -0
  59. package/docs/library/hanging.svg +11 -0
  60. package/docs/library/hinge.svg +11 -0
  61. package/docs/library/tabs.svg +11 -0
  62. package/docs/library.md +339 -0
  63. package/docs/mcp.md +88 -0
  64. package/docs/openscad.md +1 -1
  65. package/docs/shared-libraries.md +229 -0
  66. package/docs/specification.md +529 -0
  67. package/docs/versions.md +2 -2
  68. package/package.json +52 -6
package/docs/cli.md CHANGED
@@ -47,12 +47,53 @@ to try these commands.
47
47
  kerf render <file.kerf> [-o out.svg] [options]
48
48
  kerf params <file.kerf>
49
49
  kerf check <file.kerf>
50
+ kerf fmt <files or folders>
51
+ kerf fmt --check <files or folders>
52
+ kerf fmt -
53
+ kerf lsp [--timeout <seconds>]
54
+ kerf import <file.svg> [-o out.kerf] [--dpi <n>] [--tolerance <mm>] [--kerf <mm>] [--pack]
55
+ kerf add <source>
56
+ kerf remove <name>
57
+ kerf install [--frozen | --check]
58
+ kerf pack [folder] [-o file]
50
59
  ```
51
60
 
52
61
  `render` exports SVG, DXF or JSON, using the output file's extension to
53
62
  choose the format. `params` lists the editable settings as JSON. `check`
54
63
  reports parts, dimensions, cut lengths, sheets and warnings.
55
64
 
65
+ `fmt` formats `.kerf` files in place, searching folders for them, except
66
+ `kerf_libraries/`, whose files `kerf.lock` pins as they are. It sets
67
+ indentation (two spaces), the spaces between tokens, blank lines (at most one
68
+ in a row) and line endings, and keeps your line breaks and every comment
69
+ where it is, customizer comments included. It never joins or splits lines.
70
+ `--check` changes nothing: it lists the files that are not formatted and
71
+ exits with 1 when there are any, for CI. `kerf fmt -` formats standard input
72
+ to standard output, for editors. A file that does not parse is reported and
73
+ left alone.
74
+
75
+ `import` turns an SVG drawing into a `.kerf` program next to it, or to the
76
+ `-o` file; `-o -` prints it. It does not replace a program already next to
77
+ the drawing unless that file is named with `-o`. It writes the circles, rectangles, tabs, slots,
78
+ finger joints, hanger holes and keyholes it finds as the modules that make
79
+ them, and checks each part against the drawing: see
80
+ [Importing SVG drawings](import.md) for what it finds and for its options.
81
+
82
+ `lsp` runs the language server on standard input and output, for editors:
83
+ see [Editors](editors.md). It stops checking a design after `--timeout`
84
+ seconds, 10 by default.
85
+
86
+ `add`, `remove`, `install` and `pack` manage [shared libraries](shared-libraries.md)
87
+ in the project around the current folder, the nearest one with a `kerf.json`.
88
+ `add` takes `github:owner/repo@tag`, an https link to a `.tar.gz` or a
89
+ folder, records it in `kerf.json` and installs it into `kerf_libraries/`.
90
+ `install` installs what `kerf.json` names, from the exact commits and
91
+ files `kerf.lock` pins; `--frozen` fails instead of changing `kerf.lock`,
92
+ for CI, and `--check` downloads nothing and exits with 1 when
93
+ `kerf_libraries/` differs from `kerf.lock`. `pack` checks a library and
94
+ writes `name-version.tar.gz` to publish. `render` and `check` find the
95
+ installed libraries by themselves.
96
+
56
97
  ## Options
57
98
 
58
99
  | Option | Use |
@@ -81,14 +122,25 @@ kerf render name-sign.kerf -D 'name="Ada"' --font Inter-Bold.ttf -o ada.svg
81
122
  kerf render box.kerf --sheet 400x300 --kerf 0.12 --split -o box.svg
82
123
  kerf render box.kerf -o box.dxf
83
124
  kerf check box.kerf
125
+ kerf import ornament.svg -o ornament.kerf
84
126
  ```
85
127
 
86
128
  For multiple sheets, `--split` creates numbered files such as `box-1.svg`
87
129
  and `box-2.svg`. Use it for DXF exports of a design with several sheets.
88
130
 
89
131
  Warnings are written to standard error and allow rendering to continue.
90
- An error stops the command, reports its location when available and returns
91
- a non-zero exit code.
132
+ An error stops the command with a non-zero exit code. It shows the line with
133
+ the problem underlined, the calls it happened inside and, for a built-in
134
+ given a bad argument, what that built-in takes:
135
+
136
+ ```text
137
+ error: Unknown variable "widht". Did you mean "width"?
138
+ --> panel.kerf:3:9
139
+ |
140
+ 3 | rect([widht, 20]);
141
+ | ^^^^^
142
+ = in module panel(), called at panel.kerf:5:1
143
+ ```
92
144
 
93
145
  ## Fonts
94
146
 
package/docs/desktop.md CHANGED
@@ -79,6 +79,11 @@ other files on your computer. Standard library paths such as `use <kerf/box>` wo
79
79
  everywhere. A design that has not been saved yet can only use the standard
80
80
  library.
81
81
 
82
+ Libraries installed with `kerf install` work too: `use <feet/feet>` reads
83
+ `kerf_libraries/feet/feet.kerf` in the design's project, the nearest folder
84
+ at or above the design's with a `kerf.json`. See
85
+ [Shared libraries](shared-libraries.md).
86
+
82
87
  ## Fonts
83
88
 
84
89
  `text()` uses Atkinson Hyperlegible, as in the playground. The app does not
@@ -0,0 +1,121 @@
1
+ # Editors
2
+
3
+ KerfScript has a language server, `kerf lsp`, that gives editors what they
4
+ need to work on `.kerf` files:
5
+
6
+ - problems while you type: the design runs as you edit, and its errors and
7
+ warnings are marked where they are, with the module calls an error
8
+ happened inside
9
+ - hover: how to call a built-in, with its parameters and a link to the
10
+ reference; how your own modules, functions and variables are defined, with
11
+ the comment above them; the range of a setting
12
+ - completion of modules where a statement starts, functions and variables
13
+ in expressions, named arguments inside a call, and library paths after
14
+ `use <`, including those of [shared libraries](shared-libraries.md) in
15
+ `kerf_libraries/`
16
+ - parameter hints for the argument being typed
17
+ - go to definition, find references, highlighting of a name's uses and
18
+ rename, following the [scope rules](../LANGUAGE.md#scope): a parameter's
19
+ references include the named arguments that set it
20
+ - an outline of modules, functions and variables, and links on
21
+ `use <...>` paths
22
+ - in VS Code, checks and hover help for the fields of `kerf.json`
23
+ - formatting with [`kerf fmt`](cli.md#commands)
24
+
25
+ Libraries next to a design, as in `use <tabs>`, are read from its folder,
26
+ including unsaved changes in open editors. A design is checked in a worker
27
+ thread and stopped after 10 seconds (`kerf lsp --timeout <seconds>` changes
28
+ that), so a slow design never holds up typing.
29
+
30
+ ## VS Code
31
+
32
+ Install the KerfScript extension. It comes with the language server, so
33
+ nothing else is needed, and formats on save; turn that off with
34
+ `"[kerfscript]": { "editor.formatOnSave": false }` in your settings.
35
+
36
+ The extension is in
37
+ [`editors/vscode`](https://github.com/mylasertools/kerfscript/tree/main/editors/vscode).
38
+ To build it yourself:
39
+
40
+ ```sh
41
+ cd editors/vscode
42
+ npm ci
43
+ npx vsce package
44
+ code --install-extension kerfscript-0.1.0.vsix
45
+ ```
46
+
47
+ ## Other editors
48
+
49
+ Any editor with a Language Server Protocol client can run the server.
50
+ Install the [command line](cli.md) so `kerf` is on the path, and start it
51
+ as `kerf lsp`, which talks over standard input and output.
52
+
53
+ The settings below are where to start. They follow each editor's
54
+ documentation but have not been tried in those editors; the server itself
55
+ is tested through the protocol, in VS Code and as the standalone `kerf`
56
+ binary.
57
+
58
+ ### Neovim (0.11 or later)
59
+
60
+ ```lua
61
+ vim.filetype.add({ extension = { kerf = 'kerfscript' } })
62
+ vim.lsp.config('kerf', {
63
+ cmd = { 'kerf', 'lsp' },
64
+ filetypes = { 'kerfscript' },
65
+ root_markers = { '.git' },
66
+ })
67
+ vim.lsp.enable('kerf')
68
+ ```
69
+
70
+ ### Helix
71
+
72
+ In `languages.toml`:
73
+
74
+ ```toml
75
+ [language-server.kerf]
76
+ command = "kerf"
77
+ args = ["lsp"]
78
+
79
+ [[language]]
80
+ name = "kerfscript"
81
+ scope = "source.kerfscript"
82
+ file-types = ["kerf"]
83
+ comment-token = "//"
84
+ block-comment-tokens = { start = "/*", end = "*/" }
85
+ indent = { tab-width = 2, unit = " " }
86
+ language-servers = ["kerf"]
87
+ ```
88
+
89
+ ### Emacs (Eglot)
90
+
91
+ ```elisp
92
+ (define-derived-mode kerfscript-mode prog-mode "KerfScript"
93
+ (setq-local comment-start "// "))
94
+ (add-to-list 'auto-mode-alist '("\\.kerf\\'" . kerfscript-mode))
95
+ (with-eval-after-load 'eglot
96
+ (add-to-list 'eglot-server-programs '(kerfscript-mode "kerf" "lsp")))
97
+ ```
98
+
99
+ ### Sublime Text
100
+
101
+ With the [LSP](https://packagecontrol.io/packages/LSP) package, and the
102
+ [TextMate grammar](https://github.com/mylasertools/kerfscript/blob/main/editors/vscode/syntaxes/kerfscript.tmLanguage.json)
103
+ installed as a syntax, in `LSP.sublime-settings`:
104
+
105
+ ```json
106
+ {
107
+ "clients": {
108
+ "kerf": {
109
+ "enabled": true,
110
+ "command": ["kerf", "lsp"],
111
+ "selector": "source.kerfscript"
112
+ }
113
+ }
114
+ }
115
+ ```
116
+
117
+ ## In your own tools
118
+
119
+ The language server's answers come from
120
+ `@mylasertools/kerfscript/language`, which works in browsers too: see
121
+ [Language service](javascript.md#language-service).
package/docs/import.md ADDED
@@ -0,0 +1,149 @@
1
+ # Importing SVG drawings
2
+
3
+ `kerf import` turns an SVG drawing into a KerfScript program that cuts the
4
+ same shapes. Use it to make an existing design parametric, or to continue
5
+ a design drawn in Inkscape, Illustrator or a box generator.
6
+
7
+ ```sh
8
+ kerf import ornament.svg # writes ornament.kerf, if there is none
9
+ kerf import ornament.svg -o tree.kerf
10
+ kerf import ornament.svg -o - # prints the program
11
+ ```
12
+
13
+ In the [playground](https://kerfscript.mylasertools.com/playground/),
14
+ choose **Import SVG** or drop an SVG file on the editor: the program opens
15
+ in the editor, and the console says what was found.
16
+
17
+ The importer looks for the shapes KerfScript has modules for and writes
18
+ those modules instead of a list of points:
19
+
20
+ | In the drawing | In the program |
21
+ | -------------------------------------------------- | ----------------------------------------------- |
22
+ | Circles, ellipses, rectangles, rounded rectangles | `circle()`, `ellipse()`, `rect()` |
23
+ | Regular polygons and stars | `regular_polygon()`, `star()` |
24
+ | A small round hole high up in the middle of a part | `hanging_hole()`, or two of them with `spacing` |
25
+ | A round hole with a slot, for hanging on a screw | `keyhole()`, or `keyholes()` for a level pair |
26
+ | A ring around a hole on the edge of an ornament | `hanging_loop()` |
27
+ | A rectangle with tabs | `tab_rect()`, or `tabs()` on each side |
28
+ | A row of slots the tabs go through | `slots()` |
29
+ | A rectangle with finger joints | `finger_rect()` |
30
+ | Holes a divider's fingers go through | `finger_holes()` |
31
+ | Fingers and notches from another program | `rect()` with the notches cut out, in loops |
32
+ | The same hole repeated in a row or a grid | a `for` loop |
33
+ | Anything else | `polygon()` or `path()`, exactly as drawn |
34
+
35
+ For example, a sign with a hanger hole, a keyhole and a row of holes comes
36
+ back as:
37
+
38
+ ```kerf
39
+ // sign.svg: imported from an SVG drawing.
40
+ // 1 part; found 1 rectangle, 1 hanger hole, 7 circles, 1 hole pattern, 1 keyhole.
41
+
42
+ use <kerf/hanging>
43
+
44
+ $layout = "none"; // keep the parts where the drawing has them
45
+
46
+ translate([3, 3]) part("sign") {
47
+ difference() {
48
+ rect([120, 80], r = 5);
49
+ hanging_hole([60, 70]);
50
+ for (x = [30:10:90]) translate([x, 30]) circle(d = 3);
51
+ translate([20, 60]) keyhole();
52
+ }
53
+ }
54
+ ```
55
+
56
+ Change a number and render it to get a new version of the design.
57
+
58
+ ## Checked against the drawing
59
+
60
+ Every part is run once it is written and compared with the drawing. A
61
+ shape is only written as a module when its outline is within 0.05 mm of
62
+ the drawn one; set this with `--tolerance`. When a part's modules do not
63
+ match the drawing closely enough, the part is written as it is drawn, and
64
+ a comment at the top of the program says so. A part that no shape can
65
+ reproduce exactly is cut as the lines it is drawn with. Nothing in the
66
+ drawing is left out because it was not recognised.
67
+
68
+ ## Parts, holes and layers
69
+
70
+ Each closed cut outline is a part, and the outlines inside it are its
71
+ holes. An outline inside a hole is a part again: a piece that falls out.
72
+ Parts are named after their group or element, as `id`, Inkscape label or
73
+ Illustrator layer name, and otherwise numbered.
74
+
75
+ Colours become [layers](layers.md), as most laser software reads them:
76
+
77
+ - Red lines are cut and blue lines are scored. Without red, the colour
78
+ used most is cut.
79
+ - Filled shapes without a line are engraved. A drawing with no lines at
80
+ all is a silhouette, and its filled shapes are cut out.
81
+ - Groups named cut, score, engrave or guide put their shapes on that
82
+ layer, whatever their colour. Files that KerfScript wrote come back on
83
+ the layers they were written from.
84
+ - Lines in other colours go on a layer of their own, named after the
85
+ colour, set to mark and not cut.
86
+
87
+ Lines that meet end to end are joined. A loop of lines becomes a hole
88
+ when nothing is inside it, and stays as cut lines when it holds marks or
89
+ other lines, as a piece to press out does.
90
+
91
+ Some drawings cannot be held as shapes and stay cut lines exactly as
92
+ drawn: outlines that touch or cross a hole of the same part, holes that
93
+ overlap, outlines that cross themselves or have a slit drawn into them,
94
+ and closed paths with no area. Lines and marks that leave the part they
95
+ start in are kept on their own, so the part does not trim them. In a
96
+ silhouette, each filled shape is a piece of its own.
97
+
98
+ Text keeps its words, size and alignment and is written as `text()` in
99
+ the built-in font, with the drawing's font named in a comment; to use
100
+ that font, add `font = "..."` to the `text()` and pass the font file to
101
+ `kerf render` with `--font`. Text on the cut layer cuts the letters of
102
+ that font, not the drawing's: convert it to paths in your editor to cut
103
+ it as drawn. Embedded images and hidden elements are left out, and the
104
+ comment at the top says how many.
105
+
106
+ ## Material thickness and kerf
107
+
108
+ The depth of tabs and finger joints gives the material thickness, written
109
+ as `$thickness`. Change it and the joints follow. A part whose joints are
110
+ deeper, such as a tenon through a thick leg, gets its own
111
+ `material(thickness = ...)`.
112
+
113
+ Many drawings are made for one laser: their cut outlines are moved out by
114
+ half the beam's width, which rounds every outer corner a little. The
115
+ importer finds that, moves the outlines back to the sizes they were
116
+ designed at, and sets [`$kerf`](kerf.md) so the program cuts the same. A
117
+ part that cannot be moved back exactly, such as a strip as narrow as the
118
+ kerf, keeps its outlines as drawn and is cut with `material(kerf = 0)`.
119
+ Use `--kerf 0` to take the outlines as drawn, or `--kerf 0.15` to give the
120
+ kerf yourself.
121
+
122
+ ## Layout
123
+
124
+ The program keeps the parts where the drawing has them, with
125
+ `$layout = "none"`. With `--pack`, identical parts become one part with a
126
+ quantity, and KerfScript arranges them on sheets as usual.
127
+
128
+ ## Units
129
+
130
+ Drawings in millimetres, centimetres, inches or points are read at their
131
+ size. A drawing measured in pixels is read at 96 pixels per inch, or 72
132
+ for an Adobe Illustrator file; give another with `--dpi`.
133
+
134
+ ## In JavaScript
135
+
136
+ `importSvg(svg, options)` does the same in a browser or in Node.js, and
137
+ returns the program as `source`. `svg` is the text of the drawing or the
138
+ bytes of its file:
139
+
140
+ ```js
141
+ import { importSvg } from '@mylasertools/kerfscript';
142
+
143
+ const { source, summary, notes } = importSvg(svgText, { tolerance: 0.05 });
144
+ ```
145
+
146
+ `summary` says in words how many parts there are and what was found, and
147
+ `notes` holds the remarks written at the top of the program. The options
148
+ are `dpi`, `tolerance`, `kerf`, `pack` and `name`, the drawing's name for
149
+ the first comment. See [JavaScript](javascript.md#importsvg).
@@ -111,26 +111,190 @@ if (width) {
111
111
  Read the settings again after editing the source; the offsets refer to the
112
112
  string passed to `parameters()`.
113
113
 
114
+ ## format()
115
+
116
+ `format(source)` returns the program with its layout set, as `kerf fmt`
117
+ does: two-space indentation, spaces between tokens, at most one blank line in
118
+ a row and a newline at the end. Line breaks and comments stay where they
119
+ are. It throws a `KerfError` for a program that does not parse, and checks
120
+ its own result: the output always has the same tokens and comments as the
121
+ input.
122
+
123
+ ```js
124
+ import { format } from '@mylasertools/kerfscript';
125
+
126
+ format('x=1;module m(a,b=2){rect([a,b]);}');
127
+ // 'x = 1; module m(a, b = 2) { rect([a, b]); }\n'
128
+ ```
129
+
130
+ `format(source, { indent: 4 })` uses four spaces per level.
131
+
132
+ ## importSvg()
133
+
134
+ `importSvg(svg, options)` turns an SVG drawing into a program, as
135
+ `kerf import` does. `svg` is the text of the drawing, or the bytes of the
136
+ file as a `Uint8Array` or `ArrayBuffer`; bytes are read as UTF-8 or UTF-16,
137
+ which some programs write. It returns:
138
+
139
+ - `source`: the program
140
+ - `parts`: the number of parts
141
+ - `found`: what was recognised, as counts by kind
142
+ - `summary`: the same in words, such as `2 parts; found 3 circles.`
143
+ - `notes`: what to know about the drawing, also written at the top of the
144
+ program
145
+
146
+ The options are those of `kerf import`: `dpi`, `tolerance`, `kerf` and
147
+ `pack`, and `name`, the drawing's name for the comment at the top. See
148
+ [Importing SVG drawings](import.md).
149
+
150
+ ```js
151
+ import { importSvg, render } from '@mylasertools/kerfscript';
152
+
153
+ const file = input.files[0];
154
+ const { source, summary } = importSvg(await file.arrayBuffer(), { name: file.name });
155
+ const result = render(source, { fonts, defaultFont: 'Atkinson' });
156
+ ```
157
+
158
+ A drawing with text needs a font to render, as any program with `text()`
159
+ does. A large drawing can take some seconds to import: in a browser, call
160
+ `importSvg()` in a web worker so the page stays responsive. It throws an
161
+ `Error` for a file that is not SVG.
162
+
163
+ ## Syntax colouring
164
+
165
+ `@mylasertools/kerfscript/highlight` colours KerfScript code without loading
166
+ the interpreter. It never throws, so it works on unfinished code in an editor.
167
+ Its categories match the VS Code extension's grammar.
168
+
169
+ ```js
170
+ import { highlight, highlightLine } from '@mylasertools/kerfscript/highlight';
171
+
172
+ // HTML with a t-keyword, t-builtin, t-comment ... span per token
173
+ pre.innerHTML = highlight(source);
174
+
175
+ // Line by line, for editors such as CodeMirror
176
+ const state = { inBlock: false };
177
+ for (const line of source.split('\n')) {
178
+ for (const { kind, text } of highlightLine(line, state)) {
179
+ // kind: comment, section, control, string, number, keyword, constant,
180
+ // definition, builtin, layer, call, special, modifier or plain
181
+ }
182
+ }
183
+ ```
184
+
185
+ `highlight()` never puts a span across a line break, so its result can be
186
+ split into lines. `section` and `control` are customizer comments
187
+ (`/* [Size] */` and `// [40:5:400]`).
188
+
189
+ ## Built-in signatures
190
+
191
+ `SIGNATURES` lists every built-in module and function with its parameters,
192
+ defaults and a line of documentation, for editors, completion and help
193
+ text. `@mylasertools/kerfscript/signatures` has the same table without the
194
+ interpreter.
195
+
196
+ ```js
197
+ import { formatSignature, signature } from '@mylasertools/kerfscript/signatures';
198
+
199
+ const rect = signature('rect');
200
+ formatSignature(rect); // 'rect(size = 1, center = false, r = 0)'
201
+ rect.doc; // 'A rectangle with its corner at the origin'
202
+ rect.params[2]; // { name: 'r', default: '0', doc: 'Corner radius', named: true }
203
+ ```
204
+
205
+ Each signature has a `kind` (`module`, `function`, or `both` for `echo` and
206
+ `assert`), `params` in positional order, and `section`, the heading in the
207
+ [language reference](../LANGUAGE.md) that documents it. A parameter with
208
+ `named: true` can only be given by name. `rest` marks built-ins that take
209
+ any number of values, such as `str(...)`, and `forms` lists other ways to
210
+ call one, such as `log(base, x)`.
211
+
212
+ ## Language service
213
+
214
+ `@mylasertools/kerfscript/language` answers what an editor asks about a
215
+ program. It is what `kerf lsp` uses ([Editors](editors.md)), and it runs in
216
+ browsers too, for an editor on a web page. Positions are offsets into the
217
+ source in UTF-16 code units, as JavaScript strings count them.
218
+
219
+ ```js
220
+ import {
221
+ analyze,
222
+ complete,
223
+ hover,
224
+ problems,
225
+ signatureHelp,
226
+ } from '@mylasertools/kerfscript/language';
227
+
228
+ const a = analyze(source, { resolve });
229
+ hover(a, offset); // { start, end, text } with Markdown text, or undefined
230
+ complete(a, offset); // { from, to, items: [{ label, kind, detail, doc, insert }] }
231
+ signatureHelp(a, offset); // { label, params: [{ start, end, doc }], active }
232
+ problems(source, { resolve }); // [{ severity, message, start, end, related }]
233
+ ```
234
+
235
+ `analyze()` reads the program's names, scopes and libraries once for the
236
+ other functions, and works on code that does not parse: the lines with
237
+ errors are left out. Its `resolve` option finds libraries, as `render()`'s
238
+ does.
239
+
240
+ | Function | Returns |
241
+ | --------------------------- | --------------------------------------------------------------------------------- |
242
+ | `hover(a, offset)` | What the name or library path at the offset is, as Markdown |
243
+ | `complete(a, offset)` | What can be typed there, to replace the source from `from` to `to` |
244
+ | `signatureHelp(a, offset)` | The parameters of the call being typed and the one being typed |
245
+ | `definition(a, offset)` | Where the name is defined; `file` names the library when it is not in the program |
246
+ | `references(a, offset)` | Every place in the program the name is defined and used |
247
+ | `rename(a, offset, name)` | The places to change, or `{ error }` for a built-in or a library's name |
248
+ | `outline(a)` | Modules, functions and variables, nested as written |
249
+ | `problems(source, options)` | The errors and warnings from running the program; it takes `render()`'s options |
250
+
251
+ `problems()` runs the program, so call it in a worker on a page, as the
252
+ playground does with `render()`.
253
+
114
254
  ## Errors
115
255
 
116
- Language errors use `KerfError`. Its `line` and `col` identify the source
117
- location when available, and `echoes` contains output produced before the
118
- error. `missingFonts` lists fonts the application needs to supply.
256
+ Language errors use `KerfError`:
257
+
258
+ | Property | Value |
259
+ | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
260
+ | `message` | the message with its position, as `Unknown variable "w" (line 3, column 9)` |
261
+ | `reason` | the message without its position |
262
+ | `line`, `col` | where the error is, when known; columns count UTF-16 code units |
263
+ | `file` | the library path when the error is inside a library, as `kerf/box` |
264
+ | `trace` | the module and function calls it happened inside, innermost first: `{ kind, name, line, col, file, count }`, where `count` is how often a recursive call is on the stack |
265
+ | `call` | the built-in that was given an unusable argument |
266
+ | `echoes` | `echo()` output before the error |
267
+ | `warnings` | warnings before the error |
268
+ | `missingFonts` | fonts the application needs to supply |
269
+
270
+ `formatError()` writes all of that the way the command line shows it: the
271
+ source line with the problem underlined, the calls, and what a built-in
272
+ takes. Pass the source, and `resolve` to show lines inside your own
273
+ libraries; the standard library is found without it.
119
274
 
120
275
  ```js
121
- import { KerfError, render } from '@mylasertools/kerfscript';
276
+ import { formatError, render } from '@mylasertools/kerfscript';
122
277
 
123
278
  try {
124
- render('rect([80, 50]);');
279
+ render(source);
125
280
  } catch (error) {
126
- if (error instanceof KerfError) {
127
- console.error(error.message, error.line, error.col);
128
- } else {
129
- throw error;
130
- }
281
+ console.error(formatError(error, source, { file: 'panel.kerf' }));
131
282
  }
132
283
  ```
133
284
 
285
+ ```text
286
+ error: Unknown variable "widht". Did you mean "width"?
287
+ --> panel.kerf:3:9
288
+ |
289
+ 3 | rect([widht, 20]);
290
+ | ^^^^^
291
+ = in module panel(), called at panel.kerf:5:1
292
+ ```
293
+
294
+ Show it in a monospaced font so the marks line up. `formatError()` takes
295
+ any thrown value; for one that is not a `KerfError` it gives
296
+ `error: <message>`.
297
+
134
298
  ## Running untrusted code
135
299
 
136
300
  Run browser renders in a web worker so they do not block the page. Set a
@@ -154,3 +318,6 @@ render(source, { limits: { maxPoints: DEFAULT_LIMITS.maxPoints / 2 } });
154
318
  KerfScript programs have no direct file or network access. `use` and
155
319
  `include` can load the standard library and sources returned by your
156
320
  `resolve` callback. Restrict that callback to libraries you intend to expose.
321
+ In Node.js, `resolver(file)` from `@mylasertools/kerfscript/libraries`
322
+ finds a design's files and installed libraries as the command line does; see
323
+ [Shared libraries](shared-libraries.md#from-javascript).
package/docs/kerf.md CHANGED
@@ -43,6 +43,11 @@ kerf compensation in either KerfScript or your laser software, once per job.
43
43
  Repeat the measurement when the material or cutting settings change. Thick
44
44
  material can have a different cut width at its top and bottom faces.
45
45
 
46
+ The [kerf gauge](library.md#calibration) in the calibration library
47
+ measures it another way, without calipers on a square: it cuts strips in a
48
+ frame with single lines. Push the strips to one side and divide the gap
49
+ they leave by the number of strips plus one.
50
+
46
51
  ## Checking a fit
47
52
 
48
53
  After calibrating kerf, measure your sheet's thickness and set `$thickness`
@@ -57,6 +62,11 @@ one. Choose the fit you need and use the result to refine your dimensions.
57
62
  Both kerf and sheet thickness affect the fit. A tight or loose slot alone
58
63
  does not tell you which value needs adjusting.
59
64
 
65
+ For finger joints, `joint_test()` in the calibration library cuts a pair
66
+ of pieces for each of a few kerf values; the pair that holds without glue
67
+ has the kerf to use. The [calibration set](../examples/calibration-set.kerf)
68
+ cuts the gauge, the slots and the joint pairs together.
69
+
60
70
  ## Limits
61
71
 
62
72
  - Compensation can close a hole or notch narrower than the kerf. KerfScript