generated from norskeld/serpent
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(combinators/lookahead): add
lookahead
combinator
- Loading branch information
Showing
6 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
title: 'lookahead' | ||
kind: 'primitive' | ||
description: "lookahead combinator applies parser without consuming any input. If parser fails and consumes some input, so does lookahead." | ||
--- | ||
|
||
# {{ $frontmatter.title }} <Primitive /> | ||
|
||
## Signature | ||
|
||
```ts | ||
function lookahead<T>(parser: Parser<T>): Parser<T> | ||
``` | ||
|
||
## Description | ||
|
||
`lookahead` combinator applies `parser` without consuming any input. If `parser` fails and consumes some input, so does `lookahead`. | ||
|
||
## Usage | ||
|
||
The example is rather contrived, but it clearly illustrates how the combinator works, allowing one, for example, collect ambiguous results for further processing. | ||
|
||
```ts | ||
const Parser = sequence( | ||
takeLeft(string('hello'), whitespace()), | ||
lookahead(string('let')), | ||
string('lettuce') | ||
) | ||
``` | ||
|
||
::: tip Success | ||
```ts | ||
run(Parser).with('hello lettuce') | ||
{ | ||
isOk: true, | ||
pos: 13, | ||
value: [ 'hello', 'let', 'lettuce' ] | ||
} | ||
``` | ||
::: | ||
|
||
::: danger Failure | ||
```ts | ||
run(Parser).with('hello let') | ||
{ | ||
isOk: false, | ||
pos: 9, | ||
expected: 'lettuce' | ||
} | ||
``` | ||
::: | ||
|
||
::: danger Failure | ||
```ts | ||
run(Parser).with('hello something') | ||
{ | ||
isOk: false, | ||
pos: 9, | ||
expected: 'let' | ||
} | ||
``` | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { lookahead, sequence, takeLeft } from '@combinators' | ||
import { string, whitespace } from '@parsers' | ||
import { run, should, describe, it } from '@testing' | ||
|
||
describe('lookahead', () => { | ||
const parser = sequence( | ||
takeLeft(string('hello'), whitespace()), | ||
lookahead(string('let')), | ||
string('lettuce') | ||
) | ||
|
||
it('should successfully lookahead and return pos untouched', () => { | ||
const actual = run(parser, 'hello lettuce') | ||
|
||
should.beStrictEqual(actual, { | ||
isOk: true, | ||
pos: 13, | ||
value: ['hello', 'let', 'lettuce'] | ||
}) | ||
}) | ||
|
||
it('should correctly fail if placed before a failing parser (OOB check)', () => { | ||
const actual = run(parser, 'hello let') | ||
|
||
should.beStrictEqual(actual, { | ||
isOk: false, | ||
pos: 9, | ||
expected: 'lettuce' | ||
}) | ||
}) | ||
|
||
it('should correctly fail if given a failing parser (consuming check)', () => { | ||
const actual = run(parser, 'hello const') | ||
|
||
should.beStrictEqual(actual, { | ||
isOk: false, | ||
pos: 9, | ||
expected: 'let' | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { Parser } from '@types' | ||
|
||
/** | ||
* Applies `parser` without consuming any input. If `parser` fails and consumes some input, so does | ||
* `lookahead`. | ||
* | ||
* @param parser - Parser to apply | ||
* | ||
* @returns Result of `parser` | ||
*/ | ||
export function lookahead<T>(parser: Parser<T>): Parser<T> { | ||
return { | ||
parse(input, pos) { | ||
const result = parser.parse(input, pos) | ||
|
||
switch (result.isOk) { | ||
// If parser succeeded, keep the position untouched. | ||
case true: { | ||
return { | ||
isOk: true, | ||
pos, | ||
value: result.value | ||
} | ||
} | ||
|
||
// If the parser failed, then still advance the pos cursor. | ||
case false: { | ||
return result | ||
} | ||
} | ||
} | ||
} | ||
} |
497c7a8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
sigma – ./
sigma-pearl.vercel.app
sigma-norskeld.vercel.app
sigma-git-master-norskeld.vercel.app
sigma.vm.codes