Re: [Pharo-users] PetitParser question

2019-01-30 Thread Tudor Girba
Great. I am happy you found a solution :)

Cheers,
Doru


> On Jan 30, 2019, at 3:37 PM, Konrad Hinsen  wrote:
> 
> Tudor Girba  writes:
> 
>> Would this not work:
> 
> It works. And as much as I hate to admit it, my version of yesterday was
> very similar except ... that it lacked a set of parentheses :-(
> 
>> Please note that I used PetitParser2 for this one because it also
>> addresses the failure message issue you raised below. However, the
>> logic would work in PP1 as well.
> 
> It does, but I ended up switching to PP2 as well to get the right error
> messages.
> 
> Thanks a lot,
>  Konrad.

--
www.feenk.com

"We cannot reach the flow of things unless we let go."








Re: [Pharo-users] PetitParser question

2019-01-30 Thread Konrad Hinsen
Tudor Girba  writes:

> Would this not work:

It works. And as much as I hate to admit it, my version of yesterday was
very similar except ... that it lacked a set of parentheses :-(

> Please note that I used PetitParser2 for this one because it also
> addresses the failure message issue you raised below. However, the
> logic would work in PP1 as well.

It does, but I ended up switching to PP2 as well to get the right error
messages.

Thanks a lot,
  Konrad.



Re: [Pharo-users] PetitParser question

2019-01-29 Thread Tudor Girba
Hi,


> On Jan 29, 2019, at 7:04 PM, Konrad Hinsen  wrote:
> 
> Tudor Girba  writes:
> 
>> But, does it solve your problem?
> 
> Not yet. Not sure it will.

Would this not work:

p := (#digit asPParser separatedBy: ($+ asPParser / $- asPParser) trim) ==> 
[:tokens | 
| result |
result := tokens.
2 to: tokens size - 1 by: 2 do: [ :index | 
(tokens at: index) = (tokens at: 2)
ifFalse: [result := PP2Failure message: 'expected ', 
(tokens at: 2) asString, ' but got ', (tokens at: index) asString ]].
result ].


p end parse: '1+3+2 - 6' "a PP2Failure: 'expected + but got -‘"
p end parse: '1+3+2 + 6'  "#($1 $+ $3 $+ $2 $+ $6)”

?

Please note that I used PetitParser2 for this one because it also addresses the 
failure message issue you raised below. However, the logic would work in PP1 as 
well.


Cheers,
Doru



>>> More generally, there seems to be a bug in how PetitParser handles
>>> PPFailure return values. The actually reported error is always different
>>> from what is passed to PPFailure. I love software ;-)
>> 
>> What do you mean?
> 
> If I return
> 
>PPFailure message: 'whatever' at: 0
> 
> from my parser, then it does fail but it reports a different error.
> 
> After some more experiments, it looks like this is perhaps not a bug,
> but a feature that makes returning failures not so useful after all. I
> suspect that PetitParser simply backtracks after my failure and tries a
> different rule that then fails as well.
> 
> A simple example:
> 
>   | p |
>   p := #digit asParser plus flatten
>   ==> [ :value | value asSet size = 1 ifTrue: [ value ] ifFalse: 
> [ PPFailure message: 'not equal' at: 0 ]].
>   p parse: '22233'
> 
> The reported failure is "digit expected at 5".
> 
> On the other hand, if there is no path for backtracking, the reported
> failure is the expected one:
> 
>   | p |
>   p := (#digit asParser, #digit asParser) flatten
>   ==> [ :value | value first = value second ifTrue: [ value ] 
> ifFalse: [ PPFailure message: 'not equal' at: 0 ]].
>   p parse: '23'
> 
> Konrad.

--
www.feenk.com

"Quality cannot be an afterthought."




Re: [Pharo-users] PetitParser question

2019-01-29 Thread Tudor Girba
Hi,


> On Jan 29, 2019, at 6:31 PM, Konrad Hinsen  wrote:
> 
> Hi Doru,
> 
> Thanks for pointing out the use case in XML, which gave me an
> opportunity to play around with this with almost no
> effort. Unfortunately there seems to be a bug in how PPXmlParser
> constructs the PPFailure message
> (freshly reported:
> https://github.com/moosetechnology/PetitParser/issues/38).

Yes, I saw it. I mostly pointed you to the example. We originally built 
PPXmlParser as an example, and not so much as a production parser.

But, does it solve your problem?

> More generally, there seems to be a bug in how PetitParser handles
> PPFailure return values. The actually reported error is always different
> from what is passed to PPFailure. I love software ;-)

What do you mean?

Cheers,
Doru

> Konrad.

--
www.feenk.com

"If you interrupt the barber while he is cutting your hair,
you will end up with a messy haircut."




Re: [Pharo-users] PetitParser question

2019-01-29 Thread Konrad Hinsen
Hi Doru,

Thanks for pointing out the use case in XML, which gave me an
opportunity to play around with this with almost no
effort. Unfortunately there seems to be a bug in how PPXmlParser
constructs the PPFailure message
(freshly reported:
https://github.com/moosetechnology/PetitParser/issues/38).

More generally, there seems to be a bug in how PetitParser handles
PPFailure return values. The actually reported error is always different
from what is passed to PPFailure. I love software ;-)

Konrad.



Re: [Pharo-users] PetitParser question

2019-01-27 Thread Tudor Girba
Hi Konrad,

A somewhat similar issue is present in an XML grammar: the closing element must 
match the opening element. In PPXmlGrammar, you have a condition that matches 
it and throws a failure otherwise:

element
"[39]   element::=   EmptyElemTag | STag content 
ETag"

^ $< asParser , qualified , attributes , whitespace optional , ('/>' 
asParser / ($> asParser , content , [ :stream | stream position ] asParser , 
' asParser)) ==> [ :nodes | 
nodes fifth = '/>'
ifTrue: [ Array with: nodes second with: nodes third 
with: #() ]
ifFalse: [
nodes second = nodes fifth fifth
ifTrue: [ Array with: nodes second 
with: nodes third with: nodes fifth second ]
ifFalse: [ PPFailure message: 'Expected 
' context: nil at: nodes fifth third ] ] ]

Cheers,
Doru


> On Jan 27, 2019, at 4:38 PM, Konrad Hinsen  wrote:
> 
> Dear Tomo,
> 
>> This post might help you. In case of PetitParser2, it's PP2Failure instead
>> of PPFailure.
>> https://stackoverflow.com/questions/15371334/how-can-a-petitparser-parse-rule-signal-an-error
> 
> That's indeed a possible solution: parse for arbitrary operators, and then 
> add a test for equality that can make everything fail in the end. I will try 
> it out!
> 
> Thanks,
>  Konrad.
> 
> 

--
www.feenk.com

"Not knowing how to do something is not an argument for how it cannot be done."




Re: [Pharo-users] PetitParser question

2019-01-27 Thread Konrad Hinsen

Dear Tomo,


This post might help you. In case of PetitParser2, it's PP2Failure instead
of PPFailure.
https://stackoverflow.com/questions/15371334/how-can-a-petitparser-parse-rule-signal-an-error


That's indeed a possible solution: parse for arbitrary operators, and 
then add a test for equality that can make everything fail in the end. I 
will try it out!


Thanks,
  Konrad.




Re: [Pharo-users] PetitParser question

2019-01-26 Thread tomo
Dear Konrad,

This post might help you. In case of PetitParser2, it's PP2Failure instead
of PPFailure.
https://stackoverflow.com/questions/15371334/how-can-a-petitparser-parse-rule-signal-an-error

Best Regards,
---
tomo



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html



[Pharo-users] PetitParser question

2019-01-26 Thread Konrad Hinsen
Dear parsing experts,

I have been working on a medium-size parser using the PetitParser
framework, which so far has been a very productive environment.  But now
I have a problem to which I could not find the solution in "Deep into
Pharo", nor by searching around in the code.

One of my production rules requires several parsed items to be equal.
It should accept expressions such as

1 + 3 + 2 + 6
2 * 9 * 3 * 6
0 - 8 - 5 - 3

but not expressions that mix different operators (because my grammar has
no precedence rules and thus requires parentheses for disambiguation).

In various other parsing frameworks, I do this by parsing up to the
first operator, retrieving it, and permitting only that same string in
the following. But I haven't found a way to do this in PetitParser, nor
any other obvious solution.

The only solution I see right now is to have one production rule per
operator, but that makes for a huge grammar. Is there another way?

Thanks in advance,
  Konrad



Re: [Pharo-users] PetitParser Question

2018-03-06 Thread Asbath via Pharo-users
--- Begin Message ---

OK, Thanks,


Asbath


On 06/03/2018 08:43, Julien wrote:

Hello,

I think this blogpost is a good place to start: 
https://www.lukas-renggli.ch/blog/petitparser-1


There is also the chapter about PetitParser in Deep Into Pharo: 
http://pharobooks.gforge.inria.fr/PharoByExampleTwo-Eng/latest/PetitParser.pdf


Cheers,

Julien

---
Julien Delplanque
Doctorant à l’Université de Lille 1
http://juliendelplanque.be/phd.html
Equipe Rmod, Inria
Bâtiment B 40, avenue Halley 59650 Villeneuve d'Ascq
Numéro de téléphone: +333 59 35 86 40

Le 5 mars 2018 à 19:26, Asbath via Pharo-users 
> a 
écrit :



*De: *Asbath >
*Objet: **PetitParser Question*
*Date: *5 mars 2018 à 19:26:49 UTC+1
*À: *pharo-users@lists.pharo.org 


Hi everybody,

We are a group of students working on Pillar, and we notice that the 
Parser of Pillar is based on PetitParser Library. Then we want to 
know more about PetitParser !?


Thanks,

Asbath








--- End Message ---


Re: [Pharo-users] PetitParser Question

2018-03-05 Thread Stephane Ducasse
Hi Asbath

there is a chapter on Pillar in deep into pharo or the next book.
You also have a chapter on the http://themoosebook.org

Stef

On Mon, Mar 5, 2018 at 7:26 PM, Asbath via Pharo-users
 wrote:
>
>
> -- Forwarded message --
> From: Asbath 
> To: pharo-users@lists.pharo.org
> Cc:
> Bcc:
> Date: Mon, 5 Mar 2018 19:26:49 +0100
> Subject: PetitParser Question
> Hi everybody,
>
> We are a group of students working on Pillar, and we notice that the Parser 
> of Pillar is based on PetitParser Library. Then we want to know more about 
> PetitParser !?
>
> Thanks,
>
> Asbath
>
>
>



[Pharo-users] PetitParser Question

2018-03-05 Thread Asbath via Pharo-users
--- Begin Message ---

Hi everybody,

We are a group of students working on Pillar, and we notice that the 
Parser of Pillar is based on PetitParser Library. Then we want to know 
more about PetitParser !?


Thanks,

Asbath


--- End Message ---


Re: [Pharo-users] PetitParser question parsing HTML meta tags

2017-04-05 Thread Paul DeBruicker
Thanks.  I really appreciate everyone's help on this.  Was at a high level of
frustration the other day.  








monty-3 wrote
> You could use XMLHTMLParser from STHub PharoExtras/XMLParserHTML
> (supported on Pharo, Squak, and GS):
> 
> descriptions := OrderedCollection new.
> (XMLHTMLParser parseURL: aURL)
>   allElementsNamed: 'meta'
>   do: [:each |
>   ((each attributeAt: 'name') asLowercase = 'description'
>   or: [(each attributeAt: 'http-equiv') asLowercase = 
> 'description'])
>   ifTrue: [descriptions addLast: (each attributeAt: 
> 'content')]].
> 
> it accepts messy HTML and produces an XML DOM tree from it.
> 
>> Sent: Thursday, March 30, 2017 at 1:58 PM
>> From: "PAUL DEBRUICKER" 

> pdebruic@

> 
>> To: "Any question about pharo is welcome" 

> pharo-users@.pharo

> 
>> Subject: [Pharo-users] PetitParser question parsing HTML meta tags
>>
>> This is kind of a "I'm tired of thinking about this and not making much
>> progress for the amount of time I'm putting in question" but here it is: 
>> 
>> 
>> 
>> I'm trying to parse descriptions from HTML meta elements.  I can't use
>> Soup because there isn't a working GemStone port.  
>> 
>> I've got it to work with the structure:
>> 
>> 
> 
>> 
>> and 
>> 
>> 
> 
>> 
>> 
>> but I'm running into instances of: 
>> 
>> 
> 
>> 
>> and
>> 
>> 
> 
>> 
>> 
>> and am having trouble adapting my parsing code (such as it is). 
>> 
>> 
>> The parsing code that addresses the first two cases is:
>> 
>> 
>> 
>> parseHtmlPageForDescription: htmlString
>>   | startParser endParser ppStream descParser result text lower str
>> doubleQuoteIndex |
>>   lower := 'escription' asParser.
>>   startParser := '
> >
>endParser := '>' asParser.
>>   ppStream := htmlString readStream asPetitStream.
>>   descParser := ((#'any' asParser starLazy: startParser , lower)
>> , (#'any' asParser starLazy: endParser)) ==> #'second'.
>>   result := descParser parse: ppStream.
>>   text := (result
>> inject: (WriteStream on: String new)
>> into: [ :stream :char | 
>>   stream nextPut: char.
>>   stream ])
>> contents trimBoth.
>>   str := text copyFrom: (text findString: 'content=') + 9 to: text size.
>>   doubleQuoteIndex := 8 - ((str last: 7) indexOf: $").
>>   ^ str copyFrom: 1 to: str size - doubleQuoteIndex
>> 
>> 
>> I can't figure out how to change the startParser parser to accept the
>> second idiom.  And maybe there's a better approach altogether.  Anyway. 
>> If anyone has any ideas on different approaches I'd appreciate learning
>> them.  
>> 
>> 
>> Thanks for giving it some thought
>> 
>> Paul
>>





--
View this message in context: 
http://forum.world.st/PetitParser-question-parsing-HTML-meta-tags-tp4940587p4941367.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.



Re: [Pharo-users] PetitParser question parsing HTML meta tags

2017-04-02 Thread monty
XMLParserHTML is the fastest HTML parser on Pharo, Squeak, and GS. It has DOM 
and SAX parsers and works with other libs such as PharoExtras/XPath and 
PharoExtras/XMLParserStAX.

Element and attribute names are normalized to lowercase, and printing XML DOM 
trees back as HTML is complicated by browsers not recognizing XML-style 
self-closing tags ending with "/>" for some elements (like "script"), so use 
#printedWithoutSelfClosingTags/#printWithoutSelfClosingTagsOn:/#printWithoutSelfClosingTagsToFileNamed:
 instead.

> Sent: Thursday, March 30, 2017 at 1:58 PM
> From: "PAUL DEBRUICKER" <pdebr...@gmail.com>
> To: "Any question about pharo is welcome" <pharo-users@lists.pharo.org>
> Subject: [Pharo-users] PetitParser question parsing HTML meta tags
>
> This is kind of a "I'm tired of thinking about this and not making much 
> progress for the amount of time I'm putting in question" but here it is: 
> 
> 
> 
> I'm trying to parse descriptions from HTML meta elements.  I can't use Soup 
> because there isn't a working GemStone port.  
> 
> I've got it to work with the structure:
> 
> 
> 
> and 
> 
> 
> 
> 
> but I'm running into instances of: 
> 
> 
> 
> and
> 
> 
> 
> 
> and am having trouble adapting my parsing code (such as it is). 
> 
> 
> The parsing code that addresses the first two cases is:
> 
> 
> 
> parseHtmlPageForDescription: htmlString
>   | startParser endParser ppStream descParser result text lower str 
> doubleQuoteIndex |
>   lower := 'escription' asParser.
>   startParser := '   endParser := '>' asParser.
>   ppStream := htmlString readStream asPetitStream.
>   descParser := ((#'any' asParser starLazy: startParser , lower)
> , (#'any' asParser starLazy: endParser)) ==> #'second'.
>   result := descParser parse: ppStream.
>   text := (result
> inject: (WriteStream on: String new)
> into: [ :stream :char | 
>   stream nextPut: char.
>   stream ])
> contents trimBoth.
>   str := text copyFrom: (text findString: 'content=') + 9 to: text size.
>   doubleQuoteIndex := 8 - ((str last: 7) indexOf: $").
>   ^ str copyFrom: 1 to: str size - doubleQuoteIndex
> 
> 
> I can't figure out how to change the startParser parser to accept the second 
> idiom.  And maybe there's a better approach altogether.  Anyway.  If anyone 
> has any ideas on different approaches I'd appreciate learning them.  
> 
> 
> Thanks for giving it some thought
> 
> Paul
> 



Re: [Pharo-users] PetitParser question parsing HTML meta tags

2017-03-30 Thread Martin McClure
On 03/30/2017 10:58 AM, PAUL DEBRUICKER wrote:
> I can't figure out how to change the startParser parser to accept the second 
> idiom.  And maybe there's a better approach altogether.  Anyway.  If anyone 
> has any ideas on different approaches I'd appreciate learning them.  

This looks like a job for the ordered choice operator. Perhaps something
very roughly like this...

descTag := nameDescTag / httpDescTag.
nameDescTag := 'name=' , descAnyCase.
httpDescTag := 'http-equiv=' , descAnyCase.
descAnyCase := 'description' asParser / 'Description' asParser.

And so on.

HTH.

Regards,

-Martin



[Pharo-users] PetitParser question parsing HTML meta tags

2017-03-30 Thread PAUL DEBRUICKER
This is kind of a "I'm tired of thinking about this and not making much 
progress for the amount of time I'm putting in question" but here it is: 



I'm trying to parse descriptions from HTML meta elements.  I can't use Soup 
because there isn't a working GemStone port.  

I've got it to work with the structure:



and 




but I'm running into instances of: 



and




and am having trouble adapting my parsing code (such as it is). 


The parsing code that addresses the first two cases is:



parseHtmlPageForDescription: htmlString
  | startParser endParser ppStream descParser result text lower str 
doubleQuoteIndex |
  lower := 'escription' asParser.
  startParser := ' #'second'.
  result := descParser parse: ppStream.
  text := (result
inject: (WriteStream on: String new)
into: [ :stream :char | 
  stream nextPut: char.
  stream ])
contents trimBoth.
  str := text copyFrom: (text findString: 'content=') + 9 to: text size.
  doubleQuoteIndex := 8 - ((str last: 7) indexOf: $").
  ^ str copyFrom: 1 to: str size - doubleQuoteIndex


I can't figure out how to change the startParser parser to accept the second 
idiom.  And maybe there's a better approach altogether.  Anyway.  If anyone has 
any ideas on different approaches I'd appreciate learning them.  


Thanks for giving it some thought

Paul


[Pharo-users] PetitParser Question

2015-03-17 Thread James Foster
I’m following the example in the book 
(http://pharobooks.gforge.inria.fr/PharoByExampleTwo-Eng/latest/PetitParser.pdf 
http://pharobooks.gforge.inria.fr/PharoByExampleTwo-Eng/latest/PetitParser.pdf,
 pp. 9-10) with a slight modification. I believe that the example evaluates 
things that have the same precedence from right-to-left (is that correct?). 
I’ve tried to adjust things so that it evaluates from left to right, but get an 
error. Any advice?

James

| number term prod prim start |
number := #digit asParser plus flatten == [ :str | str asNumber ].
term := PPDelegateParser new.
prod := PPDelegateParser new.
prim := PPDelegateParser new.
term setParser: (term memoized , $+ asParser trim , prod == [ :nodes | nodes 
first + nodes last ]) / prod.
prod setParser: (prim , $* asParser trim , prod == [ :nodes | nodes first * 
nodes last ]) / prim.
prim setParser: ($( asParser trim , term , $) asParser trim == [ :nodes | 
nodes second ]) / number.
start := term end.
start parse: '1 + 2'