save comment position

This commit is contained in:
z7zmey
2018-04-15 21:39:26 +03:00
parent c2f938e55c
commit 781a55659b
10 changed files with 114 additions and 61 deletions

View File

@@ -8,6 +8,8 @@ import (
"io"
"unicode"
"github.com/z7zmey/php-parser/position"
"github.com/cznic/golex/lex"
"github.com/z7zmey/php-parser/comment"
)
@@ -521,7 +523,17 @@ func (l *Lexer) newToken(chars []lex.Char) Token {
}
func (l *Lexer) addComment(chars []lex.Char) {
c := comment.NewComment(string(l.charsToBytes(chars)))
firstChar := chars[0]
lastChar := chars[len(chars)-1]
pos := position.NewPosition(
l.File.Line(firstChar.Pos()),
l.File.Line(lastChar.Pos()),
int(firstChar.Pos()),
int(lastChar.Pos()),
)
c := comment.NewComment(string(l.charsToBytes(chars)), pos)
l.Comments = append(l.Comments, c)
}

View File

@@ -5,6 +5,8 @@ import (
"reflect"
"testing"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/scanner"
@@ -873,7 +875,7 @@ func TestCommentEnd(t *testing.T) {
src := `<?php //test`
expected := []*comment.Comment{
comment.NewComment("//test"),
comment.NewComment("//test", position.NewPosition(1, 1, 7, 12)),
}
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
@@ -890,7 +892,7 @@ func TestCommentNewLine(t *testing.T) {
src := "<?php //test\n$a"
expected := []*comment.Comment{
comment.NewComment("//test\n"),
comment.NewComment("//test\n", position.NewPosition(1, 1, 7, 13)),
}
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
@@ -907,7 +909,7 @@ func TestCommentNewLine1(t *testing.T) {
src := "<?php //test\r$a"
expected := []*comment.Comment{
comment.NewComment("//test\r"),
comment.NewComment("//test\r", position.NewPosition(1, 1, 7, 13)),
}
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
@@ -924,7 +926,7 @@ func TestCommentNewLine2(t *testing.T) {
src := "<?php #test\r\n$a"
expected := []*comment.Comment{
comment.NewComment("#test\r\n"),
comment.NewComment("#test\r\n", position.NewPosition(1, 1, 7, 13)),
}
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
@@ -942,7 +944,7 @@ func TestCommentWithPhpEndTag(t *testing.T) {
//test?> test`
expected := []*comment.Comment{
comment.NewComment("//test"),
comment.NewComment("//test", position.NewPosition(2, 2, 8, 13)),
}
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
@@ -960,7 +962,7 @@ func TestInlineComment(t *testing.T) {
/*test*/`
expected := []*comment.Comment{
comment.NewComment("/*test*/"),
comment.NewComment("/*test*/", position.NewPosition(2, 2, 8, 15)),
}
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
@@ -978,7 +980,7 @@ func TestEmptyInlineComment(t *testing.T) {
/**/`
expected := []*comment.Comment{
comment.NewComment("/**/"),
comment.NewComment("/**/", position.NewPosition(2, 2, 8, 11)),
}
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
@@ -996,7 +998,7 @@ func TestEmptyInlineComment2(t *testing.T) {
/***/`
expected := []*comment.Comment{
comment.NewComment("/***/"),
comment.NewComment("/***/", position.NewPosition(2, 2, 8, 12)),
}
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")

View File

@@ -13,7 +13,7 @@ func TestToken(t *testing.T) {
tkn := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3)
c := []*comment.Comment{
comment.NewComment("test comment"),
comment.NewComment("test comment", nil),
}
tkn.SetComments(c)