4.1. Grammar
The accepted syntax for IP-address literals is:
grammar IPNet ::= V4Net | V6Net -- V4 is tried first; a string is V6 only -- if it is not accepted as V4 V4Net ::= V4Addr ['/' V4Prefix] V4Addr ::= NumV4 '.' NumV4 '.' NumV4 '.' NumV4 NumV4 ::= Digit{1,3} V4Prefix ::= Digit{1,2} V6Net ::= V6Addr ['/' V6Prefix] V6Addr ::= H16 (':' H16){7} -- 8 groups, no '::' | [H16 (':' H16)*] '::' [H16 (':' H16)*] -- one '::', sides total < 8 H16 ::= HexDigit{1,4} V6Prefix ::= Digit{1,3} Digit ::= '0' | '1' | … | '9' HexDigit ::= Digit | 'a'…'f' | 'A'…'F' value value(V4Net) = the IPv4 address whose four octets are the NumV4 values, with prefix nat(V4Prefix) (32 when '/' is absent) value(V6Net) = the IPv6 address whose eight hextets are the H16 hex values (the '::' gap expanding to the missing zero hextets), with prefix nat(V6Prefix) (128 when '/' is absent) constraints - each NumV4 ≤ 255, V4Prefix ≤ 32 - each H16 ≤ 0xffff (automatic from ≤ 4 hex digits), V6Prefix ≤ 128 - a numeric group or prefix may not have a leading zero unless it is exactly "0" (`str.startsWith "0" → str = "0"`) - '::' appears at most once; it stands for one or more all-zero H16 groups (the two sides must total strictly fewer than 8 groups)
A string is valid if and only if it satisfies the grammar and constraints above. The ip() constructor tries the IPv4 grammar first and only falls through to IPv6 when the IPv4 parse fails; the two grammars accept disjoint sets of strings.
4.1.1. Relationship to the IETF text representation
Cedar's grammar aligns with the IETF draft Text Representation of IPv4 and IPv6 Addresses (draft-main-ipaddr-text-rep-00) on the numeric-token rules — IPv4 octets 0–255 with no leading zeros, IPv6 H16 groups of one to four case-insensitive hex digits, and :: used at most once for a run of one or more zero groups. It deliberately differs in two ways:
-
No embedded IPv4 in IPv6. The IETF
ls32production makes forms like::ffff:1.2.3.4canonical; Cedar rejects them — every IPv6 group must be pure hexadecimal (soip("::ffff:127.0.0.1")is invalid). -
CIDR prefix. The IETF grammar covers only bare addresses; Cedar extends every address with an optional
'/'-prefix suffix.
The address-generation recommendations of the IETF draft (lowercase, omit leading zeros, and
elide the longest zero run) are not parser constraints. Cedar's canonical toString representation
instead uses fixed-width lowercase hextets and does not elide zero runs.
