OSDN Git Service

Regular updates
[twpd/master.git] / parsimmon.md
1 ---
2 title: Parsimmon
3 category: JavaScript libraries
4 layout: 2017/sheet
5 ---
6
7 ### Basic usage
8 ```js
9 const P = require('parsimmon')
10
11 P.regexp(/[a-z]+/)
12 .parse('hello')
13 //=> { status: true, value: ['hello'] }
14 ```
15
16 ### Atoms
17
18 ```js
19 P.regexp(/[a-z]+/)
20 P.string('hello')
21 P.oneOf('abc')             // like P.regexp(/[abc]/)
22
23 P.whitespace
24 P.optWhitespace
25 P.eof
26 ```
27
28 ### Combinators
29
30 ```js
31 P.seq(a, b, c)             // sequence of these
32 P.alt(a, b)                // any of these
33 P.sepBy(a, P.string(','))  // sequence of `a`, separated by ','
34 P.sepBy1(a, P.string(',')) // same, at least once
35
36 a.or(b)                    // like P.alt(a, b)
37 a.skip(b)                  // parses `b` but discards it
38
39 a.many()
40 a.times(3)
41 a.times(1, 4)              // 1 <= x <= 4
42 a.atMost(10)
43 a.atLeast(10)
44 ```
45
46 ### Formatting
47
48 ```js
49 P.seq(P.number, P.oneOf('+-*/'), P.number)
50 .map(([left, oper, right]) => ({ oper, left, right }))
51 ```
52
53 ### Reference
54
55 - <https://github.com/jneen/parsimmon/blob/master/API.md>