From ae8d6ec60ce9e1975aa6bd03e258a351e73b8560 Mon Sep 17 00:00:00 2001 From: Vladislav Mamon Date: Fri, 26 Nov 2021 15:46:00 +0500 Subject: [PATCH] feat(parsers/newline): add `newline` parser --- src/internal/parsers/newline.ts | 14 ++++++++ src/parsers.ts | 1 + tests/internal/parsers/newline.spec.ts | 45 ++++++++++++++++++++++++++ tests/parsers.spec.ts | 2 ++ 4 files changed, 62 insertions(+) create mode 100644 src/internal/parsers/newline.ts create mode 100644 tests/internal/parsers/newline.spec.ts diff --git a/src/internal/parsers/newline.ts b/src/internal/parsers/newline.ts new file mode 100644 index 0000000..c74b276 --- /dev/null +++ b/src/internal/parsers/newline.ts @@ -0,0 +1,14 @@ +import { Parser } from '../state' + +import { choice } from '../combinators/choice' +import { error } from '../combinators/error' +import { string } from './string' + +const EOL_UNIX = '\n' +const EOL_NON_UNIX = '\r\n' + +export function newline(): Parser { + return error(choice(string(EOL_UNIX), string(EOL_NON_UNIX)), 'newline') +} + +export { newline as eol } diff --git a/src/parsers.ts b/src/parsers.ts index 1f3d9ee..65d5e68 100644 --- a/src/parsers.ts +++ b/src/parsers.ts @@ -1,5 +1,6 @@ export * from './internal/parsers/defer' export * from './internal/parsers/eof' +export * from './internal/parsers/newline' export * from './internal/parsers/float' export * from './internal/parsers/integer' export * from './internal/parsers/lazy' diff --git a/tests/internal/parsers/newline.spec.ts b/tests/internal/parsers/newline.spec.ts new file mode 100644 index 0000000..3ac680b --- /dev/null +++ b/tests/internal/parsers/newline.spec.ts @@ -0,0 +1,45 @@ +import { sequence } from '@lib/internal/combinators/sequence' +import { newline } from '@lib/internal/parsers/newline' +import { letters } from '@lib/internal/parsers/letter' + +import { run, result, should } from '@tests/@helpers' + +const tcase = `Hello\nWorld\n` +const tcaseLit = `Hello +World +` + +describe(newline, () => { + it('should succeed if given a newline (Unix)', () => { + const actual = run(newline(), '\n') + const expected = result('success', '\n') + + should.matchState(actual, expected) + }) + + it('should succeed if given a newline sequence (non-Unix)', () => { + const actual = run(newline(), '\r\n') + const expected = result('success', '\r\n') + + should.matchState(actual, expected) + }) + + it('should succeed if given a string with a newline at the end', () => { + const parser = sequence(letters(), newline(), letters(), newline()) + + const actualExplicit = run(parser, tcase) + const actualImplicit = run(parser, tcaseLit) + + const expected = result('success', ['Hello', '\n', 'World', '\n']) + + should.matchState(actualExplicit, expected) + should.matchState(actualImplicit, expected) + }) + + it('should fail if given a string without a newline', () => { + const actual = run(sequence(letters(), newline()), 'Hello') + const expected = result('failure', 'newline') + + should.matchState(actual, expected) + }) +}) diff --git a/tests/parsers.spec.ts b/tests/parsers.spec.ts index 57a362c..0c89e96 100644 --- a/tests/parsers.spec.ts +++ b/tests/parsers.spec.ts @@ -15,6 +15,8 @@ it('should expose parsers', () => { 'letters', 'nothing', 'nil', + 'newline', + 'eol', 'regexp', 're', 'rest',