help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Eval in templates - Re: How to tame compiler?


From: Jean Louis
Subject: Eval in templates - Re: How to tame compiler?
Date: Mon, 3 May 2021 09:51:35 +0300
User-agent: Mutt/2.0.6 (2021-03-06)

* Eduardo Ochs <eduardoochs@gmail.com> [2021-05-03 04:59]:
> just as a curiosity, here is how eev implements template
> interpolation:
> 
>   http://angg.twu.net/eev-current/eev-template0.el.html
>   http://angg.twu.net/eev-current/eev-template0.el
> 
> I tried several other implementations of template interpolation
> functions before that one, and from time to time I would find another
> way that would be both simpler and more powerful than the previous
> one... the one above is so simple that in one of the times that I gave
> a minicourse on LaTeX and Emacs I followed the code of `ee-template0'
> with the students in the third day of the minicourse - one day after
> teaching them briefly how to understand code with `defun's and
> `let's.

That is so interesting. This templating system has slightly different
concept. In my eval-ing, I have to get some value no matter what, I am
also thinking I have freedom to make errors, to the system is not
allowed to fail.

(setq rcd-template-delimiter-open "(") ⇒ "("
(setq rcd-template-delimiter-close ")") ⇒ ")"
(setq text1 "We will evaluate (+ 2 2) to result ((+ 2 2))") ;; won't interpolate
(setq text2 "We will evaluate (+ 2 2) to result ( (+ 2 2) )")
(setq text3 "We will evaluate (+ 2 2) to result ( (+ 2 2 )") ;; error allowed

This does not interpolate, as I demand spaces after delimiters:
(rcd-template-eval text1) ⇒ "We will evaluate (+ 2 2) to result ((+ 2 2))"

This does interpolate:
(rcd-template-eval text2) ⇒ "We will evaluate (+ 2 2) to result 4"

Error is interpolated to "", but I could log it in future:
(rcd-template-eval text3) ⇒ "We will evaluate (+ 2 2) to result "

Compared to this that works:
(ee-template0 "{<} a{(+ 2 3)} {>}") ⇒ "{ a5 }"

And this one giving error:
(ee-template0 "{<} a{(+ 2 3} {>}")

Concept is the same. You also do not use dynamic binding, which
is quite clear why.

In the sense of teaching is fine. I guess you almost never had a
problem when evaluation fails for some reason.

In the sense of generation of hundreds of emails for people, or
generation of thousands of HTML pages, the template system IMHO
is not allowed to fail. Errors however, could be reported,
templating system could then tell me in which page or email the
error has been found. But it has to move silently over the failure.

Thus I would expect also this one, to produce following:
(ee-template0 "{<} a{(+ 2 3} {>}")  ⇒ "{ a }"

And more perfect would be if error would be reported somewhere in silent.

> My trick to handle lexical binding was to tell the students that
> dynamic binding is much easier to understand than lexical binding, and
> that they should treat dynamic binding as an advanced topic that would
> make more sense when they had at least one week of experience with
> Emacs - and my minicourse was only five days long, from monday to
> friday... but the students in that minicourse were mathematicians with
> little experience in programming, not compsci people who would see
> immediately that dynamic binding is "wrong".

Thus you had the luck to have a different type of audience.

Here is also demonstrated how eev tools have different context,
programs are invoked from within text manuall. Your tools are in
general, beyond templating, meant to be evaluated in the text or
within the text and in that concept there is also no need for
lexical binding. Small snippets are evaluated one after each
other by user's demand, functions evaluated may come originally
from programs with lexical binding.

Thus I have improved my function to evalute with errors reported,
as it makes so much more sense. It is made to interpolate
thousands of times within minutes automatically.

Source: https://gnu.support/files/sources/rcd-template.el

(defun rcd-template-eval (string &optional delimiters)
  "Evaluates Emacs Lisp enclosed by `rcd-template-delimiter-open' and 
