#25: save position within node

This commit is contained in:
z7zmey
2018-06-24 10:19:44 +03:00
parent 6ac67675d5
commit 1ebb0c6fad
268 changed files with 53606 additions and 7719 deletions

View File

@@ -445,7 +445,6 @@ type Lexer struct {
heredocLabel string
tokenBytesBuf *bytes.Buffer
TokenPool sync.Pool
PositionPool sync.Pool
}
// Rune2Class returns the rune integer id
@@ -484,9 +483,6 @@ func NewLexer(src io.Reader, fName string) *Lexer {
TokenPool: sync.Pool{
New: func() interface{} { return &Token{} },
},
PositionPool: sync.Pool{
New: func() interface{} { return &position.Position{} },
},
}
}
@@ -533,18 +529,15 @@ func (l *Lexer) createToken(chars []lex.Char) *Token {
firstChar := chars[0]
lastChar := chars[len(chars)-1]
pos := l.PositionPool.Get().(*position.Position)
pos.StartLine = l.File.Line(firstChar.Pos())
pos.EndLine = l.File.Line(lastChar.Pos())
pos.StartPos = int(firstChar.Pos())
pos.EndPos = int(lastChar.Pos())
token := l.TokenPool.Get().(*Token)
token.Position = pos
token.Comments = l.Comments
token.Value = l.tokenString(chars)
token.StartLine = l.File.Line(firstChar.Pos())
token.EndLine = l.File.Line(lastChar.Pos())
token.StartPos = int(firstChar.Pos())
token.EndPos = int(lastChar.Pos())
return token
}

View File

@@ -2,14 +2,16 @@ package scanner
import (
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/position"
)
// Token value returned by lexer
type Token struct {
Value string
Position *position.Position
Comments []*comment.Comment
Value string
Comments []*comment.Comment
StartLine int
EndLine int
StartPos int
EndPos int
}
func (t *Token) String() string {

View File

@@ -4,18 +4,18 @@ import (
"reflect"
"testing"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/scanner"
)
func TestToken(t *testing.T) {
pos := position.NewPosition(1, 1, 0, 3)
tkn := &scanner.Token{
Value: `foo`,
Position: pos,
Value: `foo`,
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 3,
}
c := []*comment.Comment{
@@ -31,8 +31,4 @@ func TestToken(t *testing.T) {
if tkn.String() != `foo` {
t.Errorf("token value is not equal\n")
}
if tkn.Position != pos {
t.Errorf("token position is not equal\n")
}
}