-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathparse.h
153 lines (139 loc) · 3.44 KB
/
parse.h
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright 2002-2004 John Plevyak, All Rights Reserved
*/
#ifndef _parse_H_
#define _parse_H_
#define NO_DPN ((D_ParseNode *)0x1)
#define DPN_TO_PN(_dpn) ((PNode *)(_dpn == NULL ? NULL : (((char *)_dpn) - ((long)sizeof(PNode) - (long)sizeof(D_ParseNode)))))
#define is_epsilon_PNode(_pn) ((_pn)->parse_node.start_loc.s == (_pn)->parse_node.end)
/* #define TRACK_PNODES 1 */
struct PNode;
struct SNode;
struct ZNode;
struct Parser;
typedef Vec(struct ZNode *) VecZNode;
typedef Vec(VecZNode *) VecVecZNode;
typedef Vec(struct SNode *) VecSNode;
typedef Vec(struct PNode *) VecPNode;
typedef struct PNodeHash {
struct PNode **v;
uint i; /* size index (power of 2) */
uint m; /* max size (highest prime < i ** 2) */
uint n; /* size */
struct PNode *all;
} PNodeHash;
typedef struct SNodeHash {
struct SNode **v;
uint i; /* size index (power of 2) */
uint m; /* max size (highest prime < i ** 2) */
uint n; /* size */
struct SNode *all;
struct SNode *last_all;
} SNodeHash;
typedef struct Reduction {
struct ZNode *znode;
struct SNode *snode;
struct D_Reduction *reduction;
struct SNode *new_snode;
int new_depth;
struct Reduction *next;
} Reduction;
typedef struct Shift {
struct SNode *snode;
struct Shift *next;
} Shift;
typedef struct Parser {
D_Parser user;
/* string to parse */
char *start, *end;
struct D_ParserTables *t;
/* statistics */
int states, pnodes, scans, shifts, reductions, compares, ambiguities;
/* parser state */
PNodeHash pnode_hash;
SNodeHash snode_hash;
Reduction *reductions_todo;
Shift *shifts_todo;
D_Scope *top_scope;
struct SNode *accept;
int last_syntax_error_line;
/* memory management */
Reduction *free_reductions;
Shift *free_shifts;
int live_pnodes;
struct PNode *free_pnodes;
struct SNode *free_snodes;
struct ZNode *free_znodes;
Vec(D_Reduction *) error_reductions;
ShiftResult *shift_results;
int nshift_results;
D_Shift *code_shifts;
int ncode_shifts;
/* comments */
struct Parser *whitespace_parser;
/* interface support */
void *pinterface1;
#ifdef TRACK_PNODES
struct PNode *xall;
#endif
} Parser;
/*
Parse Node - the 'symbol' and the constructed parse subtrees.
*/
typedef struct PNode {
uint hash;
AssocKind assoc;
int priority;
AssocKind op_assoc;
int op_priority;
#ifndef USE_GC
uint32 refcount;
#endif
uint height; /* max tree height */
uint8 evaluated;
uint8 error_recovery;
D_Reduction *reduction;
D_Shift *shift;
VecPNode children;
struct PNode *all_next;
struct PNode *bucket_next;
struct PNode *ambiguities;
struct PNode *latest; /* latest version of this PNode */
char *ws_before;
char *ws_after;
D_Scope *initial_scope;
void *initial_globals;
#ifdef TRACK_PNODES
struct PNode *xnext;
struct PNode *xprev;
#endif
D_ParseNode parse_node; /* public fields */
} PNode;
/*
State Node - the 'state'.
*/
typedef struct SNode {
d_loc_t loc;
#ifndef USE_GC
uint32 refcount;
#endif
uint32 depth : 31; /* max stack depth (less loops) */
uint32 in_error_recovery_queue : 1;
D_State *state;
D_Scope *initial_scope;
void *initial_globals;
PNode *last_pn;
VecZNode zns;
struct SNode *bucket_next;
struct SNode *all_next;
} SNode;
/*
(Z)Symbol Node - holds one of the symbols associated with a state.
*/
typedef struct ZNode {
PNode *pn;
VecSNode sns;
} ZNode;
#define znode_next(_z) (*(ZNode **)&((_z)->pn))
D_ParseNode *ambiguity_count_fn(D_Parser *pp, int n, D_ParseNode **v);
#endif