From fff9700f91e18deb8d857e4f79aa4f5faee50241 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 28 Jul 2023 15:42:18 -0400 Subject: [PATCH] Report the unexpected token for P_UNEXPECTED_TOKEN returns - close #11 --- assets/parser.d.erb | 5 +++++ spec/test_error_positions.d | 2 ++ spec/test_parser_rule_from_multiple_states.d | 1 + 3 files changed, 8 insertions(+) diff --git a/assets/parser.d.erb b/assets/parser.d.erb index eeb16d1..5d505fb 100644 --- a/assets/parser.d.erb +++ b/assets/parser.d.erb @@ -113,6 +113,9 @@ public struct <%= @grammar.prefix %>context_t /** Parse result value. */ <%= @grammar.prefix %>value_t parse_result; + + /** Unexpected token received. */ + <%= @grammar.prefix %>token_t token; } /************************************************************************** @@ -842,6 +845,7 @@ private size_t check_reduce(size_t state_id, <%= @grammar.prefix %>token_t token * can be accessed with <%= @grammar.prefix %>result(). * @retval P_UNEXPECTED_TOKEN * An unexpected token was encountered that does not match any grammar rule. + * The value context.token holds the unexpected token. * @reval P_DECODE_ERROR * The decoder encountered invalid text encoding. * @reval P_UNEXPECTED_INPUT @@ -917,6 +921,7 @@ public size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * cont * after it, so that if the caller wants to report the error position, * it will point to the correct position of the unexpected token. */ context.text_position = token_info.position; + context.token = token; return P_UNEXPECTED_TOKEN; } } diff --git a/spec/test_error_positions.d b/spec/test_error_positions.d index 89bfbaa..c8c4059 100644 --- a/spec/test_error_positions.d +++ b/spec/test_error_positions.d @@ -17,11 +17,13 @@ unittest p_context_init(&context, input); assert(p_parse(&context) == P_UNEXPECTED_TOKEN); assert(p_position(&context) == p_position_t(2, 3)); + assert(context.token == TOKEN_a); input = "12"; p_context_init(&context, input); assert(p_parse(&context) == P_UNEXPECTED_TOKEN); assert(p_position(&context) == p_position_t(0, 0)); + assert(context.token == TOKEN_num); input = "a 12\n\nab"; p_context_init(&context, input); diff --git a/spec/test_parser_rule_from_multiple_states.d b/spec/test_parser_rule_from_multiple_states.d index e861610..a69b1fe 100644 --- a/spec/test_parser_rule_from_multiple_states.d +++ b/spec/test_parser_rule_from_multiple_states.d @@ -13,6 +13,7 @@ unittest p_context_init(&context, input); assert(p_parse(&context) == P_UNEXPECTED_TOKEN); assert(p_position(&context) == p_position_t(0, 1)); + assert(context.token == TOKEN___EOF); input = "a b"; p_context_init(&context, input);