From 3314add2fb26f129c630728607cf03c172af6fe5 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sun, 23 Jul 2023 14:06:09 +0200 Subject: [PATCH] Domain parsing is now almost complete. --- drop/PlayingWithParsers.purs | 60 +++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/drop/PlayingWithParsers.purs b/drop/PlayingWithParsers.purs index 24eb67b..89fa04e 100644 --- a/drop/PlayingWithParsers.purs +++ b/drop/PlayingWithParsers.purs @@ -5,13 +5,13 @@ import Prelude import Control.Alt ((<|>)) import Control.Lazy (defer) -import Data.Array (many, snoc, last) +import Data.Array (many, length, snoc, last) import Data.Either (Either(..)) import Data.Foldable (fold, foldl) import Data.Maybe (Maybe(..)) import Data.Array.NonEmpty as NonEmpty import Data.String.CodeUnits as CU --- import Data.String (joinWith) +import Data.String as S -- length -- import Data.String.Regex as R -- import Data.String.Regex.Flags as RF import Data.Tuple (Tuple(..)) @@ -65,9 +65,18 @@ render state [ Bulma.hero_danger "A simple input" "Nothing much to see" , Bulma.section_small $ [ h1 "Examples of domain parsing in Purescript" - ] <> test_domains [ "ex.net", "e-x.net", "e-.net", "-x.net", "truc-blah.example.com", "te.s-t.net", "example.com" ] + ] <> test_domains [ "ex.net" + , "e-x.net" + , "way-too-long--way-too-long--way-too-long--way-too-long--way-too-long.net" + , "way-too-long--way-too-long--way-too-long--way-too-long.way-too-long--way-too-long--way-too-long--way-too-long.way-too-long--way-too-long--way-too-long--way-too-long.way-too-long--way-too-long--way-too-long--way-too-long.way-too-long--way-too-long--way-too-long--way-too-long.way-too-long--way-too-long--way-too-long--way-too-long.way-too-long--way-too-long--way-too-long--way-too-long.way-too-long--way-too-long--way-too-long--way-too-long.way-too-long--way-too-long--way-too-long--way-too-long.way-too-long--way-too-long--way-too-long--way-too-long.way-too-long--way-too-long--way-too-long--way-too-long.way-too-long--way-too-long--way-too-long--way-too-long.net" + , "e-.net" + , "-x.net" + , "truc-blah.example.com" + , "te.s-t.net" + , "example.com" + ] , Bulma.section_small [ render_stuff ] - ] + ] where -- Some helpers. title = Bulma.h3 @@ -94,14 +103,14 @@ render state -- test_domains :: Array String -> _ test_domains doms = fold $ map tests_on_domain doms - + -- tests_on_domain :: String -> _ tests_on_domain d = [ Bulma.strong $ d <> " -> [ldhstr, label, subdomain, domain]" - , p $ d <> " : ldhstr : " <> (show $ runParser d ldhstr) - , p $ d <> " : label : " <> (show $ runParser d label) - -- , p $ show $ runParser d subdomain - --, p $ show $ runParser d domain + , p $ d <> " : ldhstr : " <> (show $ runParser d ldhstr) + , p $ d <> " : label : " <> (show $ runParser d label) + , p $ d <> " : subdomain : " <> (show $ runParser d subdomain) + , p $ d <> " : domain : " <> (show $ runParser d domain) ] aye :: Parser String Char @@ -123,37 +132,44 @@ parse_stuff = do -- From RFC 1035: ::= | " " domain :: Parser String String -domain = sub_eof <|> string " " +domain = PC.try (string " ") <|> sub_eof sub_eof :: Parser String String sub_eof = do - sub <- PC.try subdomain + sub <- subdomain + -- TODO: optional "." at the end? eof pure sub -- From RFC 1035: ::=