使用smalltalk编写一个petit解析器,如何使用创建的方法'标识符?

use*_*591 3 parsing smalltalk pharo petitparser

我目前有一个方法

pMain

| parser |  


parser :=  'proc' asParser, #space asParser,  "<---- im trying to use the method identifier here - so i tried self identifier, instead of 'proc' asParser
            #letter asParser plus, $( asParser, 
           'int' asParser,#space asParser, 
           #letter asParser plus, $) asParser, 
           #space asParser, 'corp' asParser.    

   ^ parser
Run Code Online (Sandbox Code Playgroud)

我也有这两种方法

1-关键词方法

keywords
^ keywords ifNil: [
    keywords := Set newFrom: #(
        proc corp
        if then else fi
        do od
        print as
        while
        for from to by
        return
        int string real array format bool
        true false
        mutable static
        )
]
Run Code Online (Sandbox Code Playgroud)

2-标识符方法

identifier
^ ((#letter asParser , #word asParser star) flatten) >=> [ : ctxt : aBlock | | parse |
    parse := aBlock value.
    (self keywords includes: parse) ifTrue: [
        PPFailure message: 'keyword matched' context: ctxt
    ] ifFalse: [
        parse
    ]]
Run Code Online (Sandbox Code Playgroud)

问题:pMain中如何使用标识符解析器?

我喂这条线

   MyParser new pMain:= 'proc a( int a ) corp'
Run Code Online (Sandbox Code Playgroud)

Luk*_*gli 6

'proc' asParser返回一个接受字符串的解析器'proc'; 这类似于$p asParser返回接受该字符的解析器$p.

我想你的问题是关于如何引用解析器的制作.在子类中,PPCompositeParser您可以通过创建返回其解析器的方法(您这样做)来完成此操作.然后通过读取相同名称的相应实例变量来相互引用(除非您使用PetitParser工具,否则必须自己创建).

您可以在文档中找到有关复合解析器的教程.