Search code examples
parsingsmalltalkpetitparser

Finding tokens in a Smalltalk String with PetitParser


I want to parse

'This,is,an,example,text'

like in findTokens

'This,is,an,example,text' findTokens: $, 
an OrderedCollection('This' 'is' 'an' 'example' 'text')

but cannot figure out how to do it with PetitParser, delimitedBy: and separatedBy: didn't helped me I tried

( #any asParser delimitedBy: $, asParser ) plus flatten parse:  'This,is,an,example,text'

but obviously not worked


Solution

  • I use this pattern all the time with PetitParser when I want to exclude something. Just define either "what I'm looking for" or "what I want to exclude" (whichever's easier to describe) as a parser, and then negate it, and process as necessary.

    s := 'This,is,an,example,text'.
    separator := $, asParser ==> [ :n | nil ].
    token := separator negate plus flatten.
    p := (token separatedBy: separator) ==> [ :nodes |
        nodes copyWithout: nil ].
    p parse: s.