Join the parse results of the given parser into an array. * * If the parser fails at the first attempt, it will return an empty array.
( parser: ParserComponent<T>, separator: string, )
| 266 | * If the parser fails at the first attempt, it will return an empty array. |
| 267 | */ |
| 268 | function join<T>( |
| 269 | parser: ParserComponent<T>, |
| 270 | separator: string, |
| 271 | ): ParserComponent<T[]> { |
| 272 | const Separator = character(separator); |
| 273 | return (scanner: Scanner): ParseResult<T[]> => { |
| 274 | const out: T[] = []; |
| 275 | const first = parser(scanner); |
| 276 | if (!first.ok) return success(out); |
| 277 | out.push(first.body); |
| 278 | while (!scanner.eof()) { |
| 279 | if (!Separator(scanner).ok) break; |
| 280 | const result = parser(scanner); |
| 281 | if (!result.ok) { |
| 282 | throw new SyntaxError(`Invalid token after "${separator}"`); |
| 283 | } |
| 284 | out.push(result.body); |
| 285 | } |
| 286 | return success(out); |
| 287 | }; |
| 288 | } |
| 289 | |
| 290 | /** Join the parse results of the given parser into an array. |
| 291 | * |
no test coverage detected