Skip to content

Commit

Permalink
fix(parsers/oneOf): halt at reaching eoi
Browse files Browse the repository at this point in the history
  • Loading branch information
norskeld committed Jan 5, 2023
1 parent 992acee commit b20f2a2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/__tests__/parsers/oneOf.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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')))
})
})
8 changes: 8 additions & 0 deletions src/parsers/oneOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export function oneOf(chars: string): Parser<string> {

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)

Expand Down

0 comments on commit b20f2a2

Please sign in to comment.