From efe41a0d3fcf6a215bc001a2a283c1c7bcdefd67 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Thu, 18 Jan 2024 04:51:50 +0100 Subject: [PATCH] Big improvement on the base structure type. --- src/DomainParser.purs | 48 ++++++++++++++-------------- src/Parser.purs | 74 +++++++++++++++++++++++++------------------ 2 files changed, 69 insertions(+), 53 deletions(-) diff --git a/src/DomainParser.purs b/src/DomainParser.purs index 09c0ecd..be6d495 100644 --- a/src/DomainParser.purs +++ b/src/DomainParser.purs @@ -12,14 +12,17 @@ import Data.String.CodeUnits as CU import Control.Alt ((<|>)) import Control.Plus (empty) +import Parser (Parser(..), Position + , success, fail, failError + , alphanum, char, letter, many1, parse, string) + +type Size = Int data DomainError = SubdomainTooLarge Position Size | DomainTooLarge Size | InvalidCharacter Position | EOFExpected -import Parser (Parser(..), success, fail, failError, alphanum, char, letter, many1, parse, string) - -- | From RFC 1035: ::= | let_dig :: forall e. Parser e Char let_dig = alphanum @@ -47,7 +50,7 @@ last_char = A.last <<< CU.toCharArray parse_last_char :: forall e. String -> Parser e Char -> Boolean parse_last_char s p = case last_char s of Nothing -> false - Just c -> case parse p (CU.singleton c) of + Just c -> case parse p { string: CU.singleton c, position: 0 } of Left _ -> false _ -> true @@ -56,21 +59,20 @@ parse_last_char s p = case last_char s of -- | the code will "continue to work" but without what's been parsed. -- | This may sound reasonable but it prevents knowing if a problem actually occured! -- | We cannot do a `try parser <|> alternative` since it will always work! -try :: forall e a. Parser e a -> Parser e a +try :: forall e a. Parser e a -> Parser e (Maybe a) try p = Parser p' - where p' str = case parse p str of - Left { pos: _, input: _, error: e } -> case e of - Nothing -> fail 0 str - error -> failError 0 str error - right -> right - -- Right { pos: pos, input: input, result: value } -> success pos input value + where p' input = case parse p input of + Left _ -> Right { suffix: input, result: Nothing } + Right { suffix, result } -> Right { suffix, result: Just result } -- | From RFC 1035: