Skip to content

Commit e49484c

Browse files
committed
Update Docusaurus to fix broken Markdown
This fixes many broken things: * Most importantly, the Syntax Reference page no longer shows broken Markdown * Added syntax highlighting wherever missing * Fixed some headings not being in table of contents * Fixed broken anchors * There are a few false warnings left * Updated Docusaurus * Unorphaned or merged pages * Imported Docker page * Use a symlink relative path for viewing images locally Fixes: ohmjs/ohm#475
1 parent eac5c72 commit e49484c

24 files changed

Lines changed: 6689 additions & 5732 deletions

blog/2026-02-20-ohm-v18.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ _aka "The One that Compiles to Wasm"._
99

1010
After nearly a year of development, we're excited to announce the first beta release of Ohm v18 — the biggest change to Ohm since its initial release. We've totally reworked the core parsing engine to be WebAssembly-based, making parsing around 20x faster on real-world grammars while using a fraction of the memory.
1111

12+
{/* truncate */}
13+
1214
## What's new
1315

1416
Every version of Ohm up to v17 worked the same way under the hood: when you call `grammar.match()`, Ohm walks a tree of parsing expression objects (PExprs), calling `eval()` on each node. (It's a so-called [tree-walking interpreter](https://craftinginterpreters.com/a-tree-walk-interpreter.html).) In the process, it builds up a huge parse tree, with each node a separate object that must be managed by the GC.

docs/api-reference.md

Lines changed: 108 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ This page documents the API of Ohm/JS, a JavaScript library for working with gra
66

77
**NOTE:** For grammars defined in a JavaScript string literal (i.e., not in a separate .ohm file), it's recommended to use a [template literal with the String.raw tag](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw).
88

9-
<b><pre class="api">ohm.grammar(source: string, optNamespace?: object) &rarr; Grammar</pre></b>
9+
```ts
10+
ohm.grammar(source: string, optNamespace?: object): Grammar
11+
```
1012

1113
Instantiate the Grammar defined by `source`. If specified, `optNamespace` is an object in which references to other grammars should be resolved. For example, if the grammar source begins with an inheritance declaration like `MyGrammar <: OtherGrammar { ... }`, then `optNamespace` should have a property named `OtherGrammar`.
1214

13-
<b><pre class="api">ohm.grammars(source: string, optNamespace?: object) &rarr; object</pre></b>
15+
```ts
16+
ohm.grammars(source: string, optNamespace?: object): object
17+
```
1418

1519
Create a new object containing Grammar instances for all of the grammars defined in `source`. As with `ohm.grammar`, if `optNamespace` is specified, it is an object in which references to other grammars should be resolved. Additionally, it will be the prototype of the returned object.
1620

@@ -21,7 +25,7 @@ Here is an example of instantiating a Grammar:
2125
const ohm = require('ohm-js');
2226
-->
2327

24-
```
28+
```js
2529
const parentDef = String.raw`
2630
Parent {
2731
start = "parent"
@@ -36,7 +40,8 @@ const parentGrammar = ohm.grammar(parentDef);
3640
-->
3741

3842
In the next example We instantiate a new grammar, *Child*, that inherits from our *Parent* grammar. We use the `ohm.grammars` method, which returns an object of our grammars:
39-
```
43+
44+
```js
4045
const childDef = String.raw`
4146
Child <: Parent {
4247
start := "child"
@@ -54,7 +59,8 @@ console.log(Object.keys(childGrammar));
5459
-->
5560

5661
You could also concatenate the grammar definitions, and then instantiate them. This results in an object with both Grammars:
57-
```
62+
63+
```js
5864
const combinedDef = parentDef.concat(childDef);
5965
const grammars = ohm.grammars(combinedDef);
6066
console.log(Object.keys(grammars));
@@ -70,49 +76,72 @@ console.log(Object.keys(grammars));
7076

7177
A Grammar instance `g` has the following methods:
7278

73-
<a name="Grammar.match"><b><pre class="api">g.match(str: string, optStartRule?: string) &rarr; MatchResult</pre></b></a>
79+
<span id="Grammar.match"></span>
80+
```ts
81+
g.match(str: string, optStartRule?: string): MatchResult
82+
```
7483

7584
Try to match `str` against `g`, returning a [MatchResult](#matchresult-objects). If `optStartRule` is given, it specifies the rule on which to start matching. By default, the start rule is inherited from the supergrammar, or if there is no supergrammar specified, it is the first rule in `g`'s definition.
7685

77-
<b><pre class="api">g.matcher()</pre></b>
86+
```ts
87+
g.matcher()
88+
```
7889

7990
Create a new [Matcher](#matcher-objects) object which supports incrementally matching `g` against a changing input string.
8091

81-
<a name="Grammar.trace"><b><pre class="api" id="trace">g.trace(str: string, optStartRule?: string) &rarr; Trace</pre></b></a>
92+
<span id="Grammar.trace"></span>
93+
```ts
94+
g.trace(str: string, optStartRule?: string): Trace
95+
```
8296

8397
Try to match `str` against `g`, returning a Trace object. `optStartRule` has the same meaning as in `g.match`. Trace objects have a `toString()` method, which returns a string which summarizes each parsing step (useful for debugging).
8498

85-
<b><pre class="api">g.createSemantics() &rarr; Semantics</pre></b>
99+
```ts
100+
g.createSemantics(): Semantics
101+
```
86102

87-
Create a new [Semantics](#semantics) object for `g`.
103+
Create a new [Semantics](#semantics-objects) object for `g`.
88104

89-
<b><pre class="api" id="extendSemantics">g.extendSemantics(superSemantics: Semantics) &rarr; Semantics</pre></b>
105+
<span id="extendSemantics"></span>
106+
```ts
107+
g.extendSemantics(superSemantics: Semantics): Semantics
108+
```
90109

91-
Create a new [Semantics](#semantics) object for `g` that inherits all of the operations and attributes in `superSemantics`. `g` must be a descendent of the grammar associated with `superSemantics`.
110+
Create a new [Semantics](#semantics-objects) object for `g` that inherits all of the operations and attributes in `superSemantics`. `g` must be a descendent of the grammar associated with `superSemantics`.
92111

93112
## Matcher objects
94113

95114
Matcher objects can be used to incrementally match a changing input against the Matcher's grammar, e.g. in an editor or IDE. When a Matcher's input is modified via `replaceInputRange`, further calls to `match` will reuse the partial results of previous calls wherever possible. Generally, this means that small changes to the input will result in very short match times.
96115

97116
A Matcher instance `m` has the following methods:
98117

99-
<b><pre class="api">m.getInput() &rarr; string</pre></b>
118+
```ts
119+
m.getInput(): string
120+
```
100121

101122
Return the current input string.
102123

103-
<b><pre class="api">m.setInput(str: string)</pre></b>
124+
```ts
125+
m.setInput(str: string)
126+
```
104127

105128
Set the input string to `str`.
106129

107-
<b><pre class="api">m.replaceInputRange(startIdx: number, endIdx: number, str: string)</pre></b>
130+
```ts
131+
m.replaceInputRange(startIdx: number, endIdx: number, str: string)
132+
```
108133

109134
Edit the current input string, replacing the characters between `startIdx` and `endIdx` with `str`.
110135

111-
<b><pre class="api">m.match(optStartRule?: string) &rarr; MatchResult</pre></b>
136+
```ts
137+
m.match(optStartRule?: string): MatchResult
138+
```
112139

113140
Like [Grammar's `match` method](#Grammar.match), but operates incrementally.
114141

115-
<b><pre class="api">m.trace(optStartRule?: string) &rarr; Trace</pre></b>
142+
```ts
143+
m.trace(optStartRule?: string): Trace
144+
```
116145

117146
Like [Grammar's `trace` method](#Grammar.trace), but operates incrementally.
118147

@@ -122,35 +151,47 @@ Internally, a successful MatchResult contains a _parse tree_, which is made up o
122151

123152
A MatchResult instance `r` has the following methods:
124153

125-
<b><pre class="api">r.succeeded() &rarr; boolean</pre></b>
154+
```ts
155+
r.succeeded(): boolean
156+
```
126157

127158
Return `true` if the match succeeded, otherwise `false`.
128159

129-
<b><pre class="api">r.failed() &rarr; boolean</pre></b>
160+
```ts
161+
r.failed(): boolean
162+
```
130163

131164
Return `true` if the match failed, otherwise `false`.
132165

133166
### MatchFailure objects
134167

135168
When `r.failed()` is `true`, `r` has the following additional properties and methods:
136169

137-
<b><pre class="api">r.message: string</pre></b>
170+
```ts
171+
r.message: string
172+
```
138173

139174
Contains a message indicating where and why the match failed. This message is suitable for end users of a language (i.e., people who do not have access to the grammar source).
140175

141-
<b><pre class="api">r.shortMessage: string</pre></b>
176+
```ts
177+
r.shortMessage: string
178+
```
142179

143180
Contains an abbreviated version of `r.message` that does not include an excerpt from the invalid input.
144181

145-
<b><pre class="api">r.getRightmostFailurePosition() &rarr; number</pre></b>
182+
```ts
183+
r.getRightmostFailurePosition(): number
184+
```
146185

147186
Return the index in the input stream at which the match failed.
148187

149-
<b><pre class="api">r.getRightmostFailures() &rarr; Array</pre></b>
188+
```ts
189+
r.getRightmostFailures(): Array
190+
```
150191

151192
Return an array of Failure objects describing the failures the occurred at the rightmost failure position.
152193

153-
<h2 id="semantics">Semantics, Operations, and Attributes</h2>
194+
## Semantics, Operations, and Attributes
154195

155196
An Operation represents a function that can be applied to a successful match result. Like a [Visitor](http://en.wikipedia.org/wiki/Visitor_pattern), an operation is evaluated by recursively walking the parse tree, and at each node, invoking the matching semantic action from its _action dictionary_.
156197

@@ -165,21 +206,29 @@ This returns a parse node, whose properties correspond to the operations and att
165206

166207
A Semantics instance `s` has the following methods, which all return `this` so they can be chained:
167208

168-
<b><pre class="api">mySemantics.addOperation(nameOrSignature: string, actionDict: object) &rarr; Semantics</pre></b>
209+
```ts
210+
mySemantics.addOperation(nameOrSignature: string, actionDict: object): Semantics
211+
```
169212

170213
Add a new Operation to this Semantics, using the [semantic actions](#semantic-actions) contained in `actionDict`. The first argument is either a name (e.g. `'prettyPrint'`) or a _signature_ which specifies the operation name and zero or more named parameters (e.g., `'prettyPrint()'`, `'prettyPrint(depth, strict)'`). It is an error if there is already an operation or attribute called `name` in this semantics.
171214

172215
If the operation has arguments, they are accessible via `this.args` within a semantic action. For example, `this.args.depth` would hold the value of the `depth` argument for the current action.
173216

174-
<b><pre class="api">mySemantics.addAttribute(name: string, actionDict: object) &rarr; Semantics</pre></b>
217+
```ts
218+
mySemantics.addAttribute(name: string, actionDict: object): Semantics
219+
```
175220

176221
Exactly like `semantics.addOperation`, except it will add an Attribute to the semantics rather than an Operation.
177222

178-
<b><pre class="api">mySemantics.extendOperation(name: string, actionDict: object) &rarr; Semantics</pre></b>
223+
```ts
224+
mySemantics.extendOperation(name: string, actionDict: object): Semantics
225+
```
179226

180227
Extend the Operation named `name` with the semantic actions contained in `actionDict`. `name` must be the name of an operation in the super semantics — i.e., you must first extend the Semantics via [`extendSemantics`](#extendSemantics) before you can extend any of its operations.
181228

182-
<b><pre class="api">semantics.extendAttribute(name: string, actionDict: object) &rarr; Semantics</pre></b>
229+
```ts
230+
semantics.extendAttribute(name: string, actionDict: object): Semantics
231+
```
183232

184233
Exactly like `semantics.extendOperation`, except it will extend an Attribute of the super semantics rather than an Operation.
185234

@@ -189,7 +238,7 @@ A semantic action is a function that computes the value of an operation or attri
189238

190239
- _Rule application_, or _non-terminal_ nodes, which correspond to rule application expressions
191240
- _Terminal_ nodes, for string and number literals, and keyword expressions
192-
- _Iteration_ nodes, which are associated with expressions inside a [repetition operator](./syntax-reference.md#repetition-operators) (`*`, `+`, and `?`)
241+
- _Iteration_ nodes, which are associated with expressions inside a [repetition operator](./syntax-reference.md#repetition-operators---) (`*`, `+`, and `?`)
193242

194243
Generally, you write a semantic action for each rule in your grammar, and store them together in an _action dictionary_. For example, given the following grammar:
195244

@@ -239,7 +288,7 @@ The matching semantic action for a particular node is chosen as follows:
239288
- On a terminal node (e.g., a node produced by the parsing expression `"hello"`), use the semantic action named `_terminal`.
240289
- On an iteration node (e.g., a node produced by the parsing expression `letter+`), use the semantic action named `_iter`.
241290

242-
<span id="special-actions"></span>The `_iter`, `_nonterminal`, and `_terminal` actions are sometimes called _special actions_. `_iter` and `_nonterminal` take a variable number of arguments, which are typically captured into an array using [rest parameter syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) , e.g. `_iter(...children) {'{'} ... {'}'}`. The `_terminal` action takes no arguments.
291+
<span id="special-actions"></span>The `_iter`, `_nonterminal`, and `_terminal` actions are sometimes called _special actions_. `_iter` and `_nonterminal` take a variable number of arguments, which are typically captured into an array using [rest parameter syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) , e.g. <code>_iter(...children) &lbrace; ... &rbrace;</code>. The `_terminal` action takes no arguments.
243292

244293
<!-- @markscript
245294
markscript.transformNextBlock((code) =>
@@ -250,47 +299,65 @@ The matching semantic action for a particular node is chosen as follows:
250299

251300
_**NOTE:** Versions of Ohm prior to v16.0 had slightly different behaviour with regards to default semantic actions. See [here](./releases/ohm-js-16.0.md#default-semantic-actions) for more details._
252301

253-
Note that you can also write semantic actions for built-in rules like `letter` or `digit`. For `ListOf`, please see the documentation on [asIteration](#asIteration) below.
302+
Note that you can also write semantic actions for built-in rules like `letter` or `digit`. For `ListOf`, please see the documentation on [asIteration](#asiteration) below.
254303

255304
### Parse Nodes
256305

257306
Each parse node is associated with a particular _parsing expression_ (a fragment of an Ohm grammar), and the node captures any input that was successfully parsed by that expression. Unlike many parsing frameworks, Ohm does not have a syntax for binding/capturing -- every parsing expression captures all the input it consumes, and produces a fixed number of values.
258307

259308
A node `n` has the following methods and properties:
260309

261-
<b><pre class="api">n.child(idx: number) &rarr; Node</pre></b>
310+
```ts
311+
n.child(idx: number): Node
312+
```
262313

263314
Get the child at index `idx`.
264315

265-
<b><pre class="api">n.isTerminal() &rarr; boolean</pre></b>
316+
```ts
317+
n.isTerminal(): boolean
318+
```
266319

267320
`true` if the node is a terminal node, otherwise `false`.
268321

269-
<b><pre class="api">n.isIteration() &rarr; boolean</pre></b>
322+
```ts
323+
n.isIteration(): boolean
324+
```
270325

271326
`true` if the node is an iteration node (i.e., if it associated with a repetition operator in the grammar), otherwise `false`.
272327

273-
<b><pre class="api">n.children: Array</pre></b>
328+
```ts
329+
n.children: Array
330+
```
274331

275332
An array containing the node's children.
276333

277-
<b><pre class="api">n.ctorName: string</pre></b>
334+
```ts
335+
n.ctorName: string
336+
```
278337

279338
The name of grammar rule that created the node.
280339

281-
<b><pre class="api">n.source: Interval</pre></b>
340+
```ts
341+
n.source: Interval
342+
```
282343

283344
Captures the portion of the input that was consumed by the node.
284345

285-
<b><pre class="api" id="Node-sourceString">n.sourceString: string</pre></b>
346+
```ts
347+
n.sourceString: string
348+
```
286349

287350
The substring of the input that was consumed by the node. Equivalent to `n.source.contents`.
288351

289-
<b><pre class="api">n.numChildren: number</pre></b>
352+
```ts
353+
n.numChildren: number
354+
```
290355

291356
The number of child nodes that the node has.
292357

293-
<b><pre class="api">n.isOptional() &rarr; boolean</pre></b>
358+
```ts
359+
n.isOptional(): boolean
360+
```
294361

295362
`true` if the node is an iterator node having either one or no child (? operator), otherwise `false`.
296363

@@ -323,7 +390,7 @@ G {
323390
const s = g_asIteration.createSemantics();
324391
-->
325392

326-
```
393+
```js
327394
s.addOperation('upper()', {
328395
Start(list) {
329396
return list.asIteration().children.map(c => c.upper());

0 commit comments

Comments
 (0)