unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* sxml-match bad pattern syntax
@ 2021-12-28  6:21 Thien-Thi Nguyen
  2021-12-28  7:19 ` tomas
  0 siblings, 1 reply; 6+ messages in thread
From: Thien-Thi Nguyen @ 2021-12-28  6:21 UTC (permalink / raw)
  To: guile-user


[-- Attachment #1.1: Type: text/plain, Size: 328 bytes --]


In an effort to join the current millennium, i have started to
play w/ Guile 2.x's SXML facilities.  Good stuff!  I've run into
a problem, however, and hope i can get help resolving it here.

The following small program attempts to use ‘sxml-match’ to
remove an unwanted attribute from a (well-formed) SXML snippet.

[-- Attachment #1.2: UNBOGUS --]
[-- Type: text/plain, Size: 303 bytes --]

#!/usr/bin/guile -s
!#
(use-modules
 (sxml match))

(define (unbogus x)
  (sxml-match x
    ((a (@ . ,attrs) ...)
     `(a (@ ,@(delete '(shape "rect") attrs)) ...))))

(define bad '(a (@ (shape "rect") (href "foo.html")) "kid"))

(pk bad)

(pk (unbogus bad))
;; expect: (a (@ (href "foo.html")) "kid")

[-- Attachment #1.3: Type: text/plain, Size: 754 bytes --]


When i run it, i see it fail w/ error message:

 sxml/sxml-match.ss:342:31: Throw to key `sxml-match-error'
 with args `(#f "bad pattern syntax (symbol not allowed
 in this context)" [...]

(output folded and truncated).  To my untrained eye, the form
looks similar to the examples in Info node ‘(guile) sxml-match’
but evidently i am missing something.  What?

-- 
Thien-Thi Nguyen -----------------------------------------------
 (defun responsep (query)               ; (2021) Software Libero
   (pcase (context query)               ;       = Dissenso Etico
     (`(technical ,ml) (correctp ml))
     ...))                              748E A0E8 1CB8 A748 9BFA
--------------------------------------- 6CE4 6703 2224 4C80 7502


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 219 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: sxml-match bad pattern syntax
  2021-12-28  6:21 sxml-match bad pattern syntax Thien-Thi Nguyen
@ 2021-12-28  7:19 ` tomas
  2021-12-28  7:58   ` Thien-Thi Nguyen
  0 siblings, 1 reply; 6+ messages in thread
From: tomas @ 2021-12-28  7:19 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 1534 bytes --]

On Tue, Dec 28, 2021 at 01:21:31AM -0500, Thien-Thi Nguyen wrote:
> 
> In an effort to join the current millennium,

:-)

>                                             i have started to
> play w/ Guile 2.x's SXML facilities.  Good stuff!  I've run into
> a problem, however, and hope i can get help resolving it here.
> 
> The following small program attempts to use ‘sxml-match’ to
> remove an unwanted attribute from a (well-formed) SXML snippet.

> #!/usr/bin/guile -s
> !#
> (use-modules
>  (sxml match))
> 
> (define (unbogus x)
>   (sxml-match x
>     ((a (@ . ,attrs) ...)
>      `(a (@ ,@(delete '(shape "rect") attrs)) ...))))

I /think/ the ellipsis is at a wrong place there. Note that I'm coming
from "traditional" match, and I just have wrapped half of my brain
around that (which, BTW, is somewhat painful :-)

So take this with a grain of salt.

* Phenomenology (aka: I barely know what I'm doing):

If you replace your ellipses above by ". ,rest", things seem to work:

 (define (unbogus x)
   (sxml-match x
     ((a (@ . ,attrs) . ,rest)
      `(a (@ ,@(delete '(shape "rect") attrs)) . ,rest))))

 (unbogus '(a (@ (shape "rect") (href "foo.html")) "kid"))

 => (a (@ (href "foo.html")) "kid")

* Philosophy (aka blah, blah)

If those ellipses resemble what match do, I think they are wrong there:

  (@ . ,attrs) ...

would mean zero or more times the shape "(@ . ,attrs)". I think this
isn't what you want.

Put grains of salt everywhere :)

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: sxml-match bad pattern syntax
  2021-12-28  7:19 ` tomas
@ 2021-12-28  7:58   ` Thien-Thi Nguyen
  2021-12-28  8:28     ` tomas
  0 siblings, 1 reply; 6+ messages in thread
From: Thien-Thi Nguyen @ 2021-12-28  7:58 UTC (permalink / raw)
  To: guile-user


[-- Attachment #1.1: Type: text/plain, Size: 1479 bytes --]


() <tomas@tuxteam.de>
() Tue, 28 Dec 2021 08:19:04 +0100

   I /think/ the ellipsis is at a wrong place there. Note that
   I'm coming from "traditional" match, and I just have wrapped
   half of my brain around that (which, BTW, is somewhat painful
   :-)

Haha, i know exactly how you feel.

   So take this with a grain of salt.

   * Phenomenology (aka: I barely know what I'm doing):

   If you replace your ellipses above by ". ,rest", things seem
   to work:

    (define (unbogus x)
      (sxml-match x
        ((a (@ . ,attrs) . ,rest)
         `(a (@ ,@(delete '(shape "rect") attrs)) . ,rest))))

    (unbogus '(a (@ (shape "rect") (href "foo.html")) "kid"))

    => (a (@ (href "foo.html")) "kid")

Cool.  I guest the ". ,rest" is a shortcut for the ellipses.

   * Philosophy (aka blah, blah)

   If those ellipses resemble what match do, I think they are
   wrong there:

     (@ . ,attrs) ...

   would mean zero or more times the shape "(@ . ,attrs)". I
   think this isn't what you want.

Ah, right!  The ellipses are a tail that need to follow a head.
I guess i was confused by the documentation's use of ellipses in
the conventional sense rather than the literal sense:

 (define (album->html x)
   (sxml-match x
     [(album (@ (title ,t)) (catalog (num ,n) (fmt ,f)) ...)
      `(ul (li ,t)
           (li (b ,n) (i ,f)) ...)]))

Anyway, thanks to your help, i've managed to cobble together the
following code, attached here for the benefit of future self:

[-- Attachment #1.2: UNBOGUS --]
[-- Type: text/plain, Size: 559 bytes --]

#!/usr/bin/guile -s
!#
(use-modules
 (ice-9 pretty-print)
 (sxml match))

(define (pp x)
  (pretty-print x)
  (newline))

(define (unbogus x)
  (sxml-match x
    ((a (@ . ,attrs) . ,rest)
     `(a (@ ,@(delete '(shape "rect") attrs)) . ,rest))
    (,otherwise
     (if (string? x)
         x
         `(,(car x) ,(cadr x)
           ,@(map unbogus (cddr x)))))))

(define one '(a (@ (shape "rect") (href "foo.html")) "kid"))
(newline)
(pp one)
(pp (unbogus one))

(define bad `(p (@) "some text and " ,one " and " ,one))
(newline)
(pp bad)
(pp (unbogus bad))

[-- Attachment #1.3: Type: text/plain, Size: 441 bytes --]

Maybe it's idiomatic.  Feedback on how to improve it welcome!

-- 
Thien-Thi Nguyen -----------------------------------------------
 (defun responsep (query)               ; (2021) Software Libero
   (pcase (context query)               ;       = Dissenso Etico
     (`(technical ,ml) (correctp ml))
     ...))                              748E A0E8 1CB8 A748 9BFA
--------------------------------------- 6CE4 6703 2224 4C80 7502


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 219 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: sxml-match bad pattern syntax
  2021-12-28  7:58   ` Thien-Thi Nguyen
@ 2021-12-28  8:28     ` tomas
  2021-12-28  9:21       ` Thien-Thi Nguyen
  0 siblings, 1 reply; 6+ messages in thread
From: tomas @ 2021-12-28  8:28 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 1880 bytes --]

On Tue, Dec 28, 2021 at 02:58:56AM -0500, Thien-Thi Nguyen wrote:
> 
> () <tomas@tuxteam.de>
> () Tue, 28 Dec 2021 08:19:04 +0100
> 
>    I /think/ the ellipsis is at a wrong place there [...]

> Haha, i know exactly how you feel.

%-)

> Ah, right!  The ellipses are a tail that need to follow a head.

No, the ellipses tell the matcher that the symbol to its left acutally
stands for "zero or more of this". Consequently, you have to somehow [1]
use those ellipses on the right hand side (aka template) whenever you use
them on the left hand side (aka pattern) and vice versa.

> I guess i was confused by the documentation's use of ellipses in
> the conventional sense rather than the literal sense:

I think of the ellipses as a kind of funny Kleene star. Not really at
home in any world: in the regular world, because it isn't a star, in
the Scheme world, because it is postfix.

So, to come back on your original example,

  (@ . ,attrs) ...

would match either empty (remember: zero or more) or

  (@ (foo 1) (bar 2))

or

  (@ (meep 1)) (@ "this ain't what we expected))

... so zero or more of (@ . stuff) where stuff stands for any
S-expression (note that it's sloppy: it will match things which aren't
lists of well-formed attribute lists [2]).

It won't match

  (@ (foo 1)) (@ (bar 2)) ("rumpelstilzchen")

for example.

> Anyway, thanks to your help, i've managed to cobble together the
> following code, attached here for the benefit of future self:

[...]

Thanks. And thanks for the code :)

> Maybe it's idiomatic.  Feedback on how to improve it welcome!

For the idiomatic part, I'll have to defer to those with more chevrons
;-)

Cheers

[1] Yes, some handwaving here.
[2] which isn't what you want to match, anyway. You want to match things
   with one attribute list in them.

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: sxml-match bad pattern syntax
  2021-12-28  8:28     ` tomas
@ 2021-12-28  9:21       ` Thien-Thi Nguyen
  2021-12-28 10:06         ` tomas
  0 siblings, 1 reply; 6+ messages in thread
From: Thien-Thi Nguyen @ 2021-12-28  9:21 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 1709 bytes --]


() <tomas@tuxteam.de>
() Tue, 28 Dec 2021 09:28:32 +0100

   > Ah, right!  The ellipses are a tail that need to follow a
   > head.

   No, the ellipses tell the matcher that the symbol to its left
   acutally stands for "zero or more of this". Consequently, you
   have to somehow [1] use those ellipses on the right hand side
   (aka template) whenever you use them on the left hand side
   (aka pattern) and vice versa.

I think we're saying the same thing, but in any case, i
understand what you're saying.

   > I guess i was confused by the documentation's use of
   > ellipses in the conventional sense rather than the literal
   > sense:

   I think of the ellipses as a kind of funny Kleene star. Not
   really at home in any world: in the regular world, because it
   isn't a star, in the Scheme world, because it is postfix.

That's a really good analogy, IMHO.  It's an operator of sorts.

   [examples]

Yes, it's becoming more clear to me now.

   For the idiomatic part, I'll have to defer to those with more
   chevrons ;-)

:-D

I think the ‘otherwise’ clause is a bit unwieldy; it presumes
normal form 3 (attribute lists always present).  However, i
couldn't find a way to match "any element".  ISTM the element
portion (first symbol in the form) MUST be non-variable.

-- 
Thien-Thi Nguyen -----------------------------------------------
 (defun responsep (query)               ; (2021) Software Libero
   (pcase (context query)               ;       = Dissenso Etico
     (`(technical ,ml) (correctp ml))
     ...))                              748E A0E8 1CB8 A748 9BFA
--------------------------------------- 6CE4 6703 2224 4C80 7502


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 219 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: sxml-match bad pattern syntax
  2021-12-28  9:21       ` Thien-Thi Nguyen
@ 2021-12-28 10:06         ` tomas
  0 siblings, 0 replies; 6+ messages in thread
From: tomas @ 2021-12-28 10:06 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 1016 bytes --]

On Tue, Dec 28, 2021 at 04:21:39AM -0500, Thien-Thi Nguyen wrote:
> 
> () <tomas@tuxteam.de>
> () Tue, 28 Dec 2021 09:28:32 +0100
> 
>    > Ah, right!  The ellipses are a tail that need to follow a
>    > head.
> 
>    No, the ellipses tell the matcher that the symbol to its left

[...]

> I think we're saying the same thing, but in any case, i
> understand what you're saying.

I see now. I was mis-reading you.

[...]

> I think the ‘otherwise’ clause is a bit unwieldy; it presumes
> normal form 3 (attribute lists always present).  However, i
> couldn't find a way to match "any element".  ISTM the element
> portion (first symbol in the form) MUST be non-variable.

At first blush it seems so. At least, when I try [1], it yells at
me with something something

  Throw to key `sxml-match-error' with args `(#f "bad pattern syntax (not an element pattern)" ...

Hm.

Cheers

[1] what I try is matching the tag to an unquoted var, like
   so (match ((,tag ...))).
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-12-28 10:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-28  6:21 sxml-match bad pattern syntax Thien-Thi Nguyen
2021-12-28  7:19 ` tomas
2021-12-28  7:58   ` Thien-Thi Nguyen
2021-12-28  8:28     ` tomas
2021-12-28  9:21       ` Thien-Thi Nguyen
2021-12-28 10:06         ` tomas

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