aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/doc/src/erl_scan.xml
blob: 342f491dd0ef6a1b3812569a8c6926d0d6404131 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>1996</year><year>2015</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

    </legalnotice>

    <title>erl_scan</title>
    <prepared>Robert Virding</prepared>
    <responsible>Bjarne D&auml;cker</responsible>
    <docno>1</docno>
    <approved>Bjarne D&auml;cker</approved>
    <checked></checked>
    <date>97-01-24</date>
    <rev>B</rev>
    <file>erl_scan.sgml</file>
  </header>
  <module>erl_scan</module>
  <modulesummary>The Erlang Token Scanner</modulesummary>
  <description>
    <p>This module contains functions for tokenizing characters into
      Erlang tokens.</p>
  </description>
  <datatypes>
    <datatype>
      <name name="attribute_info"></name>
    </datatype>
    <datatype>
      <name name="attributes"></name>
    </datatype>
    <datatype>
      <name name="attributes_data"></name>
    </datatype>
    <datatype>
      <name name="category"></name>
    </datatype>
    <datatype>
      <name name="column"></name>
    </datatype>
    <datatype>
      <name name="error_description"></name>
    </datatype>
    <datatype>
      <name name="error_info"></name>
    </datatype>
    <datatype>
      <name name="info_line"></name>
    </datatype>
    <datatype>
      <name name="info_location"></name>
    </datatype>
    <datatype>
      <name name="line"></name>
    </datatype>
    <datatype>
      <name name="location"></name>
    </datatype>
    <datatype>
      <name name="option"></name>
    </datatype>
    <datatype>
      <name name="options"></name>
    </datatype>
    <datatype>
      <name name="symbol"></name>
    </datatype>
    <datatype>
      <name name="resword_fun"></name>
    </datatype>
    <datatype>
      <name name="token"></name>
    </datatype>
    <datatype>
      <name name="token_info"></name>
    </datatype>
    <datatype>
      <name name="tokens"></name>
    </datatype>
    <datatype>
      <name name="tokens_result"></name>
    </datatype>
  </datatypes>
  <funcs>
    <func>
      <name name="string" arity="1"/>
      <name name="string" arity="2"/>
      <name name="string" arity="3"/>
      <fsummary>Scan a string and return the Erlang tokens</fsummary>
      <desc>
        <p>Takes the list of characters <c><anno>String</anno></c> and tries to
          scan (tokenize) them. Returns <c>{ok, <anno>Tokens</anno>,
           <anno>EndLocation</anno>}</c>,
          where <c><anno>Tokens</anno></c> are the Erlang tokens from
          <c><anno>String</anno></c>. <c><anno>EndLocation</anno></c>
          is the first location after the last token.</p>
        <p><c>{error, <anno>ErrorInfo</anno>, <anno>ErrorLocation</anno>}</c>
          is returned if an error occurs.
          <c><anno>ErrorLocation</anno></c> is the first location after
          the erroneous token.</p>
        <p><c>string(<anno>String</anno>)</c> is equivalent to
          <c>string(<anno>String</anno>, 1)</c>, and
          <c>string(<anno>String</anno>,
          <anno>StartLocation</anno>)</c> is equivalent to
          <c>string(<anno>String</anno>,
          <anno>StartLocation</anno>, [])</c>.</p>
        <p><c><anno>StartLocation</anno></c> indicates the initial location
          when scanning starts. If <c><anno>StartLocation</anno></c> is a line,
          <c>attributes()</c> as well as <c><anno>EndLocation</anno></c> and
          <c><anno>ErrorLocation</anno></c> will be lines. If
          <c><anno>StartLocation</anno></c> is a pair of a line and a column
          <c>attributes()</c> takes the form of an opaque compound
          data type, and <c><anno>EndLocation</anno></c> and
          <c><anno>ErrorLocation</anno></c>
          will be pairs of a line and a column. The <em>token
          attributes</em> contain information about the column and the
          line where the token begins, as well as the text of the
          token (if the <c>text</c> option is given), all of which can
          be accessed by calling <seealso
          marker="#token_info/1">token_info/1,2</seealso>, <seealso
          marker="#attributes_info/1">attributes_info/1,2</seealso>,
          <seealso marker="#column/1">column/1</seealso>,
          <seealso marker="#line/1">line/1</seealso>,
          <seealso marker="#location/1">location/1</seealso>, and
          <seealso marker="#text/1">text/1</seealso>.</p>
        <p>A <em>token</em> is a tuple containing information about
          syntactic category, the token attributes, and the actual
          terminal symbol. For punctuation characters (e.g. <c>;</c>,
          <c>|</c>) and reserved words, the category and the symbol
          coincide, and the token is represented by a two-tuple.
          Three-tuples have one of the following forms: <c>{atom,
          Info, atom()}</c>,
          <c>{char, Info, integer()}</c>, <c>{comment, Info,
          string()}</c>, <c>{float, Info, float()}</c>, <c>{integer,
          Info, integer()}</c>, <c>{var, Info, atom()}</c>,
          and <c>{white_space, Info, string()}</c>.</p>
        <p>The valid options are:</p>
        <taglist>
        <tag><c>{reserved_word_fun, reserved_word_fun()}</c></tag>
        <item><p>A callback function that is called when the scanner
          has found an unquoted atom. If the function returns
          <c>true</c>, the unquoted atom itself will be the category
          of the token; if the function returns <c>false</c>,
          <c>atom</c> will be the category of the unquoted atom.</p>
        </item>
        <tag><c>return_comments</c></tag>
        <item><p>Return comment tokens.</p>
        </item>
        <tag><c>return_white_spaces</c></tag>
        <item><p>Return white space tokens. By convention, if there is
          a newline character, it is always the first character of the
          text (there cannot be more than one newline in a white space
          token).</p>
        </item>
        <tag><c>return</c></tag>
        <item><p>Short for <c>[return_comments, return_white_spaces]</c>.</p>
        </item>
        <tag><c>text</c></tag>
        <item><p>Include the token's text in the token attributes. The
          text is the part of the input corresponding to the token.</p>
        </item>
        </taglist>
      </desc>
    </func>
    <func>
      <name name="tokens" arity="3"/>
      <name name="tokens" arity="4"/>
      <fsummary>Re-entrant scanner</fsummary>
      <type name="char_spec"/>
      <type name="return_cont"/>
      <type_desc name="return_cont">An opaque continuation</type_desc>
      <desc>
        <p>This is the re-entrant scanner which scans characters until
          a <em>dot</em> ('.' followed by a white space) or
          <c>eof</c> has been reached. It returns:</p>
        <taglist>
          <tag><c>{done, <anno>Result</anno>, <anno>LeftOverChars</anno>}</c>
          </tag>
          <item>
            <p>This return indicates that there is sufficient input
              data to get a result. <c><anno>Result</anno></c> is:</p>
            <taglist>
              <tag><c>{ok, Tokens, EndLocation}</c>
              </tag>
              <item>
                <p>The scanning was successful. <c>Tokens</c>
                  is the list of tokens including <em>dot</em>.</p>
              </item>
              <tag><c>{eof, EndLocation}</c></tag>
              <item>
                <p>End of file was encountered before any more tokens.</p>
              </item>
              <tag><c>{error, ErrorInfo, EndLocation}</c>
              </tag>
              <item>
                <p>An error occurred. <c><anno>LeftOverChars</anno></c>
                  is the remaining characters of the input data,
                  starting from <c>EndLocation</c>.</p>
              </item>
            </taglist>
          </item>
          <tag><c>{more, <anno>Continuation1</anno>}</c></tag>
          <item>
            <p>More data is required for building a term.
              <c><anno>Continuation1</anno></c> must be passed in a new call to
              <c>tokens/3,4</c> when more data is available.</p>
          </item>
        </taglist>
        <p>The <c><anno>CharSpec</anno></c> <c>eof</c> signals end of file.
        <c><anno>LeftOverChars</anno></c> will then take the value <c>eof</c>
          as well.</p>
        <p><c>tokens(<anno>Continuation</anno>, <anno>CharSpec</anno>,
          <anno>StartLocation</anno>)</c> is equivalent to
          <c>tokens(<anno>Continuation</anno>, <anno>CharSpec</anno>,
          <anno>StartLocation</anno>, [])</c>.</p>
        <p>See <seealso marker="#string/3">string/3</seealso> for a
          description of the various options.</p>
      </desc>
    </func>
    <func>
      <name name="reserved_word" arity="1"/>
      <fsummary>Test for a reserved word</fsummary>
      <desc>
        <p>Returns <c>true</c> if <c><anno>Atom</anno></c> is an Erlang
         reserved word, otherwise <c>false</c>.</p>
      </desc>
    </func>
    <func>
      <name name="category" arity="1"/>
      <fsummary>Return the category</fsummary>
      <desc>
        <p>Returns the category of <c><anno>Token</anno></c>.
        </p>
      </desc>
    </func>
    <func>
      <name name="symbol" arity="1"/>
      <fsummary>Return the symbol</fsummary>
      <desc>
        <p>Returns the symbol of <c><anno>Token</anno></c>.
        </p>
      </desc>
    </func>
    <func>
      <name name="column" arity="1"/>
      <fsummary>Return the column</fsummary>
      <desc>
        <p>Returns the column of <c><anno>Token</anno></c>'s
          collection of annotations.
        </p>
      </desc>
    </func>
    <func>
      <name name="end_location" arity="1"/>
      <fsummary>Return the end location of the text</fsummary>
      <desc>
        <p>Returns the end location of the text of
          <c><anno>Token</anno></c>'s collection of annotations. If
            there is no text,
          <c>undefined</c> is returned.
        </p>
      </desc>
    </func>
    <func>
      <name name="line" arity="1"/>
      <fsummary>Return the line</fsummary>
      <desc>
        <p>Returns the line of <c><anno>Token</anno></c>'s collection
          of annotations.
        </p>
      </desc>
    </func>
    <func>
      <name name="location" arity="1"/>
      <fsummary>Return the location</fsummary>
      <desc>
        <p>Returns the location of <c><anno>Token</anno></c>'s
          collection of annotations.
        </p>
      </desc>
    </func>
    <func>
      <name name="text" arity="1"/>
      <fsummary>Return the text</fsummary>
      <desc>
        <p>Returns the text of <c><anno>Token</anno></c>'s collection
        of annotations. If there is no text, <c>undefined</c> is
        returned.
        </p>
      </desc>
    </func>
    <func>
      <name name="token_info" arity="1"/>
      <fsummary>Return information about a token</fsummary>
      <desc>
        <p>Returns a list containing information about the token
          <c><anno>Token</anno></c>. The order of the
          <c><anno>TokenInfoTuple</anno></c>s is not
          defined. See <seealso
          marker="#token_info/2">token_info/2</seealso> for
          information about specific
          <c><anno>TokenInfoTuple</anno></c>s.</p>
        <p>Note that if <c>token_info(Token, TokenItem)</c> returns
          <c>undefined</c> for some <c>TokenItem</c>, the
          item is not included in <c><anno>TokenInfo</anno></c>.</p>
      </desc>
    </func>
    <func>
      <name name="token_info" arity="2" clause_i="1"/>
      <name name="token_info" arity="2" clause_i="2"/>
      <fsummary>Return information about a token</fsummary>
      <type name="token_item"/>
      <type name="attribute_item"/>
      <desc>
        <p>Returns a list containing information about the token
          <c><anno>Token</anno></c>. If one single
          <c><anno>TokenItem</anno></c> is given the returned value is
          the corresponding
          <c>TokenInfoTuple</c>, or <c>undefined</c> if the
          <c>TokenItem</c> has no value. If a list of
          <c><anno>TokenItem</anno></c>s is given the result is a list of
          <c><anno>TokenInfoTuple</anno></c>. The
          <c><anno>TokenInfoTuple</anno></c>s will
          appear with the corresponding <c><anno>TokenItem</anno></c>s in
          the same order as the <c><anno>TokenItem</anno></c>s
	  appear in the list of <c>TokenItem</c>s.
          <c><anno>TokenItem</anno></c>s with no value are not included
          in the list of <c><anno>TokenInfoTuple</anno></c>.</p>
	<p>The following <c><anno>TokenInfoTuple</anno></c>s with corresponding
	   <c><anno>TokenItem</anno></c>s are valid:</p>
        <taglist>
          <tag><c>{category, </c><seealso marker="#type-category">
            category()</seealso><c>}</c></tag>
          <item><p>The category of the token.</p>
          </item>
          <tag><c>{column, </c><seealso marker="#type-column">
            column()</seealso><c>}</c></tag>
          <item><p>The column where the token begins.</p>
          </item>
          <tag><c>{length, integer() > 0}</c></tag>
          <item><p>The length of the token's text.</p>
          </item>
          <tag><c>{line, </c><seealso marker="#type-line">
            line()</seealso><c>}</c></tag>
          <item><p>The line where the token begins.</p>
          </item>
          <tag><c>{location, </c><seealso marker="#type-location">
            location()</seealso><c>}</c></tag>
          <item><p>The line and column where the token begins, or
            just the line if the column unknown.</p>
          </item>
          <tag><c>{symbol, </c><seealso marker="#type-symbol">
            symbol()</seealso><c>}</c></tag>
          <item><p>The token's symbol.</p>
          </item>
          <tag><c>{text, string()}</c></tag>
          <item><p>The token's text.</p>
          </item>
        </taglist>
      </desc>
    </func>
    <func>
      <name name="attributes_info" arity="1"/>
      <fsummary>Return information about token attributes</fsummary>
      <desc>
        <p>Returns a list containing information about the token
          attributes <c><anno>Attributes</anno></c>. The order of the
          <c><anno>AttributeInfoTuple</anno></c>s is not defined.
          See <seealso
          marker="#attributes_info/2">attributes_info/2</seealso> for
          information about specific
          <c><anno>AttributeInfoTuple</anno></c>s.</p>
        <p>Note that if <c>attributes_info(Token, AttributeItem)</c>
          returns <c>undefined</c> for some <c>AttributeItem</c> in
          the list above, the item is not included in
          <c><anno>AttributesInfo</anno></c>.</p>
      </desc>
    </func>
    <func>
      <name name="attributes_info" arity="2" clause_i="1"/>
      <name name="attributes_info" arity="2" clause_i="2"/>
      <fsummary>Return information about a token attributes</fsummary>
      <type name="attribute_item"/>
      <desc>
        <p>Returns a list containing information about the token
          attributes <c><anno>Attributes</anno></c>. If one single
          <c><anno>AttributeItem</anno></c> is given the returned value is the
          corresponding <c><anno>AttributeInfoTuple</anno></c>,
          or <c>undefined</c> if the <c><anno>AttributeItem</anno></c>
          has no value. If a list of <c><anno>AttributeItem</anno></c>
          is given the result is a list of
          <c><anno>AttributeInfoTuple</anno></c>.
          The <c><anno>AttributeInfoTuple</anno></c>s
          will appear with the corresponding <c><anno>AttributeItem</anno></c>s
          in the same order as the <c><anno>AttributeItem</anno></c>s
          appear in the list of <c><anno>AttributeItem</anno></c>s.
          <c><anno>AttributeItem</anno></c>s with no
          value are not included in the list of
	  <c><anno>AttributeInfoTuple</anno></c>.</p>
	<p>The following <c><anno>AttributeInfoTuple</anno></c>s with
          corresponding <c><anno>AttributeItem</anno></c>s are valid:</p>
        <taglist>
          <tag><c>{column, </c><seealso marker="#type-column">
            column()</seealso><c>}</c></tag>
          <item><p>The column where the token begins.</p>
          </item>
          <tag><c>{length, integer() > 0}</c></tag>
          <item><p>The length of the token's text.</p>
          </item>
          <tag><c>{line, </c><seealso marker="#type-line">
            line()</seealso><c>}</c></tag>
          <item><p>The line where the token begins.</p>
          </item>
          <tag><c>{location, </c><seealso marker="#type-location">
            location()</seealso><c>}</c></tag>
          <item><p>The line and column where the token begins, or
            just the line if the column unknown.</p>
          </item>
          <tag><c>{text, string()}</c></tag>
          <item><p>The token's text.</p>
          </item>
        </taglist>
      </desc>
    </func>
    <func>
      <name name="set_attribute" arity="3"/>
      <fsummary>Set a token attribute value</fsummary>
      <desc>
        <p>Sets the value of the <c>line</c> attribute of the token
          attributes <c><anno>Attributes</anno></c>.</p>
        <p>The <c><anno>SetAttributeFun</anno></c> is called with the value of
          the <c>line</c> attribute, and is to return the new value of
          the <c>line</c> attribute.</p>
      </desc>
    </func>
    <func>
      <name name="format_error" arity="1"/>
      <fsummary>Format an error descriptor</fsummary>
      <desc>
        <p>Takes an <c><anno>ErrorDescriptor</anno></c> and returns
          a string which
          describes the error or warning. This function is usually
          called implicitly when processing an <c>ErrorInfo</c>
          structure (see below).</p>
      </desc>
    </func>
  </funcs>

  <section>
    <title>Error Information</title>
    <p>The <c>ErrorInfo</c> mentioned above is the standard
      <c>ErrorInfo</c> structure which is returned from all IO
      modules. It has the following format:</p>
    <code type="none">
{ErrorLocation, Module, ErrorDescriptor}</code>
    <p>A string which describes the error is obtained with the
      following call:</p>
    <code type="none">
Module:format_error(ErrorDescriptor)</code>
  </section>

  <section>
    <title>Notes</title>
    <p>The continuation of the first call to the re-entrant input
      functions must be <c>[]</c>. Refer to Armstrong, Virding and
      Williams, 'Concurrent Programming in Erlang', Chapter 13, for a
      complete description of how the re-entrant input scheme works.</p>
  </section>

  <section>
    <title>See Also</title>
    <p><seealso marker="io">io(3)</seealso>,
       <seealso marker="erl_anno">erl_anno(3)</seealso>,
       <seealso marker="erl_parse">erl_parse(3)</seealso></p>
  </section>
</erlref>