1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

[#148] Added mode to store comments

This commit is contained in:
wolfgarnet 2016-01-12 10:15:49 +01:00
parent fa2636115f
commit 6d8bdc96fd
2 changed files with 20 additions and 7 deletions

View File

@ -239,14 +239,22 @@ func (self *_parser) scan() (tkn token.Token, literal string, idx file.Idx) {
tkn = self.switch2(token.MULTIPLY, token.MULTIPLY_ASSIGN)
case '/':
if self.chr == '/' {
runes := self.readSingleLineComment()
literal = string(runes)
tkn = token.COMMENT
return
if self.mode & StoreComments != 0 {
runes := self.readSingleLineComment()
literal = string(runes)
tkn = token.COMMENT
return
}
self.skipSingleLineComment()
continue
} else if self.chr == '*' {
literal = string(self.readMultiLineComment())
tkn = token.COMMENT
return
if self.mode & StoreComments != 0 {
literal = string(self.readMultiLineComment())
tkn = token.COMMENT
return
}
self.skipMultiLineComment()
continue
} else {
// Could be division, could be RegExp literal
tkn = self.switch2(token.SLASH, token.QUOTIENT_ASSIGN)

View File

@ -49,6 +49,7 @@ type Mode uint
const (
IgnoreRegExpErrors Mode = 1 << iota // Ignore RegExp compatibility errors (allow backtracking)
StoreComments // Store the comments from source to the comments map
)
type _parser struct {
@ -285,8 +286,12 @@ func (self *_parser) position(idx file.Idx) file.Position {
// Comments on the same line will be grouped together and returned.
// After the first line break, comments will be added as statement comments.
func (self *_parser) findComments(ignoreLineBreak bool) []*ast.Comment {
if self.mode & StoreComments == 0 {
return nil
}
comments := make([]*ast.Comment, 0)
newline := false
for self.implicitSemicolon == false || ignoreLineBreak {