Created
March 28, 2011 15:37
-
-
Save jrudolph/890674 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Project properties | |
#Mon Mar 28 17:17:14 CEST 2011 | |
project.organization=vv | |
project.name=ParserTest | |
sbt.version=0.7.5.RC1 | |
project.version=1.0 | |
build.scala.versions=2.8.1 | |
project.initialize=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait SsdpType | |
case object Notify extends SsdpType | |
case object Search extends SsdpType | |
case object OK extends SsdpType | |
import scala.util.parsing.combinator._ | |
case class SsdpMessage(header: SsdpType, values: List[(String, String)]) | |
object SsdpParser extends RegexParsers { | |
def fulldoc: Parser[SsdpMessage] = header ~ nameValuePairs <~ "\r\n" ^^ { | |
case header ~ values => SsdpMessage(header, values) | |
} | |
def nameValuePairs:Parser[List[(String, String)]] = repsep(nameValuePair, "\r\n") | |
def nameValuePair:Parser[(String, String)] = ( | |
//name | |
("""[-a-zA-Z0-9.]*""".r <~ ":") | |
//value | |
~ """[a-zA-Z0-9\:/,_; \[\].\-\"\'\?=]*""".r ) ^^ { | |
case name ~ value => (name, value) | |
} | |
def header: Parser[SsdpType] = okLine//notifyLine | okLine | mSearch | |
def notifyLine:Parser[SsdpType] = "NOTIFY * HTTP/1.1\r\n" ^^ (x =>Notify) | |
def mSearch:Parser[SsdpType] = "M-SEARCH * HTTP/1.1\r\n" ^^ (x=>Search) | |
def okLine:Parser[SsdpType] = "HTTP/1.1 200 OK\r\n" ^^ (x=>OK) | |
override def skipWhitespace = false | |
val input = """HTTP/1.1 200 OK | |
Cache-Control: max-age=300 | |
Date: Mon, 28 Mar 2011 06:37:31 GMT | |
Ext: | |
Location: http://192.168.1.1:1780/InternetGatewayDevice.xml | |
Server: POSIX UPnP/1.0 DD-WRT Linux/V24 | |
ST: urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1 | |
USN: uuid:01A0DDA7-7404-815B-63C4-539B920D5E56::urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1 | |
""" | |
def main(args: Array[String]) { | |
println(phrase(fulldoc)(new scala.util.parsing.input.CharArrayReader(input.toCharArray))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment