如何使用PetitParser解析以关键字开头的标识符?

5
我将使用PetitParser解析编程语言中的标识符。其中一个要求是标识符名称不能是关键字(例如null),因此null不会是一个有效的标识符。我可以想到的最简单的解析器如下:
identifier := ('null' asParser not,  #word asParser plus)

然而,如果输入以关键字开头,则会失败:

identifier end parse: 'nullable'

您有什么建议来解决这个问题吗?谢谢!

1个回答

6
identifier := ('null' asParser, #word asParser plus) /
    ('null' asParser not, #word asParser plus).

identifier end parse: 'nullable'. "=> #('null' #($a $b $l $e))"
identifier end parse: 'null'. "=>  'at 0'"
identifier end parse: 'foo' "=> #(nil #($f $o $o))"
< p > 在0是PetitParser的默认“解析失败”错误,表明解析器将接受“可空”的普通单词,而不是“null”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接