From b431f79db6deb75229748f80a3e9e448b6dacf31 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sun, 3 Jul 2016 06:05:39 +0200 Subject: Revert "error: improved parser error reporting" this will not work properly for unicode chars This reverts commit 3968529cad2baec099acff5fee47b5fe24624b03. --- src/error.c | 4 +- src/template-parser.c | 2 +- tests/check_error.c | 24 ++----- tests/check_source_parser.c | 28 +++------ tests/check_template_parser.c | 141 ++++++++++++++---------------------------- 5 files changed, 64 insertions(+), 135 deletions(-) diff --git a/src/error.c b/src/error.c index acce506..586386a 100644 --- a/src/error.c +++ b/src/error.c @@ -96,8 +96,8 @@ blogc_error_parser(blogc_error_type_t type, const char *src, size_t src_len, rv = blogc_error_new(type, msg); else rv = blogc_error_new_printf(type, - "%s\nError occurred near line %d, position %d:\n%s\n%*s", msg, - lineno, pos, line, pos, "^"); + "%s\nError occurred near line %d, position %d: %s", msg, lineno, + pos, line); free(msg); free(line); diff --git a/src/template-parser.c b/src/template-parser.c index 751fd9e..de6eb52 100644 --- a/src/template-parser.c +++ b/src/template-parser.c @@ -622,7 +622,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) if (*err == NULL) { if (state == TEMPLATE_BLOCK_IF_STRING_OPERAND) *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, - start2, "Found an open double-quoted string."); + start2 - 1, "Found an open double-quoted string."); else if (if_count != 0) *err = blogc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, "%d open 'ifdef' and/or 'ifndef' statements were not closed!", diff --git a/tests/check_error.c b/tests/check_error.c index a954095..8818b00 100644 --- a/tests/check_error.c +++ b/tests/check_error.c @@ -49,27 +49,21 @@ test_error_parser(void **state) assert_non_null(error); assert_int_equal(error->type, 1); assert_string_equal(error->msg, - "asd 10\nError occurred near line 3, position 2:\n" - "chunda\n" - " ^"); + "asd 10\nError occurred near line 3, position 2: chunda"); blogc_error_free(error); a = "bola\nguda\nchunda"; error = blogc_error_parser(1, a, strlen(a), 11, "asd %d", 10); assert_non_null(error); assert_int_equal(error->type, 1); assert_string_equal(error->msg, - "asd 10\nError occurred near line 3, position 2:\n" - "chunda\n" - " ^"); + "asd 10\nError occurred near line 3, position 2: chunda"); blogc_error_free(error); a = "bola\nguda\nchunda"; error = blogc_error_parser(1, a, strlen(a), 0, "asd %d", 10); assert_non_null(error); assert_int_equal(error->type, 1); assert_string_equal(error->msg, - "asd 10\nError occurred near line 1, position 1:\n" - "bola\n" - "^"); + "asd 10\nError occurred near line 1, position 1: bola"); blogc_error_free(error); a = ""; error = blogc_error_parser(1, a, strlen(a), 0, "asd %d", 10); @@ -88,27 +82,21 @@ test_error_parser_crlf(void **state) assert_non_null(error); assert_int_equal(error->type, 1); assert_string_equal(error->msg, - "asd 10\nError occurred near line 3, position 2:\n" - "chunda\n" - " ^"); + "asd 10\nError occurred near line 3, position 2: chunda"); blogc_error_free(error); a = "bola\r\nguda\r\nchunda"; error = blogc_error_parser(1, a, strlen(a), 13, "asd %d", 10); assert_non_null(error); assert_int_equal(error->type, 1); assert_string_equal(error->msg, - "asd 10\nError occurred near line 3, position 2:\n" - "chunda\n" - " ^"); + "asd 10\nError occurred near line 3, position 2: chunda"); blogc_error_free(error); a = "bola\r\nguda\r\nchunda"; error = blogc_error_parser(1, a, strlen(a), 0, "asd %d", 10); assert_non_null(error); assert_int_equal(error->type, 1); assert_string_equal(error->msg, - "asd 10\nError occurred near line 1, position 1:\n" - "bola\n" - "^"); + "asd 10\nError occurred near line 1, position 1: bola"); blogc_error_free(error); } diff --git a/tests/check_source_parser.c b/tests/check_source_parser.c index 2dcaad6..d94dc70 100644 --- a/tests/check_source_parser.c +++ b/tests/check_source_parser.c @@ -220,9 +220,7 @@ test_source_parse_config_invalid_key(void **state) assert_int_equal(err->type, BLOGC_ERROR_SOURCE_PARSER); assert_string_equal(err->msg, "Can't find a configuration key or the content separator.\n" - "Error occurred near line 1, position 1:\n" - "bola: guda\n" - "^"); + "Error occurred near line 1, position 1: bola: guda"); blogc_error_free(err); sb_trie_free(source); } @@ -238,9 +236,7 @@ test_source_parse_config_no_key(void **state) assert_int_equal(err->type, BLOGC_ERROR_SOURCE_PARSER); assert_string_equal(err->msg, "Invalid configuration key.\n" - "Error occurred near line 1, position 4:\n" - "BOLa\n" - " ^"); + "Error occurred near line 1, position 4: BOLa"); blogc_error_free(err); sb_trie_free(source); } @@ -256,9 +252,7 @@ test_source_parse_config_no_key2(void **state) assert_int_equal(err->type, BLOGC_ERROR_SOURCE_PARSER); assert_string_equal(err->msg, "Your last configuration key is missing ':' and the value\n" - "Error occurred near line 1, position 5:\n" - "BOLA\n" - " ^"); + "Error occurred near line 1, position 5: BOLA"); blogc_error_free(err); sb_trie_free(source); } @@ -275,9 +269,7 @@ test_source_parse_config_no_value(void **state) assert_int_equal(err->type, BLOGC_ERROR_SOURCE_PARSER); assert_string_equal(err->msg, "Configuration value not provided for 'BOLA'.\n" - "Error occurred near line 1, position 6:\n" - "BOLA:\n" - " ^"); + "Error occurred near line 1, position 6: BOLA:"); blogc_error_free(err); sb_trie_free(source); } @@ -294,9 +286,7 @@ test_source_parse_config_no_value2(void **state) assert_int_equal(err->type, BLOGC_ERROR_SOURCE_PARSER); assert_string_equal(err->msg, "Configuration value not provided for 'BOLA'.\n" - "Error occurred near line 1, position 6:\n" - "BOLA:\n" - " ^"); + "Error occurred near line 1, position 6: BOLA:"); blogc_error_free(err); sb_trie_free(source); } @@ -500,9 +490,7 @@ test_source_parse_config_value_no_line_ending(void **state) assert_int_equal(err->type, BLOGC_ERROR_SOURCE_PARSER); assert_string_equal(err->msg, "No line ending after the configuration value for 'BOLA'.\n" - "Error occurred near line 1, position 10:\n" - "BOLA: asd\n" - " ^"); + "Error occurred near line 1, position 10: BOLA: asd"); blogc_error_free(err); sb_trie_free(source); } @@ -519,9 +507,7 @@ test_source_parse_invalid_separator(void **state) assert_int_equal(err->type, BLOGC_ERROR_SOURCE_PARSER); assert_string_equal(err->msg, "Invalid content separator. Must be more than one '-' characters.\n" - "Error occurred near line 2, position 4:\n" - "---#\n" - " ^"); + "Error occurred near line 2, position 4: ---#"); blogc_error_free(err); sb_trie_free(source); } diff --git a/tests/check_template_parser.c b/tests/check_template_parser.c index 8264ffa..d2d5e8d 100644 --- a/tests/check_template_parser.c +++ b/tests/check_template_parser.c @@ -455,9 +455,7 @@ test_template_parse_invalid_block_start(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid statement syntax. Must begin with lowercase letter.\n" - "Error occurred near line 1, position 4:\n" - "{% ASD %}\n" - " ^"); + "Error occurred near line 1, position 4: {% ASD %}"); blogc_error_free(err); a = "{%-- block entry %}\n"; err = NULL; @@ -467,9 +465,7 @@ test_template_parse_invalid_block_start(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid statement syntax. Duplicated whitespace cleaner before statement.\n" - "Error occurred near line 1, position 4:\n" - "{%-- block entry %}\n" - " ^"); + "Error occurred near line 1, position 4: {%-- block entry %}"); blogc_error_free(err); a = "{% block entry --%}\n"; err = NULL; @@ -479,9 +475,7 @@ test_template_parse_invalid_block_start(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid statement syntax. Duplicated whitespace cleaner after statement.\n" - "Error occurred near line 1, position 17:\n" - "{% block entry --%}\n" - " ^"); + "Error occurred near line 1, position 17: {% block entry --%}"); blogc_error_free(err); } @@ -499,9 +493,7 @@ test_template_parse_invalid_block_nested(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Blocks can't be nested.\n" - "Error occurred near line 2, position 9:\n" - "{% block listing %}\n" - " ^"); + "Error occurred near line 2, position 9: {% block listing %}"); blogc_error_free(err); } @@ -519,9 +511,7 @@ test_template_parse_invalid_foreach_nested(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "'foreach' statements can't be nested.\n" - "Error occurred near line 2, position 11:\n" - "{% foreach B %}\n" - " ^"); + "Error occurred near line 2, position 11: {% foreach B %}"); blogc_error_free(err); } @@ -537,9 +527,7 @@ test_template_parse_invalid_block_not_open(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "'endblock' statement without an open 'block' statement.\n" - "Error occurred near line 1, position 12:\n" - "{% endblock %}\n" - " ^"); + "Error occurred near line 1, position 12: {% endblock %}"); blogc_error_free(err); } @@ -555,9 +543,8 @@ test_template_parse_invalid_endif_not_open(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "'endif' statement without an open 'if', 'ifdef' or 'ifndef' statement.\n" - "Error occurred near line 1, position 28:\n" - "{% block listing %}{% endif %}{% endblock %}\n" - " ^"); + "Error occurred near line 1, position 28: " + "{% block listing %}{% endif %}{% endblock %}"); blogc_error_free(err); } @@ -573,9 +560,7 @@ test_template_parse_invalid_endforeach_not_open(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "'endforeach' statement without an open 'foreach' statement.\n" - "Error occurred near line 1, position 14:\n" - "{% endforeach %}\n" - " ^"); + "Error occurred near line 1, position 14: {% endforeach %}"); blogc_error_free(err); } @@ -592,9 +577,7 @@ test_template_parse_invalid_block_name(void **state) assert_string_equal(err->msg, "Invalid statement type: Allowed types are: 'block', 'endblock', 'ifdef', " "'ifndef', 'else', 'endif', 'foreach' and 'endforeach'.\n" - "Error occurred near line 1, position 10:\n" - "{% chunda %}\n" - " ^"); + "Error occurred near line 1, position 10: {% chunda %}"); blogc_error_free(err); } @@ -610,9 +593,7 @@ test_template_parse_invalid_block_type_start(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid block syntax. Must begin with lowercase letter.\n" - "Error occurred near line 1, position 10:\n" - "{% block ENTRY %}\n" - " ^"); + "Error occurred near line 1, position 10: {% block ENTRY %}"); blogc_error_free(err); } @@ -628,9 +609,7 @@ test_template_parse_invalid_block_type(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid block type. Allowed types are: 'entry', 'listing' and 'listing_once'.\n" - "Error occurred near line 1, position 16:\n" - "{% block chunda %}\n" - " ^"); + "Error occurred near line 1, position 16: {% block chunda %}"); blogc_error_free(err); } @@ -646,9 +625,8 @@ test_template_parse_invalid_ifdef_start(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid variable name. Must begin with uppercase letter.\n" - "Error occurred near line 1, position 27:\n" - "{% block entry %}{% ifdef guda %}\n" - " ^"); + "Error occurred near line 1, position 27: " + "{% block entry %}{% ifdef guda %}"); blogc_error_free(err); } @@ -664,9 +642,8 @@ test_template_parse_invalid_foreach_start(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid foreach variable name. Must begin with uppercase letter.\n" - "Error occurred near line 1, position 29:\n" - "{% block entry %}{% foreach guda %}\n" - " ^"); + "Error occurred near line 1, position 29: " + "{% block entry %}{% foreach guda %}"); blogc_error_free(err); } @@ -682,9 +659,8 @@ test_template_parse_invalid_ifdef_variable(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid variable name. Must be uppercase letter, number or '_'.\n" - "Error occurred near line 1, position 28:\n" - "{% block entry %}{% ifdef BoLA %}\n" - " ^"); + "Error occurred near line 1, position 28: " + "{% block entry %}{% ifdef BoLA %}"); blogc_error_free(err); } @@ -700,9 +676,8 @@ test_template_parse_invalid_ifdef_variable2(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid variable name. Must begin with uppercase letter.\n" - "Error occurred near line 1, position 27:\n" - "{% block entry %}{% ifdef 0123 %}\n" - " ^"); + "Error occurred near line 1, position 27: " + "{% block entry %}{% ifdef 0123 %}"); blogc_error_free(err); } @@ -718,9 +693,8 @@ test_template_parse_invalid_foreach_variable(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid foreach variable name. Must be uppercase letter, number or '_'.\n" - "Error occurred near line 1, position 30:\n" - "{% block entry %}{% foreach BoLA %}\n" - " ^"); + "Error occurred near line 1, position 30: " + "{% block entry %}{% foreach BoLA %}"); blogc_error_free(err); } @@ -736,9 +710,8 @@ test_template_parse_invalid_foreach_variable2(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid foreach variable name. Must begin with uppercase letter.\n" - "Error occurred near line 1, position 29:\n" - "{% block entry %}{% foreach 0123 %}\n" - " ^"); + "Error occurred near line 1, position 29: {% block entry %}" + "{% foreach 0123 %}"); blogc_error_free(err); } @@ -754,9 +727,8 @@ test_template_parse_invalid_if_operator(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid 'if' operator. Must be '<', '>', '<=', '>=', '==' or '!='.\n" - "Error occurred near line 1, position 29:\n" - "{% block entry %}{% if BOLA = \"asd\" %}\n" - " ^"); + "Error occurred near line 1, position 29: " + "{% block entry %}{% if BOLA = \"asd\" %}"); blogc_error_free(err); } @@ -772,9 +744,8 @@ test_template_parse_invalid_if_operand(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid 'if' operand. Must be double-quoted static string or variable.\n" - "Error occurred near line 1, position 32:\n" - "{% block entry %}{% if BOLA == asd %}\n" - " ^"); + "Error occurred near line 1, position 32: " + "{% block entry %}{% if BOLA == asd %}"); blogc_error_free(err); } @@ -790,9 +761,8 @@ test_template_parse_invalid_if_operand2(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Found an open double-quoted string.\n" - "Error occurred near line 1, position 32:\n" - "{% block entry %}{% if BOLA == \"asd %}\n" - " ^"); + "Error occurred near line 1, position 31: " + "{% block entry %}{% if BOLA == \"asd %}"); blogc_error_free(err); } @@ -808,9 +778,8 @@ test_template_parse_invalid_if_operand3(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid 'if' operand. Must be double-quoted static string or variable.\n" - "Error occurred near line 1, position 32:\n" - "{% block entry %}{% if BOLA == 0123 %}\n" - " ^"); + "Error occurred near line 1, position 32: " + "{% block entry %}{% if BOLA == 0123 %}"); blogc_error_free(err); } @@ -826,9 +795,7 @@ test_template_parse_invalid_else1(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "'else' statement without an open 'if', 'ifdef' or 'ifndef' statement.\n" - "Error occurred near line 1, position 8:\n" - "{% else %}\n" - " ^"); + "Error occurred near line 1, position 8: {% else %}"); blogc_error_free(err); } @@ -844,9 +811,8 @@ test_template_parse_invalid_else2(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "More than one 'else' statement for an open 'if', 'ifdef' or 'ifndef' " - "statement.\nError occurred near line 1, position 60:\n" - "{% if BOLA == \"123\" %}{% if GUDA == \"1\" %}{% else %}{% else %}\n" - " ^"); + "statement.\nError occurred near line 1, position 60: {% if BOLA == \"123\" " + "%}{% if GUDA == \"1\" %}{% else %}{% else %}"); blogc_error_free(err); } @@ -869,9 +835,7 @@ test_template_parse_invalid_else3(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "More than one 'else' statement for an open 'if', 'ifdef' or 'ifndef' " - "statement.\nError occurred near line 7, position 8:\n" - "{% else %}\n" - " ^"); + "statement.\nError occurred near line 7, position 8: {% else %}"); blogc_error_free(err); } @@ -887,9 +851,7 @@ test_template_parse_invalid_block_end(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid statement syntax. Must end with '%}'.\n" - "Error occurred near line 1, position 16:\n" - "{% block entry }}\n" - " ^"); + "Error occurred near line 1, position 16: {% block entry }}"); blogc_error_free(err); } @@ -905,9 +867,8 @@ test_template_parse_invalid_variable_name(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid variable name. Must begin with uppercase letter.\n" - "Error occurred near line 1, position 21:\n" - "{% block entry %}{{ bola }}{% endblock %}\n" - " ^"); + "Error occurred near line 1, position 21: " + "{% block entry %}{{ bola }}{% endblock %}"); blogc_error_free(err); } @@ -923,9 +884,8 @@ test_template_parse_invalid_variable_name2(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid variable name. Must be uppercase letter, number or '_'.\n" - "Error occurred near line 1, position 22:\n" - "{% block entry %}{{ Bola }}{% endblock %}\n" - " ^"); + "Error occurred near line 1, position 22: " + "{% block entry %}{{ Bola }}{% endblock %}"); blogc_error_free(err); } @@ -941,9 +901,8 @@ test_template_parse_invalid_variable_name3(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid variable name. Must begin with uppercase letter.\n" - "Error occurred near line 1, position 21:\n" - "{% block entry %}{{ 0123 }}{% endblock %}\n" - " ^"); + "Error occurred near line 1, position 21: {% block entry %}{{ 0123 }}" + "{% endblock %}"); blogc_error_free(err); } @@ -959,9 +918,8 @@ test_template_parse_invalid_variable_end(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid statement syntax. Must end with '}}'.\n" - "Error occurred near line 1, position 26:\n" - "{% block entry %}{{ BOLA %}{% endblock %}\n" - " ^"); + "Error occurred near line 1, position 26: " + "{% block entry %}{{ BOLA %}{% endblock %}"); blogc_error_free(err); } @@ -977,9 +935,7 @@ test_template_parse_invalid_close(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid statement syntax. Must end with '}'.\n" - "Error occurred near line 1, position 17:\n" - "{% block entry %%\n" - " ^"); + "Error occurred near line 1, position 17: {% block entry %%"); blogc_error_free(err); } @@ -995,9 +951,8 @@ test_template_parse_invalid_close2(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid statement syntax. Must end with '}'.\n" - "Error occurred near line 1, position 27:\n" - "{% block entry %}{{ BOLA }%{% endblock %}\n" - " ^"); + "Error occurred near line 1, position 27: " + "{% block entry %}{{ BOLA }%{% endblock %}"); blogc_error_free(err); } -- cgit v1.2.3-18-g5258