10
解析1行註釋的方法比這更簡單嗎?在Pharo中使用PetitParser解析註釋
comment
^'//' asParser ,
(#any asParser starLazy: (#newline asParser)) ,
#newline asParser
==> [ :result | nil "Ignore comments" ]
program
^(comment/instruction) star
==> [ :result | N2TProgramNode new
setNodes: (result copyWithout: nil) ]
我特別不確定重複(#newline asParser)和#copyWithout :.
盧卡斯的回答後,我想出了更簡單的解決方案如下:
program
^programEntity star
==> [ :result | N2TProgramNode new setNodes: result]
programEntity
^instruction trim: ignorable
ignorable
^comment/#space asParser
comment
^'//' asParser , #newline asParser negate star