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(parsers/whitespace): add
whitespace
& whitespaceOptional
par…
…sers
- Loading branch information
Showing
4 changed files
with
104 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Parser } from '../state' | ||
|
||
import { regexp } from './regexp' | ||
|
||
const WHITESPACE_REQUIRED_RE = /\s+/g | ||
const WHITESPACE_OPTIONAL_RE = /\s*/g | ||
|
||
export function whitespace(): Parser<string> { | ||
return regexp(WHITESPACE_REQUIRED_RE, 'whitespace') | ||
} | ||
|
||
export function whitespaceOptional(): Parser<string> { | ||
return regexp(WHITESPACE_OPTIONAL_RE, 'optional whitespace') | ||
} | ||
|
||
export { whitespace as ws, whitespaceOptional as wsOpt } |
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,82 @@ | ||
import { whitespace, whitespaceOptional } from '@lib/internal/parsers/whitespace' | ||
import { sequence } from '@lib/internal/combinators/sequence' | ||
import { string } from '@lib/internal/parsers/string' | ||
import { regexp } from '@lib/internal/parsers/regexp' | ||
|
||
import { run, result, should } from '@tests/@helpers' | ||
|
||
describe(whitespace, () => { | ||
it('should succeed if given a string of spaces', () => { | ||
const space = ' ' | ||
|
||
const actual = run(whitespace(), space.repeat(4)) | ||
const expected = result('success', space.repeat(4)) | ||
|
||
should.matchState(actual, expected) | ||
}) | ||
|
||
it('should succeed if given a string starting with spaces', () => { | ||
const space = ' ' | ||
|
||
const actual = run(whitespace(), space.repeat(4) + 'const') | ||
const expected = result('success', space.repeat(4)) | ||
|
||
should.matchState(actual, expected) | ||
}) | ||
|
||
it('should succeed if given a mixed string with spaces', () => { | ||
const identifier = regexp(/\w+/g, 'identifier') | ||
const keyword = string('let') | ||
const ws = whitespace() | ||
|
||
const actual = run(sequence(keyword, ws, identifier), 'let identity') | ||
const expected = result('success', ['let', ' ', 'identity']) | ||
|
||
should.matchState(actual, expected) | ||
}) | ||
|
||
it('should fail if given a non-matching input', () => { | ||
const actual = run(sequence(string('let'), whitespace(), string('rec')), 'letrec') | ||
const expected = result('failure', 'whitespace') | ||
|
||
should.matchState(actual, expected) | ||
}) | ||
}) | ||
|
||
describe(whitespaceOptional, () => { | ||
it('should succeed if given a mixed string with spaces', () => { | ||
const letKeyword = string('let') | ||
const identifier = regexp(/\w+/g, 'identifier') | ||
const wsOptional = whitespaceOptional() | ||
|
||
const parser = sequence(letKeyword, wsOptional, identifier) | ||
|
||
const actual = run(parser, 'let identity') | ||
const expected = result('success', ['let', ' ', 'identity']) | ||
|
||
should.matchState(actual, expected) | ||
}) | ||
|
||
it('should succeed if given a mixed string with optional spaces', () => { | ||
const letKeyword = string('let') | ||
const equalKeyword = string('=') | ||
const identifier = regexp(/\w+/g, 'identifier') | ||
const wsOptional = whitespaceOptional() | ||
const wsRequired = whitespace() | ||
|
||
const parser = sequence( | ||
letKeyword, | ||
wsRequired, | ||
identifier, | ||
wsOptional, | ||
equalKeyword, | ||
wsOptional, | ||
identifier | ||
) | ||
|
||
const actual = run(parser, 'let identity=something') | ||
const expected = result('success', ['let', ' ', 'identity', '', '=', '', 'something']) | ||
|
||
should.matchState(actual, expected) | ||
}) | ||
}) |
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