This program aims to restore the guile REPL session dump (see input file).

When I added the while loop, ELISP stated giving me this error when the end of the file is reacher:

Scan error: "Containing expression ends prematurely", 715, 716

Can someone please tell me what the problem is?

This is the program:


(defun retore-guile-session()
  (while (progn (re-search-forward "scheme@(guile-user)" (point-max) t))
     (append-to-buffer "scratch" (find-start-of-sexp)

(defun find-start-of-sexp ()

   (re-search-forward "(" (point-max) t)

(defun find-end-of-sexp ()

And this is the input file?


GNU Guile 2.0.11
Copyright (C) 1995-2014 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.
scheme@(guile-user)> (+ 1 2)
$2 = 3
scheme@(guile-user)> (+1 2)
ERROR: In procedure 1:
ERROR: Wrong type to apply: 1

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]> 
scheme@(guile-user) [1]> 
scheme@(guile-user) [1]> (+ 1 2 (*
                 2 3))
$3 = 9
scheme@(guile-user) [1]> 

Can someone please tell me, why is ELISP giving me error Scan error: "Containing expression ends prematurely", 715, 716 when the while loop reaches the end of the file?

Thanks in advance.

  • In case of errors, activate debug-on-error (you can use M-x toggle-debug-on-error) to get a backtrace. – YoungFrog Mar 21 '16 at 9:03

You specified t for the no-error argument of re-search-forward in find-start-of-sexp. This is the case where re-search-forward just returns nil if there is no match. Note, that point is not moved in this case.

The last occurrence of scheme@(guile-user) is not followed by a sexp embraced by parenthesis. Therefore, the search for ( fails and point remains behind scheme@(guile-user).

After the search you go one char backwards in find-start-of-sexp and end up before the ) of scheme@(guile-user). Therefore, the next forward-sexp naturally fails and gives the scan error you wonder about.

One easy cure of the problem would be to replace t in re-search-forward by some other non-nil symbol like 'noerr. In this case point would be placed at end of buffer if the search fails. But, I wonder whether this is what you really want. For an example: What happens with the first occurrences of scheme@(guile-user)? The next pair of parenthesis are the argument delimiters of the next scheme@(guile-user)!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.