* How to work with the tree produced by (ice-9 peg)'s match-pattern?
@ 2024-12-04 12:06 Tomas Volf
2024-12-05 1:24 ` Zelphir Kaltstahl
0 siblings, 1 reply; 4+ messages in thread
From: Tomas Volf @ 2024-12-04 12:06 UTC (permalink / raw)
To: guile-user
[-- Attachment #1: Type: text/plain, Size: 3218 bytes --]
Hi :)
I recently discovered the (ice-9 peg) module, and it seems really
useful. However I have to admit I struggle a bit with processing the
tree produced by it.
Let me put forward an example:
--8<---------------cut here---------------start------------->8---
(use-modules (ice-9 peg))
(define-peg-pattern Opt body
(and Name Space (+ (and Enum Space)) Default))
(define-peg-pattern Name all
(+ Alpha))
(define-peg-pattern Enum all
(+ Alpha))
(define-peg-pattern Default all
(and (ignore "(default: ") (* Alpha) (ignore ")")))
(define-peg-pattern Alpha body
(range #\a #\z))
(define-peg-pattern Space none
" ")
--8<---------------cut here---------------end--------------->8---
Now I can feed it few strings to get the parsed trees:
--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> (peg:tree (match-pattern Opt "name a b c (default: a)"))
$11 = ((Name "name") ((Enum "a") (Enum "b") (Enum "c")) (Default "a"))
scheme@(guile-user)> (peg:tree (match-pattern Opt "name a b c (default: )"))
$12 = ((Name "name") ((Enum "a") (Enum "b") (Enum "c")) Default)
scheme@(guile-user)> (peg:tree (match-pattern Opt "name a (default: a)"))
$13 = ((Name "name") (Enum "a") (Default "a"))
scheme@(guile-user)> (peg:tree (match-pattern Opt "name a (default: )"))
$14 = ((Name "name") (Enum "a") Default)
--8<---------------cut here---------------end--------------->8---
What we see here is that it produces trees in 4 different "shapes",
depending on the input string. And I am unsure how to reasonably
process them.
My current approach looks like:
--8<---------------cut here---------------start------------->8---
(define process-tree
(match-lambda
((('Name name) (('Enum enum) ...) ('Default def))
(pk name enum def))
((('Name name) ('Enum enum) ... ('Default def))
(pk name enum def))
((('Name name) (('Enum enum) ...) 'Default)
(pk name enum #f))
((('Name name) ('Enum enum) ... 'Default)
(pk name enum #f))))
--8<---------------cut here---------------end--------------->8---
Which *does* work:
--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> (for-each process-tree (list $11 $12 $13 $14))
;;; ("name" ("a" "b" "c") "a")
;;; ("name" ("a" "b" "c") #f)
;;; ("name" ("a") "a")
;;; ("name" ("a") #f)
--8<---------------cut here---------------end--------------->8---
However it is very repetitive and the code is pretty hard to work with.
(Real code even has few more branches.) And I am not sure how to do a
better job at this.
It would really simplify things if I would be able to *always* have a
list of 'Enum, even when there is just a single one. Same for default.
I tried keyword-flatten, but probably I just do not understand how to
use it. Or maybe (ice-9 match) is just a wrong tool here?
Would someone be able to provide general guidance how to approach this,
or at least recommend some projects using (ice-9 peg) that I could
study?
Thanks and have a nice day,
Tomas
--
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 853 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How to work with the tree produced by (ice-9 peg)'s match-pattern?
2024-12-04 12:06 How to work with the tree produced by (ice-9 peg)'s match-pattern? Tomas Volf
@ 2024-12-05 1:24 ` Zelphir Kaltstahl
2024-12-06 19:45 ` Tomas Volf
0 siblings, 1 reply; 4+ messages in thread
From: Zelphir Kaltstahl @ 2024-12-05 1:24 UTC (permalink / raw)
To: ~; +Cc: Guile User
On 04.12.24 13:06, Tomas Volf wrote:
> Hi :)
>
> I recently discovered the (ice-9 peg) module, and it seems really
> useful. However I have to admit I struggle a bit with processing the
> tree produced by it.
>
> Let me put forward an example:
>
> --8<---------------cut here---------------start------------->8---
> (use-modules (ice-9 peg))
>
> (define-peg-pattern Opt body
> (and Name Space (+ (and Enum Space)) Default))
>
> (define-peg-pattern Name all
> (+ Alpha))
> (define-peg-pattern Enum all
> (+ Alpha))
> (define-peg-pattern Default all
> (and (ignore "(default: ") (* Alpha) (ignore ")")))
>
> (define-peg-pattern Alpha body
> (range #\a #\z))
> (define-peg-pattern Space none
> " ")
> --8<---------------cut here---------------end--------------->8---
>
> Now I can feed it few strings to get the parsed trees:
>
> --8<---------------cut here---------------start------------->8---
> scheme@(guile-user)> (peg:tree (match-pattern Opt "name a b c (default: a)"))
> $11 = ((Name "name") ((Enum "a") (Enum "b") (Enum "c")) (Default "a"))
> scheme@(guile-user)> (peg:tree (match-pattern Opt "name a b c (default: )"))
> $12 = ((Name "name") ((Enum "a") (Enum "b") (Enum "c")) Default)
> scheme@(guile-user)> (peg:tree (match-pattern Opt "name a (default: a)"))
> $13 = ((Name "name") (Enum "a") (Default "a"))
> scheme@(guile-user)> (peg:tree (match-pattern Opt "name a (default: )"))
> $14 = ((Name "name") (Enum "a") Default)
> --8<---------------cut here---------------end--------------->8---
>
> What we see here is that it produces trees in 4 different "shapes",
> depending on the input string. And I am unsure how to reasonably
> process them.
>
> My current approach looks like:
>
> --8<---------------cut here---------------start------------->8---
> (define process-tree
> (match-lambda
> ((('Name name) (('Enum enum) ...) ('Default def))
> (pk name enum def))
> ((('Name name) ('Enum enum) ... ('Default def))
> (pk name enum def))
> ((('Name name) (('Enum enum) ...) 'Default)
> (pk name enum #f))
> ((('Name name) ('Enum enum) ... 'Default)
> (pk name enum #f))))
> --8<---------------cut here---------------end--------------->8---
>
> Which *does* work:
>
> --8<---------------cut here---------------start------------->8---
> scheme@(guile-user)> (for-each process-tree (list $11 $12 $13 $14))
>
> ;;; ("name" ("a" "b" "c") "a")
>
> ;;; ("name" ("a" "b" "c") #f)
>
> ;;; ("name" ("a") "a")
>
> ;;; ("name" ("a") #f)
> --8<---------------cut here---------------end--------------->8---
>
> However it is very repetitive and the code is pretty hard to work with.
> (Real code even has few more branches.) And I am not sure how to do a
> better job at this.
>
> It would really simplify things if I would be able to *always* have a
> list of 'Enum, even when there is just a single one. Same for default.
> I tried keyword-flatten, but probably I just do not understand how to
> use it. Or maybe (ice-9 match) is just a wrong tool here?
>
> Would someone be able to provide general guidance how to approach this,
> or at least recommend some projects using (ice-9 peg) that I could
> study?
>
> Thanks and have a nice day,
> Tomas
Hello Tomas,
Without getting into the details, perhaps my peg utils module could be useful
for you:
https://codeberg.org/ZelphirKaltstahl/advent-of-code-2024/src/commit/3f64ee60297ccc89085c9425522bd8b8e83fe99d/utils/peg-tree-utils.scm
Best regards, Zelphir
--
repositories:https://notabug.org/ZelphirKaltstahl,https://codeberg.org/ZelphirKaltstahl
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How to work with the tree produced by (ice-9 peg)'s match-pattern?
2024-12-05 1:24 ` Zelphir Kaltstahl
@ 2024-12-06 19:45 ` Tomas Volf
2024-12-06 20:16 ` Zelphir Kaltstahl
0 siblings, 1 reply; 4+ messages in thread
From: Tomas Volf @ 2024-12-06 19:45 UTC (permalink / raw)
To: Zelphir Kaltstahl; +Cc: Guile User
[-- Attachment #1: Type: text/plain, Size: 635 bytes --]
Hello,
Zelphir Kaltstahl <zelphirkaltstahl@posteo.de> writes:
> Without getting into the details, perhaps my peg utils module could be useful
> for you:
>
> https://codeberg.org/ZelphirKaltstahl/advent-of-code-2024/src/commit/3f64ee60297ccc89085c9425522bd8b8e83fe99d/utils/peg-tree-utils.scm
That does definitely look very useful, thank you for sharing it. May I
ask what the license for the code is? There does not seem to be COPYING
file nor a header in the file itself.
Have a nice day,
Tomas
--
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 853 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How to work with the tree produced by (ice-9 peg)'s match-pattern?
2024-12-06 19:45 ` Tomas Volf
@ 2024-12-06 20:16 ` Zelphir Kaltstahl
0 siblings, 0 replies; 4+ messages in thread
From: Zelphir Kaltstahl @ 2024-12-06 20:16 UTC (permalink / raw)
To: ~; +Cc: Guile User
On 06.12.24 20:45, Tomas Volf wrote:
> Hello,
>
> Zelphir Kaltstahl<zelphirkaltstahl@posteo.de> writes:
>
>> Without getting into the details, perhaps my peg utils module could be useful
>> for you:
>>
>> https://codeberg.org/ZelphirKaltstahl/advent-of-code-2024/src/commit/3f64ee60297ccc89085c9425522bd8b8e83fe99d/utils/peg-tree-utils.scm
> That does definitely look very useful, thank you for sharing it. May I
> ask what the license for the code is? There does not seem to be COPYING
> file nor a header in the file itself.
>
> Have a nice day,
> Tomas
Hi Tomas!
Good point, I should add a license file.
I did not add one before, because I did not consider the AoC code to be
something worth protecting, but since I use several "utils" libraries, maybe
adding a license file is a good idea.
Usually all my code written in my free time is GPLv3 (or later) or AGPL3 (or later).
I don't think that particular peg tree utils code is that special, so feel free
to use however you want to use it.
Best regards, Zelphir
--
repositories:https://notabug.org/ZelphirKaltstahl,https://codeberg.org/ZelphirKaltstahl
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-12-06 20:16 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-04 12:06 How to work with the tree produced by (ice-9 peg)'s match-pattern? Tomas Volf
2024-12-05 1:24 ` Zelphir Kaltstahl
2024-12-06 19:45 ` Tomas Volf
2024-12-06 20:16 ` Zelphir Kaltstahl
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).