Skip to content

Commit 5f2010b

Browse files
authored
Merge pull request tree-sitter#607 from ret2libc/gcc-4-compat-2
Do not use multiple unnamed structs inside of unions
2 parents c393591 + 780e9ce commit 5f2010b

File tree

6 files changed

+56
-42
lines changed

6 files changed

+56
-42
lines changed

cli/src/generate/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ impl Generator {
10221022
for (i, entry) in parse_table_entries {
10231023
add!(
10241024
self,
1025-
" [{}] = {{.count = {}, .reusable = {}}},",
1025+
" [{}] = {{ .entry = {{.count = {}, .reusable = {}}} }},",
10261026
i,
10271027
entry.actions.len(),
10281028
entry.reusable

lib/include/tree_sitter/parser.h

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ typedef struct {
6262
TSStateId state;
6363
bool extra : 1;
6464
bool repetition : 1;
65-
};
65+
} shift;
6666
struct {
6767
TSSymbol symbol;
6868
int16_t dynamic_precedence;
6969
uint8_t child_count;
7070
uint8_t production_id;
71-
};
71+
} reduce;
7272
} params;
7373
TSParseActionType type : 4;
7474
} TSParseAction;
@@ -83,7 +83,7 @@ typedef union {
8383
struct {
8484
uint8_t count;
8585
bool reusable : 1;
86-
};
86+
} entry;
8787
} TSParseActionEntry;
8888

