Alb*_*lli 4 parsing smalltalk petitparser
我想使用PetitParser解析编程语言中的标识符.
其中一个要求是标识符的名称不是关键字(例如null),因此它null不是有效的标识符.
我能为这种情况考虑的最小解析器是:
identifier := ('null' asParser not, #word asParser plus)
Run Code Online (Sandbox Code Playgroud)
但是,如果输入以关键字开头,则失败:
identifier end parse: 'nullable'
Run Code Online (Sandbox Code Playgroud)
你有什么建议可以解决这个问题吗?谢谢!
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))"
Run Code Online (Sandbox Code Playgroud)
该at 0是PetitParser的默认"分析失败"的错误,表示解析器会接受"可空",正常的话,而不是"空".