OSDN Git Service

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