在 JavaTokenParsers 中将空格设置为分隔符 [英] Setting Whitespace as Delimiter in JavaTokenParsers
问题描述
扩展 JavaTokenParsers
,我有以下内容:
Extending JavaTokenParsers
, I have the following:
class Foo extends JavaTokenParsers {
lazy val check = id ~ action ~ obj
lazy val id = "FOO" | "BAR"
lazy val action = "GET" | "SET"
lazy val obj = "BAZ" | "BIZ"
}
我曾假设空格将充当分隔符.换句话说,当 check
成功解析以下表达式时,我变得很困惑:FOO GETBAZ
.
I had assumed that whitespace would act as a delimiter. In other words, I became confused when check
parsed the following expression successfully: FOO GETBAZ
.
val result = parseAll(check, "FOO GETBAZ")
println(result.get)
结果
((FOO~GET)~BAZ)
((FOO~GET)~BAZ)
我如何使用空格作为 delimiter
,即上述内容无法成功解析,因为 GETBAZ
与 action
中的任何一个都不匹配's GET
还是 SET
?
How can I use whitespace as a delimiter
, i.e. the above wouldn't parse successfully since GETBAZ
does not match either of action
's GET
or SET
?
推荐答案
JavaTokenParser
向 RegexParsers
添加了一些方法,但它不会改变 的行为文字
,它会匹配它的参数而不用担心它周围的东西.
JavaTokenParser
adds some methods to RegexParsers
, but it doesn't change the behavior of literal
, which will match its argument without worrying about what's around it.
skipWhitespace
设置也不会帮助您,因为它只指定是否忽略空格 - 而不是是否需要.
The skipWhitespace
setting isn't going to help you, either, since it only specifies whether whitespace will be ignored—not whether it's required.
您有几个选择.一种是使用带有单词边界的正则表达式:
You have a couple of options. One would be to use regular expressions with word boundaries:
class Foo extends JavaTokenParsers {
def word(s: String): Parser[String] = regex(s"\\b$s\\b".r)
lazy val check = id ~ action ~ obj
val id = word("FOO") | word("BAR")
val action = word("GET") | word("SET")
val obj = word("BAZ") | word("BIZ")
}
或ident
:
class Foo extends JavaTokenParsers {
def word(s: String): Parser[String] = ident.filter(_ == s)
lazy val check = id ~ action ~ obj
val id = word("FOO") | word("BAR")
val action = word("GET") | word("SET")
val obj = word("BAZ") | word("BIZ")
}
或者您可以在每个项目之间手动添加空格解析器.
Or you could manually add whitespace parsers between each of your items.
我可能会采用 \b
解决方案,但这在很大程度上取决于品味和偏好.
I'd probably go with the \b
solution, but it's largely a matter of taste and preferences.
这篇关于在 JavaTokenParsers 中将空格设置为分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!