From b20f2a25a523794de68fb4416e885697b2d46dfb Mon Sep 17 00:00:00 2001 From: Vladislav Mamon Date: Thu, 5 Jan 2023 13:34:06 +0300 Subject: [PATCH] fix(parsers/oneOf): halt at reaching eoi --- src/__tests__/parsers/oneOf.spec.ts | 7 ++++++- src/parsers/oneOf.ts | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/__tests__/parsers/oneOf.spec.ts b/src/__tests__/parsers/oneOf.spec.ts index a953bda..9a2efae 100644 --- a/src/__tests__/parsers/oneOf.spec.ts +++ b/src/__tests__/parsers/oneOf.spec.ts @@ -1,4 +1,5 @@ -import { oneOf } from '@parsers' +import { sequence } from '@combinators' +import { string, oneOf } from '@parsers' import { run, result, should, describe, testFailure, it } from '@testing' describe('oneOf', () => { @@ -12,4 +13,8 @@ describe('oneOf', () => { it('should fail if input character is not among given ones', () => { testFailure('q-combinator', oneOf('xyz')) }) + + it('should fail if reached the end of input', () => { + testFailure('prefix', sequence(string('prefix'), oneOf('XY'))) + }) }) diff --git a/src/parsers/oneOf.ts b/src/parsers/oneOf.ts index a773bed..477bc30 100644 --- a/src/parsers/oneOf.ts +++ b/src/parsers/oneOf.ts @@ -12,6 +12,14 @@ export function oneOf(chars: string): Parser { return { parse(input, pos) { + if (input.length === pos) { + return { + isOk: false, + pos, + expected: 'oneOf @ reached the end of input' + } + } + const nextPos = pos + 1 const char = input.substring(pos, nextPos)