我正在使用 Scala 的 Parser Combinator 框架,扩展 RegexParsers
类。我有一个 identifier
标记,它以字母开头,可以包含字母字符、破折号、下划线和数字,只要它不是保留字之一即可。我尝试使用解析器的 not()
来阻止使用保留字,但它也匹配以保留字为前缀的标识符。
def reserved = "and" | "or"
def identifier: Parser[String] = not(reserved) ~> """[a-zA-Z][\.a-zA-Z0-9_-]*""".r
但是,当我尝试解析像 and-today
这样的标识符时,我收到一条错误消息,指出 Expected Failure
。
如果保留字与 token 完全匹配,而不仅仅是前缀,我该如何过滤?
在这种情况下使用 not()
时,还有办法改进错误报告吗?在其他情况下,我得到解析器期望的正则表达式,但在这种情况下,它只是说 Failure
而没有任何细节。
最佳答案
您可以使用 filterWithError
过滤掉保留字并自定义错误消息,如下所示:
val reservedWords = HashSet("and", "or")
val idRegex= """[a-zA-Z][\.a-zA-Z0-9_-]*""".r
val identifier = Parser(input =>
idRegex(input).filterWithError(
!reservedWords.contains(_),
reservedWord => s"YOUR ERROR MESSAGE FOR $reservedWord",
input
)
)
关于scala - 如何过滤解析器组合器中的保留字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44334353/