parser combinators - Scala rep separator for specific area of text -
imaging i've got following:
--open client: enter nick age 28 rosewell, usa client: enter maria age 19 cleveland, usa --open-- i need result close following: list(list(nick, age 28, rosewell), list(maria, age19, cleveland))
it can many clients inside open body can imagine, list can have size, it's not fixed.
i trying make of following:
repsep(".*".r , "client: enter" + lineseparator) in case can parse line list((client: enter)), how make sure work same piece of parse text?
i guess using regexparsers (just note skips white spaces default). i'm assuming ends "\n\n--open--" instead (if can change otherwise i'll show how modify repsep parser). change see text has following structure:
- each client separated text
"client: enter" - then need parse each line after non-empty, separated carriage return
- if have empty line, parse 2 line separators , repeat step 2 if possible otherwise means reach end of input
implementation of parser straightforward:
object clientparser extends regexparsers { override def skipwhitespace = false def lineseparator = "\n" def root = "--open" ~> lineseparator ~> rep(client) <~ "--open--" def client = ("client: enter" ~ lineseparator) ~> repsep(".+".r, lineseparator) <~ rep(lineseparator) } running with:
--open client: enter nick age 28 rosewell; usa client: enter maria age 19 cleveland; usa --open-- you get:
[12.9] parsed: list(list(nick, age 28, rosewell; usa), list(maria, age 19, cleveland; usa))
Comments
Post a Comment