1 /*
2  * reader.h - Reader API
3  *
4  *   Copyright (c) 2000-2020  Shiro Kawai  <shiro@acm.org>
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *   1. Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *
13  *   2. Redistributions in binary form must reproduce the above copyright
14  *      notice, this list of conditions and the following disclaimer in the
15  *      documentation and/or other materials provided with the distribution.
16  *
17  *   3. Neither the name of the authors nor the names of its contributors
18  *      may be used to endorse or promote products derived from this
19  *      software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /* This file is included from gauche.h */
35 
36 #ifndef GAUCHE_READER_H
37 #define GAUCHE_READER_H
38 
39 SCM_CLASS_DECL(Scm_ReadContextClass);
40 #define SCM_CLASS_READ_CONTEXT   (&Scm_ReadContextClass)
41 #define SCM_READ_CONTEXT(obj)    ((ScmReadContext*)(obj))
42 #define SCM_READ_CONTEXT_P(obj)  SCM_XTYPEP(obj, SCM_CLASS_READ_CONTEXT)
43 
44 SCM_EXTERN ScmReadContext *Scm_MakeReadContext(ScmReadContext *proto);
45 SCM_EXTERN int             Scm_ReadContextLiteralImmutable(ScmReadContext *);
46 SCM_EXTERN ScmReadContext *Scm_CurrentReadContext();
47 SCM_EXTERN ScmReadContext *Scm_SetCurrentReadContext(ScmReadContext *ctx);
48 
49 /* Reader-lexical-mode
50     permissive
51     legacy
52     warn-legacy
53     strict-r7
54    This is kept in a parameter, so it's thread-local.
55 
56    NB: The actual implementation is in port.c, because ports require
57    this working at initialization, but reader is initialized after ports.
58    It is debatable which module these should belong to.  Reader *always*
59    reference port's reader lexical mode, and the global reader lexical
60    mode parameter is only used to initialize individual ports.  So these
61    can be in port's API.  But in that case, we may want other name
62    (e.g. DefaultReaderLexicalMode).
63  */
64 SCM_EXTERN ScmObj Scm_ReaderLexicalMode(void);
65 /* returns previous setting */
66 SCM_EXTERN ScmObj Scm_SetReaderLexicalMode(ScmObj);
67 
68 /* An object to keep unrealized circular reference (e.g. #N=) during
69  * 'read'.  It is replaced by the reference value before exiting 'read',
70  * and it shouldn't leak out to the normal Scheme program, except the
71  * code that handles it explicitly (like read-time constructor).
72  * This object is also used in Scm_UnwrapSyntax to track circular structures.
73  */
74 typedef struct ScmReadReferenceRec {
75     SCM_HEADER;
76     ScmObj value;               /* realized reference.  initially UNBOUND */
77 } ScmReadReference;
78 
79 SCM_CLASS_DECL(Scm_ReadReferenceClass);
80 #define SCM_CLASS_READ_REFERENCE  (&Scm_ReadReferenceClass)
81 #define SCM_READ_REFERENCE(obj)   ((ScmReadReference*)(obj))
82 #define SCM_READ_REFERENCE_P(obj) SCM_XTYPEP(obj, SCM_CLASS_READ_REFERENCE)
83 #define SCM_READ_REFERENCE_REALIZED(obj) \
84    (!SCM_EQ(SCM_READ_REFERENCE(obj)->value, SCM_UNBOUND))
85 
86 /*
87  * Standard Read entries
88  */
89 SCM_EXTERN ScmObj Scm_Read(ScmObj port);
90 SCM_EXTERN ScmObj Scm_ReadWithContext(ScmObj port, ScmReadContext *ctx);
91 SCM_EXTERN ScmObj Scm_ReadList(ScmObj port, ScmChar closer);
92 SCM_EXTERN ScmObj Scm_ReadListWithContext(ScmObj port, ScmChar closer,
93                                           ScmReadContext *ctx);
94 SCM_EXTERN ScmObj Scm_ReadFromString(ScmString *string);
95 SCM_EXTERN ScmObj Scm_ReadFromCString(const char *string);
96 
97 SCM_EXTERN void   Scm_ReadError(ScmPort *port, const char *fmt, ...);
98 
99 /*
100  * Common utilities to handle hex-digit escapes
101  */
102 SCM_EXTERN ScmChar Scm_ReadXdigitsFromString(const char *buf,
103                                              int buflen,
104                                              ScmChar key,
105                                              ScmObj mode,
106                                              int terminator,
107                                              const char**);
108 SCM_EXTERN ScmObj  Scm_ReadXdigitsFromPort(ScmPort *port, int key, ScmObj mode,
109                                            int incompletep, ScmDString *buf);
110 
111 SCM_EXTERN long Scm_ReadDigitsAsLong(ScmPort *port, ScmChar ch, int radix,
112                                      ScmChar *next,
113                                      ScmSize *numread);
114 SCM_EXTERN long Scm_ParseDigitsAsLong(const char *buf, ScmSize len, int radix,
115                                       ScmSize *numread);
116 
117 /*
118  * SRFI-10 hash-comma syntax
119  */
120 SCM_EXTERN ScmObj Scm_DefineReaderCtor(ScmObj symbol, ScmObj proc,
121                                        ScmObj finisher, ScmObj module);
122 SCM_EXTERN ScmObj Scm_GetReaderCtor(ScmObj symbol, ScmObj module);
123 
124 /*
125  * Hash-bang reader directive handlers
126  */
127 SCM_EXTERN ScmObj Scm_DefineReaderDirective(ScmObj symbol, ScmObj proc);
128 
129 #endif  /*GAUCHE_READER_H*/
130 
131