sub_eof
This commit is contained in:
parent
d327b943f9
commit
15f4983fe2
@ -16,16 +16,16 @@ import Control.Plus (empty)
|
|||||||
|
|
||||||
import Parser
|
import Parser
|
||||||
|
|
||||||
-- From RFC 1035: <let-dig> ::= <letter> | <digit>
|
-- | From RFC 1035: <let-dig> ::= <letter> | <digit>
|
||||||
let_dig :: Parser Char
|
let_dig :: Parser Char
|
||||||
let_dig = alphanum
|
let_dig = alphanum
|
||||||
|
|
||||||
-- From RFC 1035: <let-dig-hyp> ::= <let-dig> | "-"
|
-- | From RFC 1035: <let-dig-hyp> ::= <let-dig> | "-"
|
||||||
-- Either a Letter, Digital or an Hyphenation character.
|
-- | Either a Letter, Digital or an Hyphenation character.
|
||||||
let_dig_hyp :: Parser Char
|
let_dig_hyp :: Parser Char
|
||||||
let_dig_hyp = let_dig <|> char '-'
|
let_dig_hyp = let_dig <|> char '-'
|
||||||
|
|
||||||
-- From RFC 1035: <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
|
-- | From RFC 1035: <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
|
||||||
ldh_str :: Parser (Array Char)
|
ldh_str :: Parser (Array Char)
|
||||||
ldh_str = many1 let_dig_hyp
|
ldh_str = many1 let_dig_hyp
|
||||||
|
|
||||||
@ -33,6 +33,10 @@ ldh_str = many1 let_dig_hyp
|
|||||||
label_maxsize :: Int
|
label_maxsize :: Int
|
||||||
label_maxsize = 7
|
label_maxsize = 7
|
||||||
|
|
||||||
|
-- TODO: 255?
|
||||||
|
max_domain_length :: Int
|
||||||
|
max_domain_length = 15
|
||||||
|
|
||||||
last_char :: String -> Maybe Char
|
last_char :: String -> Maybe Char
|
||||||
last_char = A.last <<< CU.toCharArray
|
last_char = A.last <<< CU.toCharArray
|
||||||
|
|
||||||
@ -54,7 +58,7 @@ try p = Parser p'
|
|||||||
Nothing -> Just (Tuple mempty str) -- FIXME! Need a better base structure.
|
Nothing -> Just (Tuple mempty str) -- FIXME! Need a better base structure.
|
||||||
Just x -> pure x
|
Just x -> pure x
|
||||||
|
|
||||||
-- From RFC 1035: <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
|
-- | From RFC 1035: <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
|
||||||
label :: Parser String
|
label :: Parser String
|
||||||
label = do
|
label = do
|
||||||
l <- letter
|
l <- letter
|
||||||
@ -66,7 +70,7 @@ label = do
|
|||||||
then empty
|
then empty
|
||||||
else pure labelstr
|
else pure labelstr
|
||||||
|
|
||||||
-- From RFC 1035: <subdomain> ::= <label> | <subdomain> "." <label>
|
-- | From RFC 1035: <subdomain> ::= <label> | <subdomain> "." <label>
|
||||||
subdomain :: Parser String
|
subdomain :: Parser String
|
||||||
subdomain = do
|
subdomain = do
|
||||||
-- First: read a label. This is bare minimum for a subdomain.
|
-- First: read a label. This is bare minimum for a subdomain.
|
||||||
@ -79,26 +83,30 @@ subdomain = do
|
|||||||
then pure lab
|
then pure lab
|
||||||
else pure $ lab <> "." <> upperlabels
|
else pure $ lab <> "." <> upperlabels
|
||||||
|
|
||||||
---- -- Second: the rest is optional.
|
-- | Test for the end-of-file (no more input).
|
||||||
---- r <- PC.optionMaybe (PC.try point_sub)
|
-- | If not EOF the parser fails (Nothing), otherwise it provides an empty string.
|
||||||
---- case r of
|
eof :: Parser String
|
||||||
---- Nothing -> pure lab
|
eof = Parser \str -> case S.length str of
|
||||||
---- Just sub -> pure $ lab <> sub
|
0 -> Just (Tuple "" str)
|
||||||
----
|
_ -> Nothing -- this means an error
|
||||||
---- where
|
|
||||||
---- point_sub :: Parser String
|
-- | Test for the domain to be a list of subdomains then an end-of-file.
|
||||||
---- point_sub = do
|
-- | Said otherwise, the input must only contain a domain (with or without a final dot '.').
|
||||||
---- point <- char '.'
|
sub_eof :: Parser String
|
||||||
---- sub <- defer \_ -> subdomain
|
sub_eof = do
|
||||||
---- pure $ CU.singleton point <> sub
|
sub <- subdomain
|
||||||
|
maybe_final_point <- char '.' <|> pure ' '
|
||||||
|
_ <- eof -- In case there is still some input, it fails.
|
||||||
|
let parsed_domain = did_we_parsed_the_final_point maybe_final_point sub
|
||||||
|
if S.length parsed_domain > max_domain_length
|
||||||
|
then empty -- TODO: error management
|
||||||
|
else pure parsed_domain
|
||||||
|
where
|
||||||
|
did_we_parsed_the_final_point '.' sub = sub <> "."
|
||||||
|
did_we_parsed_the_final_point _ sub = sub
|
||||||
|
|
||||||
{-
|
{-
|
||||||
import Control.Lazy (defer)
|
|
||||||
import Data.Array.NonEmpty as NonEmpty
|
|
||||||
import Data.Either (Either(..))
|
import Data.Either (Either(..))
|
||||||
import Data.Maybe (Maybe(..))
|
|
||||||
import Data.String as S -- length
|
|
||||||
import Data.String.CodeUnits as CU
|
|
||||||
-- import Data.String.Regex as R
|
-- import Data.String.Regex as R
|
||||||
-- import Data.String.Regex.Flags as RF
|
-- import Data.String.Regex.Flags as RF
|
||||||
import Data.Tuple (Tuple(..))
|
import Data.Tuple (Tuple(..))
|
||||||
@ -118,18 +126,6 @@ import Parsing.String (char, string, eof)
|
|||||||
domain :: Parser String String
|
domain :: Parser String String
|
||||||
domain = PC.try (string " ") <|> sub_eof
|
domain = PC.try (string " ") <|> sub_eof
|
||||||
|
|
||||||
sub_eof :: Parser String String
|
|
||||||
sub_eof = do
|
|
||||||
sub <- subdomain
|
|
||||||
maybe_final_point <- PC.optionMaybe (char '.')
|
|
||||||
eof
|
|
||||||
let parsed_domain = did_we_parsed_the_final_point maybe_final_point sub
|
|
||||||
if S.length parsed_domain > 255
|
|
||||||
then fail $ "domain length is > 255 bytes (" <> show (S.length parsed_domain) <> ")"
|
|
||||||
else pure parsed_domain
|
|
||||||
where
|
|
||||||
did_we_parsed_the_final_point Nothing sub = sub
|
|
||||||
did_we_parsed_the_final_point (Just _) sub = sub <> "."
|
|
||||||
|
|
||||||
-- | Converting a single letter parser to a String parser.
|
-- | Converting a single letter parser to a String parser.
|
||||||
char_to_string :: Parser String Char -> Parser String String
|
char_to_string :: Parser String Char -> Parser String String
|
||||||
|
@ -95,6 +95,29 @@ main = do
|
|||||||
logtest "subdomain" subdomain "-" id
|
logtest "subdomain" subdomain "-" id
|
||||||
logtest "subdomain" subdomain "a-" id
|
logtest "subdomain" subdomain "a-" id
|
||||||
|
|
||||||
|
log ""
|
||||||
|
|
||||||
|
logtest "sub_eof" sub_eof "example.org" id
|
||||||
|
logtest "sub_eof" sub_eof "" id
|
||||||
|
logtest "sub_eof" sub_eof "a.x" id
|
||||||
|
logtest "sub_eof" sub_eof "a2.org" id
|
||||||
|
logtest "sub_eof" sub_eof "a33.org" id
|
||||||
|
logtest "sub_eof" sub_eof "a444.org" id
|
||||||
|
logtest "sub_eof" sub_eof "a5555.org" id
|
||||||
|
logtest "sub_eof" sub_eof "a66666.org" id
|
||||||
|
logtest "sub_eof" sub_eof "a777777.org" id
|
||||||
|
logtest "sub_eof" sub_eof "a8888888.org" id
|
||||||
|
logtest "sub_eof" sub_eof "xblah.a.x" id
|
||||||
|
logtest "sub_eof" sub_eof "xblah.a2.org" id
|
||||||
|
logtest "sub_eof" sub_eof "xblah.a33.org" id
|
||||||
|
logtest "sub_eof" sub_eof "xblah.a444.org" id
|
||||||
|
logtest "sub_eof" sub_eof "xblah.a5555.org" id
|
||||||
|
logtest "sub_eof" sub_eof "xblah.a66666.org" id
|
||||||
|
logtest "sub_eof" sub_eof "xblah.a777777.org" id
|
||||||
|
logtest "sub_eof" sub_eof "xblah.a8888888.org" id
|
||||||
|
logtest "sub_eof" sub_eof "-" id
|
||||||
|
logtest "sub_eof" sub_eof "a-" id
|
||||||
|
|
||||||
-- log $ "parsing the first 'f' in 'fiction' (sat): " <> case parse (sat (\x -> x == 'f')) "fiction" of
|
-- log $ "parsing the first 'f' in 'fiction' (sat): " <> case parse (sat (\x -> x == 'f')) "fiction" of
|
||||||
-- Just (Tuple x y) -> show x <> " " <> show y
|
-- Just (Tuple x y) -> show x <> " " <> show y
|
||||||
-- Nothing -> "failed"
|
-- Nothing -> "failed"
|
||||||
|
Loading…
Reference in New Issue
Block a user