On Sat Nov 29, 2025 at 5:58 PM CET, Alexey Egorov via Chicken-users wrote:
> Hello.
> I'm using Chicken 5.4.0. I tried to use read-with-source-info and got
> quite strange results. Despite reading from file (by means of
> with-input-from-file), subsequent calls to get-line-number report
> input file as <stdin> or even #f. The following example demonstrates
> the issue:
> test.txt:
> (a b c d e)
> (1 2 3 4 5)
> (a b c)
>[...]
>
> The result is the same on both Linux and macOS. Replacing
> (read-with-source-info) with explicit (read-with-source-info
> (current-input-port)) doesn't change anything.
> Searching the bugtracker didn't yield any results. Should I file a
> report there, by the way?

Well, read-with-source-info is a bit of a hack, intended for use
mainly in the compiler and interpreter and only of partial usefulness
for user code. The way it works is this: when reading an s-expression
that is a list and that starts with a symbol, we attach line number
info to the symbol, since that is the only object we can store
arbitrary data on (the property list of the symbol to be precise).
We obtain the current source file and line number from internal
variables, first the filename from some unexposed variable that
is set when the compiler opens a file, or when the interpreter
loads source code, second the line number from a "port position",
a counter attached to the input port.

One thing you can do is set the variable "##sys#current-source-filename",
which will at least give you a proper filename in the source info,
but remember that this only works for lists with a symbol in the
first position.

I will add another optional argument to read-with-source-info
to pass a filename, but the functionality of this will
always be only partially complete, since we don't have the ability
to store arbitrary info for each read expression (regardless
of type).

I hope this helps a little.

>
> One more thing, markup in the corresponding section of the manual is
> evidently broken (on the wiki too:
> https://wiki.call-cc.org/man/5/Module (chicken
> syntax)#read-with-source-info/).
> Here's the patch:
> diff --git a/manual/Module (chicken syntax) b/manual/Module (chicken syntax)
> index 19e550be..48127201 100644
> --- a/manual/Module (chicken syntax)
> +++ b/manual/Module (chicken syntax)
> @@ -368,10 +368,10 @@ renamed.
>
>  <procedure>(read-with-source-info [port])</procedure>
>
> -Exactly like {{{read}}} from the {{{scheme}}} module, except it
> +Exactly like {{read}} from the {{scheme}} module, except it
>  registers the expression it read into the line number database, so
> -that if {{{(read-with-source-info)}}} returns {{{OBJ}}},
> -{{{(get-line-number OBJ)}}} will return the line number in {{{port}}}.
> +that if {{(read-with-source-info)}} returns {{OBJ}},
> +{{(get-line-number OBJ)}} will return the line number in {{port}}.
>
>  The port argument may be omitted, in which case it defaults to the
>  value returned by {{current-input-port}}. It is an error to read from

Thanks a lot, I have applied these changes.


cheers,
felix


Reply via email to