Skip to content

Commit

Permalink
Merge pull request #1708 from xushiwei/semi
Browse files Browse the repository at this point in the history
scanner: ... insertSemi only not in ()
  • Loading branch information
xushiwei committed Feb 5, 2024
2 parents e393046 + 841741a commit 5ed66d4
Showing 1 changed file with 16 additions and 6 deletions.
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

0 comments on commit 5ed66d4

Please sign in to comment.