I am learning to write a simple parser-composer. I am writing down the rules and write unit-test to write, as I go. However, I have been blocked on using repsep () with whitespace as a separator.
Object MyParser extends RegexParsers {lazy val listVal: Parser [list [string]] = elem ('{'} & lt; ~ repsep ("" \ d + "" The rules were simplified to clarify this problem. When I started parsing With "{1 2 3}", it always complains that it does not match: [1.4] Failure: `} 'expected but 2 found
I'm wondering what is the right way to write a rule as I said
Thanks
By default, RegexParsers
Hardwired parsers leave spaces before trying to match any terminal chin, unless you interpret the white space as unusual, you can work with it. If you want to treat as an ignored white spot then something other than the default ( \ s +
) , Then you projected val whitespace: Regex = ...
in your RegexParsers
parser. If you do not do this, then there is no such whistlepace switcher, override def skipWhitespace = false
.
Edit: So, change it:
repsep ("" "\ d +" ".," "\" + "" ".R)
To do this:
< Representative (" "\ d +" "" ". R)
and RegexParsers
should be unchanged for everything you want.
By the way, the usual use of repsep
For comma-separated things like things, you can make sure that the commas are there but they do not have to keep them. The result is a parse tree (or AST).
Comments
Post a Comment