Account for parse stack merges when shifting

Previously, when the parse stack was split into 3 or more heads, it was
possible for head 3 to be accidentally skipped if head 2 merged with head 1.
This commit is contained in:
Max Brunsfeld 2015-11-05 21:21:15 -08:00
parent a0eca388e8
commit 30b6530fd1
3 changed files with 57 additions and 16 deletions

View file

@ -76,3 +76,43 @@ int main() {
(expression_statement (call_expression
(identifier)
(math_expression (identifier) (identifier)))))))
==========================================
function-like macros that produce types
==========================================
GIT_INLINE(int) x = 5;
---
(translation_unit (declaration
(macro_type (identifier) (identifier))
(identifier)
(initializer (number))))
============================================
3-way ambiguities (regression)
============================================
int main() {
/*
* Could be either:
* - function call
* - declaration w/ parenthesized declarator
* - declaration w/ macro type, no declarator
*/
ABC(d);
efg hij;
}
---
(translation_unit
(function_definition
(identifier)
(function_declarator (identifier))
(compound_statement
(comment)
(expression_statement (call_expression (identifier) (identifier)))
(declaration (identifier) (identifier)))))

View file

@ -36,7 +36,7 @@ template function / relational expression ambiguities
int main() {
someVariable < someValue > 0.0;
someTemplate<SomeType>(y);
someTemplate<SomeType>(0, 0);
}
---
@ -52,7 +52,7 @@ int main() {
(expression_statement
(call_expression
(template_call (identifier) (type_id (type_specifier (identifier))))
(identifier)))))))
(number) (number)))))))
==================================================
template class / relational expression ambiguities

View file

@ -20,6 +20,12 @@
#define SYM_NAME(sym) self->language->symbol_names[sym]
typedef enum {
ConsumeResultShifted,
ConsumeResultRemoved,
ConsumeResultFinished
} ConsumeResult;
/*
* Private
*/
@ -139,13 +145,16 @@ static void ts_parser__get_next_lookahead(TSParser *self) {
* Parse Actions
*/
static void ts_parser__shift(TSParser *self, int head, TSStateId parse_state) {
ts_stack_push(self->stack, head, parse_state, self->lookahead);
static ConsumeResult ts_parser__shift(TSParser *self, int head, TSStateId parse_state) {
if (ts_stack_push(self->stack, head, parse_state, self->lookahead))
return ConsumeResultRemoved;
else
return ConsumeResultShifted;
}
static void ts_parser__shift_extra(TSParser *self, int head, TSStateId state) {
static bool ts_parser__shift_extra(TSParser *self, int head, TSStateId state) {
ts_tree_set_extra(self->lookahead);
ts_parser__shift(self, head, state);
return ts_parser__shift(self, head, state);
}
static TSTree *ts_parser__reduce(TSParser *self, int head, TSSymbol symbol,
@ -288,12 +297,6 @@ static TSTree *ts_parser__finish(TSParser *self) {
return root;
}
typedef enum {
ConsumeResultShifted,
ConsumeResultRemoved,
ConsumeResultFinished
} ConsumeResult;
/*
* Continue performing parse actions for the given head until the current
* lookahead symbol is consumed.
@ -345,13 +348,11 @@ static ConsumeResult ts_parser__consume_lookahead(TSParser *self, int head) {
case TSParseActionTypeShift:
DEBUG("shift state:%u", action.data.to_state);
ts_parser__shift(self, current_head, action.data.to_state);
return ConsumeResultShifted;
return ts_parser__shift(self, current_head, action.data.to_state);
case TSParseActionTypeShiftExtra:
DEBUG("shift_extra");
ts_parser__shift_extra(self, current_head, state);
return ConsumeResultShifted;
return ts_parser__shift_extra(self, current_head, state);
case TSParseActionTypeReduce:
DEBUG("reduce sym:%s, child_count:%u", SYM_NAME(action.data.symbol),