Skip to content

Instantly share code, notes, and snippets.

@akiradeveloper
Created May 23, 2015 01:49
Show Gist options
  • Save akiradeveloper/bf10e5c5ff6da8effa5f to your computer and use it in GitHub Desktop.
Save akiradeveloper/bf10e5c5ff6da8effa5f to your computer and use it in GitHub Desktop.
parser combinator sample
import scala.util.parsing.combinator._
class SampleParsers extends RegexParsers {
def ok: Parser[Int] = success(0)
def int: Parser[Int] = """[0-9][0-9]?""".r ^^ { _.toInt }
def char: Parser[Char] = """[A-z]""".r ^^ { _.charAt(0) }
def lit: Parser[Int] = "[" ~> int <~ "]"
def rep0lit = rep(lit)
def rep1lit = rep1(lit)
}
object Sample extends SampleParsers {
def main(args: Array[String]) {
// sbt "run Sample hoge"
// println(args(0)) // Sample
// println(args(1)) // hoge
println(parseAll(ok, "null"))
println(parseAll(char, "c"))
println(parseAll(lit, "[0]"))
println(parseAll(rep0lit, ""))
println(parseAll(rep0lit, "[3]"))
println(parseAll(rep0lit, "[3]"))
println(parseAll(rep1lit, "[10][23]"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment