package urlx

type parser Peg {
  url uri
}

URL <- URI
    / Path
    / Scheme
    / Host
    / OnlyPort
    End

URI <- < Scheme '//' Host Path? > {
  p.url.uri = text
}

Scheme <- < [a-zA-Z][a-zA-Z+0-9]* ':' > {
  p.url.scheme = text[:len(text)-1]
}

Host <- IPPort / HostNamePort / IPV4 / HostName / BracketsPort / Brackets

IPPort <- IP ':' Port
HostNamePort <- HostName ':' Port
BracketsPort <- Brackets ':' Port

IP <- IPV4

IPV4 <- < [0-9]+ '.' [0-9]+ '.' [0-9]+ '.' [0-9]+ > {
  p.url.host = text
}

HostName <- < [a-zA-Z][a-zA-Z0-9]* > {
  p.url.host = text
}

OnlyPort <- ':' Port / Port

Port <- < [0-9]+ > {
  p.url.port = text
}

Path <- RelPath / AbsPath

RelPath <- < '.' '/' .* > {
  p.url.path = text
}

AbsPath <- < '/' .* > {
  p.url.path = text
}

Brackets <- '[::]' {
  p.url.host = "[::]"
}

End
  <- !.