Parsing 如何使用PetitParser解析以关键字开头的标识符?
我想使用PetitParser在编程语言中解析标识符 其中一个要求是标识符的名称不是关键字(例如Parsing 如何使用PetitParser解析以关键字开头的标识符?,parsing,smalltalk,petitparser,Parsing,Smalltalk,Petitparser,我想使用PetitParser在编程语言中解析标识符 其中一个要求是标识符的名称不是关键字(例如null),因此null将不是有效的标识符 对于这种情况,我能想到的最小解析器是: identifier := ('null' asParser not, #word asParser plus) 但是,如果输入以关键字开头,则会失败: identifier end parse: 'nullable' 你有什么解决这个问题的建议吗?谢谢大家! identifier := ('null' asPa
null
),因此null
将不是有效的标识符
对于这种情况,我能想到的最小解析器是:
identifier := ('null' asParser not, #word asParser plus)
但是,如果输入以关键字开头,则会失败:
identifier end parse: 'nullable'
你有什么解决这个问题的建议吗?谢谢大家!
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))"
0处的是PetitParser的默认“解析失败”错误,表明解析器将接受“可空”、正常单词,而不是“空”