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

时间:2012-12-20 11:15:47

标签: parsing smalltalk petitparser

我想使用PetitParser解析编程语言中的标识符。

其中一个要求是标识符的名称不是关键字(例如null),因此null不是有效的标识符。

我能想到的最小解析器是:

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

但是,如果输入以关键字开头,则失败:

identifier end parse: 'nullable'

你有什么建议可以解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:5)

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))"

at 0是PetitParser的默认“无法解析”错误,表明解析器将接受'可空',正常单词,而不是'null'。

相关问题