Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scanner: ... insertSemi only not in () #1708

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Scanner struct {
offset int // character offset
rdOffset int // reading offset (position after current character)
lineOffset int // current line offset
nParen int
insertSemi bool // insert a semicolon before next newline

// public state - ok to modify
Expand Down Expand Up @@ -783,6 +784,11 @@ func (s *Scanner) switch4(tok0, tok1 token.Token, ch2 rune, tok2, tok3 token.Tok
return tok0
}

func (s *Scanner) tokSEMICOLON() token.Token {
s.nParen = 0
return token.SEMICOLON
}

// Scan scans the next token and returns the token position, the token,
// and its literal string if applicable. The source end is indicated by
// token.EOF.
Expand Down Expand Up @@ -850,15 +856,15 @@ scanAgain:
case -1:
if s.insertSemi {
s.insertSemi = false // EOF consumed
return pos, token.SEMICOLON, "\n"
return pos, s.tokSEMICOLON(), "\n"
}
tok = token.EOF
case '\n':
// we only reach here if s.insertSemi was
// set in the first place and exited early
// from s.skipWhitespace()
s.insertSemi = false // newline consumed
return pos, token.SEMICOLON, "\n"
return pos, s.tokSEMICOLON(), "\n"
case '"':
insertSemi = true
tok = token.STRING
Expand All @@ -879,17 +885,21 @@ scanAgain:
if s.ch == '.' && s.peek() == '.' {
s.next()
s.next() // consume last '.'
insertSemi = true
if s.nParen == 0 {
insertSemi = true
}
tok = token.ELLIPSIS
}
case ',':
tok = token.COMMA
case ';':
tok = token.SEMICOLON
tok = s.tokSEMICOLON()
lit = ";"
case '(':
s.nParen++
tok = token.LPAREN
case ')':
s.nParen--
insertSemi = true
tok = token.RPAREN
case '[':
Expand Down Expand Up @@ -920,7 +930,7 @@ scanAgain:
s.offset = s.file.Offset(pos)
s.rdOffset = s.offset + 1
s.insertSemi = false // newline consumed
return pos, token.SEMICOLON, "\n"
return pos, s.tokSEMICOLON(), "\n"
}
comment := s.scanComment()
if s.mode&ScanComments == 0 {
Expand All @@ -939,7 +949,7 @@ scanAgain:
s.offset = s.file.Offset(pos)
s.rdOffset = s.offset + 1
s.insertSemi = false // newline consumed
return pos, token.SEMICOLON, "\n"
return pos, s.tokSEMICOLON(), "\n"
}
comment := s.scanComment()
if s.mode&ScanComments == 0 {
Expand Down