github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/ast/function_decl.go (about)

     1  package ast
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  // FunctionDecl is node represents a function declaration.
     8  type FunctionDecl struct {
     9  	Addr         Address
    10  	Pos          Position
    11  	Prev         string
    12  	Parent       string
    13  	Position2    string
    14  	Name         string
    15  	Type         string
    16  	Type2        string
    17  	IsExtern     bool
    18  	IsImplicit   bool
    19  	IsUsed       bool
    20  	IsReferenced bool
    21  	IsStatic     bool
    22  	IsInline     bool
    23  	ChildNodes   []Node
    24  }
    25  
    26  func parseFunctionDecl(line string) *FunctionDecl {
    27  	groups := groupsFromRegex(
    28  		`(?:parent (?P<parent>0x[0-9a-f]+) )?
    29  		(?:prev (?P<prev>0x[0-9a-f]+) )?
    30  		<(?P<position1>.*?)>
    31  		(?P<position2> <scratch space>[^ ]+| [^ ]+)?
    32  		(?P<implicit> implicit)?
    33  		(?P<used> used)?
    34  		(?P<referenced> referenced)?
    35  		 (?P<name>[_\w]+)
    36  		 '(?P<type>.*?)'
    37  		(:'(?P<type2>.*?)')?
    38  		(?P<extern> extern)?
    39  		(?P<static> static)?
    40  		(?P<inline> inline)?
    41  		`,
    42  		line,
    43  	)
    44  
    45  	return &FunctionDecl{
    46  		Addr:         ParseAddress(groups["address"]),
    47  		Pos:          NewPositionFromString(groups["position1"]),
    48  		Parent:       groups["parent"],
    49  		Prev:         groups["prev"],
    50  		Position2:    strings.TrimSpace(groups["position2"]),
    51  		Name:         groups["name"],
    52  		Type:         groups["type"],
    53  		Type2:        groups["type2"],
    54  		IsExtern:     len(groups["extern"]) > 0,
    55  		IsImplicit:   len(groups["implicit"]) > 0,
    56  		IsUsed:       len(groups["used"]) > 0,
    57  		IsReferenced: len(groups["referenced"]) > 0,
    58  		IsStatic:     len(groups["static"]) > 0,
    59  		IsInline:     len(groups["inline"]) > 0,
    60  		ChildNodes:   []Node{},
    61  	}
    62  }
    63  
    64  // AddChild adds a new child node. Child nodes can then be accessed with the
    65  // Children attribute.
    66  func (n *FunctionDecl) AddChild(node Node) {
    67  	n.ChildNodes = append(n.ChildNodes, node)
    68  }
    69  
    70  // Address returns the numeric address of the node. See the documentation for
    71  // the Address type for more information.
    72  func (n *FunctionDecl) Address() Address {
    73  	return n.Addr
    74  }
    75  
    76  // Children returns the child nodes. If this node does not have any children or
    77  // this node does not support children it will always return an empty slice.
    78  func (n *FunctionDecl) Children() []Node {
    79  	return n.ChildNodes
    80  }
    81  
    82  // Position returns the position in the original source code.
    83  func (n *FunctionDecl) Position() Position {
    84  	return n.Pos
    85  }