-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdparse_tree.c
More file actions
71 lines (59 loc) · 1.93 KB
/
dparse_tree.c
File metadata and controls
71 lines (59 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
$URL: http://manta.univ.gda.pl/svn/wb/parsing/dparser/D/dparse_tree.c $
$Revision: 1.2 $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dparse_tree.h"
char *dup_str(char *s, char *e); /* defined in util.h */
/* tunables */
#define MAX_LINE_LENGTH 44 /* must be at least 4 */
#define INDENT_SPACES 4
static void xprint_parsetree(D_ParserTables pt, D_ParseNode *pn, int depth, print_node_fn_t fn, void *client_data);
static void xprint_parsetree(D_ParserTables pt, D_ParseNode *pn, int depth, print_node_fn_t fn, void *client_data) {
int nch = d_get_number_of_children(pn), i;
char *name = (char *)pt.symbols[pn->symbol].name;
/*
int len = pn->end_skip - pn->start_loc.s;
char *value = malloc(len+2);
memcpy(value, pn->start_loc.s, len);
value[len] = 0;
*/
char *value = dup_str(pn->start_loc.s, pn->end);
fn(depth, name, value, client_data);
free(value);
depth++;
if (nch != 0) {
for (i = 0; i < nch; i++) {
D_ParseNode *xpn = d_get_child(pn, i);
xprint_parsetree(pt, xpn, depth, fn, client_data);
}
}
}
void print_parsetree(D_ParserTables pt, D_ParseNode *pn, print_node_fn_t fn, void *client_data) {
xprint_parsetree(pt, pn, 0, (NULL == fn) ? print_node_default : fn, client_data);
}
void print_node_parenthesised(int depth, char *name, char *value, void *client_data) {
(void)depth;
(void)value;
(void)client_data;
printf("( %s )", name);
}
static char *change_newline2space(char *s) {
char *ss = s;
while (*ss++)
if (*ss == '\n') *ss = ' ';
if (strlen(s) > MAX_LINE_LENGTH) {
*(s + MAX_LINE_LENGTH - 3) = '.';
*(s + MAX_LINE_LENGTH - 2) = '.';
*(s + MAX_LINE_LENGTH - 1) = '.';
*(s + MAX_LINE_LENGTH) = '\0';
}
return s;
}
void print_node_default(int depth, char *name, char *value, void *client_data) {
(void)client_data;
printf("%*s", depth * INDENT_SPACES, "");
printf("%s %s.\n", name, change_newline2space(value));
}