comparison libinterp/parse-tree/oct-parse.in.yy @ 17745:93b3d03b05e7 classdef

maint: periodic merge of default to classdef
author John W. Eaton <jwe@octave.org>
date Wed, 23 Oct 2013 22:29:06 -0400
parents dc4124992f0c d63878346099
children 97e49b588f5d
comparison
equal deleted inserted replaced
17735:dc4124992f0c 17745:93b3d03b05e7
1 /* 1 /*
2 2
3 Copyright (C) 1993-2012 John W. Eaton 3 Copyright (C) 1993-2013 John W. Eaton
4 Copyright (C) 2009 David Grundberg 4 Copyright (C) 2009 David Grundberg
5 Copyright (C) 2009-2010 VZLU Prague 5 Copyright (C) 2009-2010 VZLU Prague
6 6
7 This file is part of Octave. 7 This file is part of Octave.
8 8
105 // Forward declarations for some functions defined at the bottom of 105 // Forward declarations for some functions defined at the bottom of
106 // the file. 106 // the file.
107 107
108 static void yyerror (octave_base_parser& parser, const char *s); 108 static void yyerror (octave_base_parser& parser, const char *s);
109 109
110 // Finish building a statement.
111 template <class T>
112 static tree_statement *
113 make_statement (T *arg)
114 {
115 octave_comment_list *comment = octave_comment_buffer::get_comment ();
116
117 return new tree_statement (arg, comment);
118 }
119
120 #define ABORT_PARSE \ 110 #define ABORT_PARSE \
121 do \ 111 do \
122 { \ 112 { \
123 yyerrok; \ 113 yyerrok; \
124 if ((interactive || forced_interactive) \ 114 if ((interactive || forced_interactive) \
133 #define scanner lexer.scanner 123 #define scanner lexer.scanner
134 124
135 %} 125 %}
136 126
137 // Bison declarations. 127 // Bison declarations.
128
129 // The grammar currently has 14 shift/reduce conflicts. Ensure that
130 // we notice if that number changes.
131
132 %expect 14
138 133
139 // Don't add spaces around the = here; it causes some versions of 134 // Don't add spaces around the = here; it causes some versions of
140 // bison to fail to properly recognize the directive. 135 // bison to fail to properly recognize the directive.
141 136
142 %name-prefix="octave_" 137 %name-prefix="octave_"
416 | list1 sep statement 411 | list1 sep statement
417 { $$ = parser.append_statement_list ($1, $2, $3, true); } 412 { $$ = parser.append_statement_list ($1, $2, $3, true); }
418 ; 413 ;
419 414
420 statement : expression 415 statement : expression
421 { $$ = make_statement ($1); } 416 { $$ = parser.make_statement ($1); }
422 | command 417 | command
423 { $$ = make_statement ($1); } 418 { $$ = parser.make_statement ($1); }
424 | word_list_cmd 419 | word_list_cmd
425 { $$ = make_statement ($1); } 420 { $$ = parser.make_statement ($1); }
426 ; 421 ;
427 422
428 // ================= 423 // =================
429 // Word-list command 424 // Word-list command
430 // ================= 425 // =================
1685 stmt_begin : // empty 1680 stmt_begin : // empty
1686 { lexer.at_beginning_of_statement = true; } 1681 { lexer.at_beginning_of_statement = true; }
1687 ; 1682 ;
1688 1683
1689 stash_comment : // empty 1684 stash_comment : // empty
1690 { $$ = octave_comment_buffer::get_comment (); } 1685 { $$ = lexer.get_comment (); }
1691 ; 1686 ;
1692 1687
1693 parse_error : LEXICAL_ERROR 1688 parse_error : LEXICAL_ERROR
1694 { parser.bison_error ("parse error"); } 1689 { parser.bison_error ("parse error"); }
1695 | error 1690 | error
1760 delete stmt_list; 1755 delete stmt_list;
1761 1756
1762 delete &lexer; 1757 delete &lexer;
1763 } 1758 }
1764 1759
1765 void octave_base_parser::init (void)
1766 {
1767 LEXER = &lexer;
1768 }
1769
1770 void 1760 void
1771 octave_base_parser::reset (void) 1761 octave_base_parser::reset (void)
1772 { 1762 {
1773 endfunction_found = false; 1763 endfunction_found = false;
1774 autoloading = false; 1764 autoloading = false;
2445 { 2435 {
2446 tree_command *retval = 0; 2436 tree_command *retval = 0;
2447 2437
2448 if (end_token_ok (end_tok, token::unwind_protect_end)) 2438 if (end_token_ok (end_tok, token::unwind_protect_end))
2449 { 2439 {
2450 octave_comment_list *tc = octave_comment_buffer::get_comment (); 2440 octave_comment_list *tc = lexer.comment_buf.get_comment ();
2451 2441
2452 int l = unwind_tok->line (); 2442 int l = unwind_tok->line ();
2453 int c = unwind_tok->column (); 2443 int c = unwind_tok->column ();
2454 2444
2455 retval = new tree_unwind_protect_command (body, cleanup_stmts, 2445 retval = new tree_unwind_protect_command (body, cleanup_stmts,
2477 { 2467 {
2478 tree_command *retval = 0; 2468 tree_command *retval = 0;
2479 2469
2480 if (end_token_ok (end_tok, token::try_catch_end)) 2470 if (end_token_ok (end_tok, token::try_catch_end))
2481 { 2471 {
2482 octave_comment_list *tc = octave_comment_buffer::get_comment (); 2472 octave_comment_list *tc = lexer.comment_buf.get_comment ();
2483 2473
2484 int l = try_tok->line (); 2474 int l = try_tok->line ();
2485 int c = try_tok->column (); 2475 int c = try_tok->column ();
2486 2476
2487 tree_identifier *id = 0; 2477 tree_identifier *id = 0;
2497 if (expr && expr->is_identifier ()) 2487 if (expr && expr->is_identifier ())
2498 { 2488 {
2499 id = dynamic_cast<tree_identifier *> (expr); 2489 id = dynamic_cast<tree_identifier *> (expr);
2500 2490
2501 cleanup_stmts->pop_front (); 2491 cleanup_stmts->pop_front ();
2492
2493 stmt->set_expression (0);
2494 delete stmt;
2502 } 2495 }
2503 } 2496 }
2504 } 2497 }
2505 2498
2506 retval = new tree_try_catch_command (body, cleanup_stmts, id, 2499 retval = new tree_try_catch_command (body, cleanup_stmts, id,
2528 2521
2529 maybe_warn_assign_as_truth_value (expr); 2522 maybe_warn_assign_as_truth_value (expr);
2530 2523
2531 if (end_token_ok (end_tok, token::while_end)) 2524 if (end_token_ok (end_tok, token::while_end))
2532 { 2525 {
2533 octave_comment_list *tc = octave_comment_buffer::get_comment (); 2526 octave_comment_list *tc = lexer.comment_buf.get_comment ();
2534 2527
2535 lexer.looping--; 2528 lexer.looping--;
2536 2529
2537 int l = while_tok->line (); 2530 int l = while_tok->line ();
2538 int c = while_tok->column (); 2531 int c = while_tok->column ();
2556 tree_expression *expr, 2549 tree_expression *expr,
2557 octave_comment_list *lc) 2550 octave_comment_list *lc)
2558 { 2551 {
2559 maybe_warn_assign_as_truth_value (expr); 2552 maybe_warn_assign_as_truth_value (expr);
2560 2553
2561 octave_comment_list *tc = octave_comment_buffer::get_comment (); 2554 octave_comment_list *tc = lexer.comment_buf.get_comment ();
2562 2555
2563 lexer.looping--; 2556 lexer.looping--;
2564 2557
2565 int l = until_tok->line (); 2558 int l = until_tok->line ();
2566 int c = until_tok->column (); 2559 int c = until_tok->column ();
2583 2576
2584 bool parfor = tok_id == PARFOR; 2577 bool parfor = tok_id == PARFOR;
2585 2578
2586 if (end_token_ok (end_tok, parfor ? token::parfor_end : token::for_end)) 2579 if (end_token_ok (end_tok, parfor ? token::parfor_end : token::for_end))
2587 { 2580 {
2588 octave_comment_list *tc = octave_comment_buffer::get_comment (); 2581 octave_comment_list *tc = lexer.comment_buf.get_comment ();
2589 2582
2590 lexer.looping--; 2583 lexer.looping--;
2591 2584
2592 int l = for_tok->line (); 2585 int l = for_tok->line ();
2593 int c = for_tok->column (); 2586 int c = for_tok->column ();
2677 { 2670 {
2678 tree_if_command *retval = 0; 2671 tree_if_command *retval = 0;
2679 2672
2680 if (end_token_ok (end_tok, token::if_end)) 2673 if (end_token_ok (end_tok, token::if_end))
2681 { 2674 {
2682 octave_comment_list *tc = octave_comment_buffer::get_comment (); 2675 octave_comment_list *tc = lexer.comment_buf.get_comment ();
2683 2676
2684 int l = if_tok->line (); 2677 int l = if_tok->line ();
2685 int c = if_tok->column (); 2678 int c = if_tok->column ();
2686 2679
2687 if (list && ! list->empty ()) 2680 if (list && ! list->empty ())
2730 { 2723 {
2731 tree_switch_command *retval = 0; 2724 tree_switch_command *retval = 0;
2732 2725
2733 if (end_token_ok (end_tok, token::switch_end)) 2726 if (end_token_ok (end_tok, token::switch_end))
2734 { 2727 {
2735 octave_comment_list *tc = octave_comment_buffer::get_comment (); 2728 octave_comment_list *tc = lexer.comment_buf.get_comment ();
2736 2729
2737 int l = switch_tok->line (); 2730 int l = switch_tok->line ();
2738 int c = switch_tok->column (); 2731 int c = switch_tok->column ();
2739 2732
2740 if (list && ! list->empty ()) 2733 if (list && ! list->empty ())
2914 = new octave_user_function (lexer.symtab_context.curr_scope (), 2907 = new octave_user_function (lexer.symtab_context.curr_scope (),
2915 param_list, 0, body); 2908 param_list, 0, body);
2916 2909
2917 if (fcn) 2910 if (fcn)
2918 { 2911 {
2919 octave_comment_list *tc = octave_comment_buffer::get_comment (); 2912 octave_comment_list *tc = lexer.comment_buf.get_comment ();
2920 2913
2921 fcn->stash_trailing_comment (tc); 2914 fcn->stash_trailing_comment (tc);
2922 fcn->stash_fcn_end_location (end_fcn_stmt->line (), 2915 fcn->stash_fcn_end_location (end_fcn_stmt->line (),
2923 end_fcn_stmt->column ()); 2916 end_fcn_stmt->column ());
2924 } 2917 }
3185 3178
3186 if (nm != cls_name) 3179 if (nm != cls_name)
3187 bison_error ("invalid classdef definition, the class name must match the file name"); 3180 bison_error ("invalid classdef definition, the class name must match the file name");
3188 else if (end_token_ok (end_tok, token::classdef_end)) 3181 else if (end_token_ok (end_tok, token::classdef_end))
3189 { 3182 {
3190 octave_comment_list *tc = octave_comment_buffer::get_comment (); 3183 octave_comment_list *tc = lexer.comment_buf.get_comment ();
3191 3184
3192 int l = tok_val->line (); 3185 int l = tok_val->line ();
3193 int c = tok_val->column (); 3186 int c = tok_val->column ();
3194 3187
3195 retval = new tree_classdef (a, id, sc, body, lc, tc, 3188 retval = new tree_classdef (a, id, sc, body, lc, tc,
3216 { 3209 {
3217 tree_classdef_properties_block *retval = 0; 3210 tree_classdef_properties_block *retval = 0;
3218 3211
3219 if (end_token_ok (end_tok, token::properties_end)) 3212 if (end_token_ok (end_tok, token::properties_end))
3220 { 3213 {
3221 octave_comment_list *tc = octave_comment_buffer::get_comment (); 3214 octave_comment_list *tc = lexer.comment_buf.get_comment ();
3222 3215
3223 int l = tok_val->line (); 3216 int l = tok_val->line ();
3224 int c = tok_val->column (); 3217 int c = tok_val->column ();
3225 3218
3226 retval = new tree_classdef_properties_block (a, plist, lc, tc, l, c); 3219 retval = new tree_classdef_properties_block (a, plist, lc, tc, l, c);
3243 { 3236 {
3244 tree_classdef_methods_block *retval = 0; 3237 tree_classdef_methods_block *retval = 0;
3245 3238
3246 if (end_token_ok (end_tok, token::methods_end)) 3239 if (end_token_ok (end_tok, token::methods_end))
3247 { 3240 {
3248 octave_comment_list *tc = octave_comment_buffer::get_comment (); 3241 octave_comment_list *tc = lexer.comment_buf.get_comment ();
3249 3242
3250 int l = tok_val->line (); 3243 int l = tok_val->line ();
3251 int c = tok_val->column (); 3244 int c = tok_val->column ();
3252 3245
3253 retval = new tree_classdef_methods_block (a, mlist, lc, tc, l, c); 3246 retval = new tree_classdef_methods_block (a, mlist, lc, tc, l, c);
3270 { 3263 {
3271 tree_classdef_events_block *retval = 0; 3264 tree_classdef_events_block *retval = 0;
3272 3265
3273 if (end_token_ok (end_tok, token::events_end)) 3266 if (end_token_ok (end_tok, token::events_end))
3274 { 3267 {
3275 octave_comment_list *tc = octave_comment_buffer::get_comment (); 3268 octave_comment_list *tc = lexer.comment_buf.get_comment ();
3276 3269
3277 int l = tok_val->line (); 3270 int l = tok_val->line ();
3278 int c = tok_val->column (); 3271 int c = tok_val->column ();
3279 3272
3280 retval = new tree_classdef_events_block (a, elist, lc, tc, l, c); 3273 retval = new tree_classdef_events_block (a, elist, lc, tc, l, c);
3297 { 3290 {
3298 tree_classdef_enum_block *retval = 0; 3291 tree_classdef_enum_block *retval = 0;
3299 3292
3300 if (end_token_ok (end_tok, token::enumeration_end)) 3293 if (end_token_ok (end_tok, token::enumeration_end))
3301 { 3294 {
3302 octave_comment_list *tc = octave_comment_buffer::get_comment (); 3295 octave_comment_list *tc = lexer.comment_buf.get_comment ();
3303 3296
3304 int l = tok_val->line (); 3297 int l = tok_val->line ();
3305 int c = tok_val->column (); 3298 int c = tok_val->column ();
3306 3299
3307 retval = new tree_classdef_enum_block (a, elist, lc, tc, l, c); 3300 retval = new tree_classdef_enum_block (a, elist, lc, tc, l, c);
3647 } 3640 }
3648 3641
3649 return list; 3642 return list;
3650 } 3643 }
3651 3644
3645 // Finish building a statement.
3646 template <class T>
3647 tree_statement *
3648 octave_base_parser::make_statement (T *arg)
3649 {
3650 octave_comment_list *comment = lexer.get_comment ();
3651
3652 return new tree_statement (arg, comment);
3653 }
3654
3652 tree_statement_list * 3655 tree_statement_list *
3653 octave_base_parser::make_statement_list (tree_statement *stmt) 3656 octave_base_parser::make_statement_list (tree_statement *stmt)
3654 { 3657 {
3655 return new tree_statement_list (stmt); 3658 return new tree_statement_list (stmt);
3656 } 3659 }
3727 3730
3728 void 3731 void
3729 octave_push_parser::init (void) 3732 octave_push_parser::init (void)
3730 { 3733 {
3731 parser_state = yypstate_new (); 3734 parser_state = yypstate_new ();
3732
3733 octave_base_parser::init ();
3734 } 3735 }
3735 3736
3736 // Parse input from INPUT. Pass TRUE for EOF if the end of INPUT should 3737 // Parse input from INPUT. Pass TRUE for EOF if the end of INPUT should
3737 // finish the parse. 3738 // finish the parse.
3738 3739
3768 } 3769 }
3769 3770
3770 static void 3771 static void
3771 safe_fclose (FILE *f) 3772 safe_fclose (FILE *f)
3772 { 3773 {
3773 // FIXME -- comments at the end of an input file are
3774 // discarded (otherwise, they would be appended to the next
3775 // statement, possibly from the command line or another file, which
3776 // can be quite confusing).
3777
3778 octave_comment_list *tc = octave_comment_buffer::get_comment ();
3779
3780 delete tc;
3781
3782 if (f) 3774 if (f)
3783 fclose (static_cast<FILE *> (f)); 3775 fclose (static_cast<FILE *> (f));
3784 } 3776 }
3785 3777
3786 static octave_function * 3778 static octave_function *
4572 eval_string (const std::string& eval_str, bool silent, 4564 eval_string (const std::string& eval_str, bool silent,
4573 int& parse_status, int nargout) 4565 int& parse_status, int nargout)
4574 { 4566 {
4575 octave_value_list retval; 4567 octave_value_list retval;
4576 4568
4577 unwind_protect frame;
4578
4579 // octave_base_parser constructor sets this for us.
4580 frame.protect_var (LEXER);
4581
4582 octave_parser parser (eval_str); 4569 octave_parser parser (eval_str);
4583 4570
4584 do 4571 do
4585 { 4572 {
4586 parser.reset (); 4573 parser.reset ();
4587 4574
4588 parse_status = parser.run (); 4575 parse_status = parser.run ();
4589
4590 // Unmark forced variables.
4591 frame.run (1);
4592 4576
4593 if (parse_status == 0) 4577 if (parse_status == 0)
4594 { 4578 {
4595 if (parser.stmt_list) 4579 if (parser.stmt_list)
4596 { 4580 {