Can ".." occur in golang source?

156 views
Skip to first unread message

Ryan Keppel

unread,
Oct 7, 2020, 3:22:43 AM10/7/20
to golang-nuts
In the current Golang implementation of scanning, there's some extra code to handle ".." in the source (as two dot tokens). Would this ever happen in practice? Two floats together?

Jan Mercl

unread,
Oct 7, 2020, 4:04:21 AM10/7/20
to Ryan Keppel, golang-nuts
On Wed, Oct 7, 2020 at 9:23 AM Ryan Keppel <ryan.k...@gmail.com> wrote:

> In the current Golang

The name is Go. There's no Golang programming language.

> implementation of scanning, there's some extra code to handle ".." in the source (as two dot tokens).

Do you mean this? https://golang.org/src/go/scanner/scanner.go#L837

> Would this ever happen in practice?

Sure, why not? Scanner will happily scan two consecutive dot tokens,
that's its job.

> Two floats together?

No. This seems to conflate what a scanner is for with the grammar of
the language. Package scanner can handle sources like `for package if
not break ..123..` just fine. The language specification not so much.
But in Go, as in many other languages, the lexical and syntax grammars
are two different things, even though the latter builds upon the
former.

Ryan Keppel

unread,
Oct 7, 2020, 4:29:24 AM10/7/20
to golang-nuts
Yes, I mean that code section. I did a quick test and two floats tokenizes just fine (2..5 tokenizes to "2." and ".5")--it doesn't invoke the described code. I don't think Go in practice would allow two dot tokens in a row. Go's parser is very loose ("1 + 10 = 20" isn't a syntax error) and I don't see it allowing that.

Ryan Keppel

unread,
Oct 7, 2020, 4:31:55 AM10/7/20
to golang-nuts

Jesper Louis Andersen

unread,
Oct 7, 2020, 4:40:36 AM10/7/20
to Ryan Keppel, golang-nuts
On Wed, Oct 7, 2020 at 10:29 AM Ryan Keppel <ryan.k...@gmail.com> wrote:
Yes, I mean that code section. I did a quick test and two floats tokenizes just fine (2..5 tokenizes to "2." and ".5")--it doesn't invoke the described code. I don't think Go in practice would allow two dot tokens in a row. Go's parser is very loose ("1 + 10 = 20" isn't a syntax error) and I don't see it allowing that.


That code section scans for token.ELLIPSIS, i.e., '...'.

The scanner has ch := s.ch already from the "outer" switch. When you hit the default case, you run s.next() which advances the scanner. Now, s.ch points to the second '.' and the s.peek() will have us peek at the third '.'. The calls to s.next() twice advances us beyond that, and we have an ellipsis, i.e., '...'

Lines of interest, though they might move: 791, 809, 837-844 (The future reader might be interested in knowing this was around go version 1.15.2)

Brian Candler

unread,
Oct 7, 2020, 4:56:39 AM10/7/20
to golang-nuts
On Wednesday, 7 October 2020 09:31:55 UTC+1, Ryan Keppel wrote:
I meant this code: https://golang.org/src/cmd/compile/internal/syntax/scanner.go#L187 .

That code says:

- dot followed by a decimal is a number
- dot followed by dot followed by dot is a _DotDotDot (ellipsis)
- otherwise, it's just a _Dot

Reply all
Reply to author
Forward
0 new messages