@@ -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
0 commit comments