8989
struct TSLanguage {
@@ -167,22 +167,28 @@ struct TSLanguage {
167167

168168
#define ACTIONS(id) id
169169

170-
#define SHIFT(state_value) \
171-
{ \
172-
{ \
173-
.type = TSParseActionTypeShift, \
174-
.params = {.state = state_value}, \
175-
} \
170+
#define SHIFT(state_value) \
171+
{ \
172+
{ \
173+
.params = { \
174+
.shift = { \
175+
.state = state_value \
176+
} \
177+
}, \
178+
.type = TSParseActionTypeShift \
179+
} \
176180
}
177181

178182
#define SHIFT_REPEAT(state_value) \
179183
{ \
180184
{ \
181-
.type = TSParseActionTypeShift, \
182185
.params = { \
183-
.state = state_value, \
184-
.repetition = true \
186+
.shift = { \
187+
.state = state_value, \
188+
.repetition = true \
189+
} \
185190
}, \
191+
.type = TSParseActionTypeShift \
186192
} \
187193
}
188194

@@ -194,20 +200,26 @@ struct TSLanguage {
194200
#define SHIFT_EXTRA() \
195201
{ \
196202
{ \
197-
.type = TSParseActionTypeShift, \
198-
.params = {.extra = true} \
203+
.params = { \
204+
.shift = { \
205+
.extra = true \
206+
} \
207+
}, \
208+
.type = TSParseActionTypeShift \
199209
} \
200210
}
201211

202212
#define REDUCE(symbol_val, child_count_val, ...) \
203213
{ \
204214
{ \
205-
.type = TSParseActionTypeReduce, \
206215
.params = { \
207-
.symbol = symbol_val, \
208-
.child_count = child_count_val, \
209-
__VA_ARGS__ \
210-
} \
216+
.reduce = { \
217+
.symbol = symbol_val, \
218+
.child_count = child_count_val, \
219+
__VA_ARGS__ \
220+
}, \
221+
}, \
222+
.type = TSParseActionTypeReduce \
211223
} \
212224
}
213225

lib/src/language.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ void ts_language_table_entry(
3333
assert(symbol < self->token_count);
3434
uint32_t action_index = ts_language_lookup(self, state, symbol);
3535
const TSParseActionEntry *entry = &self->parse_actions[action_index];
36-
result->action_count = entry->count;
37-
result->is_reusable = entry->reusable;
36+
result->action_count = entry->entry.count;
37+
result->is_reusable = entry->entry.reusable;
3838
result->actions = (const TSParseAction *)(entry + 1);
3939
}
4040
}

lib/src/language.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static inline TSStateId ts_language_next_state(const TSLanguage *self,
9393
if (count > 0) {
9494
TSParseAction action = actions[count - 1];
9595
if (action.type == TSParseActionTypeShift) {
96-
return action.params.extra ? state : action.params.state;
96+
return action.params.shift.extra ? state : action.params.shift.state;
9797
}
9898
}
9999
return 0;

lib/src/parser.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -951,15 +951,15 @@ static bool ts_parser__do_all_potential_reductions(
951951
switch (action.type) {
952952
case TSParseActionTypeShift:
953953
case TSParseActionTypeRecover:
954-
if (!action.params.extra && !action.params.repetition) has_shift_action = true;
954+
if (!action.params.shift.extra && !action.params.shift.repetition) has_shift_action = true;
955955
break;
956956
case TSParseActionTypeReduce:
957-
if (action.params.child_count > 0)
957+
if (action.params.reduce.child_count > 0)
958958
ts_reduce_action_set_add(&self->reduce_actions, (ReduceAction){
959-
.symbol = action.params.symbol,
960-
.count = action.params.child_count,
961-
.dynamic_precedence = action.params.dynamic_precedence,
962-
.production_id = action.params.production_id,
959+
.symbol = action.params.reduce.symbol,
960+
.count = action.params.reduce.child_count,
961+
.dynamic_precedence = action.params.reduce.dynamic_precedence,
962+
.production_id = action.params.reduce.production_id,
963963
});
964964
default:
965965
break;
@@ -1250,7 +1250,7 @@ static void ts_parser__recover(
12501250
// be counted in error cost calculations.
12511251
unsigned n;
12521252
const TSParseAction *actions = ts_language_actions(self->language, 1, ts_subtree_symbol(lookahead), &n);
1253-
if (n > 0 && actions[n - 1].type == TSParseActionTypeShift && actions[n - 1].params.extra) {
1253+
if (n > 0 && actions[n - 1].type == TSParseActionTypeShift && actions[n - 1].params.shift.extra) {
12541254
MutableSubtree mutable_lookahead = ts_subtree_make_mut(&self->tree_pool, lookahead);
12551255
ts_subtree_set_extra(&mutable_lookahead);
12561256
lookahead = ts_subtree_from_mut(mutable_lookahead);
@@ -1379,17 +1379,17 @@ static bool ts_parser__advance(
13791379

13801380
switch (action.type) {
13811381
case TSParseActionTypeShift: {
1382-
if (action.params.repetition) break;
1382+
if (action.params.shift.repetition) break;
13831383
TSStateId next_state;
1384-
if (action.params.extra) {
1384+
if (action.params.shift.extra) {
13851385

13861386
// TODO: remove when TREE_SITTER_LANGUAGE_VERSION 9 is out.
13871387
if (state == ERROR_STATE) continue;
13881388

13891389
next_state = state;
13901390
LOG("shift_extra");
13911391
} else {
1392-
next_state = action.params.state;
1392+
next_state = action.params.shift.state;
13931393
LOG("shift state:%u", next_state);
13941394
}
13951395

@@ -1398,18 +1398,18 @@ static bool ts_parser__advance(
13981398
next_state = ts_language_next_state(self->language, state, ts_subtree_symbol(lookahead));
13991399
}
14001400

1401-
ts_parser__shift(self, version, next_state, lookahead, action.params.extra);
1401+
ts_parser__shift(self, version, next_state, lookahead, action.params.shift.extra);
14021402
if (did_reuse) reusable_node_advance(&self->reusable_node);
14031403
return true;
14041404
}
14051405

14061406
case TSParseActionTypeReduce: {
14071407
bool is_fragile = table_entry.action_count > 1;
14081408
bool is_extra = lookahead.ptr == NULL;
1409-
LOG("reduce sym:%s, child_count:%u", SYM_NAME(action.params.symbol), action.params.child_count);
1409+
LOG("reduce sym:%s, child_count:%u", SYM_NAME(action.params.reduce.symbol), action.params.reduce.child_count);
14101410
StackVersion reduction_version = ts_parser__reduce(
1411-
self, version, action.params.symbol, action.params.child_count,
1412-
action.params.dynamic_precedence, action.params.production_id,
1411+
self, version, action.params.reduce.symbol, action.params.reduce.child_count,
1412+
action.params.reduce.dynamic_precedence, action.params.reduce.production_id,
14131413
is_fragile, is_extra
14141414
);
14151415
if (reduction_version != STACK_VERSION_NONE) {

lib/src/subtree.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef struct {
2121
#define TS_MAX_INLINE_TREE_LENGTH UINT8_MAX
2222
#define TS_MAX_TREE_POOL_SIZE 32
2323

24-
static const ExternalScannerState empty_state = {.length = 0, .short_data = {0}};
24+
static const ExternalScannerState empty_state = {{.short_data = {0}}, .length = 0};
2525

2626
// ExternalScannerState
2727

@@ -208,7 +208,7 @@ Subtree ts_subtree_new_leaf(
208208
.has_external_tokens = has_external_tokens,
209209
.is_missing = false,
210210
.is_keyword = is_keyword,
211-
.first_leaf = {.symbol = 0, .parse_state = 0},
211+
{{.first_leaf = {.symbol = 0, .parse_state = 0}}}
212212
};
213213
return (Subtree) {.ptr = data};
214214
}
@@ -464,15 +464,17 @@ MutableSubtree ts_subtree_new_node(SubtreePool *pool, TSSymbol symbol,
464464
*data = (SubtreeHeapData) {
465465
.ref_count = 1,
466466
.symbol = symbol,
467-
.production_id = production_id,
468467
.visible = metadata.visible,
469468
.named = metadata.named,
470469
.has_changes = false,
471470
.fragile_left = fragile,
472471
.fragile_right = fragile,
473472
.is_keyword = false,
474-
.node_count = 0,
475-
.first_leaf = {.symbol = 0, .parse_state = 0},
473+
{{
474+
.node_count = 0,
475+
.production_id = production_id,
476+
.first_leaf = {.symbol = 0, .parse_state = 0},
477+
}}
476478
};
477479
MutableSubtree result = {.ptr = data};
478480
ts_subtree_set_children(result, children->contents, children->size, language);

0 commit comments

Comments
 (0)