Boron 2.1.0
i_parse_blk.c
1/*
2 Bytecode interpreter for parsing blocks
3 Copyright 2016, 2019 Karl Robillard
4*/
5
6
7#include "i_parse_blk.h"
8
9
10static inline int bitIsSet64( const uint8_t* mem, uint32_t n )
11{
12 return (n > 63) ? 0 : mem[n>>3] & 1<<(n&7);
13}
14
15/*
16 \param pc Parse rule.
17 \param it Current input position.
18
19 \returns non-zero if end of rule reached.
20*/
21int ur_parseBlockI( UBlockParser* par, const uint8_t* pc, const UCell* it )
22{
23 const uint8_t* bset;
24 const uint8_t* next = 0;
25 const UCell* start = it;
26 const UCell* sit;
27 int n, t;
28
29 // TODO: Handle repeating of any operation (e.g. 4 skip, 2 to 3 of rule).
30
31next_op:
32 switch( *pc++ )
33 {
34 default:
35 case PB_End:
36 break;
37
38 case PB_Flag:
39 par->rflag |= *pc++;
40 goto next_op;
41
42 case PB_Report:
43 par->report( par, *pc++, start, it );
44 goto next_op;
45
46 case PB_ReportEnd:
47 par->report( par, *pc, start, it );
48 break;
49
50 case PB_Next:
51 n = *pc++;
52 next = pc + n;
53 goto next_op;
54
55 case PB_Skip:
56 if( it == par->end )
57 goto fail;
58 ++it;
59 goto next_op;
60
61 case PB_LitWord:
62 n = par->atoms[ *pc++ ];
63 t = ur_type(it);
64 if( ur_isWordType(t) && ur_atom(it) == n )
65 {
66 ++it;
67 goto next_op;
68 }
69 goto fail;
70
71 case PB_Rule:
72 if( ! ur_parseBlockI( par, par->rules + *pc++, it ) )
73 goto fail;
74 it = par->it;
75 goto next_op;
76
77 case PB_Type:
78 if( ur_type(it) != *pc++ )
79 goto fail;
80 ++it;
81 goto next_op;
82
83 case PB_Typeset:
84 bset = par->rules + *pc++;
85 if( ! bitIsSet64(bset, ur_type(it)) )
86 goto fail;
87 ++it;
88 goto next_op;
89
90 case PB_OptR:
91 if( ur_parseBlockI( par, par->rules + *pc++, it ) )
92 it = par->it;
93 goto next_op;
94
95 case PB_OptT:
96 if( ur_type(it) == *pc++ )
97 ++it;
98 goto next_op;
99
100 case PB_OptTs:
101 bset = par->rules + *pc++;
102 if( bitIsSet64(bset, ur_type(it)) )
103 ++it;
104 goto next_op;
105
106 case PB_AnyR:
107 bset = par->rules + *pc++;
108 while( it != par->end && ur_parseBlockI( par, bset, it ) )
109 it = par->it;
110 goto next_op;
111
112 case PB_AnyT:
113 n = *pc++;
114 while( it != par->end && ur_type(it) == n )
115 ++it;
116 goto next_op;
117
118 case PB_AnyTs:
119 bset = par->rules + *pc++;
120 while( it != par->end && bitIsSet64(bset, ur_type(it)) )
121 ++it;
122 goto next_op;
123
124 case PB_SomeR:
125 sit = it;
126 bset = par->rules + *pc++;
127 while( it != par->end && ur_parseBlockI( par, bset, it ) )
128 it = par->it;
129 if( sit == it )
130 goto fail;
131 goto next_op;
132
133 case PB_SomeT:
134 sit = it;
135 n = *pc++;
136 while( it != par->end && ur_type(it) == n )
137 ++it;
138 if( sit == it )
139 goto fail;
140 goto next_op;
141
142 case PB_SomeTs:
143 sit = it;
144 bset = par->rules + *pc++;
145 while( it != par->end && bitIsSet64(bset, ur_type(it)) )
146 ++it;
147 if( sit == it )
148 goto fail;
149 goto next_op;
150
151 case PB_ToT:
152 n = *pc++;
153 while( it != par->end )
154 {
155 if( ur_type(it) == n )
156 goto next_op;
157 ++it;
158 }
159 goto fail;
160
161 case PB_ToTs:
162 bset = par->rules + *pc++;
163 while( it != par->end )
164 {
165 if( bitIsSet64(bset, ur_type(it)) )
166 goto next_op;
167 ++it;
168 }
169 goto fail;
170
171 case PB_ToLitWord:
172 n = par->atoms[ *pc++ ];
173 while( it != par->end )
174 {
175 t = ur_type(it);
176 if( ur_isWordType(t) && ur_atom(it) == n )
177 goto next_op;
178 ++it;
179 }
180 goto fail;
181
182 case PB_ThruT:
183 n = *pc++;
184 while( it != par->end )
185 {
186 if( ur_type(it) == n )
187 {
188 ++it;
189 goto next_op;
190 }
191 ++it;
192 }
193 goto fail;
194
195 case PB_ThruTs:
196 bset = par->rules + *pc++;
197 while( it != par->end )
198 {
199 if( bitIsSet64(bset, ur_type(it)) )
200 {
201 ++it;
202 goto next_op;
203 }
204 ++it;
205 }
206 goto fail;
207 }
208
209 par->it = it;
210 return 1;
211
212fail:
213 if( next )
214 {
215 pc = next;
216 next = 0;
217 it = start;
218 goto next_op;
219 }
220 return 0;
221}
#define ur_type(c)
Return UrlanDataType of cell.
Definition urlan.h:695
A cell holds a single value of a simple type or a reference (often to a UBuffer) for a complex type.
Definition urlan.h:248