move Positions and Comments into parser package

This commit is contained in:
z7zmey 2018-04-15 22:02:07 +03:00
parent 781a55659b
commit 56127a4a2c
14 changed files with 142 additions and 76 deletions

View File

@ -6,29 +6,9 @@ import (
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/node"
)
func TestComments(t *testing.T) {
n := node.NewIdentifier("test")
commentGroup := []*comment.Comment{
comment.NewComment("/** hello world */", nil),
comment.NewComment("// hello world", nil),
}
comments := comment.Comments{}
comments.AddComments(n, commentGroup)
if comments[n][0].String() != "/** hello world */" {
t.Errorf("expected and actual are not equal\n")
}
if comments[n][1].String() != "// hello world" {
t.Errorf("expected and actual are not equal\n")
}
}
func TestCommentPos(t *testing.T) {
func TestCommentGetPosition(t *testing.T) {
expected := position.NewPosition(0, 0, 0, 0)
comment := comment.NewComment("/** hello world */", expected)
@ -39,3 +19,15 @@ func TestCommentPos(t *testing.T) {
t.Errorf("expected and actual are not equal\n")
}
}
func TestCommentPrint(t *testing.T) {
expected := "/** hello world */"
comment := comment.NewComment(expected, nil)
actual := comment.String()
if expected != actual {
t.Errorf("expected and actual are not equal\n")
}
}

View File

@ -1,11 +0,0 @@
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...)
}

14
parser/comments.go Normal file
View File

@ -0,0 +1,14 @@
package parser
import (
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/node"
)
// Comments a collection of comment groups assigned to nodes
type Comments map[node.Node][]*comment.Comment
// AddComments add comment group to the collection
func (c Comments) AddComments(node node.Node, comments []*comment.Comment) {
c[node] = append(c[node], comments...)
}

28
parser/comments_test.go Normal file
View File

@ -0,0 +1,28 @@
package parser_test
import (
"testing"
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/parser"
)
func TestComments(t *testing.T) {
n := node.NewIdentifier("test")
commentGroup := []*comment.Comment{
comment.NewComment("/** hello world */", nil),
comment.NewComment("// hello world", nil),
}
comments := parser.Comments{}
comments.AddComments(n, commentGroup)
if comments[n][0].String() != "/** hello world */" {
t.Errorf("expected and actual are not equal\n")
}
if comments[n][1].String() != "// hello world" {
t.Errorf("expected and actual are not equal\n")
}
}

View File

@ -1,10 +1,8 @@
package parser
import (
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/errors"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
)
// Parser interface
@ -13,6 +11,6 @@ type Parser interface {
GetPath() string
GetRootNode() node.Node
GetErrors() []*errors.Error
GetComments() comment.Comments
GetPositions() position.Positions
GetComments() Comments
GetPositions() Positions
}

View File

