src/ast.c

filter: Statementstmt 83.0% 54 / 65 Branchbran 64.7% 44 / 68 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.6% 98 / 133 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/error.c ↑ Summary src/macros.c »
1 /* ------------------------------------------------------------------------
2 @NAME : ast.c
3 @DESCRIPTION: Creating, freeing and traversing the AST for a single entry.
4 -------------------------------------------------------------------------- */
5  
6 #include <stdlib.h>
7 #include "bt_internal.h"
8  
9 AST *
10 1736 bt_new_ast (bt_nodetype nodetype, const char *text, size_t len,
11 const char *filename, int line, long offset)
12 {
13 1736 AST * node = bt_xcalloc (1, sizeof (AST));
14  
15 1736 node->nodetype = nodetype;
16 1736 node->metatype = BTE_UNKNOWN;
17 1736 ▶ node->text = text ? bt_xstrndup (text, len) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
node->text = text ? bt_xstrndup (text, len) : NULL; 1736 0 1736
18 1736 node->filename = filename;
19 1736 node->line = line;
20 1736 node->offset = offset;
21 1736 return node;
22 }
23  
24 void
25 1940 bt_free_ast (AST *ast)
26 {
27 /* iterate along siblings, recurse into children */
28 3676 ▶ while (ast)
Branch: 1 branch
BranchTrueFalseTotal
while (ast) 1736 1940 3676
29 {
30 1736 AST * next = ast->right;
31 1736 bt_free_ast (ast->down);
32 1736 free (ast->text);
33 1736 free (ast);
34 1736 ast = next;
35 }
36 1940 }
37  
38  
39 AST *
40 0 bt_next_entry (AST *entry_list, AST *prev_entry)
41 {
42 0 ▶ if (entry_list == NULL || entry_list->nodetype != BTAST_ENTRY)
Branch: 2 branches
BranchTrueFalseTotal
if (entry_list == NULL || entry_list->nodetype != BTAST_ENTRY) 0 0 0
if (entry_list == NULL || entry_list->nodetype != BTAST_ENTRY) 0 0 0
43 0 return NULL;
44 0 ▶ if (prev_entry)
Branch: 1 branch
BranchTrueFalseTotal
if (prev_entry) 0 0 0
45 0 ▶ return prev_entry->nodetype == BTAST_ENTRY ? prev_entry->right : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return prev_entry->nodetype == BTAST_ENTRY ? prev_entry->right : NULL; 0 0 0
46 0 return entry_list;
47 }
48  
49 bt_metatype
50 167 bt_entry_metatype (AST *entry)
51 {
52 167 ▶ if (!entry || entry->nodetype != BTAST_ENTRY)
Branch: 2 branches
BranchTrueFalseTotal
if (!entry || entry->nodetype != BTAST_ENTRY) 167 0 167
if (!entry || entry->nodetype != BTAST_ENTRY) 0 167 167
53 0 return BTE_UNKNOWN;
54 167 return entry->metatype;
55 }
56  
57 char *
58 167 bt_entry_type (AST *entry)
59 {
60 167 ▶ if (!entry || entry->nodetype != BTAST_ENTRY)
Branch: 2 branches
BranchTrueFalseTotal
if (!entry || entry->nodetype != BTAST_ENTRY) 167 0 167
if (!entry || entry->nodetype != BTAST_ENTRY) 0 167 167
61 0 return NULL;
62 167 return entry->text;
63 }
64  
65 char *
66 167 bt_entry_key (AST *entry)
67 {
68 167 ▶ if (entry && entry->metatype == BTE_REGULAR &&
Branch: 2 branches
BranchTrueFalseTotal
if (entry && entry->metatype == BTE_REGULAR && 167 0 167
if (entry && entry->metatype == BTE_REGULAR && 142 25 167
69 142 ▶ entry->down && entry->down->nodetype == BTAST_KEY)
Branch: 2 branches
BranchTrueFalseTotal
entry->down && entry->down->nodetype == BTAST_KEY) 142 0 142
entry->down && entry->down->nodetype == BTAST_KEY) 142 0 142
70 142 return entry->down->text;
71 25 return NULL;
72 }
73  
74 AST *
75 849 bt_next_field (AST *entry, AST *prev, char **name)
76 {
77 AST * field;
78  
79 849 ▶ if (name) *name = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (name) *name = NULL; 849 0 849
80 849 ▶ if (!entry || !entry->down)
Branch: 2 branches
BranchTrueFalseTotal
if (!entry || !entry->down) 849 0 849
if (!entry || !entry->down) 0 849 849
81 0 return NULL;
82 849 ▶ if (entry->metatype != BTE_MACRODEF && entry->metatype != BTE_REGULAR)
Branch: 2 branches
BranchTrueFalseTotal
if (entry->metatype != BTE_MACRODEF && entry->metatype != BTE_REGULAR) 810 39 849
if (entry->metatype != BTE_MACRODEF && entry->metatype != BTE_REGULAR) 0 810 810
83 0 return NULL;
84  
85 849 ▶ if (prev == NULL) /* they want the first one */
Branch: 1 branch
BranchTrueFalseTotal
if (prev == NULL) /* they want the first one */ 160 689 849
86 {
87 160 field = entry->down;
88 160 ▶ if (field->nodetype == BTAST_KEY) /* skip over citation key */
Branch: 1 branch
BranchTrueFalseTotal
if (field->nodetype == BTAST_KEY) /* skip over citation key */ 142 18 160
89 142 field = field->right;
90 }
91 else
92 689 field = prev->right;
93  
94 849 ▶ if (!field)
Branch: 1 branch
BranchTrueFalseTotal
if (!field) 160 689 849
95 160 return NULL;
96 689 ▶ if (name) *name = field->text;
Branch: 1 branch
BranchTrueFalseTotal
if (name) *name = field->text; 689 0 689
97 689 return field;
98 }
99  
100 /* Walk the simple values of a field, or of a comment/preamble entry */
101 AST *
102 711 bt_next_value (AST *top, AST *prev, bt_nodetype *nodetype, char **text)
103 {
104 711 AST * value = NULL;
105  
106 711 ▶ if (nodetype) *nodetype = BTAST_BOGUS;
Branch: 1 branch
BranchTrueFalseTotal
if (nodetype) *nodetype = BTAST_BOGUS; 698 13 711
107 711 ▶ if (text) *text = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (text) *text = NULL; 698 13 711
108 711 ▶ if (!top)
Branch: 1 branch
BranchTrueFalseTotal
if (!top) 0 711 711
109 0 return NULL;
110  
111 711 ▶ if (top->nodetype == BTAST_FIELD ||
Branch: 1 branch
BranchTrueFalseTotal
if (top->nodetype == BTAST_FIELD || 22 689 711
112 22 ▶ (top->nodetype == BTAST_ENTRY &&
Branch: 1 branch
BranchTrueFalseTotal
(top->nodetype == BTAST_ENTRY && 22 0 22
113 22 ▶ (top->metatype == BTE_COMMENT || top->metatype == BTE_PREAMBLE)))
Branch: 2 branches
BranchTrueFalseTotal
(top->metatype == BTE_COMMENT || top->metatype == BTE_PREAMBLE))) 9 13 22
(top->metatype == BTE_COMMENT || top->metatype == BTE_PREAMBLE))) 9 0 9
114 711 ▶ value = prev ? prev->right : top->down;
Branch: 1 branch
BranchTrueFalseTotal
value = prev ? prev->right : top->down; 8 703 711
115  
116 711 ▶ if (value)
Branch: 1 branch
BranchTrueFalseTotal
if (value) 691 20 711
117 {
118 691 ▶ if (nodetype) *nodetype = value->nodetype;
Branch: 1 branch
BranchTrueFalseTotal
if (nodetype) *nodetype = value->nodetype; 685 6 691
119 691 ▶ if (text) *text = value->text;
Branch: 1 branch
BranchTrueFalseTotal
if (text) *text = value->text; 685 6 691
120 }
121 711 return value;
122 }
« src/error.c ↑ Summary src/macros.c »