Skip to content

Commit

Permalink
ip wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Nov 27, 2024
1 parent 8e921ec commit 258163b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 18 deletions.
82 changes: 74 additions & 8 deletions src/iri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,94 @@ struct IHierPart {

struct Authority {
user_info: Option<String>,
host: Option<Host>,
host: Host,
port: Option<String>,
}

enum Host {
IPV4(Vec<u8>),
IPV6(Vec<u8>),
RegName(String),
IPV6(Vec<u16>),
RegName(Option<String>),
}

#[allow(unused)]
mod parser {
use nom::{
bytes::streaming::take_while1,
character::complete::anychar,
error::{ParseError, VerboseError},
multi::{fold_many1, many1},
multi::{fold_many0, fold_many1, many1},
};

use crate::prelude::*;

use super::{
ip::{self, parse_ip_v4, parse_ip_v6},
Authority, Host,
};

fn parse_authority(s: &str) -> ParserResult<Authority> {
map(
tuple((
opt(parse_userinfo),
parse_host,
opt(preceded(tag(":"), parse_port)),
)),
|(user_info, host, port)| Authority {
user_info,
host,
port: port.map(String::from),
},
)(s)
}

fn parse_host(s: &str) -> ParserResult<Host> {
alt((
map(parse_ip_v4, Host::IPV4),
map(parse_ip_v6, Host::IPV6),
map(opt(parse_i_reg_name), Host::RegName),
))(s)
}
fn parse_i_segmentnz(s: &str) -> ParserResult<String> {
fold_many0(parse_ip_char, String::new, |mut acc, item| {
acc.push_str(item);
acc
})(s)
}
fn parse_i_segment0(s: &str) -> ParserResult<String> {
fold_many0(parse_ip_char, String::new, |mut acc, item| {
acc.push_str(item);
acc
})(s)
}

fn parse_i_segmentnz_nc(s: &str) -> ParserResult<String> {
fold_many1(
alt((
parse_i_unreserved,
parse_pct_encoded,
parse_sub_delims,
tag("@"),
)),
String::new,
|mut acc, item| {
acc.push_str(item);
acc
},
)(s)
}
fn parse_ip_char(s: &str) -> ParserResult<&str> {
alt((
parse_i_unreserved,
parse_pct_encoded,
parse_sub_delims,
tag(":"),
tag("@"),
))(s)
}
fn parse_scheme(s: &str) -> ParserResult<&str> {
verify(
terminated(
take_while1(|c: char| c.is_alphanumeric() || c == '.' || c == '-' || c == '+'),
tag(":"),
),
take_while1(|c: char| c.is_alphanumeric() || c == '.' || c == '-' || c == '+'),
|scheme: &str| scheme.starts_with(|c: char| c.is_alphabetic()),
)(s)
}
Expand All @@ -133,6 +196,9 @@ mod parser {
},
)(s)
}
fn parse_port(s: &str) -> ParserResult<&str> {
take_while1(|p: char| p.is_numeric())(s)
}
fn parse_i_reg_name(s: &str) -> ParserResult<String> {
fold_many1(
alt((parse_pct_encoded, parse_i_unreserved, parse_sub_delims)),
Expand Down
25 changes: 15 additions & 10 deletions src/iri_spect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ FC 3987 Internationalized Resource Identifiers January 2005
/ ipath-noscheme
/ ipath-empty

iauthority = [ iuserinfo "@" ] ihost [ ":" port ]

ihost = IP-literal / IPv4address / ireg-name


ipath = ipath-abempty ; begins with "/" or is empty
Expand All @@ -21,14 +19,9 @@ FC 3987 Internationalized Resource Identifiers January 2005
ipath-rootless = isegment-nz *( "/" isegment )
ipath-empty = 0<ipchar>

isegment = *ipchar
isegment-nz = 1*ipchar
isegment-nz-nc = 1*( iunreserved / pct-encoded / sub-delims
/ "@" )
; non-zero-length segment without any colon ":"

ipchar = iunreserved / pct-encoded / sub-delims / ":"
/ "@"



iquery = *( ipchar / iprivate / "/" / "?" )

Expand All @@ -53,7 +46,6 @@ RFC 3987 Internationalized Resource Identifiers January 2005



port = *DIGIT

IP-literal = "[" ( IPv6address / IPvFuture ) "]"

Expand All @@ -75,6 +67,13 @@ RFC 3987 Internationalized Resource Identifiers January 2005


DONE:

ipchar = iunreserved / pct-encoded / sub-delims / ":"
/ "@"
iauthority = [ iuserinfo "@" ] ihost [ ":" port ]

port = *DIGIT

sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
/ "*" / "+" / "," / ";" / "="

Expand Down Expand Up @@ -111,3 +110,9 @@ ls32 = ( h16 ":" h16 ) / IPv4address

ireg-name = *( iunreserved / pct-encoded / sub-delims )
iuserinfo = *( iunreserved / ncoded / sub-delims / ":" )
ihost = IP-literal / IPv4address / ireg-name
isegment = *ipchar
isegment-nz = 1*ipchar
isegment-nz-nc = 1*( iunreserved / pct-encoded / sub-delims
/ "@" )
; non-zero-length segment without any colon ":"

0 comments on commit 258163b

Please sign in to comment.