github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/ast/non_null_attr.go (about) 1 package ast 2 3 import ( 4 "strings" 5 6 "github.com/Konstantin8105/c4go/util" 7 ) 8 9 // NonNullAttr is a type of attribute that is optionally attached to a variable 10 // or struct field definition. 11 type NonNullAttr struct { 12 Addr Address 13 Pos Position 14 IsInherited bool 15 A int 16 B int 17 C int 18 D int 19 ChildNodes []Node 20 } 21 22 func parseNonNullAttr(line string) *NonNullAttr { 23 groups := groupsFromRegex( 24 `<(?P<position>.*)> 25 (?P<inherited> Inherited)? 26 (?P<a> \d+)(?P<b> \d+)?(?P<c> \d+)?(?P<d> \d+)?`, 27 line, 28 ) 29 30 a := 0 31 if groups["a"] != "" { 32 a = util.Atoi(strings.TrimSpace(groups["a"])) 33 } 34 35 b := 0 36 if groups["b"] != "" { 37 b = util.Atoi(strings.TrimSpace(groups["b"])) 38 } 39 40 c := 0 41 if groups["c"] != "" { 42 c = util.Atoi(strings.TrimSpace(groups["c"])) 43 } 44 45 d := 0 46 if groups["d"] != "" { 47 d = util.Atoi(strings.TrimSpace(groups["d"])) 48 } 49 50 return &NonNullAttr{ 51 Addr: ParseAddress(groups["address"]), 52 Pos: NewPositionFromString(groups["position"]), 53 IsInherited: len(groups["inherited"]) > 0, 54 A: a, 55 B: b, 56 C: c, 57 D: d, 58 ChildNodes: []Node{}, 59 } 60 } 61 62 // AddChild adds a new child node. Child nodes can then be accessed with the 63 // Children attribute. 64 func (n *NonNullAttr) AddChild(node Node) { 65 n.ChildNodes = append(n.ChildNodes, node) 66 } 67 68 // Address returns the numeric address of the node. See the documentation for 69 // the Address type for more information. 70 func (n *NonNullAttr) Address() Address { 71 return n.Addr 72 } 73 74 // Children returns the child nodes. If this node does not have any children or 75 // this node does not support children it will always return an empty slice. 76 func (n *NonNullAttr) Children() []Node { 77 return n.ChildNodes 78 } 79 80 // Position returns the position in the original source code. 81 func (n *NonNullAttr) Position() Position { 82 return n.Pos 83 }