From 7148fc936fddac9d6b8ef0fceb013c9512a01ddd Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Thu, 18 Jan 2024 21:28:32 +0100 Subject: [PATCH] Minor code simplification. --- src/DomainParser.purs | 8 +++---- src/Main.purs | 49 ------------------------------------------- src/Parser.purs | 9 ++++---- 3 files changed, 8 insertions(+), 58 deletions(-) diff --git a/src/DomainParser.purs b/src/DomainParser.purs index f39118f..389fd83 100644 --- a/src/DomainParser.purs +++ b/src/DomainParser.purs @@ -83,7 +83,7 @@ label = do Just l -> do s <- tryMaybe ldh_str lastpos <- current_position - let labelstr = CU.singleton l <> maybe "" (\v -> CU.fromCharArray v) s + let labelstr = CU.singleton l <> maybe "" CU.fromCharArray s if (S.length labelstr > label_maxsize) then Parser \_ -> failError pos (Just <<< SubdomainTooLarge $ S.length labelstr) else if (S.length labelstr > 1 && not (parse_last_char labelstr let_dig)) @@ -118,13 +118,13 @@ sub_eof = do maybe_final_point <- tryMaybe $ char '.' _ <- eof -- In case there is still some input, it fails. pos <- current_position - let parsed_domain = did_we_parsed_the_final_point maybe_final_point sub + let parsed_domain = did_we_parse_the_final_point maybe_final_point sub if S.length parsed_domain > max_domain_length then Parser \_ -> failError pos (Just <<< DomainTooLarge $ S.length parsed_domain) else pure parsed_domain where - did_we_parsed_the_final_point Nothing sub = sub - did_we_parsed_the_final_point _ sub = sub <> "." + did_we_parse_the_final_point Nothing sub = sub + did_we_parse_the_final_point _ sub = sub <> "." -- | From RFC 1035: ::= | " " -- | diff --git a/src/Main.purs b/src/Main.purs index ad3d723..25b58df 100644 --- a/src/Main.purs +++ b/src/Main.purs @@ -153,52 +153,3 @@ main = do logtest "domain" domain "xblah.a8888888.org" id showerror logtest "domain" domain "-" id showerror logtest "domain" domain "a-" id showerror - --- log $ "parsing the first 'f' in 'fiction' (sat): " <> case parse (sat (\x -> x == 'f')) "fiction" of --- Just (Tuple x y) -> show x <> " " <> show y --- Nothing -> "failed" --- --- log $ "parsing 'fic' in 'fiction' (string): " <> case parse (string "fic") "fiction" of --- Just (Tuple x y) -> show x <> " " <> show y --- Nothing -> "failed" --- --- log $ "parsing ident (all first alphanum) in 'ab123-blah' (ident): " <> --- case parse ident "ab123-blah" of --- Just (Tuple x y) -> show x <> " " <> show y --- Nothing -> "failed" --- --- log $ "parsing integer in '-19ab' (integer): " <> --- case parse integer "-19ab" of --- Just (Tuple x y) -> show x <> " " <> show y --- Nothing -> "failed" - --- JUST WORKS --- isffound $ parse isf "fable" --- isffound $ parse isf "f" --- isffound $ parse isf "n" --- isffound $ parse isf "" --- --- isffound $ parse isf2 "fable" --- isffound $ parse isf2 "f" --- isffound $ parse isf2 "n" --- isffound $ parse isf2 "" - --- log $ "parsing 'hi': " <> case parse ishi2 "hi" of --- Just (Tuple x y) -> show x <> " " <> show y --- Nothing -> "coudn't parse two letters" --- --- log $ "parsing 'no': " <> case parse ishi2 "no" of --- Just (Tuple x y) -> show x <> " " <> show y --- Nothing -> "coudn't parse two letters" --- --- log $ "parsing 'ho': " <> case parse ishi2 "ho" of --- Just (Tuple x y) -> show x <> " " <> show y --- Nothing -> "coudn't parse two letters" --- --- log $ "parsing 'ni': " <> case parse ishi2 "ni" of --- Just (Tuple x y) -> show x <> " " <> show y --- Nothing -> "coudn't parse two letters" --- --- log $ "parsing '': " <> case parse ishi2 "" of --- Just (Tuple x y) -> show x <> " " <> show y --- Nothing -> "coudn't parse two letters" diff --git a/src/Parser.purs b/src/Parser.purs index 889f0cf..f24344e 100644 --- a/src/Parser.purs +++ b/src/Parser.purs @@ -15,8 +15,8 @@ import Data.String.CodeUnits (toCharArray, fromCharArray) import BaseFunctions (concat, isAlpha, isAlphaNum, isDigit, isLower, isSpace, isUpper) type Position = Int -type PosString = { string :: String, position :: Position } -type Input = PosString +type PositionString = { string :: String, position :: Position } +type Input = PositionString type Error e = { position :: Position, error :: Maybe e } type Value v = { result :: v, suffix :: Input } @@ -54,8 +54,7 @@ item = Parser p where p input = case A.uncons (toCharArray input.string) of Nothing -> fail input.position - Just { head: x, tail: xs } -> success (input { string = (fromCharArray xs) - , position = input.position+1 }) x + Just { head: x, tail: xs } -> success { string: (fromCharArray xs), position: input.position+1 } x instance functorParser :: Functor (Parser e) where map :: forall a b. (a -> b) -> Parser e a -> Parser e b @@ -93,7 +92,7 @@ instance altParser :: Alt (Parser e) where instance plusParser :: Plus (Parser e) where empty :: forall v. Parser e v - empty = Parser \_ -> fail 0 + empty = Parser \input -> fail input.position instance alternativeParser :: Alternative (Parser e)