lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




Le mar. 4 juin 2019 à 23:27, Lorenzo Donati <lorenzodonatibz@tiscali.it> a écrit :
Compare:
local < toclose, static,
   nice, helpful, wonderful > resource = ....
vs.
local @toclose @static
   @nice @helpful @wonderful resource = ....

In the first case the possibility to introduce spaces "inside" the angle
brackets could be a source of confusion, too (not to mention spaces at
commas in different coding styles). And maybe also could render the
grammar ambiguous.

That last claim is dubious: whitespaces (spaces, tabs, newlines) do not play any role in th Lua syntax, except being explicit token separators (but they don't generate any token by themselves that can be significant in the syntax). Their coding style also does not matter (and various programmers have their own preferences for their projects).

---

Lua also allows the explicit separator ";" as a syntaxically-significant token, for separating statements that must not be merged into a single statement (for currying function calls).

The ";" token is currently useful only *before* _expression_-statements, and everywhere else are just no-op statements. There's currently no use at all of ";" **inside** statements (including inside expressions), because it unconditionally terminates the current statement before it but does not change its meaning or parsing rules.  In all other cases, the ";" may be dropped.

The ";" is just a mark detected internally by the parser as a "look-ahead" token, needed by the parser to solve a shift/reduce ambiguity when parsing statements (in that case the parser uses the "shift" action if the next token is not a ";" without ever needing to rollback because it will never use the "reduce" action; but if the next token is a ";" the current statement will be reduced, without that ";"). The ";" token is not reduced by itself inside any statement, except for the empty statement construct (which is a no-op, then silently discarded).

The ";" token may not occur at all before some other tokens that cannot be the start a statement or a part of a compound statement construct (notably the ";" cannot occur before operators, including "or", "and", "not", but also before symbols like "+", "-", "*", "/" or "^") .

So when you terminate any statement with a ";" before some reserved keyword (like "begin", "end", "for", "do", "else", "while", "repeat", "until", "return", "function", or "local"), this is intepreted as an additional no-op statement, after the first statement, parsed and reduced before parsing all further tokens (either "end", "until", "else" for some complex statement constructs, or other tokens marking the start of a new statement).