mirror of
https://github.com/robertkrimen/otto
synced 2025-10-19 19:55:30 +08:00
[#148] Added mode to store comments
This commit is contained in:
parent
fa2636115f
commit
6d8bdc96fd
|
|
@ -239,14 +239,22 @@ func (self *_parser) scan() (tkn token.Token, literal string, idx file.Idx) {
|
||||||
tkn = self.switch2(token.MULTIPLY, token.MULTIPLY_ASSIGN)
|
tkn = self.switch2(token.MULTIPLY, token.MULTIPLY_ASSIGN)
|
||||||
case '/':
|
case '/':
|
||||||
if self.chr == '/' {
|
if self.chr == '/' {
|
||||||
runes := self.readSingleLineComment()
|
if self.mode & StoreComments != 0 {
|
||||||
literal = string(runes)
|
runes := self.readSingleLineComment()
|
||||||
tkn = token.COMMENT
|
literal = string(runes)
|
||||||
return
|
tkn = token.COMMENT
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.skipSingleLineComment()
|
||||||
|
continue
|
||||||
} else if self.chr == '*' {
|
} else if self.chr == '*' {
|
||||||
literal = string(self.readMultiLineComment())
|
if self.mode & StoreComments != 0 {
|
||||||
tkn = token.COMMENT
|
literal = string(self.readMultiLineComment())
|
||||||
return
|
tkn = token.COMMENT
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.skipMultiLineComment()
|
||||||
|
continue
|
||||||
} else {
|
} else {
|
||||||
// Could be division, could be RegExp literal
|
// Could be division, could be RegExp literal
|
||||||
tkn = self.switch2(token.SLASH, token.QUOTIENT_ASSIGN)
|
tkn = self.switch2(token.SLASH, token.QUOTIENT_ASSIGN)
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,7 @@ type Mode uint
|
||||||
|
|
||||||
const (
|
const (
|
||||||
IgnoreRegExpErrors Mode = 1 << iota // Ignore RegExp compatibility errors (allow backtracking)
|
IgnoreRegExpErrors Mode = 1 << iota // Ignore RegExp compatibility errors (allow backtracking)
|
||||||
|
StoreComments // Store the comments from source to the comments map
|
||||||
)
|
)
|
||||||
|
|
||||||
type _parser struct {
|
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.
|
// Comments on the same line will be grouped together and returned.
|
||||||
// After the first line break, comments will be added as statement comments.
|
// After the first line break, comments will be added as statement comments.
|
||||||
func (self *_parser) findComments(ignoreLineBreak bool) []*ast.Comment {
|
func (self *_parser) findComments(ignoreLineBreak bool) []*ast.Comment {
|
||||||
|
if self.mode & StoreComments == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
comments := make([]*ast.Comment, 0)
|
comments := make([]*ast.Comment, 0)
|
||||||
|
|
||||||
|
|
||||||
newline := false
|
newline := false
|
||||||
|
|
||||||
for self.implicitSemicolon == false || ignoreLineBreak {
|
for self.implicitSemicolon == false || ignoreLineBreak {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user