Join the parse results of the given parser into an array. * * This requires the parser to succeed at least once.
( parser: ParserComponent<T>, separator: string, )
| 292 | * This requires the parser to succeed at least once. |
| 293 | */ |
| 294 | function join1<T>( |
| 295 | parser: ParserComponent<T>, |
| 296 | separator: string, |
| 297 | ): ParserComponent<T[]> { |
| 298 | const Separator = character(separator); |
| 299 | return (scanner: Scanner): ParseResult<T[]> => { |
| 300 | const first = parser(scanner); |
| 301 | if (!first.ok) return failure(); |
| 302 | const out: T[] = [first.body]; |
| 303 | while (!scanner.eof()) { |
| 304 | if (!Separator(scanner).ok) break; |
| 305 | const result = parser(scanner); |
| 306 | if (!result.ok) { |
| 307 | throw new SyntaxError(`Invalid token after "${separator}"`); |
| 308 | } |
| 309 | out.push(result.body); |
| 310 | } |
| 311 | return success(out); |
| 312 | }; |
| 313 | } |
| 314 | |
| 315 | function kv<T>( |
| 316 | keyParser: ParserComponent<string[]>, |