import scala.
util.
parsing.
combinator.
JavaTokenParsers
case class IntArrayDef
(name
: String, value
: IndexedSeq
[Int
])
lazy
val parse
= types ~
> repsep
((defIntArray | defInt
),
",") <~
";"
lazy
val types
= "int|string".
r
lazy
val defInt
= ident ~
("=" ~
> wholeNumber
).
? ^^
{ case ident ~ None
=> println
(ident +
":" +
0) case ident ~ Some
(default
) => println
(ident +
":" + default.
toString()) }
lazy
val defIntArray
= ident ~
"[" ~ wholeNumber ~
"]" ~
("=" ~
"{" ~
> repsep
(wholeNumber,
",") <~
"}").
? ^^
{ case ident ~
_ ~ size ~
_ ~ None
=> println
(ident + IndexedSeq.
fill(size.
toInt)(0).
toString()) case ident ~
_ ~ size ~
_ ~ Some
(defaults
) => println
(ident +
":" + defaults.
toString()) //println(ident + defaults.map(_.toInt).toIndexedSeq.toString())
}
def apply
(str
: String
) = parseAll
(parse, str
) }
def main
(args
: Array
[String
]): Unit
= { HogeParser("string hoge, hage[3] = { 1, 2, 3 }, huge = 5;").get
}
}
aW1wb3J0IHNjYWxhLnV0aWwucGFyc2luZy5jb21iaW5hdG9yLkphdmFUb2tlblBhcnNlcnMKCmNhc2UgY2xhc3MgSW50RGVmKG5hbWU6IFN0cmluZywgdmFsdWU6IEludCkKY2FzZSBjbGFzcyBJbnRBcnJheURlZihuYW1lOiBTdHJpbmcsIHZhbHVlOiBJbmRleGVkU2VxW0ludF0pCgpvYmplY3QgSG9nZVBhcnNlciBleHRlbmRzIEphdmFUb2tlblBhcnNlcnMgewoJbGF6eSB2YWwgcGFyc2UgPSB0eXBlcyB+PiByZXBzZXAoKGRlZkludEFycmF5IHwgZGVmSW50KSwgIiwiKSA8fiAiOyIKCQoJbGF6eSB2YWwgdHlwZXMgPSAiaW50fHN0cmluZyIucgoJCglsYXp5IHZhbCBkZWZJbnQgPSBpZGVudCB+ICgiPSIgfj4gd2hvbGVOdW1iZXIpLj8gXl4gewoJCWNhc2UgaWRlbnQgfiBOb25lID0+IHByaW50bG4oaWRlbnQgKy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