diff --git a/assets/parser.c.erb b/assets/parser.c.erb index dd9c179..a70db48 100644 --- a/assets/parser.c.erb +++ b/assets/parser.c.erb @@ -841,7 +841,7 @@ static 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. + * The function p_token(&context) can be used to get the unexpected token. * @reval P_DECODE_ERROR * The decoder encountered invalid text encoding. * @reval P_UNEXPECTED_INPUT @@ -974,3 +974,13 @@ size_t <%= @grammar.prefix %>user_terminate_code(<%= @grammar.prefix %>context_t { return context->user_terminate_code; } + +/** + * Get the parse token. + * + * @return Parse token. + */ +<%= @grammar.prefix %>token_t <%= @grammar.prefix %>token(<%= @grammar.prefix %>context_t * context) +{ + return context->token; +} diff --git a/assets/parser.d.erb b/assets/parser.d.erb index 7135a82..79cee1c 100644 --- a/assets/parser.d.erb +++ b/assets/parser.d.erb @@ -859,7 +859,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. + * The function p_token(&context) can be used to get the unexpected token. * @reval P_DECODE_ERROR * The decoder encountered invalid text encoding. * @reval P_UNEXPECTED_INPUT @@ -983,3 +983,13 @@ public size_t <%= @grammar.prefix %>user_terminate_code(<%= @grammar.prefix %>co { return context.user_terminate_code; } + +/** + * Get the parse token. + * + * @return Parse token. + */ +public <%= @grammar.prefix %>token_t <%= @grammar.prefix %>token(<%= @grammar.prefix %>context_t * context) +{ + return context.token; +} diff --git a/assets/parser.h.erb b/assets/parser.h.erb index 1c2a95a..ed7853e 100644 --- a/assets/parser.h.erb +++ b/assets/parser.h.erb @@ -137,3 +137,5 @@ size_t <%= @grammar.prefix %>parse(<%= @grammar.prefix %>context_t * context); <%= @grammar.prefix %>position_t <%= @grammar.prefix %>position(<%= @grammar.prefix %>context_t * context); size_t <%= @grammar.prefix %>user_terminate_code(<%= @grammar.prefix %>context_t * context); + +<%= @grammar.prefix %>token_t <%= @grammar.prefix %>token(<%= @grammar.prefix %>context_t * context); diff --git a/doc/user_guide.md b/doc/user_guide.md index 53adf5d..3645707 100644 --- a/doc/user_guide.md +++ b/doc/user_guide.md @@ -726,6 +726,22 @@ if (p_parse(&context) == P_USER_TERMINATED) } ``` +### `p_token` + +The `p_token()` function can be used to retrieve the current parse token. +This is useful after `p_parse()` returns a `P_UNEXPECTED_TOKEN` value. +terminate code after `p_parse()` returns a `P_USER_TERMINATED` value to +indicate what token the parser was not expecting. + +Example: + +``` +if (p_parse(&context) == P_UNEXPECTED_TOKEN) +{ + p_token_t unexpected_token = p_token(&context); +} +``` + ##> Data ### `p_token_names` diff --git a/spec/test_error_positions.c b/spec/test_error_positions.c index 15aaf01..6c457f1 100644 --- a/spec/test_error_positions.c +++ b/spec/test_error_positions.c @@ -14,14 +14,14 @@ int main() assert(p_parse(&context) == P_UNEXPECTED_TOKEN); assert(p_position(&context).row == 2); assert(p_position(&context).col == 3); - assert(context.token == TOKEN_a); + assert(p_token(&context) == TOKEN_a); input = "12"; p_context_init(&context, (uint8_t const *)input, strlen(input)); assert(p_parse(&context) == P_UNEXPECTED_TOKEN); assert(p_position(&context).row == 0); assert(p_position(&context).col == 0); - assert(context.token == TOKEN_num); + assert(p_token(&context) == TOKEN_num); input = "a 12\n\nab"; p_context_init(&context, (uint8_t const *)input, strlen(input)); diff --git a/spec/test_error_positions.d b/spec/test_error_positions.d index 352b799..5ebf7d4 100644 --- a/spec/test_error_positions.d +++ b/spec/test_error_positions.d @@ -17,13 +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); + assert(p_token(&context) == 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); + assert(p_token(&context) == TOKEN_num); input = "a 12\n\nab"; p_context_init(&context, input);