Re: irregex match objects

2022-03-04 Thread asebian
> I haven't looked very deep into this, but it seems the replacement
> lambda is called with the same match object for every substitution.
> You don't seem to distinguish between the different matches in any
> way - the result of the lambda will always be the same.

Thanks, felix for having a glimpse. And an even bigger 'thank you' for
initializing and sharing chicken!

Problem solved!
`irregex-match-valid-index?` does not tell us whether the match
happened at a certain index, and the documentation even says so:
"Returns #t iff [sic] the index-or-name named submatch or index is
defined in the match object."
Valid means defined here - not matched.
I have written a function that dumps the content of the match object.
Of course all the indices appearing in the dump are defined as
indicated by *. Actually I am not quite sure what the purpose of
`irregex-match-valid-index?` is.

Cheers!

#!/usr/bin/env -S csi -s

(import (chicken format)
(chicken irregex)
(chicken string)
srfi-1)

(define (dump match-obj port)
  (let ((match? (irregex-match-data? match-obj)))
(when match?
  (fprintf port
   "match-data? ~A~N"
   match?)
  (letrec ((num (irregex-match-num-submatches match-obj))
   (names (irregex-match-names match-obj))
   (iter (lambda (index)
   (when (<= index num)
 (fprintf port
  "~A ~A, ~A: '~A'~N"
  (if (irregex-match-valid-index?
   match-obj index)
  #\*
  #\space)
  index
  (string-intersperse
   (map car
(filter (lambda (pair)
(= index (cdr pair)))
  names))
   ", ")
  (irregex-match-substring match-obj
   index))
 (iter (add1 index))
(fprintf port
 "num-submatches: ~A~N"
 num)
(iter 0)))
(display (make-string 32 #\-) port)
(newline port))
  (void))

(display
 (irregex-replace/all
  '(: bow
  (or (=> "f" (w/nocase "fred"))
  (=> "w" (w/nocase "wilma")))
  eow)
  "fred dino wilma barney"
  (lambda (match-obj)
(dump match-obj (current-output-port))
(letrec ((iter (lambda (alist)
 (unless (null? alist)
   (let ((pair (car alist)))
 (assert (pair? pair))
 (if (irregex-match-substring match-obj
  (cdr pair))
 (let ((name (car pair)))
   (assert (string? name))
   (cond
((string=? name "f") "Wilma")
((string=? name "w") "Fred")
(else (error "unknown named match"
 name
 (iter (cdr alist
  (iter (irregex-match-names match-obj))
(newline)



Chicken 5 on Cygwin

2022-03-04 Thread Claude Marinier
AllĂ´,

I see that Cygwin includes Chicken 4. This is not a problem at home where I
can compile from sources. At work, I am working on approval for Cygwin as a
whole. It would be nice if Cygwin included Chicken 5.

Who is responsible for this? Is it the Chicken community?

Assuming we have to do this ourselves. How difficult is it to upgrade?

Merci.

-- 
Claude Marinier


Re: irregex match objects

2022-03-04 Thread felix . winkelmann
> Under these assumptions I concluded that it should be possible to
> detect what has been matched by checking whether the indices of the
> assoc. list are valid in the current match object, stopping after the
> first successful test.
>
> (import  (chicken irregex))
> ;;; swapping fred and wilma
> (irregex-replace/all '(: bow
>  (or (=> "f" (w/nocase "fred"))
>  (=> "w" (w/nocase "wilma")))
>  eow)
>  "fred dino wilma barney"
>  (lambda (match-obj)
>(assert match-obj)
>(letrec ((iter (lambda (alist)
> (unless (null? alist)
>   (let ((pair (car alist)))
> (assert (pair? pair))
> (if
> (irregex-match-valid-index? match-obj (cdr pair))
> (let ((name (car pair)))
>   (assert (string? name))
>   (cond
>((string=? name "f") 
> "Wilma")
>((string=? name "w") 
> "Fred")
>(else (error
> "unknown named match" name
> (iter (cdr alist
>  (iter (irregex-match-names match-obj)
>
> To my suprise the result is "Fred dino Fred barney" instead of "Wilma
> dino Fred barney". What did I miss?

I haven't looked very deep into this, but it seems the replacement
lambda is called with the same match object for every substitution.
You don't seem to distinguish between the different matches in any
way - the result of the lambda will always be the same.


felix