Mercurial > hg > octave-nkf
diff libinterp/parse-tree/oct-parse.yy @ 16149:49dfba4fd3c5
use pure parser and reentrant lexer interfaces
Making the Octave parser and lexer properly reentrant (and perhaps
eventually thread safe as well) is still a work in progress. With the
current set of changes the parser and lexer still use many global
variables, so these changes alone do NOT make the Octave parser
reentrant unless you take care to properly save and restore (typically
with an unwind_protect object) relevant global values before and after
calling the parser. Even if global variables are properly saved and
restored, the parser will NOT be thread safe.
* lex.ll: Use %option reentrant an %option bison-bridge.
(yylval): Delete macro.
(YY_EXTRA_TYPE, curr_lexer): New macros. Undefine curr_lexer
(YY_FATAL_ERROR): Update decl for reentrant scanner.
(lexical_feedback::reset): Update call to yyrestart for reentrant
scanner interface.
(lexical_feedback::fatal_error): Update call to yy_fatal_error for
reentrant scanner interface.
(lexical_feedback::text_yyinput): Update calls to yyinput and yyunput
for reentrant scanner interface.
(lexical_feedback::flex_yyleng): Use function interface to access
yyleng.
(lexical_feedback::flex_yytext): Use function interface to access
yytext.
(lexical_feedback::push_token, lexical_feedback::current_token):
Use function interface to access yylval.
* oct-parse.yy: Use %define api.pure, %parse-param, and %lex-param
options.
(curr_lexer): Define for syntax rules section.
(scanner): New macro.
* oct-parse.yy: Include oct-parse.h.
(octave_lex): Declare.
(yyerror): Update declaration for pure parser.
* parse.h (octave_lex): Delete decl.
* oct-parse.yy (octave_parser::run): Pass pointer to octave_parser
object to octave_parse.
* lex.ll (lexical_feedback::octave_read): Call fatal_error directly
instead of using YY_FATAL_ERROR.
* oct-parse.yy (parse_fcn_file): Pass line and column info for lexter
to gobble_leading_whitespace. Access prep_for_script_file,
prep_for_function_file, parsing_class_method, input_line_number, and
current_input_column through curr_parser.
* parse.h, oct-parse.yy (YY_BUFFER_STATE, create_buffer,
current_buffer, switch_to_buffer, delete_buffer, clear_all_buffers):
Delete.
* toplev.cc (main_loop): Don't create new buffer for lexer.
* input.cc (get_debug_input): Likewise.
* oct-parse.yy (eval_string, parse_fcn_file): Likewise.
* octave.cc (octave_initialize_interpreter): Likewise.
* input.cc (get_debug_input): Likewise.
* oct-parse.yy (eval_string, parse_fcn_file): Create parser as needed.
* octave.cc (octave_initialize_interpreter): Likewise.
* input.cc (get_debug_input): Likewise.
* input.cc (input_even_hook): Allow function to run even if currently
defining a function.
* lex.h, lex.ll (curr_lexer): Delete global variable.
* parse.h, oct-parse.yy (octave_parser::curr_lexer): New data member.
(octave_parser::octave_parser): Create lexer here.
(curr_parser): Delete global variable.
* toplev.cc (main_loop): Don't protect global curr_lexer and
curr_parser variables.
* oct-parse.yy (eval_string, parse_fcn_file): Likewise.
* input.cc (get_debug_input): Likewise.
* lex.h, lex.ll (curr_lexer): Delete global variable.
* parse.h, oct-parse.yy (CURR_LEXER): New temporary global.
(octave_parser::octave_parser): Set global CURR_LEXER here.
* toplev.cc (main_loop): Protect CURR_LEXER prior to constructing
new parser object.
* input.cc (get_debug_input): Likewise.
* oct-parse.yy (eval_string, parse_fcn_file): Likewise.
* lex.h, lex.ll (lexical_feedback::scanner): New data member.
(lexical_feedback::init): Create it. Call yylex_set_extra to store
pointer to lexical_feedback object in scanner data.
(lexical_feedback::~lexical_feedback): Delete it.
* lex.ll (YYG): New macro.
(lexical_feedback::reset, lexical_feedback::prep_for_script_file,
lexical_feedback::prep_for_function_file,
lexical_feedback::process_comment,
lexical_feedback::handle_close_bracket,
lexical_feedback::handle_identifier, lexical_feedback::lexer_debug):
Use it to access scanner data.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 27 Feb 2013 18:49:16 -0500 |
parents | ed8ce5da525f |
children | e309eb7940be |
line wrap: on
line diff
--- a/libinterp/parse-tree/oct-parse.yy +++ b/libinterp/parse-tree/oct-parse.yy @@ -77,6 +77,15 @@ #include "utils.h" #include "variables.h" +// oct-parse.h must be included after pt-all.h +#include <oct-parse.h> + +extern int octave_lex (YYSTYPE *, void *); + +// Global access to currently active lexer. +// FIXME -- to be removed after more parser+lexer refactoring. +lexical_feedback *CURR_LEXER = 0; + #if defined (GNULIB_NAMESPACE) // Calls to the following functions appear in the generated output from // Bison without the namespace tag. Redefine them so we will use them @@ -86,9 +95,6 @@ #define malloc GNULIB_NAMESPACE::malloc #endif -// The state of the parser. -octave_parser *curr_parser = 0; - // Buffer for help text snagged from function files. std::stack<std::string> help_buf; @@ -155,7 +161,7 @@ // Forward declarations for some functions defined at the bottom of // the file. -static void yyerror (const char *s); +static void yyerror (octave_parser *curr_parser, const char *s); // Finish building a statement. template <class T> @@ -182,6 +188,9 @@ } \ while (0) +#define curr_lexer curr_parser->curr_lexer +#define scanner curr_lexer->scanner + %} // Bison declarations. @@ -191,6 +200,19 @@ %name-prefix="octave_" +// We are using the pure parser interface and the reentrant lexer +// interface but the Octave parser and lexer are NOT properly +// reentrant because both still use many global variables. It should be +// safe to create a parser object and call it while anotehr parser +// object is active (to parse a callback function while the main +// interactive parser is waiting for input, for example) if you take +// care to properly save and restore (typically with an unwind_protect +// object) relevant global values before and after the nested call. + +%define api.pure +%parse-param { octave_parser *curr_parser } +%lex-param { void *scanner } + %union { // The type of the basic tokens returned by the lexer. @@ -1493,8 +1515,10 @@ // Generic error messages. +#undef curr_lexer + static void -yyerror (const char *s) +yyerror (octave_parser *curr_parser, const char *s) { curr_parser->bison_error (s); } @@ -1502,7 +1526,7 @@ int octave_parser::run (void) { - return octave_parse (); + return octave_parse (this); } // Error mesages for mismatched end tokens. @@ -3281,7 +3305,7 @@ if (eof) break; - txt = curr_lexer->grab_comment_block (stdio_reader, true, eof); + txt = CURR_LEXER->grab_comment_block (stdio_reader, true, eof); if (txt.empty ()) break; @@ -3377,17 +3401,19 @@ { bool eof; - frame.protect_var (curr_lexer); - curr_lexer = new lexical_feedback (); - frame.add_fcn (lexical_feedback::cleanup, curr_lexer); - - frame.protect_var (curr_parser); - curr_parser = new octave_parser (); + // octave_parser constructor sets this for us. + frame.protect_var (CURR_LEXER); + + octave_parser *curr_parser = new octave_parser (); frame.add_fcn (octave_parser::cleanup, curr_parser); curr_parser->reset (); - std::string help_txt = gobble_leading_white_space (ffile, eof); + std::string help_txt + = gobble_leading_white_space + (ffile, eof, + curr_parser->curr_lexer->input_line_number, + curr_parser->curr_lexer->current_input_column); if (! help_txt.empty ()) help_buf.push (help_txt); @@ -3439,14 +3465,6 @@ reading_script_file = true; } - YY_BUFFER_STATE old_buf = current_buffer (); - YY_BUFFER_STATE new_buf = create_buffer (ffile); - - frame.add_fcn (switch_to_buffer, old_buf); - frame.add_fcn (delete_buffer, new_buf); - - switch_to_buffer (new_buf); - frame.protect_var (primary_fcn_ptr); primary_fcn_ptr = 0; @@ -3460,11 +3478,11 @@ help_buf.push (help_txt); if (reading_script_file) - curr_lexer->prep_for_script_file (); + curr_parser->curr_lexer->prep_for_script_file (); else - curr_lexer->prep_for_function_file (); - - curr_lexer->parsing_class_method = ! dispatch_type.empty (); + curr_parser->curr_lexer->prep_for_function_file (); + + curr_parser->curr_lexer->parsing_class_method = ! dispatch_type.empty (); frame.protect_var (global_command); @@ -3486,9 +3504,11 @@ } else { + int l = curr_parser->curr_lexer->input_line_number; + int c = curr_parser->curr_lexer->current_input_column; + tree_statement *end_of_script - = curr_parser->make_end ("endscript", curr_lexer->input_line_number, - curr_lexer->current_input_column); + = curr_parser->make_end ("endscript", l, c); curr_parser->make_script (0, end_of_script); @@ -4187,12 +4207,10 @@ unwind_protect frame; - frame.protect_var (curr_lexer); - curr_lexer = new lexical_feedback (); - frame.add_fcn (lexical_feedback::cleanup, curr_lexer); - - frame.protect_var (curr_parser); - curr_parser = new octave_parser (); + // octave_parser constructor sets this for us. + frame.protect_var (CURR_LEXER); + + octave_parser *curr_parser = new octave_parser (); frame.add_fcn (octave_parser::cleanup, curr_parser); frame.protect_var (get_input_from_eval_string); @@ -4220,14 +4238,6 @@ current_eval_string = s; - YY_BUFFER_STATE old_buf = current_buffer (); - YY_BUFFER_STATE new_buf = create_buffer (0); - - frame.add_fcn (switch_to_buffer, old_buf); - frame.add_fcn (delete_buffer, new_buf); - - switch_to_buffer (new_buf); - do { curr_parser->reset ();