Finding Tokens in a Smalltalk String with PetitParser

I want to make out

'This,is,an,example,text' 

as in findTokens

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

but I can’t figure out how to do this with PetitParser, delimitedBy: and separated By: didn’t help me, I tried

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

but obviously didn't work

+7
source share
4 answers

I use this template all the time with PetitParser when I want to exclude something. Just define either "what I am looking for" or "what I want to exclude" (depending on which is easier to describe) as a parser, and then undo it and process it 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. 
+1
source

You can use delimitedBy: in combination with withoutSeparators :

 |text parser| text := 'This,is,an,example,text'. parser := (#word asParser plus flatten delimitedBy: ($, asParser)) withoutSeparators. parser parse: text 

This seems to be the last improvement for PetitParser.

+3
source

a #delimitedBy: b expands to a , (b , a) star , so your as-is parser says, "give me one character separated by commas."

This is not very readable, but it does what you want:

 ((($, asParser not , #any asParser) ==> [:nodes | nodes second]) plus flatten delimitedBy: $, asParser 

The first paragraph says: "Parse anything that is not a comma." So, '12,24' you get #('12' $, '24') .

+2
source

Try

 (#word asParser plus flatten separatedBy: $, asParser) ==> [:nodes| nodes copyWithout: $, ] 

I hope I understand what you wanted

+1
source

All Articles