[英]How to parse identifiers that start with a keyword with PetitParser?
我想使用PetitParser解析编程语言中的标识符。
其中一个要求是标识符的名称不是关键字(例如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))"
at 0
是PetitParser的默认“无法解析”错误,表明解析器将接受'nullable',正常单词,而不是'null'。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.