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

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