parsingsmalltalkpharopetitparser

writing a petit parser using smalltalk, how to use created method 'identifier?


i currently have a method

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

i also have these two methods

1- keywords method

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
        )
]

2- identifier method

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
    ]]

Question: how is the identifier parser used in pMain?

I feed it this line

   MyParser new pMain:= 'proc a( int a ) corp'

Solution

  • 'proc' asParser returns a parser that accepts the string 'proc'; this is similar to $p asParser that returns a parser that accepts the character $p.

    I guess your question is about how to refer to parser productions. In subclasses of PPCompositeParser you can do this by creating a method that returns its parser (you did this). Then productions refer to each other by reading the respective instance variable of the same name (you have to create those yourself, unless you use the PetitParser tools).

    You can find a tutorial about composite parsers in the documentation.