(Apologies to Ian to whom I emailed this earlier rather than to the
mailing list.)
As I suggested earlier, using terminology like "statement terminator"
would make things much clearer than terminology like "semicolon
injection". Your debate over specification/implementation could be
solved here. I think what Peter is saying is that "the Go
implementation injects semicolons as part of its implementation of the
Go specification on how to interpret linebreaks".
In one sense, Peter's right: a Go lexer/parser/compiler suite could be
written that makes the exact same transformation between source code
and executable binary, without ever specifically doing something like
"injecting semicolons" insome intermediate stage.
In another, Ian's right: the Go specification
(
http://golang.org/doc/go_spec.html#Semicolons), when formalizing the
exact meaning of a newline in its context, does so by describing one
possible approach: semicolon injection.
Here is the full wording of the implementation:
---
The formal grammar uses semicolons ";" as terminators in a number of
productions. Go programs may omit most of these semicolons using the
following two rules:
1. When the input is broken into tokens, a semicolon is
automatically inserted into the token stream at the end of a non-blank
line if the line's final token is
* an identifier
* an integer, floating-point, character, or string literal
* one of the keywords break, continue, fallthrough, or return
* one of the operators and delimiters ++, --, ), ], or }
2. To allow complex statements to occupy a single line, a semicolon
may be omitted before a closing ")" or "}".
To reflect idiomatic use, code examples in this document elide
semicolons using these rules.
---
Now, I suggest the specification could be changed to something like
the following, while remaining exactly the same semantically (i.e. the
specification as a whole still specifies the exact same one-way
transformation between source code and executable binary).
---
The semicolon is Go's explicit non-context-sensitive statement terminator.
Go also includes a second statement terminator, the newline character.
The newline is context-sensitive, and is interpreted as a terminator
if the line that it ends is non-blank and its final token is one of:
* an identifier
* an integer, floating-point, character, or string literal
* one of the keywords break, continue, fallthrough, or return
* one of the operators and delimiters ++, --, ), ], or }
To reflect idiomatic use, code examples in this document elide
semicolons using these rules.
---
This does not include yet include rule two ('To allow complex
statements to occupy a single line, a semicolon may be omitted before
a closing ")" or "}"'). This doesn't seem to have been discussed at
all in this thread. Would I be wrong in saying that the ")" and "}"
are in fact treated the same as the newline? i.e., if any of the
above listed tokens precede the closing parenthesis/brace, it has a
statement termination injected before it?