Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
20430f03
Commit
20430f03
authored
Sep 27, 2010
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
go/scanner: treat EOF like a newline for purposes of semicolon insertion
R=rsc CC=golang-dev
https://golang.org/cl/2216054
parent
b4e358d7
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
15 deletions
+25
-15
src/pkg/go/scanner/scanner.go
src/pkg/go/scanner/scanner.go
+17
-11
src/pkg/go/scanner/scanner_test.go
src/pkg/go/scanner/scanner_test.go
+8
-4
No files found.
src/pkg/go/scanner/scanner.go
View file @
20430f03
...
...
@@ -196,26 +196,27 @@ func (S *Scanner) scanComment(pos token.Position) {
}
func
(
S
*
Scanner
)
find
Newline
(
pos
token
.
Position
)
bool
{
func
(
S
*
Scanner
)
find
LineEnd
(
pos
token
.
Position
)
bool
{
// first '/' already consumed; assume S.ch == '/' || S.ch == '*'
// read ahead until a newline or non-comment token is found
newline
:=
false
// read ahead until a newline
, EOF,
or non-comment token is found
lineend
:=
false
for
pos1
:=
pos
;
S
.
ch
>=
0
;
{
if
S
.
ch
==
'/'
{
//-style comment always contains a newline
newline
=
true
lineend
=
true
break
}
S
.
scanComment
(
pos1
)
if
pos1
.
Line
<
S
.
pos
.
Line
{
/*-style comment contained a newline */
newline
=
true
lineend
=
true
break
}
S
.
skipWhitespace
()
// S.insertSemi is set
if
S
.
ch
==
'\n'
{
newline
=
true
if
S
.
ch
<
0
||
S
.
ch
==
'\n'
{
// line end
lineend
=
true
break
}
if
S
.
ch
!=
'/'
{
...
...
@@ -230,12 +231,12 @@ func (S *Scanner) findNewline(pos token.Position) bool {
}
}
// reset position to where it was upon calling find
Newline
// reset position to where it was upon calling find
LineEnd
S
.
pos
=
pos
S
.
offset
=
pos
.
Offset
+
1
S
.
next
()
return
newline
return
lineend
}
...
...
@@ -507,7 +508,8 @@ var newline = []byte{'\n'}
//
// If the returned token is token.SEMICOLON, the corresponding
// literal value is ";" if the semicolon was present in the source,
// and "\n" if the semicolon was inserted because of a newline.
// and "\n" if the semicolon was inserted because of a newline or
// at EOF.
//
// For more tolerant parsing, Scan will return a valid token if
// possible even if a syntax error was encountered. Thus, even
...
...
@@ -539,6 +541,10 @@ scanAgain:
S
.
next
()
// always make progress
switch
ch
{
case
-
1
:
if
S
.
insertSemi
{
S
.
insertSemi
=
false
// EOF consumed
return
pos
,
token
.
SEMICOLON
,
newline
}
tok
=
token
.
EOF
case
'\n'
:
// we only reach here if S.insertSemi was
...
...
@@ -607,7 +613,7 @@ scanAgain:
case
'/'
:
if
S
.
ch
==
'/'
||
S
.
ch
==
'*'
{
// comment
if
S
.
insertSemi
&&
S
.
find
Newline
(
pos
)
{
if
S
.
insertSemi
&&
S
.
find
LineEnd
(
pos
)
{
// reset position to the beginning of the comment
S
.
pos
=
pos
S
.
offset
=
pos
.
Offset
+
1
...
...
src/pkg/go/scanner/scanner_test.go
View file @
20430f03
...
...
@@ -403,11 +403,10 @@ var lines = []string{
"foo $/*comment*/
\n
"
,
"foo $/*
\n
*/"
,
"foo $/*comment*/
\n
"
,
"foo $/*0*/ /*1*/ /*2*/
\n
"
,
"foo $/*comment*/
\n
"
,
"foo $/*0*/ /*1*/ /*2*/
\n
"
,
"foo $/**/ /*-------------*/ /*----
\n
*/bar $/*
\n
*/baa"
,
"foo $/**/ /*-------------*/ /*----
\n
*/bar $/*
\n
*/baa
$
\n
"
,
"package main$
\n\n
func main() {
\n\t
if {
\n\t\t
return /* */ }$
\n
}$
\n
"
,
}
...
...
@@ -416,9 +415,14 @@ var lines = []string{
func
TestSemis
(
t
*
testing
.
T
)
{
for
_
,
line
:=
range
lines
{
checkSemi
(
t
,
line
,
AllowIllegalChars
|
InsertSemis
)
}
for
_
,
line
:=
range
lines
{
checkSemi
(
t
,
line
,
AllowIllegalChars
|
InsertSemis
|
ScanComments
)
// if the input ended in newlines, the input must tokenize the
// same with or without those newlines
for
i
:=
len
(
line
)
-
1
;
i
>=
0
&&
line
[
i
]
==
'\n'
;
i
--
{
checkSemi
(
t
,
line
[
0
:
i
],
AllowIllegalChars
|
InsertSemis
)
checkSemi
(
t
,
line
[
0
:
i
],
AllowIllegalChars
|
InsertSemis
|
ScanComments
)
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment