@@ -3,21 +3,6 @@ import assert from 'node:assert/strict';
33import * as ohm from 'ohm-js' ;
44import { 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…
4547const LOC_IDENTITY = { line : 0 , col : 0 } ;
4648
47- // …and this is our binary operator.
49+ // …and this is our "combine" operator.
4850function sumLocations ( a , b ) {
4951 return {
5052 line : a . line + b . line ,
0 commit comments