From 0ce58d6f9e5eb4c46d682ecf4c85804bbd1f1a0a Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Wed, 10 Jan 2024 10:02:14 +0100 Subject: [PATCH] Add grammer definition --- elm.json | 1 + src/Internal/Tools/Grammar.elm | 74 ++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/Internal/Tools/Grammar.elm diff --git a/elm.json b/elm.json index 78baf2d..6b8e2db 100644 --- a/elm.json +++ b/elm.json @@ -13,6 +13,7 @@ "dependencies": { "elm/core": "1.0.0 <= v < 2.0.0", "elm/json": "1.0.0 <= v < 2.0.0", + "elm/parser": "1.0.0 <= v < 2.0.0", "elm/time": "1.0.0 <= v < 2.0.0", "miniBill/elm-fast-dict": "1.0.0 <= v < 2.0.0" }, diff --git a/src/Internal/Tools/Grammar.elm b/src/Internal/Tools/Grammar.elm new file mode 100644 index 0000000..85db0d4 --- /dev/null +++ b/src/Internal/Tools/Grammar.elm @@ -0,0 +1,74 @@ +module Internal.Tools.Grammar exposing (..) + +{-| + + +# Identifier Grammar + +The specification defines +[some identifiers](https://spec.matrix.org/v1.9/appendices/#identifier-grammar) +to use the Common Namespaced Identifier Grammar. This is a common grammar +intended for non-user-visible identifiers, with a defined mechanism for +implementations to create new identifiers. + +This module documents those identifiers, allowing the Elm SDK to use them. + +-} + +import Parser as P exposing (Parser) + + +{-| Parse an IPv6 address +-} +ipv6addressParser : Parser String +ipv6addressParser = + P.chompWhile validIPv6Char + |> P.getChompedString + |> P.andThen + (\out -> + if String.length out > 45 then + P.problem "an ipv6 address has no more than 45 digits" + + else if String.length out < 2 then + P.problem "an ipv6 address has at least 2 digits" + + else + -- TODO: ipv6 has more specific rules + -- https://datatracker.ietf.org/doc/html/rfc3513#section-2.2 + P.succeed out + ) + + +{-| Parse a port value +-} +portParser : Parser Int +portParser = + P.chompWhile Char.isDigit + |> P.getChompedString + |> P.andThen + (\out -> + if String.length out > 5 then + P.problem "a port has no more than 5 digits" + + else if String.length out < 1 then + P.problem "a port has at least 1 digit" + + else + case String.toInt out of + Nothing -> + P.problem "Expected port int" + + Just i -> + P.succeed i + ) + + +{-| Check whether a char is a valid IPv6char +-} +validIPv6char : Char -> Bool + + +validIPv6Char c = + "0123456789ABCDEFabcdef:." + |> String.toList + |> List.member c