`rcd-template-delimiter-close'.

Optional DELIMITERS list may be provided to change default
delimiters, first list member has to be the opening delimiter and
second the closing delimiter.

Space or new line has to follow `rcd-template-delimiter-open' and
precede `rcd-template-delimiter-close' for evaluation to get
invoked."
  (let* ((delimiters (or delimiters (list rcd-template-delimiter-open 
rcd-template-delimiter-close)))
         (open (car delimiters))
         (close (cadr delimiters))
         (add (length open))
         (minus (length close)))
    (with-temp-buffer
      (insert string)
      (goto-char 0)
      (while (re-search-forward (rx (literal open)
                                    (one-or-more (or blank "\n"))
                                    (group (minimal-match (one-or-more 
anything)))
                                    (one-or-more (or blank "\n"))
                                    (literal close))
                                nil t)
        (let* ((match-beginning (match-beginning 0))
               (match-end (match-end 0))
               (match (buffer-substring-no-properties
                       (+ add match-beginning) (- match-end minus)))
               (lisp (condition-case error
                         (read-from-string match)
                       (error (princ (format "rcd-template-eval: 
`read-from-string' error: %s" error))
                              "")))
               (lisp (if (listp lisp) (car lisp) lisp))
               (value (condition-case error
                          (eval lisp)
                        (error (princ (format "rcd-template-eval: `eval' error: 
%s" error)))))
               (value (cond ((null value) "")
                            (t (format "%s" value)))))
          (delete-region match-beginning match-end)
          (insert (format "%s" value))))
      (buffer-string))))

(rcd-template-eval "( (+ 2 2) )" '("(" ")")) ⇒ "4"

(rcd-template-eval "( (+ 2 2 )" '("(" ")")) ⇒ "" 
     with error reporting in *Messages* buffer:
     rcd-template-eval: `read-from-string' error: (end-of-file)

(rcd-template-eval "We expect 4 but fail: ( (+2 2) )" '("(" ")")) ⇒ "We expect 
4 but fail: "
     with error reporting in *Messages* buffer:
     rcd-template-eval: `eval' error: (invalid-function 2)

(defun ee-template-test (text)
  (rcd-template-eval text '("{" "}")))

My regexp asks for spaces or new lines between delimiters:

(ee-template-test "{<} a{ (+ 2 3 } {>}") ⇒ "{<} a {>}"
     with error reporting in *Messages* buffer:
     rcd-template-eval: `read-from-string' error: (end-of-file)

but of course, it does not do your trick:
(ee-template-test "{<} a{ (+ 2 3) } {>}") ⇒ "{<} a5 {>}"

If I would like to enclose it with open delimiters in a result, this would work:
(ee-template-test "{ \"{\" } a{ (+ 2 3) } { \"}\" }") ⇒ "{ a5 }"

And I use the habit that people would not tend to write space
after parenthesis.

I could write: (maybe I am wrong), but I would never write 
( maybe I am not wrong ).

That may allow using parenthesis in templates which makes it more
Lisp-like:

(rcd-template-eval "We know that (+ 2 2) evaluates to ( (+ 2 2) )") ⇒ "We know 
that (+ 2 2) evaluates to 4"

Then `eval' within `eval' help in avoiding repetition of demonstrated code:
(rcd-template-eval "We know that ( (setq code '(+ 2 2)) ) evaluates to ( (eval 
code) )") ⇒ "We know that (+ 2 2) evaluates to 4"

Regular expression is I see hard coded:
(defvar ee-template00-re "{\\([^{}]+\\)}")

And I have envisioned it to rather have flexible delimiters:

(rcd-template-eval "<% user-login-name %>" '("<%" "%>")) ⇒ "jimmy"

as it emulates some other templating engines and allows re-use of templates:
(rcd-template-eval "<% @var user-login-name %>" '("<% @var" "%>")) ⇒ "jimmy"

Maybe you wish to include error reporting and silent failure as
well, or no?

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://stallmansupport.org/
https://rms-support-letter.github.io/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]