Skip to content

Commit

Permalink
fix(combinators/sepBy): do not mutate position on no match (#79)
Browse files Browse the repository at this point in the history
This fix is kindly provided by @gilbert. Thank you for your
contribution!

Closes #77.
  • Loading branch information
norskeld authored Jun 4, 2023
1 parent 34959df commit baf8c16
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/__tests__/combinators/sepBy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sepBy, sepBy1 } from '@combinators'
import { sepBy, sepBy1, sequence } from '@combinators'
import { string } from '@parsers'
import { run, result, should, describe, it } from '@testing'

Expand Down Expand Up @@ -26,6 +26,14 @@ describe('sepBy', () => {

should.matchState(actual, expected)
})

it('should successfully continue if nothing matched', () => {
const parser = sequence(sepBy(string('hello'), string('?')), sepBy(string('bye'), string('?')))
const actual = run(parser, 'bye?bye?')
const expected = result(true, [[], ['bye', 'bye']])

should.matchState(actual, expected)
})
})

describe('sepBy1', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/combinators/sepBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export function sepBy<T, S>(parser: Parser<T>, sep: Parser<S>): Parser<Array<T>>

return {
isOk: true,
span: [pos, resultP.pos],
pos: resultP.pos,
span: [pos, pos],
pos,
value: []
}
}
Expand Down

1 comment on commit baf8c16

@vercel
Copy link

@vercel vercel bot commented on baf8c16 Jun 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.