]> granicus.if.org Git - php/commitdiff
Ok, call me crazy, because I probably am.
authorZeev Suraski <zeev@php.net>
Fri, 23 Apr 1999 03:32:33 +0000 (03:32 +0000)
committerZeev Suraski <zeev@php.net>
Fri, 23 Apr 1999 03:32:33 +0000 (03:32 +0000)
Thread safe version now uses a C++ scanner object.  Works fully.

14 files changed:
Zend/FlexLexer.h
Zend/flex.skl
Zend/zend-scanner.h
Zend/zend-scanner.l
Zend/zend.h
Zend/zend_alloc.c
Zend/zend_alloc.h
Zend/zend_compile.h
Zend/zend_globals.h
Zend/zend_highlight.c
Zend/zend_highlight.h
Zend/zend_indent.c
Zend/zend_operators.h
Zend/zend_variables.h

index 20044810b83ef3fb3921599580fa67963416854a..549177b8b9acb8a6beba8c6eb9e9f31ac72a7f5b 100644 (file)
@@ -9,16 +9,17 @@
 // This code is derived from software contributed to Berkeley by
 // Kent Williams and Tom Epperly.
 //
-// Redistribution and use in source and binary forms are permitted provided
-// that: (1) source distributions retain this entire copyright notice and
-// comment, and (2) distributions including binaries display the following
-// acknowledgement:  ``This product includes software developed by the
-// University of California, Berkeley and its contributors'' in the
-// documentation or other materials provided with the distribution and in
-// all advertising materials mentioning features or use of this software.
-// Neither the name of the University nor the names of its contributors may
-// be used to endorse or promote products derived from this software without
-// specific prior written permission.
+// Redistribution and use in source and binary forms with or without
+// modification are permitted provided that: (1) source distributions retain
+// this entire copyright notice and comment, and (2) distributions including
+// binaries display the following acknowledgement:  ``This product includes
+// software developed by the University of California, Berkeley and its
+// contributors'' in the documentation or other materials provided with the
+// distribution and in all advertising materials mentioning features or use
+// of this software.  Neither the name of the University nor the names of
+// its contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+
 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 // WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
@@ -178,6 +179,8 @@ protected:
 
        int yy_more_flag;
        int yy_more_len;
+       int yy_more_offset;
+       int yy_prev_more_offset;
 };
 
 #endif
index b3b5098c141d3f71e8e4c6f066e1a8243620d670..7c87706948f216ad4e62982d31227c54fe4c4cfc 100644 (file)
@@ -135,7 +135,6 @@ extern FILE *yyin, *yyout;
                { \
                /* Undo effects of setting up yytext. */ \
                *yy_cp = yy_hold_char; \
-               YY_RESTORE_YY_MORE_OFFSET \
                yy_c_buf_p = yy_cp = yy_bp + n - YY_MORE_ADJ; \
                YY_DO_BEFORE_ACTION; /* set up yytext again */ \
                } \
@@ -525,7 +524,6 @@ do_action:  /* This label is used only to access EOF actions. */
 
                /* Undo the effects of YY_DO_BEFORE_ACTION. */
                *yy_cp = yy_hold_char;
