diff --git a/comment/comment_test.go b/comment/comment_test.go index c9389e2..72e1981 100644 --- a/comment/comment_test.go +++ b/comment/comment_test.go @@ -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") + } +} diff --git a/comment/comments.go b/comment/comments.go deleted file mode 100644 index 8ef687d..0000000 --- a/comment/comments.go +++ /dev/null @@ -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...) -} diff --git a/parser/comments.go b/parser/comments.go new file mode 100644 index 0000000..c5aac8b --- /dev/null +++ b/parser/comments.go @@ -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...) +} diff --git a/parser/comments_test.go b/parser/comments_test.go new file mode 100644 index 0000000..bdec9ef --- /dev/null +++ b/parser/comments_test.go @@ -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") + } +} diff --git a/parser/parser.go b/parser/parser.go index e9b4e93..24dc804 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -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 } diff --git a/parser/position_builder.go b/parser/position_builder.go index 9f9260e..178b1cb 100644 --- a/parser/position_builder.go +++ b/parser/position_builder.go @@ -8,7 +8,7 @@ import ( // Builder provide functions to constuct positions type Builder struct { - Positions *position.Positions + Positions *Positions } type startPos struct { diff --git a/parser/position_builder_test.go b/parser/position_builder_test.go index 6166967..4d3f5a3 100644 --- a/parser/position_builder_test.go +++ b/parser/position_builder_test.go @@ -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, diff --git a/parser/positions.go b/parser/positions.go new file mode 100644 index 0000000..6c2c0a9 --- /dev/null +++ b/parser/positions.go @@ -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 +} diff --git a/parser/positions_test.go b/parser/positions_test.go new file mode 100644 index 0000000..d836c5c --- /dev/null +++ b/parser/positions_test.go @@ -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") + } +} diff --git a/php5/parser.go b/php5/parser.go index fd8fce3..1285cd7 100644 --- a/php5/parser.go +++ b/php5/parser.go @@ -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 } diff --git a/php7/parser.go b/php7/parser.go index 4529c35..21f48f4 100644 --- a/php7/parser.go +++ b/php7/parser.go @@ -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 } diff --git a/position/position.go b/position/position.go index 0ca3c80..d4099cd 100644 --- a/position/position.go +++ b/position/position.go @@ -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 -} diff --git a/position/position_test.go b/position/position_test.go new file mode 100644 index 0000000..0e322b6 --- /dev/null +++ b/position/position_test.go @@ -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") + } +} diff --git a/visitor/dumper.go b/visitor/dumper.go index 50c0c20..6930f1b 100644 --- a/visitor/dumper.go +++ b/visitor/dumper.go @@ -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 }