Skip to content

Instantly share code, notes, and snippets.

@nartamonov
Last active August 30, 2016 15:39
Show Gist options
  • Save nartamonov/9a3cd8715ece6904f7625c4ec4402fe3 to your computer and use it in GitHub Desktop.
Save nartamonov/9a3cd8715ece6904f7625c4ec4402fe3 to your computer and use it in GitHub Desktop.
Пример парсера URL на parboiled2 (из доклада "No more regular expressions", см. https://skillsmatter.com/skillscasts/5847-no-more-regular-expressions)
import org.parboiled2._
import org.parboiled2.CharPredicate._
class URLParser(val input: ParserInput) extends Parser {
def URL = rule { URLPattern ~> UrlParts }
def URLPattern = rule { Scheme ~ "://" ~ Host ~ Port ~ Path ~ QueryParams }
def QueryParams = rule { '?' ~ oneOrMore(QPPair).separatedBy('&') ~> (kvs => Map(kvs: _*)) }
def QPPair = rule { capture(oneOrMore(Alpha)) ~ '=' ~ capture(oneOrMore(Alpha)) ~> ((k: String, v: String) => (k, v)) }
def Path = rule { '/' ~ capture(oneOrMore(oneOrMore(Alpha)).separatedBy('/')) }
def Port = rule { optional(':' ~ capture(oneOrMore(Digit)) ~> (_.toInt)) }
def Host = rule { capture(oneOrMore(Alpha ++ ".-_")) }
def Scheme = rule { capture(oneOrMore(Alpha)) }
}
case class UrlParts(scheme: String, host: String, port: Option[Int], path: String, queryParams: Map[String,String])
// For test:
// new URLParser("https://url.spec.whatwg.org:80/foo?foo=bar").URL.run() --> scala.util.Try[UrlParts] = Success(UrlParts(https,url.spec.whatwg.org,Some(80),foo,Map(foo -> bar)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment