Skip to content

Commit ba87d03

Browse files
committed
semantics example: more comment cleanup
1 parent 3d4441d commit ba87d03

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

examples/semantics/lineAndCol.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,6 @@ import assert from 'node:assert/strict';
33
import * as ohm from 'ohm-js';
44
import {recoverSourceOrder} from 'ohm-js/extras';
55

6-
/*
7-
A simple grammar containing only lexical rules, i.e. rules whose name begins
8-
with a lowercase letter. They need to be _lexical_ rules, because otherwise
9-
Ohm's implicit space skipping wouldn't let us easily see the newlines.
10-
*/
11-
const g = ohm.grammar(String.raw`
12-
Listy {
13-
start = expr spaces
14-
expr = spaces (list | num | nil)
15-
list = spaces "[" expr* spaces "]"
16-
num = digit+
17-
nil = "nil"
18-
}
19-
`);
20-
216
/*
227
Our goal will be to compute line and column information for all of the nodes
238
in the tree, and do it in a way that's friendly to incremental evaluation.
@@ -40,11 +25,28 @@ const g = ohm.grammar(String.raw`
4025
Another (fancier) term for what we're doing here is "monoid-cached tree".
4126
*/
4227

43-
// A monoid requires an identity element, and an associative binary operator.
44-
// This is our identity (neutral) element…
28+
/*
29+
A simple grammar containing only lexical rules, i.e. rules whose name begins
30+
with a lowercase letter. They need to be _lexical_ rules, because otherwise
31+
Ohm's implicit space skipping wouldn't let us easily see the newlines.
32+
*/
33+
const g = ohm.grammar(String.raw`
34+
Listy {
35+
start = expr spaces
36+
expr = spaces (list | num | nil)
37+
list = spaces "[" expr* spaces "]"
38+
num = digit+
39+
nil = "nil"
40+
}
41+
`);
42+
43+
// In abstract algebra, a _monoid_ is a set (or a data type) that has
44+
// (a) a "combine" operation that's associative, i.e. (a + b) + c = a + (b + c)
45+
// (b) an identity or neutral element.
46+
// This is our identity element…
4547
const LOC_IDENTITY = {line: 0, col: 0};
4648

47-
// …and this is our binary operator.
49+
// …and this is our "combine" operator.
4850
function sumLocations(a, b) {
4951
return {
5052
line: a.line + b.line,

0 commit comments

Comments
 (0)