-               YY_RESTORE_YY_MORE_OFFSET
 
                if ( yy_current_buffer->yy_buffer_status == YY_BUFFER_NEW )
                        {
index e28cf599bfd2e2b34ca9d79620984b737c12372f..d20d923556a32e31e76a92d04d35a7e594fb6a3d 100644 (file)
 #ifndef _LANGUAGE_SCANNER_H
 #define _LANGUAGE_SCANNER_H
 
+#ifdef ZTS
+class ZendFlexLexer : public yyFlexLexer
+{
+public:
+       int lex_scan(zval *zendlval CLS_DC);
+};
+
+#endif /* ZTS */
+
+
+#ifndef ZTS
 typedef struct {
        YY_BUFFER_STATE buffer_state;
        int state;
@@ -25,5 +36,11 @@ typedef struct {
        FILE *in;
        char *filename;
 } zend_lex_state;
+#else
+typedef struct {
+       ZendFlexLexer *ZFL;
+       istream *input_file;
+} zend_lex_state;
+#endif
 
 #endif
index 2c0ca69944cf3cf4fd5718cf0956c18f30d543cf..9f0813a907770940a1617dbd091ee464c367f1bd 100644 (file)
@@ -8,7 +8,7 @@
    +----------------------------------------------------------------------+
    | This source file is subject to the Zend license, that is bundled     |
    | with this package in the file LICENSE.  If you did not receive a     |
-   | copy of the Zend license, please mail us at zend@zend.com so we can  |
+   | copy of the Zen license, please mail us at zend@zend.com so we can  |
    | send you a copy immediately.                                         |
    +----------------------------------------------------------------------+
    | Authors: Andi Gutmans <andi@zend.com>                                |
 
 #include <errno.h>
 
+#ifdef ZTS
+#include <fstream.h>
+#endif
+
 #include "zend.h"
 #include "zend_alloc.h"
 #include "zend_compile.h"
 #include <stdarg.h>
 #endif
 
+#ifdef ZTS
+#define YY_DECL int ZendFlexLexer::lex_scan(zval *zendlval CLS_DC)
+#else
 #define YY_DECL int lex_scan(zval *zendlval CLS_DC)
+#endif
 #define ECHO { ZEND_WRITE( yytext, yyleng ); }
 
-#ifdef __cplusplus
+#ifdef ZTS
 #  define MY_INPUT yyinput
 #else
 #  define MY_INPUT input
@@ -86,6 +94,7 @@ do { \
 }
                
 
+BEGIN_EXTERN_C()
 void startup_scanner(CLS_D)
 {
        CG(heredoc) = NULL;
@@ -100,29 +109,26 @@ void shutdown_scanner(CLS_D)
                CG(heredoc_len)=0;
        }
 }
-
-
-void reset_scanner(CLS_D)
-{
-       YY_TLS_VARS
-
-       BEGIN(INITIAL);
-       CG(zend_lineno)=1;
-}
+END_EXTERN_C()
 
 
 static inline void save_lexical_state(zend_lex_state *lex_state CLS_DC)
 {
+#ifndef ZTS
        memcpy(&lex_state->buffer_state,&YY_CURRENT_BUFFER,sizeof(YY_BUFFER_STATE));
        lex_state->in = yyin;
        lex_state->lineno = CG(zend_lineno);
        lex_state->state = YYSTATE;
        lex_state->filename = zend_get_compiled_filename();
+#else
+       lex_state->ZFL = CG(ZFL);
+#endif
 }
 
 
-static inline void restore_lexical_state(zend_lex_state *lex_state CLS_DC)
+inline void restore_lexical_state(zend_lex_state *lex_state CLS_DC)
 {
+#ifndef ZTS
        YY_BUFFER_STATE original_buffer_state = YY_CURRENT_BUFFER;
 
        if (lex_state->buffer_state) {
@@ -136,11 +142,17 @@ static inline void restore_lexical_state(zend_lex_state *lex_state CLS_DC)
        CG(zend_lineno) = lex_state->lineno;
        BEGIN(lex_state->state);
        zend_restore_compiled_filename(lex_state->filename);
+#else
+       delete(CG(ZFL));
+       CG(ZFL) = lex_state->ZFL;
+#endif
 }
 
 
+BEGIN_EXTERN_C()
 inline int open_file_for_scanning(zend_file_handle *file_handle CLS_DC)
 {
+#ifndef ZTS
        FILE *tmp;
        YY_BUFFER_STATE buffer_state = YY_CURRENT_BUFFER;
 
@@ -167,8 +179,15 @@ inline int open_file_for_scanning(zend_file_handle *file_handle CLS_DC)
        
        zend_set_compiled_filename(file_handle->filename);
        return SUCCESS;
-}
+#else
+       ifstream *input_file = new ifstream(file_handle->filename);
+       CG(ZFL) = new ZendFlexLexer;
 
+       CG(ZFL)->switch_streams(input_file, &cout);
+       return SUCCESS;
+#endif
+}
+END_EXTERN_C()
 
 
 ZEND_API zend_op_array *compile_files(int mark_as_ref CLS_DC, int file_count, ...)
@@ -202,6 +221,7 @@ ZEND_API zend_op_array *v_compile_files(int mark_as_ref CLS_DC, int file_count,
                }
                if (open_file_for_scanning(file_handle CLS_CC)==FAILURE) {
                        zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, file_handle->filename);
+
                        destroy_op_array(op_array);
                        efree(op_array);
                        retval = NULL;
@@ -212,7 +232,9 @@ ZEND_API zend_op_array *v_compile_files(int mark_as_ref CLS_DC, int file_count,
                                retval = NULL;
                                break;
                        } else {
+#ifndef ZTS
                                fclose(yyin);
+#endif
                                restore_lexical_state(&original_lex_state CLS_CC);
                                CG(active_op_array) = original_active_op_array;
                                retval = op_array;
@@ -253,18 +275,21 @@ zend_op_array *compile_filename(zval *filename CLS_DC)
 
 static inline int prepare_string_for_scanning(zval *str)
 {
+#ifndef ZTS
        /* enforce two trailing NULLs for flex... */
        str->value.str.val = (char *) erealloc(str->value.str.val,str->value.str.len+2);
        str->value.str.val[str->value.str.len+1]=0;
 
        yyin=NULL;
        yy_scan_buffer(str->value.str.val, str->value.str.len+2);
+#endif
        return SUCCESS;
 }
 
 
 zend_op_array *compile_string(zval *source_string CLS_DC)
 {
+#ifndef ZTS
        zend_lex_state original_lex_state;
        zend_op_array *op_array = (zend_op_array *) emalloc(sizeof(zend_op_array));
        zend_op_array *original_active_op_array = CG(active_op_array);
@@ -299,9 +324,13 @@ zend_op_array *compile_string(zval *source_string CLS_DC)
        zval_dtor(&tmp);
 
        return retval;
+#else
+       return NULL;
+#endif
 }
 
 
+BEGIN_EXTERN_C()
 int require_filename(char *filename CLS_DC)
 {
        zend_file_handle file_handle;
@@ -322,11 +351,14 @@ int require_file(zend_file_handle *file_handle CLS_DC)
                return FAILURE;
        }
        zendparse(CLS_C);
+#ifndef ZTS
        fclose(yyin);
+#endif
        restore_lexical_state(&original_lex_state CLS_CC);
        return SUCCESS;
 }
 
+
 int highlight_file(char *filename, zend_syntax_highlighter_ini *syntax_highlighter_ini)
 {
        zend_lex_state original_lex_state;
@@ -341,7 +373,9 @@ int highlight_file(char *filename, zend_syntax_highlighter_ini *syntax_highlight
                return FAILURE;
        }
        zend_highlight(syntax_highlighter_ini);
+#ifndef ZTS
        fclose(yyin);
+#endif
        restore_lexical_state(&original_lex_state CLS_CC);
        return SUCCESS;
 }
@@ -366,6 +400,40 @@ int highlight_string(zval *str, zend_syntax_highlighter_ini *syntax_highlighter_
 }
 
 
+#ifdef ZTS
+int lex_scan(zval *zendlval CLS_DC)
+{
+       return CG(ZFL)->lex_scan(zendlval CLS_CC);
+}
+
+
+int yyFlexLexer::yylex()
+{
+       fprintf(stderr, "Error:  yyFlexLexer::yylex() called\n");
+       return -1;
+}
+
+#endif
+
+
+
+#ifdef ZTS
+const char *zend_get_zendtext(CLS_D)
+{
+       return CG(ZFL)->YYText();
+}
+
+
+int zend_get_zendleng(CLS_D)
+{
+       return CG(ZFL)->YYLeng();
+}
+#endif
+
+
+END_EXTERN_C()
+
+
 /* redefine YY_INPUT to handle urls for win32*/
 #if 0 /*WIN32|WINNT*/
 #define YY_INPUT(buf,result,max_size) \
@@ -422,9 +490,6 @@ ESCAPED_AND_WHITESPACE [\n\t\r #'.:;,()|^&+-/*=%!~<>?@]+
 %option noyylineno
 %option noyywrap
 %%
-%{
-TLS_VARS;
-%}
 
 <IN_SCRIPTING>"exit" {
        return T_EXIT;
index 140dc65fed388dc97f0faf1790004cfec08f7a6d..677a48697e1aeef57233580b8d63288258d2c68b 100644 (file)
 
 #define ZEND_VERSION "0.80A"
 
+
+#ifdef __cplusplus
+#define BEGIN_EXTERN_C() extern "C" {
+#define END_EXTERN_C() }
+#else
+#define BEGIN_EXTERN_C()
+#define END_EXTERN_C()
+#endif
+
+
 #include <stdio.h>
 
 /*
@@ -174,13 +184,15 @@ ZEND_API extern char *undefined_variable_string;
 #define ZEND_PUTS(str)                                 zend_write((str), strlen((str)))
 #define ZEND_PUTC(c)                                   zend_write(&(c), 1), (c)
 
+BEGIN_EXTERN_C()
 extern ZEND_API int (*zend_printf)(const char *format, ...);
 extern ZEND_API int (*zend_write)(const char *str, uint str_length);
 extern ZEND_API void (*zend_error)(int type, const char *format, ...);
 extern FILE *(*zend_fopen)(const char *filename);
-extern void (*zend_message_dispatcher)(long message, void *data);
 extern void (*zend_block_interruptions)();
 extern void (*zend_unblock_interruptions)();
+extern void (*zend_message_dispatcher)(long message, void *data);
+END_EXTERN_C()
 
 
 void zenderror(char *error);
@@ -195,8 +207,6 @@ extern zend_utility_values zend_uv;
 #define HANDLE_UNBLOCK_INTERRUPTIONS()         if (zend_unblock_interruptions) { zend_unblock_interruptions(); }
 
 
-
-
 /* Messages for applications of Zend */
 #define ZMSG_ENABLE_TRACK_VARS                 1L
 #define ZMSG_FAILED_INCLUDE_FOPEN              2L
index 0e165c41c27ff2faac3442047717dcb8bf15ba46..9326ce876ab63ef149a9ea8cb707c6e4c01d4d94 100644 (file)
@@ -86,6 +86,7 @@ static zend_alloc_globals alloc_globals;
        p->pLast = (mem_header *) NULL;
 
 
+
 #if ZEND_DEBUG
 ZEND_API void *_emalloc(size_t size, char *filename, uint lineno)
 #else
@@ -515,7 +516,6 @@ ZEND_API void _persist_alloc(void *ptr)
        HANDLE_UNBLOCK_INTERRUPTIONS();
 }
 
-
 /*
  * Local variables:
  * tab-width: 4
index 85492a2c43b9690901baf2fb041413ef21431343..161c4685f408ed86772097e4012025ba75aaebad 100644 (file)
@@ -51,6 +51,8 @@ typedef union {
 
 ZEND_API char *zend_strndup(const char *s, unsigned int length);
 
+BEGIN_EXTERN_C()
+
 #if ZEND_DEBUG
 ZEND_API void *_emalloc(size_t size,char *filename,uint lineno);
 ZEND_API void _efree(void *ptr,char *filename,uint lineno);
@@ -109,6 +111,8 @@ ZEND_API void _full_mem_check(int silent, char *filename, uint lineno);
 #endif
 
 
+END_EXTERN_C()
+
 #endif
 
 /*
index 15539e7a683c661c539dc536bdd9a969d3617ade..4d5d3ae5976f73e35e6d1721afbc627005ab55df 100644 (file)
@@ -181,10 +181,10 @@ typedef struct _zend_file_handle {
 void init_compiler(CLS_D ELS_DC);
 void shutdown_compiler(CLS_D);
 
+BEGIN_EXTERN_C()
 extern ZEND_API zend_op_array *(*zend_compile_files)(int mark_as_ref CLS_DC, int file_count, ...);
 
 int lex_scan(zval *zendlval CLS_DC);
-void reset_scanner(CLS_D);
 void startup_scanner(CLS_D);
 void shutdown_scanner(CLS_D);
 
@@ -192,6 +192,12 @@ ZEND_API void zend_set_compiled_filename(char *new_compiled_filename);
 ZEND_API void zend_restore_compiled_filename(char *original_compiled_filename);
 ZEND_API char *zend_get_compiled_filename();
 
+#ifdef ZTS
+const char *zend_get_zendtext(CLS_D);
+int zend_get_zendleng(CLS_D);
+#endif
+
+END_EXTERN_C()
 
 /* parser-driven code generators */
 void do_binary_op(int op, znode *result, znode *op1, znode *op2 CLS_DC);
@@ -317,6 +323,7 @@ void do_extended_fcall_end(CLS_D);
 
 
 /* helper functions in zend-scanner.l */
+BEGIN_EXTERN_C()
 ZEND_API int require_file(zend_file_handle *file_handle CLS_DC);       
 ZEND_API int require_filename(char *filename CLS_DC);                          
 ZEND_API zend_op_array *compile_files(int mark_as_ref CLS_DC, int file_count, ...);
@@ -324,12 +331,15 @@ ZEND_API zend_op_array *v_compile_files(int mark_as_ref CLS_DC, int file_count,
 ZEND_API zend_op_array *compile_string(zval *source_string CLS_DC);    
 ZEND_API zend_op_array *compile_filename(zval *filename CLS_DC);
 inline int open_file_for_scanning(zend_file_handle *file_handle CLS_DC);
+END_EXTERN_C()
 
 #define INITIAL_OP_ARRAY_SIZE 64
 
 
+BEGIN_EXTERN_C()
 ZEND_API void init_op_array(zend_op_array *op_array, int initial_ops_size);
 ZEND_API void destroy_op_array(zend_op_array *op_array);
+END_EXTERN_C()
 
 ZEND_API void destroy_zend_function(zend_function *function);
 ZEND_API void destroy_zend_class(zend_class_entry *ce);
@@ -337,8 +347,10 @@ zend_op *get_next_op(zend_op_array *op_array CLS_DC);
 int get_next_op_number(zend_op_array *op_array);
 int print_class(zend_class_entry *class_entry);
 void print_op_array(zend_op_array *op_array, int optimizations);
+BEGIN_EXTERN_C()
 int pass_two(zend_op_array *op_array);
 void pass_include_eval(zend_op_array *op_array);
+END_EXTERN_C()
 zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array);
 
 
index 978341af1566e48f2236daab2094a74071e1df52..621d45879d86c78a8c8c2287f150bc07084be529 100644 (file)
 #include "zend_hash.h"
 #include "zend_llist.h"
 
-
 /* Define ZTS if you want a thread-safe Zend */
 /*#undef ZTS*/
 
 #ifdef ZTS
 #include "../TSRM/TSRM.h"
 
+#ifdef __cplusplus
+class ZendFlexLexer;
+#endif
+
+BEGIN_EXTERN_C()
 extern int compiler_globals_id;
 extern int executor_globals_id;
 extern int alloc_globals_id;
+END_EXTERN_C()
+
 #endif
 
 typedef struct _zend_compiler_globals zend_compiler_globals;
@@ -53,7 +59,9 @@ typedef struct _zend_alloc_globals zend_alloc_globals;
 # define CLS_FETCH()   zend_compiler_globals *compiler_globals = (zend_compiler_globals *) ts_resource(compiler_globals_id)
 # define YYPARSE_PARAM compiler_globals
 # define YYLEX_PARAM compiler_globals
+BEGIN_EXTERN_C()
 int zendparse(void *compiler_globals);
+END_EXTERN_C()
 #else
 # define CLS_D
 # define CLS_DC
@@ -138,6 +146,14 @@ struct _zend_compiler_globals {
        /* For extensions support */
        unsigned char extended_info;    /* generate extension information for debugger/profiler */
        unsigned char handle_op_arrays; /* run op_arrays through op_array handlers */
+
+#ifdef ZTS
+#ifdef __cplusplus
+       ZendFlexLexer *ZFL;
+#else
+       void *ZFL;
+#endif
+#endif
 };
 
 
index 651c3a688e8d9bc873f614594f7d7a313e86f34e..142a665d72a4313f789de4cb4fca83639cec270c 100644 (file)
 #include "zend_ptr_stack.h"
 #include "zend_globals.h"
 
+#ifndef ZTS
 extern char *zendtext;
 extern int zendleng;
-
+#else
+#define zendtext ((char *) zend_get_zendtext(CLS_C))
+#define zendleng zend_get_zendleng(CLS_C)
+#endif
 
 static void html_putc(char c)
 {
@@ -63,7 +67,6 @@ static void html_puts(char *s, uint len)
 
 
 
-
 void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini)
 {
        zval token;
index 77e7bc418cca7dab544ab6594ef176a642bd387c..0769139c62ed04cedd084f744027f4c18a83da3b 100644 (file)
@@ -33,10 +33,12 @@ typedef struct {
        char *highlight_keyword;
 } zend_syntax_highlighter_ini;
 
-void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini);
 
+BEGIN_EXTERN_C()
+void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini);
 int highlight_file(char *filename, zend_syntax_highlighter_ini *syntax_highlighter_ini);
 int highlight_string(zval *str, zend_syntax_highlighter_ini *syntax_highlighter_ini);
+END_EXTERN_C()
 
 extern zend_syntax_highlighter_ini syntax_highlighter_ini;
 
index a532bac81af9f13cc7e4193344c574aff3dfe79c..63e1e3143c4355e096aca08d85afed6fae6c58ea 100644 (file)
 #include "zend_compile.h"
 #include "zend_indent.h"
 
+#ifndef ZTS
 extern char *zendtext;
 extern int zendleng;
+#else
+#define zendtext ((char *) zend_get_zendtext(CLS_C))
+#define zendleng zend_get_zendleng(CLS_C)
+#endif
+
 
 static void handle_whitespace(int *emit_whitespace)
 {
index 873cbcb7ee231d270ae686557b6710aee22da858..ee0eb96b3d9b14d967668b64f747802f18cfa8fc 100644 (file)
@@ -45,6 +45,7 @@ ZEND_API int is_smaller_or_equal_function(zval *result, zval *op1, zval *op2);
 ZEND_API int increment_function(zval *op1);
 ZEND_API int decrement_function(zval *op2);
 
+BEGIN_EXTERN_C()
 ZEND_API void convert_scalar_to_number(zval *op);
 ZEND_API void convert_to_string(zval *op);
 ZEND_API void convert_to_long(zval *op);
@@ -55,6 +56,7 @@ ZEND_API void convert_to_object(zval *op);
 ZEND_API int add_char_to_string(zval *result, zval *op1, zval *op2);
 ZEND_API int add_string_to_string(zval *result, zval *op1, zval *op2);
 ZEND_API void convert_to_double(zval *op);
+END_EXTERN_C()
 
 ZEND_API int zval_is_true(zval *op);
 ZEND_API int compare_function(zval *result, zval *op1, zval *op2);
index a4525dae96b131cf668a372e1a216748bb5ea04a..c72990d7f5d3a7b83461bbe6679e1c58ed7ae9e5 100644 (file)
 
 ZEND_API int zend_print_variable(zval *var);
 
+BEGIN_EXTERN_C()
 ZEND_API int zval_copy_ctor(zval *zvalue);
 ZEND_API void zval_dtor(zval *zvalue);
+END_EXTERN_C()
+
 ZEND_API void zval_ptr_dtor(zval **zval_ptr);
 void zval_add_ref(zval **p);