Skip to content

Commit 86aa274

Browse files
committed
Make parser return error message on failure
1 parent 64961c5 commit 86aa274

File tree

2 files changed

+70
-38
lines changed

2 files changed

+70
-38
lines changed

parser/base.c

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const xx_token_names xx_tokens[] =
2020
{ XX_T_INTEGER, "INTEGER" },
2121
{ XX_T_DOUBLE, "DOUBLE" },
2222
{ XX_T_STRING, "STRING" },
23-
{ XX_T_IDENTIFIER, "IDENTIFIER" },
23+
{ XX_T_IDENTIFIER, "IDENTIFIER" },
2424
{ XX_T_AT, "@" },
2525
{ XX_T_COMMA, "," },
2626
{ XX_T_ASSIGN, "=" },
@@ -72,7 +72,7 @@ static void xx_parse_with_token(void* xx_parser, int opcode, int parsercode, xx_
7272
/**
7373
* Parses a programm and returning an intermediate array representation
7474
*/
75-
void xx_parse_program(zval *return_value, char *program, size_t program_length, char *file_path, zval **error_msg) {
75+
void xx_parse_program(zval *return_value, char *program, size_t program_length, char *file_path, zval *error_msg) {
7676

7777
char *error;
7878
xx_scanner_state *state;
@@ -520,12 +520,24 @@ void xx_parse_program(zval *return_value, char *program, size_t program_length,
520520

521521
default:
522522
parser_status->status = XX_PARSING_FAILED;
523-
if (!*error_msg) {
523+
if (error_msg && Z_TYPE_P(error_msg) != IS_ARRAY) {
524524
int length = (48 + strlen(file_path));
525525
error = emalloc(sizeof(char) * length);
526526
snprintf(error, length, "Scanner: unknown opcode %d on in %s line %d", token.opcode, file_path, state->active_line);
527-
//ZVAL_STRING(*error_msg, error);
527+
528+
array_init(error_msg);
529+
#if PHP_VERSION_ID >= 70000
530+
add_assoc_string(error_msg, "type", "error");
531+
add_assoc_string(error_msg, "message", error);
532+
add_assoc_string(error_msg, "file", state->active_file);
528533
efree(error);
534+
#else
535+
add_assoc_string(error_msg, "type", "error", 1);
536+
add_assoc_string(error_msg, "message", error, 0);
537+
add_assoc_string(error_msg, "file", state->active_file, 1);
538+
#endif
539+
add_assoc_long(error_msg, "line", state->active_line);
540+
add_assoc_long(error_msg, "char", state->active_char);
529541
}
530542
break;
531543
}
@@ -542,20 +554,27 @@ void xx_parse_program(zval *return_value, char *program, size_t program_length,
542554
switch (scanner_status) {
543555
case XX_SCANNER_RETCODE_ERR:
544556
case XX_SCANNER_RETCODE_IMPOSSIBLE:
545-
{
557+
if (error_msg && Z_TYPE_P(error_msg) == IS_NULL) {
546558
error = emalloc(sizeof(char) * 1024);
547559
if (state->start) {
548560
snprintf(error, 1024, "Scanner error: %d %s", scanner_status, state->start);
549561
} else {
550562
snprintf(error, 1024, "Scanner error: %d", scanner_status);
551563
}
552-
#if PHP_VERSION_ID < 70000
553-
ALLOC_INIT_ZVAL(*error_msg);
554-
ZVAL_STRING(*error_msg, error, 1);
564+
565+
array_init(error_msg);
566+
#if PHP_VERSION_ID >= 70000
567+
add_assoc_string(error_msg, "type", "error");
568+
add_assoc_string(error_msg, "message", error);
569+
add_assoc_string(error_msg, "file", state->active_file);
570+
efree(error);
555571
#else
556-
ZVAL_STRING(*error_msg, error);
572+
add_assoc_string(error_msg, "type", "error", 1);
573+
add_assoc_string(error_msg, "message", error, 0);
574+
add_assoc_string(error_msg, "file", state->active_file, 1);
557575
#endif
558-
efree(error);
576+
add_assoc_long(error_msg, "line", state->active_line);
577+
add_assoc_long(error_msg, "char", state->active_char);
559578
status = FAILURE;
560579
}
561580
break;
@@ -569,18 +588,31 @@ void xx_parse_program(zval *return_value, char *program, size_t program_length,
569588

570589
if (parser_status->status != XX_PARSING_OK) {
571590
status = FAILURE;
572-
if (parser_status->syntax_error) {
573-
#if PHP_VERSION_ID < 70000
574-
if (!*error_msg) {
575-
ALLOC_INIT_ZVAL(*error_msg);
576-
ZVAL_STRING(*error_msg, parser_status->syntax_error, 1);
577-
}
591+
if (parser_status->syntax_error && error_msg && Z_TYPE_P(error_msg) != IS_ARRAY) {
592+
array_init(error_msg);
593+
#if PHP_VERSION_ID >= 70000
594+
add_assoc_string(error_msg, "type", "error");
595+
add_assoc_string(error_msg, "message", parser_status->syntax_error);
596+
add_assoc_string(error_msg, "file", state->active_file);
597+
efree(parser_status->syntax_error);
578598
#else
579-
if (Z_TYPE_P(*error_msg) == IS_UNDEF) {
580-
ZVAL_STRING(*error_msg, parser_status->syntax_error);
581-
}
599+
add_assoc_string(error_msg, "type", "error", 1);
600+
add_assoc_string(error_msg, "message", parser_status->syntax_error, 0);
601+
add_assoc_string(error_msg, "file", state->active_file, 1);
602+
#endif
603+
add_assoc_long(error_msg, "line", state->active_line);
604+
add_assoc_long(error_msg, "char", state->active_char);
605+
606+
parser_status->syntax_error = NULL;
607+
}
608+
else if (error_msg && Z_TYPE_P(error_msg) != IS_ARRAY) {
609+
#if PHP_VERSION_ID >= 70000
610+
assert(Z_TYPE(parser_status->ret) == IS_ARRAY);
611+
ZVAL_ZVAL(error_msg, &parser_status->ret, 1, 1);
612+
#else
613+
assert(Z_TYPE_P(parser_status->ret) == IS_ARRAY);
614+
ZVAL_ZVAL(error_msg, parser_status->ret, 1, 1);
582615
#endif
583-
efree(parser_status->syntax_error);
584616
}
585617
}
586618

zephir_parser.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,45 @@
2626
#include "ext/standard/info.h"
2727
#include "php_zephir_parser.h"
2828

29-
extern void *xx_parse_program(zval *return_value, char *program, size_t program_length, char *file_path, zval **error_msg);
29+
extern void *xx_parse_program(zval *return_value, char *program, size_t program_length, char *file_path, zval *error_msg);
3030

3131
/* {{{ proto array zephir_parse_file(string content, string filepath)
3232
Parses a file and returning an intermediate array representation */
33-
PHP_FUNCTION(zephir_parse_file)
33+
static PHP_FUNCTION(zephir_parse_file)
3434
{
3535
size_t filepath_len = 0;
3636
size_t content_len = 0;
3737
char *content = NULL;
3838
char *filepath = NULL;
3939
#if PHP_VERSION_ID >= 70000
40+
zval error_msg;
4041
zval ret;
41-
zval error, *error_ptr = &error;
42-
zval **error_msg = &error_ptr;
43-
ZVAL_UNDEF(error_ptr);
42+
zval *e = &error_msg;
43+
zval *r = &ret;
4444
#else
45-
zval *ret = NULL;
46-
zval **error_msg = NULL;
45+
zval *error_msg;
46+
zval *e = error_msg;
47+
zval *ret;
48+
zval *r = ret;
4749
#endif
4850

4951
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &content, &content_len, &filepath, &filepath_len) == FAILURE) {
5052
RETURN_FALSE;
5153
}
5254

53-
#if PHP_VERSION_ID >= 70000
54-
xx_parse_program(&ret, content, content_len, filepath, error_msg);
55-
#else
55+
#if PHP_VERSION_ID < 70000
5656
MAKE_STD_ZVAL(ret);
57-
xx_parse_program(ret, content, content_len, filepath, error_msg);
57+
MAKE_STD_ZVAL(error_msg);
5858
#endif
59+
xx_parse_program(r, content, content_len, filepath, e);
5960

60-
#if PHP_VERSION_ID >= 70000
61-
if (Z_TYPE_P(error_ptr) != IS_UNDEF) {
62-
RETURN_ZVAL(error_ptr, 1, 1);
61+
if (Z_TYPE_P(e) == IS_ARRAY) {
62+
zval_ptr_dtor(&ret);
63+
RETURN_ZVAL(e, 1, 0);
6364
}
64-
RETURN_ZVAL(&ret, 1, 1);
65-
#else
66-
RETVAL_ZVAL(ret, 1, 1);
67-
#endif
65+
66+
assert(Z_TYPE_P(r) == IS_ARRAY);
67+
RETURN_ZVAL(r, 1, 1);
6868
}
6969
/* }}} */
7070

0 commit comments

Comments
 (0)