From 411f97c6c7085e739d756fffaf3c215f859f1280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Lichtenth=C3=A4ler?= Date: Sun, 7 Aug 2022 16:54:17 +0200 Subject: [PATCH] refactor: implicitly return `Parser` instances (#165) #164 --- src/combinators/optional.ts | 5 ++--- src/parsers/failure.ts | 5 ++--- src/parsers/lazy.ts | 5 ++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/combinators/optional.ts b/src/combinators/optional.ts index f20c4cf..ab0cf99 100644 --- a/src/combinators/optional.ts +++ b/src/combinators/optional.ts @@ -7,8 +7,8 @@ import { updateParserResult } from "../ParserState"; * @returns * @see https://rudus.pages.dev/docs/api/combinators/optional */ -export const optional = (parser: Parser): Parser => { - return new Parser(currentState => { +export const optional = (parser: Parser): Parser => + new Parser(currentState => { // Try to transform the current state with the given parser. const nextState = parser.transformState(currentState); @@ -18,4 +18,3 @@ export const optional = (parser: Parser): Parser => { // If the given parser can match return the transformed state. return nextState; }); -}; diff --git a/src/parsers/failure.ts b/src/parsers/failure.ts index 4548eda..e43f55c 100644 --- a/src/parsers/failure.ts +++ b/src/parsers/failure.ts @@ -6,9 +6,8 @@ import { updateParserError } from "../ParserState"; * @param errorMessage * @returns */ -export const failure = (errorMessage: string) => { - return new Parser(state => { +export const failure = (errorMessage: string) => + new Parser(state => { if (state.isError) return state; return updateParserError(state, errorMessage); }); -}; diff --git a/src/parsers/lazy.ts b/src/parsers/lazy.ts index ab3191f..f92ae40 100644 --- a/src/parsers/lazy.ts +++ b/src/parsers/lazy.ts @@ -6,8 +6,7 @@ import { Parser } from "../Parser"; * @returns * @see https://rudus.pages.dev/docs/api/parsers/lazy */ -export const lazy = (parserThunk: () => Parser): Parser => { - return new Parser(state => { +export const lazy = (parserThunk: () => Parser): Parser => + new Parser(state => { return parserThunk().transformState(state); }); -};