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

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")
}
}