@@ -7,15 +7,12 @@ let P = require('../');
7
7
8
8
///////////////////////////////////////////////////////////////////////
9
9
10
- // A little helper to wrap a parser with optional whitespace. Helper functions
11
- // that take a parser can be passed to the .thru(wrapper) method.
12
- function spaced ( parser ) {
13
- return P . optWhitespace
14
- . then ( parser )
15
- . skip ( P . optWhitespace ) ;
16
- }
17
10
18
11
let Lisp = P . createLanguage ( {
12
+
13
+ // An expression is just any of the other values we make in the language. Note
14
+ // that because we're using `.createLanguage` here we can reference other
15
+ // parsers off of the argument to our function. `r` is short for `rules` here.
19
16
Expression : function ( r ) {
20
17
return P . alt (
21
18
r . Symbol ,
@@ -24,8 +21,8 @@ let Lisp = P.createLanguage({
24
21
) ;
25
22
} ,
26
23
27
- // The basic parsers (usually the ones described via regexp) should have a
28
- // description for error message purposes.
24
+ // The basic parsers (usually the ones described via regexp) should have a
25
+ // description for error message purposes.
29
26
Symbol : function ( ) {
30
27
return P . regexp ( / [ a - z A - Z _ - ] [ a - z A - Z 0 - 9 _ - ] * / )
31
28
. desc ( 'symbol' ) ;
@@ -39,18 +36,19 @@ let Lisp = P.createLanguage({
39
36
. desc ( 'number' ) ;
40
37
} ,
41
38
42
- // `.then` throws away the first value, and `.skip` throws away the second
43
- // `.value, so we're left with just the `Expression.thru(spaced).many( )` part as
44
- // the `.yielded value from this parser .
39
+ // `.trim(P.optWhitespace)` removes whitespace from both sides, then `.many()`
40
+ // repeats the expression zero or more times. Finally, `.wrap(... )` removes
41
+ // the '(' and ')' from both sides of the list .
45
42
List : function ( r ) {
46
- return P . string ( '(' )
47
- . then ( r . Expression . thru ( spaced ) . many ( ) )
48
- . skip ( P . string ( ')' ) ) ;
43
+ return r . Expression
44
+ . trim ( P . optWhitespace )
45
+ . many ( )
46
+ . wrap ( P . string ( '(' ) , P . string ( ')' ) ) ;
49
47
} ,
50
48
51
- // Let's remember to throw away whitesapce at the top level of the parser .
49
+ // A file in Lisp is generally just zero or more expressions .
52
50
File : function ( r ) {
53
- return r . Expression . thru ( spaced ) . many ( ) ;
51
+ return r . Expression . trim ( P . optWhitespace ) . many ( ) ;
54
52
}
55
53
} ) ;
56
54
0 commit comments