move Positions
and Comments
into parser
package
This commit is contained in:
parent
781a55659b
commit
56127a4a2c
@ -6,29 +6,9 @@ import (
|
|||||||
"github.com/z7zmey/php-parser/position"
|
"github.com/z7zmey/php-parser/position"
|
||||||
|
|
||||||
"github.com/z7zmey/php-parser/comment"
|
"github.com/z7zmey/php-parser/comment"
|
||||||
"github.com/z7zmey/php-parser/node"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestComments(t *testing.T) {
|
func TestCommentGetPosition(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) {
|
|
||||||
expected := position.NewPosition(0, 0, 0, 0)
|
expected := position.NewPosition(0, 0, 0, 0)
|
||||||
|
|
||||||
comment := comment.NewComment("/** hello world */", expected)
|
comment := comment.NewComment("/** hello world */", expected)
|
||||||
@ -39,3 +19,15 @@ func TestCommentPos(t *testing.T) {
|
|||||||
t.Errorf("expected and actual are not equal\n")
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -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
14
parser/comments.go
Normal 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
28
parser/comments_test.go
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,8 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/z7zmey/php-parser/comment"
|
|
||||||
"github.com/z7zmey/php-parser/errors"
|
"github.com/z7zmey/php-parser/errors"
|
||||||
"github.com/z7zmey/php-parser/node"
|
"github.com/z7zmey/php-parser/node"
|
||||||
"github.com/z7zmey/php-parser/position"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parser interface
|
// Parser interface
|
||||||
@ -13,6 +11,6 @@ type Parser interface {
|
|||||||
GetPath() string
|
GetPath() string
|
||||||
GetRootNode() node.Node
|
GetRootNode() node.Node
|
||||||
GetErrors() []*errors.Error
|
GetErrors() []*errors.Error
|
||||||
GetComments() comment.Comments
|
GetComments() Comments
|
||||||
GetPositions() position.Positions
|
GetPositions() Positions
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
// Builder provide functions to constuct positions
|
// Builder provide functions to constuct positions
|
||||||
type Builder struct {
|
type Builder struct {
|
||||||
Positions *position.Positions
|
Positions *Positions
|
||||||
}
|
}
|
||||||
|
|
||||||
type startPos struct {
|
type startPos struct {
|
||||||
|
@ -38,7 +38,7 @@ func TestNewTokensPosition(t *testing.T) {
|
|||||||
func TestNewNodePosition(t *testing.T) {
|
func TestNewNodePosition(t *testing.T) {
|
||||||
n := node.NewIdentifier("test node")
|
n := node.NewIdentifier("test node")
|
||||||
|
|
||||||
p := &position.Positions{}
|
p := &parser.Positions{}
|
||||||
p.AddPosition(n, &position.Position{
|
p.AddPosition(n, &position.Position{
|
||||||
StartLine: 1,
|
StartLine: 1,
|
||||||
EndLine: 1,
|
EndLine: 1,
|
||||||
@ -61,7 +61,7 @@ func TestNewTokenNodePosition(t *testing.T) {
|
|||||||
tkn := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3)
|
tkn := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3)
|
||||||
n := node.NewIdentifier("test node")
|
n := node.NewIdentifier("test node")
|
||||||
|
|
||||||
p := &position.Positions{}
|
p := &parser.Positions{}
|
||||||
p.AddPosition(n, &position.Position{
|
p.AddPosition(n, &position.Position{
|
||||||
StartLine: 2,
|
StartLine: 2,
|
||||||
EndLine: 2,
|
EndLine: 2,
|
||||||
@ -84,7 +84,7 @@ func TestNewNodeTokenPosition(t *testing.T) {
|
|||||||
n := node.NewIdentifier("test node")
|
n := node.NewIdentifier("test node")
|
||||||
tkn := scanner.NewToken([]byte(`foo`), 2, 2, 10, 12)
|
tkn := scanner.NewToken([]byte(`foo`), 2, 2, 10, 12)
|
||||||
|
|
||||||
p := &position.Positions{}
|
p := &parser.Positions{}
|
||||||
p.AddPosition(n, &position.Position{
|
p.AddPosition(n, &position.Position{
|
||||||
StartLine: 1,
|
StartLine: 1,
|
||||||
EndLine: 1,
|
EndLine: 1,
|
||||||
@ -108,7 +108,7 @@ func TestNewNodeListPosition(t *testing.T) {
|
|||||||
n2 := node.NewIdentifier("test node")
|
n2 := node.NewIdentifier("test node")
|
||||||
|
|
||||||
builder := parser.Builder{
|
builder := parser.Builder{
|
||||||
Positions: &position.Positions{
|
Positions: &parser.Positions{
|
||||||
n1: &position.Position{
|
n1: &position.Position{
|
||||||
StartLine: 1,
|
StartLine: 1,
|
||||||
EndLine: 1,
|
EndLine: 1,
|
||||||
@ -136,7 +136,7 @@ func TestNewNodesPosition(t *testing.T) {
|
|||||||
n2 := node.NewIdentifier("test node")
|
n2 := node.NewIdentifier("test node")
|
||||||
|
|
||||||
builder := parser.Builder{
|
builder := parser.Builder{
|
||||||
Positions: &position.Positions{
|
Positions: &parser.Positions{
|
||||||
n1: &position.Position{
|
n1: &position.Position{
|
||||||
StartLine: 1,
|
StartLine: 1,
|
||||||
EndLine: 1,
|
EndLine: 1,
|
||||||
@ -165,7 +165,7 @@ func TestNewNodeListTokenPosition(t *testing.T) {
|
|||||||
tkn := scanner.NewToken([]byte(`foo`), 3, 3, 20, 22)
|
tkn := scanner.NewToken([]byte(`foo`), 3, 3, 20, 22)
|
||||||
|
|
||||||
builder := parser.Builder{
|
builder := parser.Builder{
|
||||||
Positions: &position.Positions{
|
Positions: &parser.Positions{
|
||||||
n1: &position.Position{
|
n1: &position.Position{
|
||||||
StartLine: 1,
|
StartLine: 1,
|
||||||
EndLine: 1,
|
EndLine: 1,
|
||||||
@ -194,7 +194,7 @@ func TestNewTokenNodeListPosition(t *testing.T) {
|
|||||||
n2 := node.NewIdentifier("test node")
|
n2 := node.NewIdentifier("test node")
|
||||||
|
|
||||||
builder := parser.Builder{
|
builder := parser.Builder{
|
||||||
Positions: &position.Positions{
|
Positions: &parser.Positions{
|
||||||
n1: &position.Position{
|
n1: &position.Position{
|
||||||
StartLine: 2,
|
StartLine: 2,
|
||||||
EndLine: 2,
|
EndLine: 2,
|
||||||
@ -223,7 +223,7 @@ func TestNewNodeNodeListPosition(t *testing.T) {
|
|||||||
n3 := node.NewIdentifier("test node")
|
n3 := node.NewIdentifier("test node")
|
||||||
|
|
||||||
builder := parser.Builder{
|
builder := parser.Builder{
|
||||||
Positions: &position.Positions{
|
Positions: &parser.Positions{
|
||||||
n1: &position.Position{
|
n1: &position.Position{
|
||||||
StartLine: 1,
|
StartLine: 1,
|
||||||
EndLine: 1,
|
EndLine: 1,
|
||||||
@ -271,7 +271,7 @@ func TestNewOptionalListTokensPosition2(t *testing.T) {
|
|||||||
n3 := node.NewIdentifier("test node")
|
n3 := node.NewIdentifier("test node")
|
||||||
|
|
||||||
builder := parser.Builder{
|
builder := parser.Builder{
|
||||||
Positions: &position.Positions{
|
Positions: &parser.Positions{
|
||||||
n1: &position.Position{
|
n1: &position.Position{
|
||||||
StartLine: 1,
|
StartLine: 1,
|
||||||
EndLine: 1,
|
EndLine: 1,
|
||||||
@ -317,7 +317,7 @@ func TestNilNodeListPos(t *testing.T) {
|
|||||||
n1 := node.NewIdentifier("test node")
|
n1 := node.NewIdentifier("test node")
|
||||||
|
|
||||||
builder := parser.Builder{
|
builder := parser.Builder{
|
||||||
Positions: &position.Positions{
|
Positions: &parser.Positions{
|
||||||
n1: &position.Position{
|
n1: &position.Position{
|
||||||
StartLine: 1,
|
StartLine: 1,
|
||||||
EndLine: 1,
|
EndLine: 1,
|
||||||
@ -350,7 +350,7 @@ func TestEmptyNodeListPos(t *testing.T) {
|
|||||||
n1 := node.NewIdentifier("test node")
|
n1 := node.NewIdentifier("test node")
|
||||||
|
|
||||||
builder := parser.Builder{
|
builder := parser.Builder{
|
||||||
Positions: &position.Positions{
|
Positions: &parser.Positions{
|
||||||
n1: &position.Position{
|
n1: &position.Position{
|
||||||
StartLine: 1,
|
StartLine: 1,
|
||||||
EndLine: 1,
|
EndLine: 1,
|
||||||
|
14
parser/positions.go
Normal file
14
parser/positions.go
Normal 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
25
parser/positions_test.go
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/z7zmey/php-parser/errors"
|
"github.com/z7zmey/php-parser/errors"
|
||||||
"github.com/z7zmey/php-parser/node"
|
"github.com/z7zmey/php-parser/node"
|
||||||
"github.com/z7zmey/php-parser/parser"
|
"github.com/z7zmey/php-parser/parser"
|
||||||
"github.com/z7zmey/php-parser/position"
|
|
||||||
"github.com/z7zmey/php-parser/scanner"
|
"github.com/z7zmey/php-parser/scanner"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,8 +22,8 @@ type Parser struct {
|
|||||||
positionBuilder *parser.Builder
|
positionBuilder *parser.Builder
|
||||||
errors []*errors.Error
|
errors []*errors.Error
|
||||||
rootNode node.Node
|
rootNode node.Node
|
||||||
comments comment.Comments
|
comments parser.Comments
|
||||||
positions position.Positions
|
positions parser.Positions
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewParser creates and returns new Parser
|
// NewParser creates and returns new Parser
|
||||||
@ -62,8 +61,8 @@ func (l *Parser) Parse() int {
|
|||||||
// init
|
// init
|
||||||
l.errors = nil
|
l.errors = nil
|
||||||
l.rootNode = nil
|
l.rootNode = nil
|
||||||
l.comments = comment.Comments{}
|
l.comments = parser.Comments{}
|
||||||
l.positions = position.Positions{}
|
l.positions = parser.Positions{}
|
||||||
l.positionBuilder = &parser.Builder{
|
l.positionBuilder = &parser.Builder{
|
||||||
Positions: &l.positions,
|
Positions: &l.positions,
|
||||||
}
|
}
|
||||||
@ -99,11 +98,11 @@ func (l *Parser) GetErrors() []*errors.Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetComments returns comments list
|
// GetComments returns comments list
|
||||||
func (l *Parser) GetComments() comment.Comments {
|
func (l *Parser) GetComments() parser.Comments {
|
||||||
return l.comments
|
return l.comments
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPositions returns positions list
|
// GetPositions returns positions list
|
||||||
func (l *Parser) GetPositions() position.Positions {
|
func (l *Parser) GetPositions() parser.Positions {
|
||||||
return l.positions
|
return l.positions
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/z7zmey/php-parser/errors"
|
"github.com/z7zmey/php-parser/errors"
|
||||||
"github.com/z7zmey/php-parser/node"
|
"github.com/z7zmey/php-parser/node"
|
||||||
"github.com/z7zmey/php-parser/parser"
|
"github.com/z7zmey/php-parser/parser"
|
||||||
"github.com/z7zmey/php-parser/position"
|
|
||||||
"github.com/z7zmey/php-parser/scanner"
|
"github.com/z7zmey/php-parser/scanner"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,8 +22,8 @@ type Parser struct {
|
|||||||
positionBuilder *parser.Builder
|
positionBuilder *parser.Builder
|
||||||
errors []*errors.Error
|
errors []*errors.Error
|
||||||
rootNode node.Node
|
rootNode node.Node
|
||||||
comments comment.Comments
|
comments parser.Comments
|
||||||
positions position.Positions
|
positions parser.Positions
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewParser creates and returns new Parser
|
// NewParser creates and returns new Parser
|
||||||
@ -62,8 +61,8 @@ func (l *Parser) Parse() int {
|
|||||||
// init
|
// init
|
||||||
l.errors = nil
|
l.errors = nil
|
||||||
l.rootNode = nil
|
l.rootNode = nil
|
||||||
l.comments = comment.Comments{}
|
l.comments = parser.Comments{}
|
||||||
l.positions = position.Positions{}
|
l.positions = parser.Positions{}
|
||||||
l.positionBuilder = &parser.Builder{
|
l.positionBuilder = &parser.Builder{
|
||||||
Positions: &l.positions,
|
Positions: &l.positions,
|
||||||
}
|
}
|
||||||
@ -99,11 +98,11 @@ func (l *Parser) GetErrors() []*errors.Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetComments returns comments list
|
// GetComments returns comments list
|
||||||
func (l *Parser) GetComments() comment.Comments {
|
func (l *Parser) GetComments() parser.Comments {
|
||||||
return l.comments
|
return l.comments
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPositions returns positions list
|
// GetPositions returns positions list
|
||||||
func (l *Parser) GetPositions() position.Positions {
|
func (l *Parser) GetPositions() parser.Positions {
|
||||||
return l.positions
|
return l.positions
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,6 @@ package position
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/z7zmey/php-parser/node"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Position represents node position
|
// Position represents node position
|
||||||
@ -27,11 +25,3 @@ func NewPosition(StartLine int, EndLine int, StartPos int, EndPos int) *Position
|
|||||||
func (p Position) String() string {
|
func (p Position) String() string {
|
||||||
return fmt.Sprintf("Pos{Line: %d-%d Pos: %d-%d}", p.StartLine, p.EndLine, p.StartPos, p.EndPos)
|
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
19
position/position_test.go
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
@ -7,9 +7,8 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/z7zmey/php-parser/node"
|
"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"
|
"github.com/z7zmey/php-parser/walker"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,8 +17,8 @@ import (
|
|||||||
type Dumper struct {
|
type Dumper struct {
|
||||||
Writer io.Writer
|
Writer io.Writer
|
||||||
Indent string
|
Indent string
|
||||||
Comments comment.Comments
|
Comments parser.Comments
|
||||||
Positions position.Positions
|
Positions parser.Positions
|
||||||
NsResolver *NamespaceResolver
|
NsResolver *NamespaceResolver
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user