~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to lib/parsetools/doc/src/leex.xml

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-08-05 20:54:29 UTC
  • mfrom: (6.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090805205429-pm4pnwew8axraosl
Tags: 1:13.b.1-dfsg-5
* Fixed parentheses in Emacs mode (closes: #536891).
* Removed unnecessary conflicts with erlang-manpages package.
* Added workaround for #475459: disabled threads on sparc architecture.
  This breaks wxErlang, so it's only a temporary solution.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="latin1" ?>
 
2
<!DOCTYPE erlref SYSTEM "erlref.dtd">
 
3
 
 
4
<erlref>
 
5
  <header>
 
6
    <copyright>
 
7
      <year>2009</year><year>2009</year>
 
8
      <holder>Ericsson AB. All Rights Reserved.</holder>
 
9
    </copyright>
 
10
    <legalnotice>
 
11
      Copyright (c) 2008 Robert Virding. All rights reserved.
 
12
    </legalnotice>
 
13
 
 
14
    <title>leex</title>
 
15
    <prepared>Robert Virding</prepared>
 
16
    <responsible>nobody</responsible>
 
17
    <docno></docno>
 
18
    <approved>nobody</approved>
 
19
    <checked></checked>
 
20
    <date>2009-05-07</date>
 
21
    <rev>A</rev>
 
22
    <file>leex.xml</file>
 
23
  </header>
 
24
  <module>leex</module>
 
25
  <modulesummary>Lexical analyzer generator for Erlang</modulesummary>
 
26
  <description>
 
27
    <p>A regular expression based lexical analyzer generator for
 
28
      Erlang, similar to lex or flex.</p>
 
29
    <note><p>The Leex module should be considered experimental
 
30
      as it will be subject to changes in future releases.</p></note>
 
31
  </description>
 
32
  <section>
 
33
    <title>DATA TYPES</title>
 
34
    <code type="none">
 
35
ErrorInfo = {ErrorLine,module(),error_descriptor()}
 
36
ErrorLine = integer()
 
37
Token = tuple()</code>
 
38
  </section>
 
39
  <funcs>
 
40
    <func>
 
41
      <name>file(FileName) -> ok | error</name>
 
42
      <name>file(FileName, Options) -> ok | error</name>
 
43
      <fsummary>Generate a lexical analyzer</fsummary>
 
44
      <type>
 
45
        <v>FileName = filename()</v>
 
46
        <v>Options = Option | [Option]</v>
 
47
        <v>Option =&nbsp;-&nbsp;see below&nbsp;-</v>
 
48
        <v>FileReturn = {ok, Scannerfile} 
 
49
                      | {ok, Scannerfile, Warnings}
 
50
                      | error
 
51
                      | {error, Warnings, Errors}</v>
 
52
        <v>Scannerfile = filename()</v>
 
53
        <v>Warnings = Errors = [{filename(), [ErrorInfo]}]</v>
 
54
      </type>
 
55
      <desc>
 
56
        <p>Generates a lexical analyzer from the definition in the input
 
57
          file. The input file has the extension <c>.xrl</c>. This is
 
58
          added to the filename if it is not given. The resulting module
 
59
          is the Xrl filename without the <c>.xrl</c> extension.</p>
 
60
 
 
61
        <p>The current options are:</p>
 
62
          <taglist>
 
63
            <tag><c>dfa_graph</c></tag>
 
64
            <item><p>Generates a <c>.dot</c> file which contains a
 
65
              description of the DFA in a format which can be viewed with
 
66
              Graphviz, <c>www.graphviz.com</c>.</p>
 
67
            </item>
 
68
            <tag><c>{includefile,Includefile}</c></tag>
 
69
            <item><p>Uses a specific or customised prologue file
 
70
              instead of default
 
71
              <c>lib/parsetools/include/leexinc.hrl</c> which is
 
72
              otherwise included.</p>
 
73
            </item>
 
74
            <tag><c>{report_errors, bool()}</c></tag>
 
75
            <item><p>Causes errors to be printed as they occur. Default is
 
76
              <c>true</c>.</p>
 
77
            </item>
 
78
            <tag><c>{report_warnings, bool()}</c></tag>
 
79
            <item><p>Causes warnings to be printed as they occur. Default is
 
80
              <c>true</c>.</p>
 
81
            </item>
 
82
            <tag><c>{report, bool()}</c></tag>
 
83
            <item><p>This is a short form for both <c>report_errors</c> and
 
84
              <c>report_warnings</c>.</p>
 
85
            </item>
 
86
            <tag><c>{return_errors, bool()}</c></tag>
 
87
            <item><p>If this flag is set, <c>{error, Errors, Warnings}</c>
 
88
              is returned when there are errors. Default is <c>false</c>.</p>
 
89
            </item>
 
90
            <tag><c>{return_warnings, bool()}</c></tag>
 
91
            <item><p>If this flag is set, an extra field containing
 
92
              <c>Warnings</c> is added to the tuple returned upon
 
93
               success. Default is <c>false</c>.</p>
 
94
            </item>
 
95
            <tag><c>{return, bool()}</c></tag>
 
96
            <item><p>This is a short form for both <c>return_errors</c> and
 
97
              <c>return_warnings</c>.</p>
 
98
            </item>
 
99
            <tag><c>{scannerfile, Scannerfile}</c></tag>
 
100
            <item><p><c>Scannerfile</c> is the name of the file that
 
101
             will contain the Erlang scanner code that is generated.
 
102
             The default (<c>""</c>) is to add the extension
 
103
             <c>.erl</c> to <c>FileName</c> stripped of the
 
104
             <c>.xrl</c> extension.</p>
 
105
            </item>
 
106
            <tag><c>{verbose, bool()}</c></tag>
 
107
            <item><p>Outputs information from parsing the input file and
 
108
              generating the internal tables.</p>
 
109
            </item>
 
110
          </taglist>
 
111
        <p>Any of the Boolean options can be set to <c>true</c> by 
 
112
          stating the name of the option. For example, <c>verbose</c>
 
113
          is equivalent to <c>{verbose, true}</c>.</p>
 
114
        <p>Leex will add the extension <c>.hrl</c> to the 
 
115
          <c>Includefile</c> name and the extension <c>.erl</c> to the
 
116
          <c>Scannerfile</c> name, unless the extension is already
 
117
          there.</p>
 
118
      </desc>
 
119
    </func>
 
120
    <func>
 
121
      <name>format_error(ErrorInfo) -> Chars</name>
 
122
      <fsummary>Return an English description of a an error tuple.</fsummary>
 
123
      <type>
 
124
        <v>Chars = [char() | Chars]</v>
 
125
      </type>
 
126
      <desc>
 
127
        <p>Returns a string which describes the error
 
128
          <c>ErrorInfo</c> returned when there is an error in a
 
129
          regular expression.</p>
 
130
      </desc>
 
131
    </func>
 
132
  </funcs>
 
133
  
 
134
 
 
135
  <section>
 
136
    <title>GENERATED SCANNER EXPORTS</title>
 
137
    <p>The following functions are exported by the generated scanner.</p>
 
138
  </section>
 
139
 
 
140
  <funcs>
 
141
    <func>
 
142
      <name>string(String) -> StringRet</name>
 
143
      <name>string(String, StartLine) -> StringRet</name>
 
144
      <fsummary>Generated by Leex</fsummary>
 
145
      <type>
 
146
        <v>String = string()</v>
 
147
        <v>StringRet = {ok,Tokens,EndLine} | ErrorInfo</v>
 
148
        <v>Tokens = [Token]</v>
 
149
        <v>EndLine = StartLine = integer()</v>
 
150
      </type>
 
151
      <desc>
 
152
        <p>Scans <c>String</c> and returns all the tokens in it, or an
 
153
          error.</p>
 
154
        <note><p>It is an error if not all of the characters in
 
155
          <c>String</c> are consumed.</p></note>
 
156
      </desc>
 
157
    </func>
 
158
 
 
159
    <func>
 
160
      <name>token(Cont, Chars) -> {more,Cont1} | {done,TokenRet,RestChars}
 
161
      </name>
 
162
      <name>token(Cont, Chars, StartLine) -> {more,Cont1} 
 
163
             | {done,TokenRet,RestChars}
 
164
      </name>
 
165
      <fsummary>Generated by Leex</fsummary>
 
166
      <type>
 
167
        <v>Cont = [] | Cont1</v>
 
168
        <v>Cont1 = tuple()</v>
 
169
        <v>Chars = RestChars = string() | eof</v>
 
170
        <v>TokenRet = {ok, Token, EndLine} 
 
171
                    | {eof, EndLine}
 
172
                    | ErrorInfo</v>
 
173
        <v>StartLine = EndLine = integer()</v>
 
174
      </type>
 
175
      <desc>
 
176
        <p>This is a re-entrant call to try and scan one token from
 
177
          <c>Chars</c>. If there are enough characters in <c>Chars</c>
 
178
          to either scan a token or detect an error then this will be
 
179
          returned with <c>{done,...}</c>. Otherwise
 
180
          <c>{cont,Cont}</c> will be returned where <c>Cont</c> is
 
181
          used in the next call to <c>token()</c> with more characters
 
182
          to try an scan the token. This is continued until a token
 
183
          has been scanned. <c>Cont</c> is initially <c>[]</c>.</p>
 
184
 
 
185
        <p>It is not designed to be called directly by an application
 
186
          but used through the i/o system where it can typically be
 
187
          called in an application by:</p>
 
188
        <code>
 
189
io:request(InFile, {get_until,Prompt,Module,token,[Line]})
 
190
  -> TokenRet</code>
 
191
      </desc>
 
192
    </func>
 
193
 
 
194
    <func>
 
195
      <name>tokens(Cont, Chars) -> {more,Cont1} | {done,TokensRet,RestChars}
 
196
      </name>
 
197
      <name>tokens(Cont, Chars, StartLine) -> 
 
198
               {more,Cont1} | {done,TokensRet,RestChars}
 
199
      </name>
 
200
      <fsummary>Generated by Leex</fsummary>
 
201
      <type>
 
202
        <v>Cont = [] | Cont1</v>
 
203
        <v>Cont1 = tuple()</v>
 
204
        <v>Chars = RestChars = string() | eof</v>
 
205
        <v>TokensRet = {ok, Tokens, EndLine} 
 
206
                     | {eof, EndLine}
 
207
                     | ErrorInfo</v>
 
208
        <v>Tokens = [Token]</v>
 
209
        <v>StartLine = EndLine = integer()</v>
 
210
      </type>
 
211
      <desc>
 
212
        <p>This is a re-entrant call to try and scan tokens from
 
213
          <c>Chars</c>. If there are enough characters in <c>Chars</c>
 
214
          to either scan tokens or detect an error then this will be
 
215
          returned with <c>{done,...}</c>. Otherwise
 
216
          <c>{cont,Cont}</c> will be returned where <c>Cont</c> is
 
217
          used in the next call to <c>tokens()</c> with more
 
218
          characters to try an scan the tokens. This is continued
 
219
          until all tokens have been scanned. <c>Cont</c> is initially
 
220
          <c>[]</c>.</p>
 
221
 
 
222
        <p>This functions differs from <c>token</c> in that it will
 
223
          continue to scan tokens upto and including an
 
224
          <c>{end_token,Token}</c> has been scanned (see next
 
225
          section). It will then return all the tokens. This is
 
226
          typically used for scanning grammars like Erlang where there
 
227
          is an explicit end token, <c>'.'</c>. If no end token is
 
228
          found then the whole file will be scanned and returned. If
 
229
          an error occurs then all tokens upto and including the next
 
230
          end token will be skipped.</p>
 
231
 
 
232
        <p>It is not designed to be called directly by an application
 
233
          but used through the i/o system where it can typically be
 
234
          called in an application by:</p>
 
235
        <code>
 
236
io:request(InFile, {get_until,Prompt,Module,tokens,[Line]})
 
237
  -> TokensRet</code>
 
238
      </desc>
 
239
    </func>
 
240
  </funcs>
 
241
 
 
242
  <section>
 
243
    <title>Input File Format</title>
 
244
    <p>Erlang style comments starting with a <c>%</c> are allowed in
 
245
      scanner files. A definition file has the following format:</p>
 
246
    <code>
 
247
&lt;Header>
 
248
 
 
249
Definitions.
 
250
 
 
251
&lt;Macro Definitions>
 
252
 
 
253
Rules.
 
254
 
 
255
&lt;Token Rules>
 
256
 
 
257
Erlang Code.
 
258
 
 
259
&lt;Erlang Code></code>
 
260
 
 
261
    <p>The "Definitions.", "Rules." and "Erlang Code." headings are
 
262
      mandatory and must occur at the beginning of a source line. The
 
263
      &lt;Header>, &lt;Macro Definitions> and &lt;Erlang Code>
 
264
      sections may be empty but there must be at least one rule.</p>
 
265
 
 
266
    <p>Macro definitions have the following format:</p>
 
267
 
 
268
    <code>
 
269
NAME = VALUE</code>
 
270
 
 
271
    <p>and there must be spaces around <c>=</c>. Macros can be used in
 
272
      the regular expressions of rules by writing <c>{NAME}</c>.</p>
 
273
 
 
274
    <note><p>When macros are expanded in expressions the macro calls
 
275
      are replaced by the macro value without any form of quoting or
 
276
      enclosing in parentheses.</p></note>
 
277
 
 
278
    <p>Rules have the following format:</p>
 
279
 
 
280
    <code>
 
281
&lt;Regexp> : &lt;Erlang code>.</code>
 
282
 
 
283
    <p>The &lt;Regexp> must occur at the start of a line and not
 
284
      include any blanks; use <c>\\t</c> and <c>\\s</c> to include TAB
 
285
      and SPACE characters in the regular expression. If &lt;Regexp>
 
286
      matches then the corresponding &lt;Erlang code> is evaluated to
 
287
      generate a token. With the Erlang code the following predefined
 
288
      variables are available:</p>
 
289
 
 
290
    <taglist>
 
291
      <tag><c>TokenChars</c></tag>
 
292
      <item><p>A list of the characters in the matched token.</p>
 
293
      </item>
 
294
      <tag><c>TokenLen</c></tag>
 
295
      <item><p>The number of characters in the matched token.</p>
 
296
      </item>
 
297
      <tag><c>TokenLine</c></tag>
 
298
      <item><p>The line number where the token occurred.</p>
 
299
      </item>
 
300
    </taglist>
 
301
 
 
302
    <p>The code must return:</p>
 
303
 
 
304
    <taglist>
 
305
      <tag><c>{token,Token}</c></tag>
 
306
      <item><p>Return <c>Token</c> to the caller.</p>
 
307
      </item>
 
308
      <tag><c>{end_token,Token}</c></tag>
 
309
      <item><p>Return <c>Token</c> and is last token in a tokens call.</p>
 
310
      </item>
 
311
      <tag><c>skip_token</c></tag>
 
312
      <item><p>Skip this token completely.</p>
 
313
      </item>
 
314
      <tag><c>{error,ErrString}</c></tag>
 
315
      <item><p>An error in the token, <c>ErrString</c> is a string
 
316
         describing the error.</p>
 
317
      </item>
 
318
    </taglist>
 
319
 
 
320
    <p>It is also possible to push back characters into the input
 
321
      characters with the following returns:</p>
 
322
 
 
323
    <list>
 
324
      <item><c>{token,Token,PushBackList}</c></item>
 
325
      <item><c>{end_token,Token,PushBackList}</c></item>
 
326
      <item><c>{skip_token,PushBackList}</c></item>
 
327
    </list>
 
328
 
 
329
    <p>These have the same meanings as the normal returns but the
 
330
      characters in <c>PushBackList</c> will be prepended to the input
 
331
      characters and scanned for the next token. Note that pushing
 
332
      back a newline will mean the line numbering will no longer be
 
333
      correct.</p>
 
334
 
 
335
    <note><p>Pushing back characters gives you unexpected
 
336
      possibilities to cause the scanner to loop!</p></note>
 
337
 
 
338
    <p>The following example would match a simple Erlang integer or
 
339
      float and return a token which could be sent to the Erlang
 
340
      parser:</p>
 
341
    <code>
 
342
D = [0-9]
 
343
 
 
344
{D}+ :
 
345
  {token,{integer,TokenLine,list_to_integer(TokenChars)}}.
 
346
 
 
347
{D}+\\.{D}+((E|e)(\\+|\\-)?{D}+)? :
 
348
  {token,{float,TokenLine,list_to_float(TokenChars)}}.</code>
 
349
 
 
350
    <p>The Erlang code in the "Erlang Code." section is written into
 
351
      the output file directly after the module declaration and
 
352
      predefined exports declaration so it is possible to add extra
 
353
      exports, define imports and other attributes which are then
 
354
      visible in the whole file.</p>
 
355
  </section>
 
356
  
 
357
  <section>
 
358
    <title>Regular Expressions</title>
 
359
 
 
360
    <p>The regular expressions allowed here is a subset of the set
 
361
      found in <c>egrep</c> and in the AWK programming language, as
 
362
      defined in the book, The AWK Programming Language, by A. V. Aho,
 
363
      B. W. Kernighan, P. J. Weinberger. They are composed of the
 
364
      following characters:</p>
 
365
 
 
366
    <taglist>
 
367
      <tag><c>c</c></tag>
 
368
      <item><p>Matches the non-metacharacter c.</p>
 
369
      </item>
 
370
      <tag><c>\\c</c></tag>
 
371
      <item><p>Matches the escape sequence or literal character c.</p>
 
372
      </item>
 
373
      <tag><c>.</c></tag>
 
374
      <item><p>Matches any character.</p>
 
375
      </item>
 
376
      <tag><c>^</c></tag>
 
377
      <item><p>Matches the beginning of a string.</p>
 
378
      </item>
 
379
      <tag><c>$</c></tag>
 
380
      <item><p>Matches the end of a string.</p></item>
 
381
      <tag><c>[abc...]</c></tag>
 
382
      <item><p>Character class, which matches any of the characters
 
383
        <c>abc...</c>. Character ranges are specified by a pair of
 
384
        characters separated by a <c>-</c>.</p>
 
385
      </item>
 
386
      <tag><c>[^abc...]</c></tag>
 
387
      <item><p>Negated character class, which matches any character
 
388
        except <c>abc...</c>.</p>
 
389
      </item>
 
390
      <tag><c>r1 | r2</c></tag>
 
391
      <item><p>Alternation. It matches either <c>r1</c> or <c>r2</c>.</p>
 
392
      </item>
 
393
      <tag><c>r1r2</c></tag>
 
394
      <item><p>Concatenation. It matches <c>r1</c> and then <c>r2</c>.</p>
 
395
      </item>
 
396
      <tag><c>r+</c></tag>
 
397
      <item><p>Matches one or more <c>rs</c>.</p>
 
398
      </item>
 
399
      <tag><c>r*</c></tag>
 
400
      <item><p>Matches zero or more <c>rs</c>.</p>
 
401
      </item>
 
402
      <tag><c>r?</c></tag>
 
403
      <item><p>Matches zero or one <c>rs</c>.</p>
 
404
      </item>
 
405
      <tag><c>(r)</c></tag>
 
406
      <item><p>Grouping. It matches <c>r</c>.</p>
 
407
      </item>
 
408
    </taglist>
 
409
 
 
410
    <p>The escape sequences allowed are the same as for Erlang strings:</p>
 
411
 
 
412
    <taglist>
 
413
      <tag><c>\\b</c></tag>
 
414
      <item><p>Backspace.</p></item>
 
415
      <tag><c>\\f</c></tag>
 
416
      <item><p>Form feed.</p></item>
 
417
      <tag><c>\\n</c></tag>
 
418
      <item><p>Newline (line feed).</p></item>
 
419
      <tag><c>\\r</c></tag>
 
420
      <item><p>Carriage return.</p></item>
 
421
      <tag><c>\\t</c></tag>
 
422
      <item><p>Tab.</p></item>
 
423
      <tag><c>\\e</c></tag>
 
424
      <item><p>Escape.</p></item>
 
425
      <tag><c>\\v</c></tag>
 
426
      <item><p>Vertical tab.</p></item>
 
427
      <tag><c>\\s</c></tag>
 
428
      <item><p>Space.</p></item>
 
429
      <tag><c>\\d</c></tag>
 
430
      <item><p>Delete.</p></item>
 
431
      <tag><c>\\ddd</c></tag>
 
432
      <item><p>The octal value <c>ddd</c>.</p></item>
 
433
      <tag><c>\\xhh</c></tag>
 
434
      <item><p>The hexadecimal value <c>hh</c>.</p></item>
 
435
      <tag><c>\\x{h...}</c></tag>
 
436
      <item><p>The hexadecimal value <c>h...</c>.</p></item>
 
437
      <tag><c>\\c</c></tag>
 
438
      <item><p>Any other character literally, for example <c>\\\\</c> for
 
439
        backslash, <c>\\"</c> for <c>"</c>.</p>
 
440
      </item>
 
441
    </taglist>
 
442
 
 
443
    <p>The following examples define Erlang data types:</p>
 
444
    <code> 
 
445
Atoms [a-z][0-9a-zA-Z_]*
 
446
 
 
447
Variables [A-Z_][0-9a-zA-Z_]*
 
448
 
 
449
Floats (\\+|-)?[0-9]+\\.[0-9]+((E|e)(\\+|-)?[0-9]+)?</code>
 
450
 
 
451
    <note><p>Anchoring a regular expression with <c>^</c> and <c>$</c>
 
452
      is not implemented in the current version of Leex and just
 
453
      generates an error.</p></note>
 
454
  </section>
 
455
</erlref>