|
| 1 | +package visitor |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/rectorphp/php-parser-in-go/pkg/ast" |
| 5 | + "github.com/rectorphp/php-parser-in-go/pkg/token" |
| 6 | +) |
| 7 | + |
| 8 | +// GetDocComment returns the /** */ doc comment immediately preceding a |
| 9 | +// declaration node, or nil when there is none. It is supported for the nodes |
| 10 | +// that carry a doc block: class, interface, trait and enum declarations, |
| 11 | +// functions and methods, and property and class-constant lists. For any other |
| 12 | +// node it returns nil. |
| 13 | +// |
| 14 | +// The comment is read from the free-floating tokens that precede the node's |
| 15 | +// first significant token (an attribute group, a modifier, or the declaration |
| 16 | +// keyword). When several doc comments precede the node, the closest one is |
| 17 | +// returned. |
| 18 | +func GetDocComment(node ast.Vertex) *token.Token { |
| 19 | + return lastDocComment(leadingTokens(node)) |
| 20 | +} |
| 21 | + |
| 22 | +// GetDocCommentText is GetDocComment returning the comment text, or "" when |
| 23 | +// there is no doc comment. |
| 24 | +func GetDocCommentText(node ast.Vertex) string { |
| 25 | + if doc := GetDocComment(node); doc != nil { |
| 26 | + return string(doc.Value) |
| 27 | + } |
| 28 | + return "" |
| 29 | +} |
| 30 | + |
| 31 | +func leadingTokens(node ast.Vertex) []*token.Token { |
| 32 | + switch n := node.(type) { |
| 33 | + case *ast.StmtClass: |
| 34 | + return collectLeading(n.AttrGroups, n.Modifiers, n.ClassTkn) |
| 35 | + case *ast.StmtInterface: |
| 36 | + return collectLeading(n.AttrGroups, nil, n.InterfaceTkn) |
| 37 | + case *ast.StmtTrait: |
| 38 | + return collectLeading(n.AttrGroups, nil, n.TraitTkn) |
| 39 | + case *ast.StmtEnum: |
| 40 | + return collectLeading(n.AttrGroups, nil, n.EnumTkn) |
| 41 | + case *ast.StmtFunction: |
| 42 | + return collectLeading(n.AttrGroups, nil, n.FunctionTkn) |
| 43 | + case *ast.StmtClassMethod: |
| 44 | + return collectLeading(n.AttrGroups, n.Modifiers, n.FunctionTkn) |
| 45 | + case *ast.StmtPropertyList: |
| 46 | + return collectLeading(n.AttrGroups, n.Modifiers, nil) |
| 47 | + case *ast.StmtClassConstList: |
| 48 | + return collectLeading(n.AttrGroups, n.Modifiers, n.ConstTkn) |
| 49 | + default: |
| 50 | + return nil |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func collectLeading(attrGroups, modifiers []ast.Vertex, keyword *token.Token) []*token.Token { |
| 55 | + var tokens []*token.Token |
| 56 | + for _, group := range attrGroups { |
| 57 | + if attributeGroup, ok := group.(*ast.AttributeGroup); ok { |
| 58 | + tokens = append(tokens, attributeGroup.OpenAttributeTkn) |
| 59 | + } |
| 60 | + } |
| 61 | + for _, modifier := range modifiers { |
| 62 | + if identifier, ok := modifier.(*ast.Identifier); ok { |
| 63 | + tokens = append(tokens, identifier.IdentifierTkn) |
| 64 | + } |
| 65 | + } |
| 66 | + if keyword != nil { |
| 67 | + tokens = append(tokens, keyword) |
| 68 | + } |
| 69 | + return tokens |
| 70 | +} |
| 71 | + |
| 72 | +func lastDocComment(tokens []*token.Token) *token.Token { |
| 73 | + var found *token.Token |
| 74 | + for _, tkn := range tokens { |
| 75 | + if tkn == nil { |
| 76 | + continue |
| 77 | + } |
| 78 | + for _, free := range tkn.FreeFloating { |
| 79 | + if free.ID == token.T_DOC_COMMENT { |
| 80 | + found = free |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + return found |
| 85 | +} |
0 commit comments