diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-06 09:49:28 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-06 09:49:28 -0300 |
commit | dc6e6c48bb7eeac20f79acd9fe4aa4fbb4496303 (patch) | |
tree | 87e0ebe5d3e33d2d793d84ff7592d9eb7838391b | |
parent | e3cddc950cc84755437e3c2302d12f6c3fcb691b (diff) | |
download | lua-dc6e6c48bb7eeac20f79acd9fe4aa4fbb4496303.tar.gz lua-dc6e6c48bb7eeac20f79acd9fe4aa4fbb4496303.tar.bz2 lua-dc6e6c48bb7eeac20f79acd9fe4aa4fbb4496303.zip |
on the way to 5.0
-rw-r--r-- | manual.tex | 322 |
1 files changed, 168 insertions, 154 deletions
@@ -1,4 +1,4 @@ | |||
1 | % $Id: manual.tex,v 1.54 2001/10/31 20:31:38 roberto Exp $ | 1 | % $Id: manual.tex,v 1.55 2002/02/14 21:48:32 roberto Exp roberto $ |
2 | 2 | ||
3 | \documentclass[11pt,twoside,draft]{article} | 3 | \documentclass[11pt,twoside,draft]{article} |
4 | \usepackage{fullpage} | 4 | \usepackage{fullpage} |
@@ -20,7 +20,7 @@ | |||
20 | \newcommand{\False}{{\bf false}} | 20 | \newcommand{\False}{{\bf false}} |
21 | \newcommand{\True}{{\bf true}} | 21 | \newcommand{\True}{{\bf true}} |
22 | %\def\tecgraf{{\sf TeC\kern-.21em\lower.7ex\hbox{Graf}}} | 22 | %\def\tecgraf{{\sf TeC\kern-.21em\lower.7ex\hbox{Graf}}} |
23 | \def\tecgraf{{\sf TeCGraf}} | 23 | \def\tecgraf{{\sf Tecgraf}} |
24 | 24 | ||
25 | \newcommand{\Index}[1]{#1\index{#1@{\lowercase{#1}}}} | 25 | \newcommand{\Index}[1]{#1\index{#1@{\lowercase{#1}}}} |
26 | \newcommand{\IndexVerb}[1]{\T{#1}\index{#1@{\tt #1}}} | 26 | \newcommand{\IndexVerb}[1]{\T{#1}\index{#1@{\tt #1}}} |
@@ -81,39 +81,31 @@ Last revised on \today | |||
81 | \null\vfill | 81 | \null\vfill |
82 | 82 | ||
83 | \noindent | 83 | \noindent |
84 | Copyright \copyright\ 1994--2001 TeCGraf, PUC-Rio. All rights reserved. | 84 | Copyright \copyright\ 2002 Tecgraf, PUC-Rio. All rights reserved. |
85 | |||
86 | Permission is hereby granted, free of charge, | ||
87 | to any person obtaining a copy of this software | ||
88 | and associated documentation files (the "Software"), | ||
89 | to deal in the Software without restriction, | ||
90 | including without limitation the rights to use, copy, modify, | ||
91 | merge, publish, distribute, sublicense, | ||
92 | and/or sell copies of the Software, | ||
93 | and to permit persons to whom the Software is furnished to do so, | ||
94 | subject to the following conditions: | ||
95 | |||
96 | The above copyright notice and this permission notice shall be | ||
97 | included in all copies or substantial portions of the Software. | ||
98 | |||
99 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
100 | EXPRESS OR IMPLIED, | ||
101 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
102 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
103 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE | ||
104 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
105 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
106 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE | ||
107 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
85 | 108 | ||
86 | Permission is hereby granted, without written agreement and without license | ||
87 | or royalty fees, to use, copy, modify, translate, and distribute | ||
88 | this software and its documentation (hereby called the ``package'') | ||
89 | for any purpose, including commercial applications, subject to | ||
90 | the following conditions: | ||
91 | \begin{itemize} | ||
92 | \item The above copyright notice and this permission notice shall appear in all | ||
93 | copies or substantial portions of this package. | ||
94 | |||
95 | \item The origin of this package must not be misrepresented; you must not | ||
96 | claim that you wrote the original package. If you use this package in a | ||
97 | product, an acknowledgment in the product documentation would be greatly | ||
98 | appreciated (but it is not required). | ||
99 | |||
100 | \item Altered source versions must be plainly marked as such, and must not be | ||
101 | misrepresented as being the original package. | ||
102 | \end{itemize} | ||
103 | The authors specifically disclaim any warranties, including, but not limited | ||
104 | to, the implied warranties of merchantability and fitness for a particular | ||
105 | purpose. The package provided hereunder is on an ``as~is'' basis, and the | ||
106 | authors have no obligation to provide maintenance, support, updates, | ||
107 | enhancements, or modifications. In no event shall TeCGraf, PUC-Rio, or the | ||
108 | authors be held liable to any party for direct, indirect, special, | ||
109 | incidental, or consequential damages arising out of the use of this package | ||
110 | and its documentation. | ||
111 | |||
112 | The Lua language and this implementation have been entirely designed and | ||
113 | written by Waldemar Celes, Roberto Ierusalimschy, and Luiz Henrique de | ||
114 | Figueiredo at TeCGraf, PUC-Rio in Brazil. | ||
115 | |||
116 | This implementation contains no third-party code. | ||
117 | 109 | ||
118 | Copies of this manual can be obtained at | 110 | Copies of this manual can be obtained at |
119 | Lua's official web site, | 111 | Lua's official web site, |
@@ -140,7 +132,7 @@ Waldemar Celes | |||
140 | \tecgraf\ --- Computer Science Department --- PUC-Rio | 132 | \tecgraf\ --- Computer Science Department --- PUC-Rio |
141 | } | 133 | } |
142 | 134 | ||
143 | %\date{{\small \tt\$Date: 2001/10/31 20:31:38 $ $}} | 135 | %\date{{\small \tt\$Date: 2002/02/14 21:48:32 $ $}} |
144 | 136 | ||
145 | \maketitle | 137 | \maketitle |
146 | 138 | ||
@@ -282,12 +274,10 @@ environments, and freely switch between them \see{mangstate}. | |||
282 | 274 | ||
283 | The global environment can be manipulated by Lua code or | 275 | The global environment can be manipulated by Lua code or |
284 | by the embedding program, | 276 | by the embedding program, |
285 | which can read and write global variables | ||
286 | using the API functions from the library that implements Lua. | 277 | using the API functions from the library that implements Lua. |
287 | 278 | ||
288 | The unit of execution of Lua is called a \Def{chunk}. | 279 | The unit of execution of Lua is called a \Def{chunk}. |
289 | A chunk is simply a sequence of statements, | 280 | A chunk is simply a sequence of statements. |
290 | which are executed sequentially. | ||
291 | Statements are described in \See{stats}. | 281 | Statements are described in \See{stats}. |
292 | 282 | ||
293 | A chunk may be stored in a file or in a string inside the host program. | 283 | A chunk may be stored in a file or in a string inside the host program. |
@@ -330,7 +320,7 @@ Lua is 8-bit clean, | |||
330 | and so strings may contain any 8-bit character, | 320 | and so strings may contain any 8-bit character, |
331 | including embedded zeros (\verb|'\0'|) \see{lexical}. | 321 | including embedded zeros (\verb|'\0'|) \see{lexical}. |
332 | 322 | ||
333 | Functions are considered \emph{first-class values} in Lua. | 323 | Functions are \emph{first-class values} in Lua. |
334 | This means that functions can be stored in variables, | 324 | This means that functions can be stored in variables, |
335 | passed as arguments to other functions, and returned as results. | 325 | passed as arguments to other functions, and returned as results. |
336 | Lua can call (and manipulate) functions written in Lua and | 326 | Lua can call (and manipulate) functions written in Lua and |
@@ -353,7 +343,7 @@ The type \emph{table} implements \Index{associative arrays}, | |||
353 | that is, \Index{arrays} that can be indexed not only with numbers, | 343 | that is, \Index{arrays} that can be indexed not only with numbers, |
354 | but with any value (except \nil). | 344 | but with any value (except \nil). |
355 | Moreover, | 345 | Moreover, |
356 | tables are \emph{heterogeneous}, | 346 | tables can be \emph{heterogeneous}, |
357 | that is, they can contain values of all types. | 347 | that is, they can contain values of all types. |
358 | Tables are the sole data structuring mechanism in Lua; | 348 | Tables are the sole data structuring mechanism in Lua; |
359 | they may be used not only to represent ordinary arrays, | 349 | they may be used not only to represent ordinary arrays, |
@@ -368,10 +358,7 @@ Like indices, the value of a table field can be of any type. | |||
368 | In particular, | 358 | In particular, |
369 | because functions are first class values, | 359 | because functions are first class values, |
370 | table fields may contain functions. | 360 | table fields may contain functions. |
371 | So, tables may also carry \emph{methods}. | 361 | So, tables may also carry \emph{methods} \see{func-def}. |
372 | %The form \verb|t:f(x)| is syntactic sugar for \verb|t.f(t,x)|, | ||
373 | %which calls the method \verb|f| from the table \verb|t| passing | ||
374 | %the table itself as the first parameter \see{func-def}. | ||
375 | 362 | ||
376 | Tables, functions, and userdata values are \emph{objects}: | 363 | Tables, functions, and userdata values are \emph{objects}: |
377 | variables do not actually \emph{contain} these values, | 364 | variables do not actually \emph{contain} these values, |
@@ -394,7 +381,7 @@ Lua checks for a function in the field \verb|"add"| in its metatable. | |||
394 | If it finds one, | 381 | If it finds one, |
395 | Lua calls that function to perform the addition. | 382 | Lua calls that function to perform the addition. |
396 | 383 | ||
397 | A metatable works as a kind of an extended ``type'' for the object. | 384 | A metatable works as a kind of an extended ``type'' for the object: |
398 | Objects that share a metatable has identical behavior. | 385 | Objects that share a metatable has identical behavior. |
399 | 386 | ||
400 | A metatable controls how an object behaves in arithmetic operations, | 387 | A metatable controls how an object behaves in arithmetic operations, |
@@ -413,7 +400,8 @@ through the \verb|metatable| function \see{pdf-metatable}. | |||
413 | 400 | ||
414 | \subsection{\Index{Coercion}} \label{coercion} | 401 | \subsection{\Index{Coercion}} \label{coercion} |
415 | 402 | ||
416 | Lua provides automatic conversion between string and number values at run time. | 403 | Lua provides automatic conversion between |
404 | string and number values at run time. | ||
417 | Any arithmetic operation applied to a string tries to convert | 405 | Any arithmetic operation applied to a string tries to convert |
418 | that string to a number, following the usual rules. | 406 | that string to a number, following the usual rules. |
419 | Conversely, whenever a number is used when a string is expected, | 407 | Conversely, whenever a number is used when a string is expected, |
@@ -430,15 +418,16 @@ use the \verb|format| function \see{format}. | |||
430 | There are two kinds of variables in Lua: | 418 | There are two kinds of variables in Lua: |
431 | global variables | 419 | global variables |
432 | and local variables. | 420 | and local variables. |
433 | \Index{Global variables} do not need to be declared. | ||
434 | Variables are assumed to be global unless explicitly declared local | 421 | Variables are assumed to be global unless explicitly declared local |
435 | \see{localvar}. | 422 | \see{localvar}. |
436 | Before the first assignment, the value of a variable is \nil\ % | 423 | Before the first assignment, the value of a variable is \nil. |
437 | (this default can be changed for global variables; see \See{tag-method}). | ||
438 | 424 | ||
439 | An ordinary Lua table is used to keep all global names and values. | 425 | All global variables live as fields in ordinary Lua tables. |
440 | This table can be accessed and changed with the \verb|globals| function | 426 | Usually, globals live in a table called \Index{table of globals}. |
441 | \see{pdf-globals}. | 427 | However, a function can individually change its global table, |
428 | so that all global variables in that function will refer to that table. | ||
429 | This mechanism allows the creation of \Index{namespaces} and other | ||
430 | modularization facilities. | ||
442 | 431 | ||
443 | \Index{Local variables} are lexically scoped. | 432 | \Index{Local variables} are lexically scoped. |
444 | Therefore, local variables can be freely accessed by functions | 433 | Therefore, local variables can be freely accessed by functions |
@@ -501,11 +490,11 @@ A weak table can have weak keys, weak values, or both. | |||
501 | A table with weak keys allows the collection of its keys, | 490 | A table with weak keys allows the collection of its keys, |
502 | but prevents the collection of its values. | 491 | but prevents the collection of its values. |
503 | A table with both weak keys and weak values allows the collection of | 492 | A table with both weak keys and weak values allows the collection of |
504 | both keys and values | 493 | both keys and values. |
505 | In any case, if either the key or the value is collected, | 494 | In any case, if either the key or the value is collected, |
506 | the whole pair is removed from the table. | 495 | the whole pair is removed from the table. |
507 | The weakness of a table is controled by the | 496 | The weakness of a table is controled by the |
508 | \verb|weakmode| field in its metatable \see{weakmode}. | 497 | \verb|__weakmode| field in its metatable \see{weakmode}. |
509 | 498 | ||
510 | 499 | ||
511 | %------------------------------------------------------------------------------ | 500 | %------------------------------------------------------------------------------ |
@@ -538,13 +527,12 @@ and cannot be used as identifiers: | |||
538 | or repeat return then true | 527 | or repeat return then true |
539 | until while | 528 | until while |
540 | \end{verbatim} | 529 | \end{verbatim} |
541 | (The keyword \rwd{global} is reserved for future use.) | ||
542 | 530 | ||
543 | Lua is a case-sensitive language: | 531 | Lua is a case-sensitive language: |
544 | \T{and} is a reserved word, but \T{And} and \T{\'and} | 532 | \T{and} is a reserved word, but \T{And} and \T{\'and} |
545 | (if the locale permits) are two different, valid identifiers. | 533 | (if the locale permits) are two different, valid identifiers. |
546 | As a convention, identifiers starting with an underscore followed by | 534 | As a convention, identifiers starting with an underscore followed by |
547 | uppercase letters (such as \verb|_INPUT|) | 535 | uppercase letters (such as \verb|_VERSION|) |
548 | are reserved for internal variables. | 536 | are reserved for internal variables. |
549 | 537 | ||
550 | The following strings denote other \Index{tokens}: | 538 | The following strings denote other \Index{tokens}: |
@@ -574,7 +562,7 @@ A character in a string may also be specified by its numerical value, | |||
574 | through the escape sequence `\verb|\|\emph{ddd}', | 562 | through the escape sequence `\verb|\|\emph{ddd}', |
575 | where \emph{ddd} is a sequence of up to three \emph{decimal} digits. | 563 | where \emph{ddd} is a sequence of up to three \emph{decimal} digits. |
576 | Strings in Lua may contain any 8-bit value, including embedded zeros, | 564 | Strings in Lua may contain any 8-bit value, including embedded zeros, |
577 | which can be specified as `\verb|\000|'. | 565 | which can be specified as `\verb|\0|'. |
578 | 566 | ||
579 | Literal strings can also be delimited by matching \verb|[[| $\ldots$ \verb|]]|. | 567 | Literal strings can also be delimited by matching \verb|[[| $\ldots$ \verb|]]|. |
580 | Literals in this bracketed form may run for several lines, | 568 | Literals in this bracketed form may run for several lines, |
@@ -589,7 +577,7 @@ other quoted strings. | |||
589 | As an example, in a system using ASCII | 577 | As an example, in a system using ASCII |
590 | (in which `\verb|a|' is coded as~97, | 578 | (in which `\verb|a|' is coded as~97, |
591 | newline is coded as~10, and `\verb|1|' is coded as~49), | 579 | newline is coded as~10, and `\verb|1|' is coded as~49), |
592 | the following four literals below are equivalent: | 580 | the four literals below denote the same string: |
593 | \begin{verbatim} | 581 | \begin{verbatim} |
594 | 1) "alo\n123\"" | 582 | 1) "alo\n123\"" |
595 | 2) '\97lo\10\04923"' | 583 | 2) '\97lo\10\04923"' |
@@ -608,8 +596,14 @@ Examples of valid numerical constants are | |||
608 | \end{verbatim} | 596 | \end{verbatim} |
609 | 597 | ||
610 | \IndexEmph{Comments} start anywhere outside a string with a | 598 | \IndexEmph{Comments} start anywhere outside a string with a |
611 | double hyphen (\verb|--|) and run until the end of the line. | 599 | double hyphen (\verb|--|); |
612 | (There are no block comments in Lua.) | 600 | If the text after \verb|--| is different from \verb|[[|, |
601 | the comment is a short comment, | ||
602 | that runs until the end of the line. | ||
603 | Otherwise, it is a long comment, | ||
604 | that runs until the corresponding \verb|]]|. | ||
605 | Long comments may run for several lines, | ||
606 | and may contain nested \verb|[[| $\ldots$ \verb|]]| pairs. | ||
613 | For convenience, | 607 | For convenience, |
614 | the first line of a chunk is skipped if it starts with \verb|#|. | 608 | the first line of a chunk is skipped if it starts with \verb|#|. |
615 | This facility allows the use of Lua as a script interpreter | 609 | This facility allows the use of Lua as a script interpreter |
@@ -647,15 +641,19 @@ The expression denoting the table to be indexed has a restricted syntax; | |||
647 | The meaning of assignments and evaluations of global and | 641 | The meaning of assignments and evaluations of global and |
648 | indexed variables can be changed via metatables. | 642 | indexed variables can be changed via metatables. |
649 | An assignment to a global variable \verb|x = val| | 643 | An assignment to a global variable \verb|x = val| |
650 | is equivalent to an assignment in the global table: | 644 | is equivalent to the assignment |
651 | \verb|globals().x = val|. | 645 | \verb|_glob.x = val|, |
646 | where \verb|_glob| is the table of globals of the running function | ||
647 | (\see{global-table} for a discussion about the table of globals). | ||
652 | An assignment to an indexed variable \verb|t[i] = val| is equivalent to | 648 | An assignment to an indexed variable \verb|t[i] = val| is equivalent to |
653 | \verb|settable_event(t,i,val)|. | 649 | \verb|settable_event(t,i,val)|. |
654 | An access to a global variable \verb|x| | 650 | An access to a global variable \verb|x| |
655 | is equivalent to an access to the global table: \verb|globals().x|. | 651 | is equivalent to \verb|_glob.x| |
652 | (again, \see{global-table} for a discussion about \verb|_glob|). | ||
656 | An access to an indexed variable \verb|t[i]| is equivalent to | 653 | An access to an indexed variable \verb|t[i]| is equivalent to |
657 | a call \verb|gettable_event(t,i)|. | 654 | a call \verb|gettable_event(t,i)|. |
658 | See \See{metatable} for a complete description of these functions. | 655 | See \See{metatable} for a complete description of the |
656 | \verb|settable_event| and \verb|gettable_event| functions. | ||
659 | (These functions are not defined in Lua. | 657 | (These functions are not defined in Lua. |
660 | We use them here only for explanatory purposes.) | 658 | We use them here only for explanatory purposes.) |
661 | 659 | ||
@@ -667,8 +665,7 @@ similar to those in Pascal or C. | |||
667 | The conventional commands include | 665 | The conventional commands include |
668 | assignment, control structures, and procedure calls. | 666 | assignment, control structures, and procedure calls. |
669 | Non-conventional commands include table constructors | 667 | Non-conventional commands include table constructors |
670 | \see{tableconstructor} | 668 | and variable declarations. |
671 | and local variable declarations \see{localvar}. | ||
672 | 669 | ||
673 | \subsubsection{Chunks}\label{chunks} | 670 | \subsubsection{Chunks}\label{chunks} |
674 | The unit of execution of Lua is called a \Def{chunk}. | 671 | The unit of execution of Lua is called a \Def{chunk}. |
@@ -687,7 +684,7 @@ Non-terminals are shown in \emph{italics}, | |||
687 | keywords are shown in {\bf bold}, | 684 | keywords are shown in {\bf bold}, |
688 | and other terminal symbols are shown in {\tt typewriter} font, | 685 | and other terminal symbols are shown in {\tt typewriter} font, |
689 | enclosed in single quotes. | 686 | enclosed in single quotes. |
690 | The complete syntax of Lua in EBNF is given on page~\pageref{BNF}. | 687 | The complete syntax of Lua in extended BNF is given on page~\pageref{BNF}. |
691 | 688 | ||
692 | \subsubsection{Blocks} | 689 | \subsubsection{Blocks} |
693 | A \Index{block} is a list of statements; | 690 | A \Index{block} is a list of statements; |
@@ -702,7 +699,7 @@ A block may be explicitly delimited: | |||
702 | \end{Produc}% | 699 | \end{Produc}% |
703 | \IndexKW{do} | 700 | \IndexKW{do} |
704 | Explicit blocks are useful | 701 | Explicit blocks are useful |
705 | to control the scope of local variables \see{localvar}. | 702 | to control the scope of variable declarations. |
706 | Explicit blocks are also sometimes used to | 703 | Explicit blocks are also sometimes used to |
707 | add a \rwd{return} or \rwd{break} statement in the middle | 704 | add a \rwd{return} or \rwd{break} statement in the middle |
708 | of another block \see{control}. | 705 | of another block \see{control}. |
@@ -723,9 +720,9 @@ Expressions are discussed in \See{expressions}. | |||
723 | Before the assignment, | 720 | Before the assignment, |
724 | the list of values is \emph{adjusted} to the length of | 721 | the list of values is \emph{adjusted} to the length of |
725 | the list of variables.\index{adjustment} | 722 | the list of variables.\index{adjustment} |
726 | If there are more values than are needed, | 723 | If there are more values than needed, |
727 | the excess values are thrown away. | 724 | the excess values are thrown away. |
728 | If there are less values than are needed, | 725 | If there are less values than needed, |
729 | the list is extended with as many \nil's as needed. | 726 | the list is extended with as many \nil's as needed. |
730 | If the list of expressions ends with a function call, | 727 | If the list of expressions ends with a function call, |
731 | then all values returned by that function call enter in the list of values, | 728 | then all values returned by that function call enter in the list of values, |
@@ -739,7 +736,7 @@ So, the code | |||
739 | i = 3 | 736 | i = 3 |
740 | i, a[i] = i+1, 20 | 737 | i, a[i] = i+1, 20 |
741 | \end{verbatim} | 738 | \end{verbatim} |
742 | sets \verb|a[3]| to 20, but does not affect \verb|a[4]| | 739 | sets \verb|a[3]| to 20, without affecting \verb|a[4]| |
743 | because the \verb|i| in \verb|a[i]| is evaluated | 740 | because the \verb|i| in \verb|a[i]| is evaluated |
744 | before it is assigned 4. | 741 | before it is assigned 4. |
745 | Similarly, the line | 742 | Similarly, the line |
@@ -762,11 +759,12 @@ familiar syntax: | |||
762 | \rep{\rwd{elseif} exp \rwd{then} block} | 759 | \rep{\rwd{elseif} exp \rwd{then} block} |
763 | \opt{\rwd{else} block} \rwd{end}} | 760 | \opt{\rwd{else} block} \rwd{end}} |
764 | \end{Produc}% | 761 | \end{Produc}% |
765 | There is also a \rwd{for} statement in two flavors \see{for}. | 762 | Lua also has a \rwd{for} statement, in two flavors \see{for}. |
766 | 763 | ||
767 | The \Index{condition expression} \M{exp} of a | 764 | The \Index{condition expression} \M{exp} of a |
768 | control structure may return any value. | 765 | control structure may return any value. |
769 | All values different from \nil\ and \False\ are considered true; | 766 | All values different from \nil\ and \False\ are considered true |
767 | (in particular, the number 0 and the empty string are also true); | ||
770 | both \False\ and \nil\ are considered false. | 768 | both \False\ and \nil\ are considered false. |
771 | 769 | ||
772 | The \rwd{return} statement is used to return values | 770 | The \rwd{return} statement is used to return values |
@@ -858,51 +856,45 @@ it calls this function to produce a new value for each iteration, | |||
858 | stopping when the new value is \nil. | 856 | stopping when the new value is \nil. |
859 | It has the following syntax: | 857 | It has the following syntax: |
860 | \begin{Produc} | 858 | \begin{Produc} |
861 | \produc{stat}{\rwd{for} name \opt{\ter{,} name} \rwd{in} exp | 859 | \produc{stat}{\rwd{for} name \rep{\ter{,} name} \rwd{in} explist1 |
862 | \rwd{do} block \rwd{end}} | 860 | \rwd{do} block \rwd{end}} |
863 | \end{Produc}% | 861 | \end{Produc}% |
864 | A \rwd{for} statement like | 862 | A \rwd{for} statement like |
865 | \begin{verbatim} | 863 | \begin{verbatim} |
866 | for var1, var2 in exp do block end | 864 | for var_1, ..., var_n in explist do block end |
867 | \end{verbatim} | 865 | \end{verbatim} |
868 | is equivalent to the code: | 866 | is equivalent to the code: |
869 | \begin{verbatim} | 867 | \begin{verbatim} |
870 | do | 868 | do |
871 | local _f = exp | 869 | local _f, _s, var_1 = explist |
872 | while 1 do | 870 | while 1 do |
873 | local var1, var2 = _f() | 871 | local var_2, ..., var_n |
874 | if var1 == nil then break end | 872 | var_1, ..., var_n = _f(_s, var_1) |
873 | if var_1 == nil then break end | ||
875 | block | 874 | block |
876 | end | 875 | end |
877 | end | 876 | end |
878 | \end{verbatim} | 877 | \end{verbatim} |
879 | Note the following: | 878 | Note the following: |
880 | \begin{itemize}\itemsep=0pt | 879 | \begin{itemize}\itemsep=0pt |
881 | \item \verb|exp| is evaluated only once. | 880 | \item \verb|explist| is evaluated only once. |
882 | Its result is the function that will be evaluated for each loop. | 881 | Its results are a ``generator'' function, |
883 | \item \verb|_f| is an invisible variable. | 882 | a ``state'', and an initial value for the ``iterator variable''. |
884 | The name is here for explanatory purposes only. | 883 | \item \verb|_f| and \verb|_s| are invisible variables. |
885 | \item The behavior is \emph{undefined} if you assign to \verb|var1| | 884 | The names are here for explanatory purposes only. |
886 | or \verb|var2| inside the block. | 885 | \item The behavior is \emph{undefined} if you assign to any |
886 | \verb|var_i| inside the block. | ||
887 | \item You can use \rwd{break} to exit a \rwd{for} loop. | 887 | \item You can use \rwd{break} to exit a \rwd{for} loop. |
888 | \item The loop variables \verb|var1| and \verb|var2| are | 888 | \item The loop variables \verb|var_i| are local to the statement; |
889 | local to the statement; | ||
890 | you cannot use their values after the \rwd{for} ends. | 889 | you cannot use their values after the \rwd{for} ends. |
891 | If you need these values, | 890 | If you need these values, |
892 | then assign them to other variables before breaking or exiting the loop. | 891 | then assign them to other variables before breaking or exiting the loop. |
893 | \item The absence of the optional variable \verb|var2| does not | ||
894 | change the meaning of the loop. | ||
895 | \end{itemize} | 892 | \end{itemize} |
896 | 893 | ||
897 | If the generator is a table \verb|t|, | 894 | If the first result of the expression list is a table \verb|t| |
898 | then the loop works as if it has received the following generator function: | 895 | (instead of a function), |
899 | \begin{verbatim} | 896 | then the loop works as if it has received \verb|next, t| as its |
900 | local k,v = nil | 897 | expression list. |
901 | function generator () | ||
902 | k,v = next(t, k) | ||
903 | return k,v | ||
904 | end | ||
905 | \end{verbatim} | ||
906 | That is, the loop iterates over the (key,value) pairs of the table. | 898 | That is, the loop iterates over the (key,value) pairs of the table. |
907 | 899 | ||
908 | 900 | ||
@@ -1341,7 +1333,7 @@ is syntactic sugar for | |||
1341 | \index{visibility} | 1333 | \index{visibility} |
1342 | 1334 | ||
1343 | Lua is a lexically scoped language. | 1335 | Lua is a lexically scoped language. |
1344 | The scope of local variables begins at the first statement \emph{after} | 1336 | The scope of variables begins at the first statement \emph{after} |
1345 | their declaration and lasts until the end of the innermost block that | 1337 | their declaration and lasts until the end of the innermost block that |
1346 | includes the declaration. | 1338 | includes the declaration. |
1347 | For instance: | 1339 | For instance: |
@@ -1352,7 +1344,7 @@ For instance: | |||
1352 | print(x) --> 10 | 1344 | print(x) --> 10 |
1353 | x = x+1 | 1345 | x = x+1 |
1354 | do -- another block | 1346 | do -- another block |
1355 | local x = x+1 -- another x | 1347 | local x = x+1 -- another `x' |
1356 | print(x) --> 12 | 1348 | print(x) --> 12 |
1357 | end | 1349 | end |
1358 | print(x) --> 11 | 1350 | print(x) --> 11 |
@@ -1390,8 +1382,6 @@ In that code, | |||
1390 | each function uses a different \verb|y| variable, | 1382 | each function uses a different \verb|y| variable, |
1391 | while all of them share the same \verb|x|. | 1383 | while all of them share the same \verb|x|. |
1392 | 1384 | ||
1393 | |||
1394 | |||
1395 | \subsection{Error Handling} \label{error} | 1385 | \subsection{Error Handling} \label{error} |
1396 | 1386 | ||
1397 | Because Lua is an extension language, | 1387 | Because Lua is an extension language, |
@@ -1446,7 +1436,11 @@ If so, the value associated with that key (the \IndexEmph{handler}) | |||
1446 | controls how Lua will perform the operation. | 1436 | controls how Lua will perform the operation. |
1447 | 1437 | ||
1448 | Metatables control the operations listed next. | 1438 | Metatables control the operations listed next. |
1449 | Each operation is identified by its corresponding key. | 1439 | Each operation is identified by its corresponding name. |
1440 | The key for each operation is a string with its name prefixed by | ||
1441 | two underscores; | ||
1442 | for instance, the key for operation ``add'' is the | ||
1443 | string \verb|"__add"|. | ||
1450 | The semantics of these operations is better explained by a Lua function | 1444 | The semantics of these operations is better explained by a Lua function |
1451 | describing how the interpreter executes that operation. | 1445 | describing how the interpreter executes that operation. |
1452 | %Each function shows how a handler is called, | 1446 | %Each function shows how a handler is called, |
@@ -1483,7 +1477,7 @@ the behavior of the ``add'' operation is | |||
1483 | if o1 and o2 then -- both operands are numeric | 1477 | if o1 and o2 then -- both operands are numeric |
1484 | return o1+o2 -- '+' here is the primitive 'add' | 1478 | return o1+o2 -- '+' here is the primitive 'add' |
1485 | else -- at least one of the operands is not numeric | 1479 | else -- at least one of the operands is not numeric |
1486 | local h = getbinhandler(op1, op2, "add") | 1480 | local h = getbinhandler(op1, op2, "__add") |
1487 | if h then | 1481 | if h then |
1488 | -- call the handler with both operands | 1482 | -- call the handler with both operands |
1489 | return h(op1, op2) | 1483 | return h(op1, op2) |
@@ -1510,7 +1504,7 @@ Behavior similar to the ``add'' operation. | |||
1510 | the \verb|^| operation (exponentiation) operation. | 1504 | the \verb|^| operation (exponentiation) operation. |
1511 | \begin{verbatim} ?? | 1505 | \begin{verbatim} ?? |
1512 | function pow_op (op1, op2) | 1506 | function pow_op (op1, op2) |
1513 | local h = getbinhandler(op1, op2, "pow") | 1507 | local h = getbinhandler(op1, op2, "__pow") |
1514 | if h then | 1508 | if h then |
1515 | -- call the handler with both operands | 1509 | -- call the handler with both operands |
1516 | return h(op1, op2) | 1510 | return h(op1, op2) |
@@ -1529,7 +1523,7 @@ the unary \verb|-| operation. | |||
1529 | return -o -- '-' here is the primitive 'unm' | 1523 | return -o -- '-' here is the primitive 'unm' |
1530 | else -- the operand is not numeric. | 1524 | else -- the operand is not numeric. |
1531 | -- Try to get a handler from the operand; | 1525 | -- Try to get a handler from the operand; |
1532 | local h = metatable(op).unm | 1526 | local h = metatable(op).__unm |
1533 | if h then | 1527 | if h then |
1534 | -- call the handler with the operand and nil | 1528 | -- call the handler with the operand and nil |
1535 | return h(op, nil) | 1529 | return h(op, nil) |
@@ -1549,7 +1543,7 @@ the \verb|<| operation. | |||
1549 | elseif type(op1) == "string" and type(op2) == "string" then | 1543 | elseif type(op1) == "string" and type(op2) == "string" then |
1550 | return op1 < op2 -- lexicographic comparison | 1544 | return op1 < op2 -- lexicographic comparison |
1551 | else | 1545 | else |
1552 | local h = getbinhandler(op1, op2, "lt") | 1546 | local h = getbinhandler(op1, op2, "__lt") |
1553 | if h then | 1547 | if h then |
1554 | return h(op1, op2) | 1548 | return h(op1, op2) |
1555 | else | 1549 | else |
@@ -1574,7 +1568,7 @@ the \verb|..| (concatenation) operation. | |||
1574 | (type(op2) == "string" or type(op2) == "number") then | 1568 | (type(op2) == "string" or type(op2) == "number") then |
1575 | return op1..op2 -- primitive string concatenation | 1569 | return op1..op2 -- primitive string concatenation |
1576 | else | 1570 | else |
1577 | local h = getbinhandler(op1, op2, "concat") | 1571 | local h = getbinhandler(op1, op2, "__concat") |
1578 | if h then | 1572 | if h then |
1579 | return h(op1, op2) | 1573 | return h(op1, op2) |
1580 | else | 1574 | else |
@@ -1593,14 +1587,14 @@ See the ``gettable'' operation for its semantics. | |||
1593 | called whenever Lua accesses an indexed variable. | 1587 | called whenever Lua accesses an indexed variable. |
1594 | \begin{verbatim} | 1588 | \begin{verbatim} |
1595 | function gettable_op (table, key) | 1589 | function gettable_op (table, key) |
1596 | local h = metatable(table).gettable | 1590 | local h = metatable(table).__gettable |
1597 | if h == nil then | 1591 | if h == nil then |
1598 | if type(table) ~= "table" then | 1592 | if type(table) ~= "table" then |
1599 | error("indexed expression not a table"); | 1593 | error("indexed expression not a table"); |
1600 | else | 1594 | else |
1601 | local v = rawget(table, key) | 1595 | local v = rawget(table, key) |
1602 | if v ~= nil then return v end | 1596 | if v ~= nil then return v end |
1603 | h = metatable(table).index | 1597 | h = metatable(table).__index |
1604 | if h == nil then return nil end | 1598 | if h == nil then return nil end |
1605 | end | 1599 | end |
1606 | end | 1600 | end |
@@ -1614,7 +1608,7 @@ called whenever Lua accesses an indexed variable. | |||
1614 | called when Lua assigns to an indexed variable. | 1608 | called when Lua assigns to an indexed variable. |
1615 | \begin{verbatim} | 1609 | \begin{verbatim} |
1616 | function settable_event (table, key, value) | 1610 | function settable_event (table, key, value) |
1617 | local h = metatable(table).settable | 1611 | local h = metatable(table).__settable |
1618 | if h == nil then | 1612 | if h == nil then |
1619 | if type(table) ~= "table" then | 1613 | if type(table) ~= "table" then |
1620 | error("indexed expression not a table") | 1614 | error("indexed expression not a table") |
@@ -1634,7 +1628,7 @@ called when Lua calls a value. | |||
1634 | if type(func) == "function" then | 1628 | if type(func) == "function" then |
1635 | return func(unpack(arg)) -- regular call | 1629 | return func(unpack(arg)) -- regular call |
1636 | else | 1630 | else |
1637 | local h = metatable(func).call | 1631 | local h = metatable(func).__call |
1638 | if h then | 1632 | if h then |
1639 | tinsert(arg, 1, func) | 1633 | tinsert(arg, 1, func) |
1640 | return h(unpack(arg)) | 1634 | return h(unpack(arg)) |
@@ -1656,7 +1650,7 @@ For each userdata to be collected, | |||
1656 | Lua does the equivalent of the following function: | 1650 | Lua does the equivalent of the following function: |
1657 | \begin{verbatim} | 1651 | \begin{verbatim} |
1658 | function gc_op (obj) | 1652 | function gc_op (obj) |
1659 | local h = metatable(obj).gc | 1653 | local h = metatable(obj).__gc |
1660 | if h then | 1654 | if h then |
1661 | h(obj) | 1655 | h(obj) |
1662 | end | 1656 | end |
@@ -1673,7 +1667,7 @@ Second, metatables control the weakmode of tables \see{weak-table}. | |||
1673 | The weakmode of a table \verb|t| is defined by a string: | 1667 | The weakmode of a table \verb|t| is defined by a string: |
1674 | \label{weakmode} | 1668 | \label{weakmode} |
1675 | \begin{verbatim} | 1669 | \begin{verbatim} |
1676 | s = metatable(t).weakmode | 1670 | s = metatable(t).__weakmode |
1677 | \end{verbatim} | 1671 | \end{verbatim} |
1678 | Valid values for this string are \verb|"k"| for weak keys, | 1672 | Valid values for this string are \verb|"k"| for weak keys, |
1679 | \verb|"v"| for weak values, | 1673 | \verb|"v"| for weak values, |
@@ -1713,17 +1707,9 @@ every function in the library (except \verb|lua_open| below). | |||
1713 | Before calling any API function, | 1707 | Before calling any API function, |
1714 | you must create a state by calling | 1708 | you must create a state by calling |
1715 | \begin{verbatim} | 1709 | \begin{verbatim} |
1716 | lua_State *lua_open (int stacksize); | 1710 | lua_State *lua_open (void); |
1717 | \end{verbatim} | 1711 | \end{verbatim} |
1718 | \DefAPI{lua_open} | 1712 | \DefAPI{lua_open} |
1719 | The sole argument to this function is the stack size for the interpreter. | ||
1720 | (Each function call needs one stack position for each argument, local variable, | ||
1721 | and temporary value, plus one position for book-keeping. | ||
1722 | The stack must also have some 20 extra positions available. | ||
1723 | For very small implementations, without recursive functions, | ||
1724 | a stack size of~100 should be enough.) | ||
1725 | If \verb|stacksize| is zero, | ||
1726 | then a default size of~1024 is used. | ||
1727 | 1713 | ||
1728 | To release a state created with \verb|lua_open|, call | 1714 | To release a state created with \verb|lua_open|, call |
1729 | \begin{verbatim} | 1715 | \begin{verbatim} |
@@ -1752,7 +1738,7 @@ If you have a C~library that offers multi-threading or co-routines, | |||
1752 | then Lua can cooperate with it to implement the equivalent facility in Lua. | 1738 | then Lua can cooperate with it to implement the equivalent facility in Lua. |
1753 | The following function creates a new ``thread'' in Lua: | 1739 | The following function creates a new ``thread'' in Lua: |
1754 | \begin{verbatim} | 1740 | \begin{verbatim} |
1755 | lua_State *lua_newthread (lua_State *L, int stacksize); | 1741 | lua_State *lua_newthread (lua_State *L); |
1756 | \end{verbatim} | 1742 | \end{verbatim} |
1757 | \DefAPI{lua_newthread} | 1743 | \DefAPI{lua_newthread} |
1758 | The new state returned by this function shares with the original state | 1744 | The new state returned by this function shares with the original state |
@@ -1811,19 +1797,22 @@ When you interact with Lua API, | |||
1811 | \emph{you are responsible for controlling stack overflow}. | 1797 | \emph{you are responsible for controlling stack overflow}. |
1812 | The function | 1798 | The function |
1813 | \begin{verbatim} | 1799 | \begin{verbatim} |
1814 | int lua_stackspace (lua_State *L); | 1800 | int lua_checkstack (lua_State *L, int size); |
1815 | \end{verbatim} | 1801 | \end{verbatim} |
1816 | \DefAPI{lua_stackspace} | 1802 | \DefAPI{lua_checkstack} |
1817 | returns the number of stack positions still available. | 1803 | returns true if there is at lease \verb|size| |
1804 | stack positions still available. | ||
1818 | Whenever Lua calls C, \DefAPI{LUA_MINSTACK} | 1805 | Whenever Lua calls C, \DefAPI{LUA_MINSTACK} |
1819 | it ensures that | 1806 | it ensures that \verb|lua_checkstack(L, LUA_MINSTACK)| is true, |
1807 | that is, that | ||
1820 | at least \verb|LUA_MINSTACK| positions are still available. | 1808 | at least \verb|LUA_MINSTACK| positions are still available. |
1821 | \verb|LUA_MINSTACK| is defined in \verb|lua.h| and is at least~16, | 1809 | \verb|LUA_MINSTACK| is defined in \verb|lua.h| as 20, |
1822 | so that usually you do not have to worry about stack space | 1810 | so that usually you do not have to worry about stack space |
1823 | unless your code has loops pushing elements onto the stack. | 1811 | unless your code has loops pushing elements onto the stack. |
1824 | 1812 | ||
1825 | Most query functions accept as indices any value inside the | 1813 | Most query functions accept as indices any value inside the |
1826 | available stack space. | 1814 | available stack space, that is, indices up to the maximum stack size |
1815 | you (or Lua) have checked through \verb|lua_checkstack|. | ||
1827 | Such indices are called \emph{acceptable indices}. | 1816 | Such indices are called \emph{acceptable indices}. |
1828 | More formally, we define an \IndexEmph{acceptable index} | 1817 | More formally, we define an \IndexEmph{acceptable index} |
1829 | as follows: | 1818 | as follows: |
@@ -2178,7 +2167,7 @@ but after the call the responsibility is back to you. | |||
2178 | If you need to push other elements after calling any of these functions, | 2167 | If you need to push other elements after calling any of these functions, |
2179 | and you want to ``play safe'', | 2168 | and you want to ``play safe'', |
2180 | you must either check the stack space | 2169 | you must either check the stack space |
2181 | with \verb|lua_stackspace| | 2170 | with \verb|lua_checkstack| |
2182 | or remove the returned elements | 2171 | or remove the returned elements |
2183 | from the stack (if you do not need them). | 2172 | from the stack (if you do not need them). |
2184 | For instance, the following code | 2173 | For instance, the following code |
@@ -2573,7 +2562,7 @@ information about an active function: | |||
2573 | const char *event; /* "call", "return" */ | 2562 | const char *event; /* "call", "return" */ |
2574 | int currentline; /* (l) */ | 2563 | int currentline; /* (l) */ |
2575 | const char *name; /* (n) */ | 2564 | const char *name; /* (n) */ |
2576 | const char *namewhat; /* (n) global, tag method, local, field */ | 2565 | const char *namewhat; /* (n) `global', `local', `field', `method' */ |
2577 | int nups; /* (u) number of upvalues */ | 2566 | int nups; /* (u) number of upvalues */ |
2578 | int linedefined; /* (S) */ | 2567 | int linedefined; /* (S) */ |
2579 | const char *what; /* (S) "Lua" function, "C" function, Lua "main" */ | 2568 | const char *what; /* (S) "Lua" function, "C" function, Lua "main" */ |
@@ -2661,11 +2650,10 @@ then \verb|name| is set to \verb|NULL|. | |||
2661 | 2650 | ||
2662 | \item[namewhat] | 2651 | \item[namewhat] |
2663 | Explains the previous field. | 2652 | Explains the previous field. |
2664 | If the function is a global variable, | 2653 | It can be \verb|"global"|, \verb|"local"|, \verb|"method"|, |
2665 | \verb|namewhat| is \verb|"global"|; | 2654 | \verb|"field"|, or \verb|""| (the empty string), |
2666 | if the function is a tag method, | 2655 | according to how the function was called. |
2667 | \verb|namewhat| is \verb|"tag-method"|; | 2656 | (Lua uses the empty string when no other option seems to apply.) |
2668 | otherwise, it is \verb|""| (the empty string). | ||
2669 | 2657 | ||
2670 | \item[nups] | 2658 | \item[nups] |
2671 | Number of upvalues of the function. | 2659 | Number of upvalues of the function. |
@@ -2974,11 +2962,6 @@ Similar to \verb|dostring|, | |||
2974 | but returns the contents of a Lua chunk as a function, | 2962 | but returns the contents of a Lua chunk as a function, |
2975 | instead of executing it. | 2963 | instead of executing it. |
2976 | 2964 | ||
2977 | \subsubsection*{\ff \T{newtype (name)}}\DefLIB{newtype}\label{pdf-newtype} | ||
2978 | Creates a new type with the given name | ||
2979 | (which can be used only for table objects). | ||
2980 | Returns the tag of the new type. | ||
2981 | |||
2982 | \subsubsection*{\ff \T{next (table, [index])}}\DefLIB{next} | 2965 | \subsubsection*{\ff \T{next (table, [index])}}\DefLIB{next} |
2983 | Allows a program to traverse all fields of a table. | 2966 | Allows a program to traverse all fields of a table. |
2984 | Its first argument is a table and its second argument | 2967 | Its first argument is a table and its second argument |
@@ -3027,9 +3010,45 @@ without invoking any tag method. | |||
3027 | \verb|index| is any value different from \nil; | 3010 | \verb|index| is any value different from \nil; |
3028 | and \verb|value| is any Lua value. | 3011 | and \verb|value| is any Lua value. |
3029 | 3012 | ||
3030 | \subsubsection*{\ff \T{require (module)}}\DefLIB{require} | 3013 | \subsubsection*{\ff \T{require (packagename)}}\DefLIB{require} |
3014 | |||
3015 | Loads the given package. | ||
3016 | The function starts by looking into the table \IndexVerb{_LOADED} | ||
3017 | whether \verb|packagename| is already loaded. | ||
3018 | If it is, then \verb|require| is done. | ||
3019 | Otherwise, it searches a path looking for a file to load. | ||
3020 | |||
3021 | If the global variable \IndexVerb{LUA_PATH} is a string, | ||
3022 | this string is the path. | ||
3023 | Otherwise, \verb|require| tries the environment variable \verb|LUA_PATH|. | ||
3024 | In the last resort, it uses a predefined path. | ||
3025 | |||
3026 | The path is a sequence of \emph{templates} separated by semicolons. | ||
3027 | For each template, \verb|require| will change an eventual interrogation | ||
3028 | mark in the template to \verb|packagename|, | ||
3029 | and then will try to load the resulting file name. | ||
3030 | So, for instance, if the path is | ||
3031 | \begin{verbatim} | ||
3032 | "./?.lua;./?.lc;/usr/local/?/init.lua;/lasttry" | ||
3033 | \end{verbatim} | ||
3034 | a \verb|require "mod"| will try to load the files | ||
3035 | \verb|./mod.lua|, | ||
3036 | \verb|./mod.lc|, | ||
3037 | \verb|/usr/local/mod/init.lua|, | ||
3038 | and \verb|/lasttry|, in that order. | ||
3039 | |||
3040 | The function stops the search as soon as it can load a file, | ||
3041 | and then it runs the file. | ||
3042 | If there is any error loading or running the file, | ||
3043 | or if it cannot find any file in the path, | ||
3044 | then \verb|require| signals an error. | ||
3045 | Otherwise, it marks in table \verb|_LOADED| | ||
3046 | that the package is loaded, and returns. | ||
3047 | |||
3048 | While running a packaged file, | ||
3049 | \verb|require| defines the global variable \IndexVerb{_REQUIREDNAME} | ||
3050 | with the package name. | ||
3031 | 3051 | ||
3032 | TO BE WRITTEN. | ||
3033 | 3052 | ||
3034 | \subsubsection*{\ff \T{sort (table [, comp])}}\DefLIB{sort} | 3053 | \subsubsection*{\ff \T{sort (table [, comp])}}\DefLIB{sort} |
3035 | Sorts table elements in a given order, \emph{in-place}, | 3054 | Sorts table elements in a given order, \emph{in-place}, |
@@ -3241,7 +3260,9 @@ stands for the value of the \M{n}-th captured substring. | |||
3241 | 3260 | ||
3242 | If \verb|repl| is a function, then this function is called every time a | 3261 | If \verb|repl| is a function, then this function is called every time a |
3243 | match occurs, with all captured substrings passed as arguments, | 3262 | match occurs, with all captured substrings passed as arguments, |
3244 | in order (see below). | 3263 | in order (see below); |
3264 | if the pattern specifies no captures, | ||
3265 | then the whole match is passed as a sole argument. | ||
3245 | If the value returned by this function is a string, | 3266 | If the value returned by this function is a string, |
3246 | then it is used as the replacement string; | 3267 | then it is used as the replacement string; |
3247 | otherwise, the replacement string is the empty string. | 3268 | otherwise, the replacement string is the empty string. |
@@ -3273,7 +3294,7 @@ Here are some examples: | |||
3273 | --> x="Lua - 4.1" | 3294 | --> x="Lua - 4.1" |
3274 | 3295 | ||
3275 | local t = {} | 3296 | local t = {} |
3276 | gsub("first second word", "(%w+)", function (w) tinsert(t, w) end) | 3297 | gsub("first second word", "%w+", function (w) tinsert(t, w) end) |
3277 | --> t={"first", "second", "word"; n=3} | 3298 | --> t={"first", "second", "word"; n=3} |
3278 | \end{verbatim} | 3299 | \end{verbatim} |
3279 | 3300 | ||
@@ -3628,14 +3649,7 @@ The available formats are | |||
3628 | this is the only format that returns a number instead of a string. | 3649 | this is the only format that returns a number instead of a string. |
3629 | \item[``*a''] reads the whole file, starting at the current position. | 3650 | \item[``*a''] reads the whole file, starting at the current position. |
3630 | On end of file, it returns the empty string. | 3651 | On end of file, it returns the empty string. |
3631 | \item[``*u\emph{string}''] reads until the first occurence of | 3652 | \item[``*l''] reads the next line (skipping the end of line), |
3632 | \emph{string} in the file. | ||
3633 | The string itself is read, but it is not included in the result. | ||
3634 | If \verb|read| cannot finds the string, | ||
3635 | it reads (and returns) the file until its end, | ||
3636 | or \nil\ if the file was already at its end. | ||
3637 | \item[``*l''] equivalent to \verb|"*u\n"|. | ||
3638 | Reads the next line (skipping the end of line), | ||
3639 | returning \nil\ on end of file. | 3653 | returning \nil\ on end of file. |
3640 | This is the default format. | 3654 | This is the default format. |
3641 | \item[\emph{number}] reads a string with up to that number of characters, | 3655 | \item[\emph{number}] reads a string with up to that number of characters, |
@@ -4033,7 +4047,7 @@ The \verb|lua_pushuserdata| function has been replaced by | |||
4033 | \OrNL \rwd{break} | 4047 | \OrNL \rwd{break} |
4034 | \OrNL \rwd{for} \Nter{Name} \ter{=} exp \ter{,} exp \opt{\ter{,} exp} | 4048 | \OrNL \rwd{for} \Nter{Name} \ter{=} exp \ter{,} exp \opt{\ter{,} exp} |
4035 | \rwd{do} block \rwd{end} | 4049 | \rwd{do} block \rwd{end} |
4036 | \OrNL \rwd{for} \Nter{Name} \opt{\ter{,} \Nter{Name}} \rwd{in} exp | 4050 | \OrNL \rwd{for} \Nter{Name} \rep{\ter{,} \Nter{Name}} \rwd{in} explist1 |
4037 | \rwd{do} block \rwd{end} | 4051 | \rwd{do} block \rwd{end} |
4038 | \OrNL \rwd{function} funcname \ter{(} \opt{parlist1} \ter{)} block \rwd{end} | 4052 | \OrNL \rwd{function} funcname \ter{(} \opt{parlist1} \ter{)} block \rwd{end} |
4039 | \OrNL \rwd{local} namelist \opt{init} | 4053 | \OrNL \rwd{local} namelist \opt{init} |