@ -8,7 +8,7 @@ import (
// Builder provide functions to constuct positions
type Builder struct {
Positions *position.Positions
Positions *Positions
}
type startPos struct {

View File

@ -38,7 +38,7 @@ func TestNewTokensPosition(t *testing.T) {
func TestNewNodePosition(t *testing.T) {
n := node.NewIdentifier("test node")
p := &position.Positions{}
p := &parser.Positions{}
p.AddPosition(n, &position.Position{
StartLine: 1,
EndLine: 1,
@ -61,7 +61,7 @@ func TestNewTokenNodePosition(t *testing.T) {
tkn := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3)
n := node.NewIdentifier("test node")
p := &position.Positions{}
p := &parser.Positions{}
p.AddPosition(n, &position.Position{
StartLine: 2,
EndLine: 2,
@ -84,7 +84,7 @@ func TestNewNodeTokenPosition(t *testing.T) {
n := node.NewIdentifier("test node")
tkn := scanner.NewToken([]byte(`foo`), 2, 2, 10, 12)
p := &position.Positions{}
p := &parser.Positions{}
p.AddPosition(n, &position.Position{
StartLine: 1,
EndLine: 1,
@ -108,7 +108,7 @@ func TestNewNodeListPosition(t *testing.T) {
n2 := node.NewIdentifier("test node")
builder := parser.Builder{
Positions: &position.Positions{
Positions: &parser.Positions{
n1: &position.Position{
StartLine: 1,
EndLine: 1,
@ -136,7 +136,7 @@ func TestNewNodesPosition(t *testing.T) {
n2 := node.NewIdentifier("test node")
builder := parser.Builder{
Positions: &position.Positions{
Positions: &parser.Positions{
n1: &position.Position{
StartLine: 1,
EndLine: 1,
@ -165,7 +165,7 @@ func TestNewNodeListTokenPosition(t *testing.T) {
tkn := scanner.NewToken([]byte(`foo`), 3, 3, 20, 22)
builder := parser.Builder{
Positions: &position.Positions{
Positions: &parser.Positions{
n1: &position.Position{
StartLine: 1,
EndLine: 1,
@ -194,7 +194,7 @@ func TestNewTokenNodeListPosition(t *testing.T) {
n2 := node.NewIdentifier("test node")
builder := parser.Builder{
Positions: &position.Positions{
Positions: &parser.Positions{
n1: &position.Position{
StartLine: 2,
EndLine: 2,
@ -223,7 +223,7 @@ func TestNewNodeNodeListPosition(t *testing.T) {
n3 := node.NewIdentifier("test node")
builder := parser.Builder{
Positions: &position.Positions{
Positions: &parser.Positions{
n1: &position.Position{
StartLine: 1,
EndLine: 1,
@ -271,7 +271,7 @@ func TestNewOptionalListTokensPosition2(t *testing.T) {
n3 := node.NewIdentifier("test node")
builder := parser.Builder{
Positions: &position.Positions{
Positions: &parser.Positions{
n1: &position.Position{
StartLine: 1,
EndLine: 1,
@ -317,7 +317,7 @@ func TestNilNodeListPos(t *testing.T) {
n1 := node.NewIdentifier("test node")
builder := parser.Builder{
Positions: &position.Positions{
Positions: &parser.Positions{
n1: &position.Position{
StartLine: 1,
EndLine: 1,
@ -350,7 +350,7 @@ func TestEmptyNodeListPos(t *testing.T) {
n1 := node.NewIdentifier("test node")
builder := parser.Builder{
Positions: &position.Positions{
Positions: &parser.Positions{
n1: &position.Position{
StartLine: 1,
EndLine: 1,

14
parser/positions.go Normal file
View File

@ -0,0 +1,14 @@
package parser
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
)
// Positions a collection of positions attached to nodes
type Positions map[node.Node]*position.Position
// AddPosition attaches a position to a node
func (p Positions) AddPosition(node node.Node, position *position.Position) {
p[node] = position
}

25
parser/positions_test.go Normal file
View File

@ -0,0 +1,25 @@
package parser_test
import (
"testing"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/parser"
)
func TestPositions(t *testing.T) {
n := node.NewIdentifier("test")
expected := position.NewPosition(0, 0, 0, 0)
positions := parser.Positions{}
positions.AddPosition(n, expected)
actual := positions[n]
if actual != expected {
t.Errorf("expected and actual are not equal\n")
}
}

View File

@ -7,7 +7,6 @@ import (
"github.com/z7zmey/php-parser/errors"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/parser"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/scanner"
)
@ -23,8 +22,8 @@ type Parser struct {
positionBuilder *parser.Builder
errors []*errors.Error
rootNode node.Node
comments comment.Comments
positions position.Positions
comments parser.Comments
positions parser.Positions
}
// NewParser creates and returns new Parser
@ -62,8 +61,8 @@ func (l *Parser) Parse() int {
// init
l.errors = nil
l.rootNode = nil
l.comments = comment.Comments{}
l.positions = position.Positions{}
l.comments = parser.Comments{}
l.positions = parser.Positions{}
l.positionBuilder = &parser.Builder{
Positions: &l.positions,
}
@ -99,11 +98,11 @@ func (l *Parser) GetErrors() []*errors.Error {
}
// GetComments returns comments list
func (l *Parser) GetComments() comment.Comments {
func (l *Parser) GetComments() parser.Comments {
return l.comments
}
// GetPositions returns positions list
func (l *Parser) GetPositions() position.Positions {
func (l *Parser) GetPositions() parser.Positions {
return l.positions
}

View File

@ -7,7 +7,6 @@ import (
"github.com/z7zmey/php-parser/errors"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/parser"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/scanner"
)
@ -23,8 +22,8 @@ type Parser struct {
positionBuilder *parser.Builder
errors []*errors.Error
rootNode node.Node
comments comment.Comments
positions position.Positions
comments parser.Comments
positions parser.Positions
}
// NewParser creates and returns new Parser
@ -62,8 +61,8 @@ func (l *Parser) Parse() int {
// init
l.errors = nil
l.rootNode = nil
l.comments = comment.Comments{}
l.positions = position.Positions{}
l.comments = parser.Comments{}
l.positions = parser.Positions{}
l.positionBuilder = &parser.Builder{
Positions: &l.positions,
}
@ -99,11 +98,11 @@ func (l *Parser) GetErrors() []*errors.Error {
}
// GetComments returns comments list
func (l *Parser) GetComments() comment.Comments {
func (l *Parser) GetComments() parser.Comments {
return l.comments
}
// GetPositions returns positions list
func (l *Parser) GetPositions() position.Positions {
func (l *Parser) GetPositions() parser.Positions {
return l.positions
}

View File

@ -2,8 +2,6 @@ package position
import (
"fmt"
"github.com/z7zmey/php-parser/node"
)
// Position represents node position
@ -27,11 +25,3 @@ func NewPosition(StartLine int, EndLine int, StartPos int, EndPos int) *Position
func (p Position) String() string {
return fmt.Sprintf("Pos{Line: %d-%d Pos: %d-%d}", p.StartLine, p.EndLine, p.StartPos, p.EndPos)
}
// Positions a collection of positions attached to nodes
type Positions map[node.Node]*Position
// AddPosition attaches a position to a node
func (p Positions) AddPosition(node node.Node, position *Position) {
p[node] = position
}

19
position/position_test.go Normal file
View File

@ -0,0 +1,19 @@
package position_test
import (
"testing"
"github.com/z7zmey/php-parser/position"
)
func TestPrintPosition(t *testing.T) {
pos := position.NewPosition(1, 1, 2, 5)
expected := "Pos{Line: 1-1 Pos: 2-5}"
actual := pos.String()
if expected != actual {
t.Errorf("expected and actual are not equal\n")
}
}

View File

@ -7,9 +7,8 @@ import (
"reflect"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/parser"
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/walker"
)
@ -18,8 +17,8 @@ import (
type Dumper struct {
Writer io.Writer
Indent string
Comments comment.Comments
Positions position.Positions
Comments parser.Comments
Positions parser.Positions
NsResolver *NamespaceResolver
}