aboutsummaryrefslogtreecommitdiff
path: root/docs/read.rst
blob: 9d973f44901aeee7220a7a63833234b1494745a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
Data reader
===========

Data reader is responsible for reading from input stream and creating
data instances represented with input character sequences. In case of
Lisp, interpreter instructions are represented with data structures. Therefor,
parser for string representation of data is also parser form programming
language itself. Some of data types don't have string representation
(`builtin function`, `builtin syntax`, `function` and `syntax`) and
can not be produced by reader.

Character ``;`` represents beginning of comment which spawns to the end of line
(until ``\n`` character is read). White space characters (`` ``, ``\n``,
``\r``, ``\t``) and comments are ignored by reader. Only significance of
white space characters is as a data delimiter.


Number
------

Numbers as encoded as sequence of decimal characters (``0`` to ``9``).
Start of number is detected by starting decimal character.

Example of valid number representations::

    0
    1
    42


String
------

String is represented with arbitrary number of characters enclosed between
``"`` (maximum string length is 2047). ``\`` is used as escape character
in representation of:

    ``\n``, ``\r``, ``\t``, ``\\``, ``\"``

Start of string is detected by ``"`` character.

Example of valid string representations::

    ""
    "abc"
    "\""


Symbol
------

Symbol is represented with arbitrary sequence of characters. Sequence can not
include white space characters, ``(`` or ``)``. Sequence can not start with
decimal number character, ``"``, ``'``, ````` or ``,``.

Example of valid symbol representations::

    abc
    -
    +=/@
    symbol-123


Pair/list
---------

Pair is represented with form::

    (<first> . <second>)

where ``<first>`` and ``<second>`` are arbitrary data representations
(including other pars/lists).

Nested pairs of form::

    (<el_1> . (<el_2> . (.... . <el_n>)))

where ``<el_1>``, ``<el_2>``, ..., ``<el_n>`` is arbitrary data, can be
written as::

    (<el_1> <el_2> ... . <el_n>)

Special case of pair is empty list represented as::

    ()

which can be recursively specified as::

    (() . ())

Sequence of nested pairs with empty list as last element is called list::

    (<el_1> . (<el_2> . (.... . (<el_n> . ()))))

and can be written as::

    (<el_1> <el_2> ... <el_n>)

Example of valid pairs/lists representations::

    ()
    (1 . 2)
    (a . (1 . "b"))
    (1 2 3 4)
    ("abc" abc . 123)


Reader macros
-------------

To enable more concise representation of complex forms, reader recognize
few builtin reader macros. These do not introduce new data types and are used
only as more convenient representation of other standard data forms:

    * quote

        Recognized by starting character ``'``. Forms::

            '<data>

        are equivalent to::

            (quote <data>)

    * quasiquote

        Recognized by starting character `````. Forms::

            `<data>

        are equivalent to::

            (quasiquote <data>)

    * unquote

        Recognized by starting character ``,``. Forms::

            ,<data>

        are equivalent to::

            (unquote <data>)


Source code
-----------

read.h
''''''

.. literalinclude:: ../src_c/read.h
    :language: c


read.c
''''''

.. literalinclude:: ../src_c/read.c
    :language: c