merge DocComment and PlainComment
This commit is contained in:
parent
e65ace8984
commit
c2f938e55c
@ -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
|
||||
}
|
||||
|
@ -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{}
|
||||
|
11
comment/comments.go
Normal file
11
comment/comments.go
Normal file
@ -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...)
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
<PHP>\>\> lval.Token(l.newToken(l.Token())); return T_SR
|
||||
<PHP>\?\? lval.Token(l.newToken(l.Token())); return T_COALESCE
|
||||
<PHP>(#|[/][/])
|
||||
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)
|
||||
|
||||
<PHP>([/][*])|([/][*][*])
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -872,8 +872,8 @@ func TestSlashAfterVariable(t *testing.T) {
|
||||
func TestCommentEnd(t *testing.T) {
|
||||
src := `<?php //test`
|
||||
|
||||
expected := []comment.Comment{
|
||||
comment.NewPlainComment("//test"),
|
||||
expected := []*comment.Comment{
|
||||
comment.NewComment("//test"),
|
||||
}
|
||||
|
||||
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
|
||||
@ -889,8 +889,8 @@ func TestCommentEnd(t *testing.T) {
|
||||
func TestCommentNewLine(t *testing.T) {
|
||||
src := "<?php //test\n$a"
|
||||
|
||||
expected := []comment.Comment{
|
||||
comment.NewPlainComment("//test\n"),
|
||||
expected := []*comment.Comment{
|
||||
comment.NewComment("//test\n"),
|
||||
}
|
||||
|
||||
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
|
||||
@ -906,8 +906,8 @@ func TestCommentNewLine(t *testing.T) {
|
||||
func TestCommentNewLine1(t *testing.T) {
|
||||
src := "<?php //test\r$a"
|
||||
|
||||
expected := []comment.Comment{
|
||||
comment.NewPlainComment("//test\r"),
|
||||
expected := []*comment.Comment{
|
||||
comment.NewComment("//test\r"),
|
||||
}
|
||||
|
||||
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
|
||||
@ -923,8 +923,8 @@ func TestCommentNewLine1(t *testing.T) {
|
||||
func TestCommentNewLine2(t *testing.T) {
|
||||
src := "<?php #test\r\n$a"
|
||||
|
||||
expected := []comment.Comment{
|
||||
comment.NewPlainComment("#test\r\n"),
|
||||
expected := []*comment.Comment{
|
||||
comment.NewComment("#test\r\n"),
|
||||
}
|
||||
|
||||
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
|
||||
@ -941,8 +941,8 @@ func TestCommentWithPhpEndTag(t *testing.T) {
|
||||
src := `<?php
|
||||
//test?> 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 := `<?php
|
||||
/*test*/`
|
||||
|
||||
expected := []comment.Comment{
|
||||
comment.NewPlainComment("/*test*/"),
|
||||
expected := []*comment.Comment{
|
||||
comment.NewComment("/*test*/"),
|
||||
}
|
||||
|
||||
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
|
||||
@ -977,8 +977,8 @@ func TestEmptyInlineComment(t *testing.T) {
|
||||
src := `<?php
|
||||
/**/`
|
||||
|
||||
expected := []comment.Comment{
|
||||
comment.NewDocComment("/**/"),
|
||||
expected := []*comment.Comment{
|
||||
comment.NewComment("/**/"),
|
||||
}
|
||||
|
||||
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
|
||||
@ -995,8 +995,8 @@ func TestEmptyInlineComment2(t *testing.T) {
|
||||
src := `<?php
|
||||
/***/`
|
||||
|
||||
expected := []comment.Comment{
|
||||
comment.NewDocComment("/***/"),
|
||||
expected := []*comment.Comment{
|
||||
comment.NewComment("/***/"),
|
||||
}
|
||||
|
||||
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
|
||||
|
@ -11,7 +11,7 @@ type Token struct {
|
||||
EndLine int
|
||||
StartPos int
|
||||
EndPos int
|
||||
comments []comment.Comment
|
||||
comments []*comment.Comment
|
||||
}
|
||||
|
||||
// NewToken Token constructor
|
||||
@ -25,12 +25,12 @@ func (t Token) String() string {
|
||||
}
|
||||
|
||||
// Comments returns attached comments
|
||||
func (t Token) Comments() []comment.Comment {
|
||||
func (t Token) Comments() []*comment.Comment {
|
||||
return t.comments
|
||||
}
|
||||
|
||||
// SetComments attach comments
|
||||
func (t Token) SetComments(comments []comment.Comment) Token {
|
||||
func (t Token) SetComments(comments []*comment.Comment) Token {
|
||||
t.comments = comments
|
||||
return t
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ import (
|
||||
func TestToken(t *testing.T) {
|
||||
tkn := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3)
|
||||
|
||||
c := []comment.Comment{
|
||||
comment.NewPlainComment("test comment"),
|
||||
c := []*comment.Comment{
|
||||
comment.NewComment("test comment"),
|
||||
}
|
||||
|
||||
tkn.SetComments(c)
|
||||
|
Loading…
Reference in New Issue
Block a user