From c2f938e55c3abe0942347e4676c5731b0f7a5b3f Mon Sep 17 00:00:00 2001 From: z7zmey Date: Sun, 15 Apr 2018 15:55:33 +0300 Subject: [PATCH] merge DocComment and PlainComment --- comment/comment.go | 23 ++++++++++++----------- comment/comment_test.go | 6 +++--- comment/comments.go | 11 +++++++++++ comment/doc_comment.go | 17 ----------------- comment/plain_comment.go | 17 ----------------- php5/parser.go | 2 +- php7/parser.go | 2 +- scanner/lexer.go | 5 +++-- scanner/scanner.go | 14 +++++--------- scanner/scanner.l | 15 +++++---------- scanner/scanner_test.go | 32 ++++++++++++++++---------------- scanner/token.go | 6 +++--- scanner/token_test.go | 4 ++-- 13 files changed, 62 insertions(+), 92 deletions(-) create mode 100644 comment/comments.go delete mode 100644 comment/doc_comment.go delete mode 100644 comment/plain_comment.go diff --git a/comment/comment.go b/comment/comment.go index f65f067..c8713f2 100644 --- a/comment/comment.go +++ b/comment/comment.go @@ -1,16 +1,17 @@ package comment -import "github.com/z7zmey/php-parser/node" - -// Comment represents comment lines in the code -type Comment interface { - String() string +// Comment aggrigates information about comment /** +type Comment struct { + value string } -// Comments a collection of comment groups assigned to nodes -type Comments map[node.Node][]Comment - -// AddComments add comment group to the collection -func (c Comments) AddComments(node node.Node, comments []Comment) { - c[node] = append(c[node], comments...) +// NewComment - Comment constructor +func NewComment(value string) *Comment { + return &Comment{ + value, + } +} + +func (c *Comment) String() string { + return c.value } diff --git a/comment/comment_test.go b/comment/comment_test.go index a89b0e1..27980e9 100644 --- a/comment/comment_test.go +++ b/comment/comment_test.go @@ -10,9 +10,9 @@ import ( func TestComments(t *testing.T) { n := node.NewIdentifier("test") - commentGroup := []comment.Comment{ - comment.NewDocComment("/** hello world */"), - comment.NewPlainComment("// hello world"), + commentGroup := []*comment.Comment{ + comment.NewComment("/** hello world */"), + comment.NewComment("// hello world"), } comments := comment.Comments{} diff --git a/comment/comments.go b/comment/comments.go new file mode 100644 index 0000000..8ef687d --- /dev/null +++ b/comment/comments.go @@ -0,0 +1,11 @@ +package comment + +import "github.com/z7zmey/php-parser/node" + +// Comments a collection of comment groups assigned to nodes +type Comments map[node.Node][]*Comment + +// AddComments add comment group to the collection +func (c Comments) AddComments(node node.Node, comments []*Comment) { + c[node] = append(c[node], comments...) +} diff --git a/comment/doc_comment.go b/comment/doc_comment.go deleted file mode 100644 index c0b9a93..0000000 --- a/comment/doc_comment.go +++ /dev/null @@ -1,17 +0,0 @@ -package comment - -// DocComment represents comments that start /** -type DocComment struct { - value string -} - -// NewDocComment - DocComment constructor -func NewDocComment(value string) *DocComment { - return &DocComment{ - value, - } -} - -func (c *DocComment) String() string { - return c.value -} diff --git a/comment/plain_comment.go b/comment/plain_comment.go deleted file mode 100644 index 89371a8..0000000 --- a/comment/plain_comment.go +++ /dev/null @@ -1,17 +0,0 @@ -package comment - -// PlainComment represents comments that dont start /** -type PlainComment struct { - value string -} - -// NewPlainComment - PlainComment constructor -func NewPlainComment(value string) *PlainComment { - return &PlainComment{ - value, - } -} - -func (c *PlainComment) String() string { - return c.value -} diff --git a/php5/parser.go b/php5/parser.go index 5df3681..03a6f22 100644 --- a/php5/parser.go +++ b/php5/parser.go @@ -72,7 +72,7 @@ func (l *Parser) Parse() int { return yyParse(l) } -func (l *Parser) listGetFirstNodeComments(list []node.Node) []comment.Comment { +func (l *Parser) listGetFirstNodeComments(list []node.Node) []*comment.Comment { if len(list) == 0 { return nil } diff --git a/php7/parser.go b/php7/parser.go index 355189b..9c3b34b 100644 --- a/php7/parser.go +++ b/php7/parser.go @@ -72,7 +72,7 @@ func (l *Parser) Parse() int { return yyParse(l) } -func (l *Parser) listGetFirstNodeComments(list []node.Node) []comment.Comment { +func (l *Parser) listGetFirstNodeComments(list []node.Node) []*comment.Comment { if len(list) == 0 { return nil } diff --git a/scanner/lexer.go b/scanner/lexer.go index d3b3b03..e415f8e 100644 --- a/scanner/lexer.go +++ b/scanner/lexer.go @@ -438,7 +438,7 @@ type Lexer struct { *lex.Lexer StateStack []int PhpDocComment string - Comments []comment.Comment + Comments []*comment.Comment } // Rune2Class returns the rune integer id @@ -520,7 +520,8 @@ func (l *Lexer) newToken(chars []lex.Char) Token { return NewToken(l.charsToBytes(chars), startLine, endLine, startPos, endPos).SetComments(l.Comments) } -func (l *Lexer) addComment(c comment.Comment) { +func (l *Lexer) addComment(chars []lex.Char) { + c := comment.NewComment(string(l.charsToBytes(chars))) l.Comments = append(l.Comments, c) } diff --git a/scanner/scanner.go b/scanner/scanner.go index fde9daf..fc80d5e 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -12,7 +12,6 @@ import ( "bytes" "fmt" "github.com/cznic/golex/lex" - "github.com/z7zmey/php-parser/comment" ) const ( @@ -8311,15 +8310,12 @@ yyrule125: // \?\? yyrule126: // (#|[/][/]) { - tb := []rune{} - for _, chr := range l.Token() { - tb = append(tb, chr.Rune) - } + tb := l.Token() for { if c == -1 { break } - tb = append(tb, rune(c)) + tb = append(tb, l.Last) switch c { case '\r': c = l.Next() @@ -8342,7 +8338,7 @@ yyrule126: // (#|[/][/]) } break } - l.addComment(comment.NewPlainComment(string(tb))) + l.addComment(tb) goto yystate0 } yyrule127: // ([/][*])|([/][*][*]) @@ -8367,10 +8363,10 @@ yyrule127: // ([/][*])|([/][*][*]) lval.Token(l.newToken(l.Token())) if is_doc_comment { l.PhpDocComment = string(l.TokenBytes(nil)) - l.addComment(comment.NewDocComment(string(l.TokenBytes(nil)))) + l.addComment(l.Token()) // return T_DOC_COMMENT } else { - l.addComment(comment.NewPlainComment(string(l.TokenBytes(nil)))) + l.addComment(l.Token()) // return T_COMMENT } goto yystate0 diff --git a/scanner/scanner.l b/scanner/scanner.l index e3a105b..624d1b6 100644 --- a/scanner/scanner.l +++ b/scanner/scanner.l @@ -11,7 +11,6 @@ import ( "fmt" "bytes" "github.com/cznic/golex/lex" - "github.com/z7zmey/php-parser/comment" ) const ( @@ -253,18 +252,14 @@ NEW_LINE (\r|\n|\r\n) \>\> lval.Token(l.newToken(l.Token())); return T_SR \?\? lval.Token(l.newToken(l.Token())); return T_COALESCE (#|[/][/]) - tb := []rune{} - - for _, chr := range(l.Token()) { - tb = append(tb, chr.Rune) - } + tb := l.Token() for { if c == -1 { break } - tb = append(tb, rune(c)) + tb = append(tb, l.Last) switch c { case '\r': @@ -292,7 +287,7 @@ NEW_LINE (\r|\n|\r\n) break; } - l.addComment(comment.NewPlainComment(string(tb))) + l.addComment(tb) ([/][*])|([/][*][*]) tb := l.Token() @@ -318,10 +313,10 @@ NEW_LINE (\r|\n|\r\n) lval.Token(l.newToken(l.Token())) if is_doc_comment { l.PhpDocComment = string(l.TokenBytes(nil)) - l.addComment(comment.NewDocComment(string(l.TokenBytes(nil)))) + l.addComment(l.Token()) // return T_DOC_COMMENT } else { - l.addComment(comment.NewPlainComment(string(l.TokenBytes(nil)))) + l.addComment(l.Token()) // return T_COMMENT } diff --git a/scanner/scanner_test.go b/scanner/scanner_test.go index 6beed3c..b63afb5 100644 --- a/scanner/scanner_test.go +++ b/scanner/scanner_test.go @@ -872,8 +872,8 @@ func TestSlashAfterVariable(t *testing.T) { func TestCommentEnd(t *testing.T) { src := ` test` - expected := []comment.Comment{ - comment.NewPlainComment("//test"), + expected := []*comment.Comment{ + comment.NewComment("//test"), } lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php") @@ -959,8 +959,8 @@ func TestInlineComment(t *testing.T) { src := `