1
0
Fork 0
cl-sites/guile.html_node/Annotated-Scheme-Read.html

119 lines
5.9 KiB
HTML
Raw Normal View History

2024-12-17 12:49:28 +01:00
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This manual documents Guile version 3.0.10.
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
Inc.
Copyright (C) 2021 Maxime Devos
Copyright (C) 2024 Tomas Volf
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
copy of the license is included in the section entitled "GNU Free
Documentation License." -->
<title>Annotated Scheme Read (Guile Reference Manual)</title>
<meta name="description" content="Annotated Scheme Read (Guile Reference Manual)">
<meta name="keywords" content="Annotated Scheme Read (Guile Reference Manual)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content=".texi2any-real">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Read_002fLoad_002fEval_002fCompile.html" rel="up" title="Read/Load/Eval/Compile">
<link href="Scheme-Write.html" rel="next" title="Scheme Write">
<link href="Scheme-Read.html" rel="prev" title="Scheme Read">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Annotated-Scheme-Read">
<div class="nav-panel">
<p>
Next: <a href="Scheme-Write.html" accesskey="n" rel="next">Writing Scheme Values</a>, Previous: <a href="Scheme-Read.html" accesskey="p" rel="prev">Reading Scheme Code</a>, Up: <a href="Read_002fLoad_002fEval_002fCompile.html" accesskey="u" rel="up">Reading and Evaluating Scheme Code</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Reading-Scheme-Code_002c-For-the-Compiler"><span>6.16.3 Reading Scheme Code, For the Compiler<a class="copiable-link" href="#Reading-Scheme-Code_002c-For-the-Compiler"> &para;</a></span></h4>
<p>When something goes wrong with a Scheme program, the user will want to
know how to fix it. This starts with identifying where the error
occurred: we want to associate a source location with each component part
of source code, and propagate that source location information through
to the compiler or interpreter.
</p>
<p>For that, Guile provides <code class="code">read-syntax</code>.
</p>
<dl class="first-deffn">
<dt class="deffn" id="index-read_002dsyntax"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">read-syntax</strong> <var class="def-var-arguments">[port]</var><a class="copiable-link" href="#index-read_002dsyntax"> &para;</a></span></dt>
<dd><p>Read an s-expression from the input port <var class="var">port</var>, or from the current
input port if <var class="var">port</var> is not specified.
</p>
<p>If, after skipping white space and comments, no more bytes are available
from <var class="var">port</var>, return the end-of-file object. See <a class="xref" href="Binary-I_002fO.html">Binary I/O</a>.
Otherwise, return an annotated datum. An annotated datum is a syntax
object which associates a source location with a datum. For example:
</p>
<div class="example">
<pre class="example-preformatted">(call-with-input-string &quot; foo&quot; read-syntax)
; &rArr; #&lt;syntax:unknown file:1:2 foo&gt;
(call-with-input-string &quot;(foo)&quot; read-syntax)
; &rArr;
; #&lt;syntax:unknown file:1:0
; (#&lt;syntax unknown file:1:1 foo&gt;)&gt;
</pre></div>
<p>As the second example shows, all fields of pairs and vectors are also
annotated, recursively.
</p></dd></dl>
<p>Most users are familiar with syntax objects in the context of macros,
which use syntax objects to associate scope information with
identifiers. See <a class="xref" href="Macros.html">Macros</a>. Here we use syntax objects to associate
source location information with any datum, but without attaching scope
information. The Scheme compiler (<code class="code">compile</code>) and the interpreter
(<code class="code">eval</code>) can accept syntax objects directly as input, allowing them
to associate source information with resulting code.
See <a class="xref" href="Compilation.html">Compiling Scheme Code</a>, and See <a class="xref" href="Fly-Evaluation.html">Procedures for On the Fly Evaluation</a>.
</p>
<p>Note that there is a legacy interface for getting source locations into
the Scheme compiler or interpreter, which is to use a side table that
associates &ldquo;source properties&rdquo; with each subdatum returned by
<code class="code">read</code>, instead of wrapping the datums directly as in
<code class="code">read-syntax</code>. This has the disadvantage of not being able to
annotate all kinds of datums. See <a class="xref" href="Source-Properties.html">Source Properties</a>, for more
information.
</p>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Scheme-Write.html">Writing Scheme Values</a>, Previous: <a href="Scheme-Read.html">Reading Scheme Code</a>, Up: <a href="Read_002fLoad_002fEval_002fCompile.html">Reading and Evaluating Scheme Code</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>