Smalltalk 在Pharo中使用PetitPasser解析注释
有没有比这更简单的方法来解析单行注释Smalltalk 在Pharo中使用PetitPasser解析注释,smalltalk,pharo,petitparser,Smalltalk,Pharo,Petitparser,有没有比这更简单的方法来解析单行注释 comment ^ '//' asParser , (#any asParser starLazy: (#newline asParser)) , #newline asParser ==> [ :result | nil "Ignore comments" ] program ^ (comment / instruction) star ==> [ :r
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 aspaser)和#copyWithout:的重复
在卢卡斯的回答之后,我想出了更简单的解决方案:
program
^ programEntity star
==> [ :result | N2TProgramNode new setNodes: result]
programEntity
^ instruction trim: ignorable
ignorable
^ comment / #space asParser
comment
^ '//' asParser , #newline asParser negate star
为什么下面的注释解析器不能工作呢
'//' asParser , #newline asParser negate star
另外,您可能希望使用trim:
(如果语法允许)将注释解析包含到空白解析中,这样您就不必一直考虑它