Minor code simplification.

This commit is contained in:
Philippe Pittoli 2024-01-18 21:28:32 +01:00
parent f9923bab55
commit 7148fc936f
3 changed files with 8 additions and 58 deletions

View File

@ -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: <domain> ::= <subdomain> | " "
-- |

View File

@ -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"

View File

@ -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)