Created
April 27, 2016 15:37
-
-
Save sortega/0990a67fdf40b525b90ba0713c6a55b6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import scala.util.parsing.combinator.RegexParsers | |
| sealed trait Command | |
| case object FlushCommand extends Command | |
| case class LookupKey(key: String) extends Command | |
| case class SetKey(key: String, blob: String) extends Command | |
| object Grammar extends RegexParsers { | |
| val id: Parser[String] = """[a-zA-Z][a-zA-Z0-9]*""".r | |
| val blob = """"[^"]*"""".r | |
| val commandDelimiter = ";" | |
| def flush = "flush" ^^^ FlushCommand | |
| def lookupKey = "lookup" ~> id ^^ LookupKey.apply | |
| def setKey = "set" ~> id ~ "to" ~ blob ^^ { | |
| case key ~ _ ~ value => SetKey(key, value) | |
| } | |
| def command = (flush | lookupKey | setKey) <~ commandDelimiter | |
| def session = command * | |
| } | |
| Grammar.parse(Grammar.flush, "flush") | |
| Grammar.parseAll(Grammar.lookupKey, "lookup key1") | |
| Grammar.parse(Grammar.setKey, """set foo to "1234"""") | |
| Grammar.parse(Grammar.command, """set foo to "1234";""") | |
| Grammar.parseAll(Grammar.session, """flush; set foo to "1234"; lookup foo;""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment