Skip to content

Commit

Permalink
feat(parsers/oneOf): add oneOf parser
Browse files Browse the repository at this point in the history
  • Loading branch information
norskeld committed Jul 4, 2022
1 parent dbe157a commit 6d6c2a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './parsers/float'
export * from './parsers/integer'
export * from './parsers/letter'
export * from './parsers/nothing'
export * from './parsers/oneOf'
export * from './parsers/regexp'
export * from './parsers/rest'
export * from './parsers/run'
Expand Down
26 changes: 26 additions & 0 deletions src/parsers/oneOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { type Parser } from '../state'

export function oneOf(chars: string): Parser<string> {
const charset = [...chars]

return {
parse(input, pos) {
const nextPos = pos + 1
const char = input.substring(pos, nextPos)

if (charset.includes(char)) {
return {
isOk: true,
pos: nextPos,
value: char
}
}

return {
isOk: false,
pos,
expected: `one of: ${charset.join(', ')}`
}
}
}
}

0 comments on commit 6d6c2a6

Please sign in to comment.