|
3 | 3 | ## P.str(value: string): Parser
|
4 | 4 | Generates a new parser that consumes the input string using the specified string.
|
5 | 5 |
|
| 6 | +```ts |
| 7 | +// [Equivalent PEG] "test" |
| 8 | +const parser = P.str('test'); |
| 9 | + |
| 10 | +const result = parser.parse('test'); |
| 11 | +console.log(result); |
| 12 | +// => { success: true, value: 'test', index: 4 } |
| 13 | +``` |
| 14 | + |
6 | 15 | ## P.regexp(pattern: Regexp): Parser
|
7 | 16 | Generates a new parser that consumes the input string using the specified regular expression.
|
8 | 17 |
|
| 18 | +```ts |
| 19 | +// [Equivalent PEG] [a-z] |
| 20 | +const parser = P.regexp(/[a-z]/); |
| 21 | + |
| 22 | +const result = parser.parse('a'); |
| 23 | +console.log(result); |
| 24 | +// => { success: true, value: 'a', index: 1 } |
| 25 | +``` |
| 26 | + |
9 | 27 | ## P.seq(parsers: Parser[], select?: boolean): Parser
|
10 | 28 |
|
| 29 | +```ts |
| 30 | +// [Equivalent PEG] "a" "1" |
| 31 | +const parser = P.seq([ |
| 32 | + P.str('a'), |
| 33 | + P.str('1'), |
| 34 | +]); |
| 35 | + |
| 36 | +const result = parser.parse('a1'); |
| 37 | +console.log(result); |
| 38 | +// => { success: true, value: [ 'a', '1' ], index: 2 } |
| 39 | +``` |
| 40 | + |
| 41 | +You can also select a result to be returned from all of them: |
| 42 | +```ts |
| 43 | +// [Equivalent PEG] value0:"a" value1:"1" { return value1; } |
| 44 | +const parser = P.seq([ |
| 45 | + P.str('a'), |
| 46 | + P.str('1'), |
| 47 | +], 1); |
| 48 | + |
| 49 | +const result = parser.parse('a1'); |
| 50 | +console.log(result); |
| 51 | +// => { success: true, value: '1', index: 2 } |
| 52 | +``` |
| 53 | + |
11 | 54 | ## P.alt(parsers: Parser[]): Parser
|
12 | 55 |
|
| 56 | +```ts |
| 57 | +// [Equivalent PEG] "a" / "1" |
| 58 | +const parser = P.alt([ |
| 59 | + P.str('a'), |
| 60 | + P.str('1'), |
| 61 | +]); |
| 62 | + |
| 63 | +let result; |
| 64 | + |
| 65 | +result = parser.parse('a'); |
| 66 | +console.log(result); |
| 67 | +// => { success: true, value: 'a', index: 1 } |
| 68 | + |
| 69 | +result = parser.parse('1'); |
| 70 | +console.log(result); |
| 71 | +// => { success: true, value: '1', index: 1 } |
| 72 | +``` |
| 73 | + |
| 74 | +## P.sep(item: Parser, separator: Parser, min: number): Parser |
| 75 | + |
| 76 | +```ts |
| 77 | +// [Equivalent PEG] head:"a" tail:("," @"a")* { return [head, ...tail]; } |
| 78 | +const parser = P.sep(P.str('a'), P.str(','), 1); |
| 79 | + |
| 80 | +let result; |
| 81 | + |
| 82 | +result = parser.parse('a'); |
| 83 | +console.log(result); |
| 84 | +// => { success: true, value: [ 'a' ], index: 1 } |
| 85 | + |
| 86 | +result = parser.parse('a,a'); |
| 87 | +console.log(result); |
| 88 | +// => { success: true, value: [ 'a', 'a' ], index: 3 } |
| 89 | +``` |
| 90 | + |
| 91 | +## P.lazy(fn: () => Parser): Parser |
| 92 | +Generates a new parser that is lazy-evaluated. |
| 93 | + |
| 94 | +## P.match(parser: Parser): Parser |
| 95 | +Generates a new parser to continue if the match is successful. |
| 96 | +The generated parser does not consume input. |
| 97 | + |
| 98 | +```ts |
| 99 | +// TODO |
| 100 | +``` |
| 101 | + |
13 | 102 | ## P.notMatch(parser: Parser): Parser
|
14 | 103 | Generates a new parser to continue if the match fails.
|
15 | 104 | The generated parser does not consume input.
|
16 | 105 |
|
| 106 | +```ts |
| 107 | +// TODO |
| 108 | +``` |
17 | 109 |
|
18 | 110 | # Parsers
|
19 | 111 |
|
20 |
| -## P.char |
| 112 | +## P.char: Parser |
| 113 | +Matches any character. |
21 | 114 |
|
22 |
| -## P.cr |
| 115 | +```ts |
| 116 | +// [Equivalent PEG] . |
| 117 | +const parser = P.char; |
23 | 118 |
|
24 |
| -## P.lf |
| 119 | +const result = parser.parse('a'); |
| 120 | +console.log(result); |
| 121 | +// => { success: true, value: 'a', index: 1 } |
| 122 | +``` |
25 | 123 |
|
26 |
| -## P.newline |
| 124 | +## P.cr: Parser |
| 125 | +Matches `\r` (CR) |
27 | 126 |
|
| 127 | +## P.lf: Parser |
| 128 | +Matches `\n` (LF) |
| 129 | + |
| 130 | +## P.newline: Parser |
| 131 | +Matches `\r\n` or `\r` or `\n` |
28 | 132 |
|
29 | 133 | # Parser APIs
|
30 | 134 |
|
| 135 | +## parser.parse(input: string, state?: any): Result |
| 136 | +```ts |
| 137 | +const parser = P.str('a'); |
| 138 | + |
| 139 | +parser.parse('a'); |
| 140 | + |
| 141 | +// specify states |
| 142 | +parser.parse('a', { flag: true, count: 0 }); |
| 143 | +``` |
| 144 | + |
31 | 145 | ## parser.map(fn: (value) => any): Parser
|
| 146 | +```ts |
| 147 | +// [Equivalent PEG] value0:"a" value1:"b" value2:"c" { return [value0, value2]; } |
| 148 | +const parser = P.seq([ |
| 149 | + P.str('a'), |
| 150 | + P.str('b'), |
| 151 | + P.str('c'), |
| 152 | +]).map(value => { |
| 153 | + return [value[0], value[2]]; |
| 154 | +}); |
| 155 | + |
| 156 | +const result = parser.parse('abc'); |
| 157 | +console.log(result); |
| 158 | +// => { success: true, value: [ 'a', 'c' ], index: 3 } |
| 159 | +``` |
32 | 160 |
|
33 | 161 | ## parser.text(): Parser
|
| 162 | +```ts |
| 163 | +// [Equivalent PEG] "a" "b" "c" { return text(); } |
| 164 | +const parser = P.seq([ |
| 165 | + P.str('a'), |
| 166 | + P.str('b'), |
| 167 | + P.str('c'), |
| 168 | +]).text(); |
| 169 | + |
| 170 | +const result = parser.parse('abc'); |
| 171 | +console.log(result); |
| 172 | +// => { success: true, value: 'abc', index: 3 } |
| 173 | +``` |
34 | 174 |
|
35 | 175 | ## parser.many(min: number): Parser
|
36 | 176 |
|
37 |
| -## parser.sep(separator: Parser, min: number): Parser |
| 177 | +Matches 0 or more items: |
| 178 | +```ts |
| 179 | +// [Equivalent PEG] "abc"* |
| 180 | +const parser = P.str('abc').many(0); |
| 181 | + |
| 182 | +let result; |
| 183 | + |
| 184 | +result = parser.parse(''); |
| 185 | +console.log(result); |
| 186 | +// => { success: true, value: [], index: 0 } |
| 187 | + |
| 188 | +result = parser.parse('abc'); |
| 189 | +console.log(result); |
| 190 | +// => { success: true, value: [ 'abc' ], index: 3 } |
| 191 | +``` |
| 192 | + |
| 193 | +Matches 1 or more items: |
| 194 | +```ts |
| 195 | +// [Equivalent PEG] "abc"+ |
| 196 | +const parser = P.str('abc').many(1); |
| 197 | + |
| 198 | +let result; |
| 199 | + |
| 200 | +result = parser.parse('abc'); |
| 201 | +console.log(result); |
| 202 | +// => { success: true, value: [ 'abc' ], index: 3 } |
| 203 | + |
| 204 | +result = parser.parse('abcabc'); |
| 205 | +console.log(result); |
| 206 | +// => { success: true, value: [ 'abc', 'abc' ], index: 6 } |
| 207 | +``` |
38 | 208 |
|
39 | 209 | ## parser.option(): Parser
|
40 | 210 | Generates a new parser that returns null even if the match fails.
|
41 | 211 |
|
| 212 | +```ts |
| 213 | +// [Equivalent PEG] "a" "b"? |
| 214 | +const parser = P.seq([ |
| 215 | + P.str('a'), |
| 216 | + P.str('b').option(), |
| 217 | +]); |
| 218 | + |
| 219 | +let result; |
| 220 | + |
| 221 | +result = parser.parse('ab'); |
| 222 | +console.log(result); |
| 223 | +// => { success: true, value: [ 'a', 'b' ], index: 2 } |
| 224 | + |
| 225 | +result = parser.parse('a'); |
| 226 | +console.log(result); |
| 227 | +// => { success: true, value: [ 'a', null ], index: 1 } |
| 228 | +``` |
42 | 229 |
|
43 | 230 | # Other APIs
|
44 | 231 |
|
45 |
| -## P.createLanguage() |
| 232 | +## P.createLanguage(syntaxes: Record<string, (rules: Language) => Parser>): Language |
46 | 233 | You can use createLanguage to create a set of syntax.
|
47 | 234 |
|
| 235 | +Each rule is lazy evaluated. |
| 236 | + |
| 237 | +```ts |
| 238 | +const lang = P.createLanguage({ |
| 239 | + root: rules => { |
| 240 | + return P.alt([ |
| 241 | + rules.rule1, |
| 242 | + rules.rule2, |
| 243 | + ]); |
| 244 | + }, |
| 245 | + |
| 246 | + rule1: rules => { |
| 247 | + return P.regexp('a'); |
| 248 | + }, |
| 249 | + |
| 250 | + rule2: rules => { |
| 251 | + return P.regexp('b'); |
| 252 | + }, |
| 253 | +}); |
| 254 | + |
| 255 | +const result = lang.root.parse('a'); |
| 256 | +console.log(result); |
| 257 | +// => { success: true, value: 'a', index: 1 } |
| 258 | +``` |
| 259 | + |
48 | 260 | ## P.success()
|
49 | 261 | for custom parser.
|
50 | 262 |
|
|
0 commit comments