Mercurial > octave
changeset 33312:2ef1b84a5887
new convenience functions for the parser
* parse.h, oct-parse.yy (base_parser::make_function_def_list,
base_parser::append_function_def_list): New functions.
(fcn_list1): Use them to simplify actions.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 04 Apr 2024 03:26:19 -0400 |
parents | 5b13f73bda6f |
children | c1ab55458c3a |
files | libinterp/parse-tree/oct-parse.yy libinterp/parse-tree/parse.h |
diffstat | 2 files changed, 52 insertions(+), 75 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/parse-tree/oct-parse.yy Thu Apr 04 00:59:46 2024 -0400 +++ b/libinterp/parse-tree/oct-parse.yy Thu Apr 04 03:26:19 2024 -0400 @@ -490,15 +490,9 @@ ; fcn_list1 : function - { - octave::tree_statement *stmt = parser.make_statement ($1); - $$ = parser.make_statement_list (stmt); - } + { $$ = parser.make_function_def_list ($1); } | fcn_list1 opt_sep function - { - octave::tree_statement *stmt = parser.make_statement ($3); - $$ = parser.append_statement_list ($1, $2, stmt, false); - } + { $$ = parser.append_function_def_list ($1, $2, $3); } ; statement : expression @@ -824,9 +818,7 @@ } | power_expr '(' arg_list ')' { - $$ = parser.make_index_expression ($1, $2, $3, $4, '('); - - if (! $$) + if (! ($$ = parser.make_index_expression ($1, $2, $3, $4, '('))) { // make_index_expression deleted $1 and $3. YYABORT; @@ -834,9 +826,7 @@ } | power_expr '{' '}' { - $$ = parser.make_index_expression ($1, $2, nullptr, $3, '{'); - - if (! $$) + if (! ($$ = parser.make_index_expression ($1, $2, nullptr, $3, '{'))) { // make_index_expression deleted $1. YYABORT; @@ -844,9 +834,7 @@ } | power_expr '{' arg_list '}' { - $$ = parser.make_index_expression ($1, $2, $3, $4, '{'); - - if (! $$) + if (! ($$ = parser.make_index_expression ($1, $2, $3, $4, '{'))) { // make_index_expression deleted $1 and $3. YYABORT; @@ -872,9 +860,7 @@ colon_expr : oper_expr ':' oper_expr { - $$ = parser.make_colon_expression ($1, $2, $3); - - if (! $$) + if (! ($$ = parser.make_colon_expression ($1, $2, $3))) { // make_colon_expression deleted $1 and $3. YYABORT; @@ -882,9 +868,7 @@ } | oper_expr ':' oper_expr ':' oper_expr { - $$ = parser.make_colon_expression ($1, $2, $3, $4, $5); - - if (! $$) + if (! ($$ = parser.make_colon_expression ($1, $2, $3, $4, $5))) { // make_colon_expression deleted $1, $3, and $5. YYABORT; @@ -920,15 +904,13 @@ assign_lhs : simple_expr { - $$ = parser.validate_matrix_for_assignment ($1); - - if ($$) - { lexer.m_looking_at_matrix_or_assign_lhs = false; } - else + if (! ($$ = parser.validate_matrix_for_assignment ($1))) { // validate_matrix_for_assignment deleted $1. YYABORT; } + + lexer.m_looking_at_matrix_or_assign_lhs = false; } ; @@ -1311,7 +1293,6 @@ lexer.m_looking_at_parameter_list = false; lexer.m_arguments_is_keyword = true; lexer.m_looking_for_object_index = false; - $$ = $1; } ; @@ -1362,9 +1343,7 @@ param_list2 : param_list_elt { $$ = parser.make_parameter_list (octave::tree_parameter_list::in, $1); } | param_list2 ',' param_list_elt - { - $$ = parser.append_parameter_list ($1, $2, $3); - } + { $$ = parser.append_parameter_list ($1, $2, $3); } ; param_list_elt : decl_elt @@ -1421,13 +1400,9 @@ ; return_list1 : identifier - { - $$ = parser.make_parameter_list (octave::tree_parameter_list::out, $1); - } + { $$ = parser.make_parameter_list (octave::tree_parameter_list::out, $1); } | return_list1 ',' identifier - { - $$ = parser.append_parameter_list ($1, $2, $3); - } + { $$ = parser.append_parameter_list ($1, $2, $3); } ; // ======================= @@ -1502,10 +1477,10 @@ function_beg : push_fcn_symtab FUNCTION { + if (lexer.m_reading_classdef_file || lexer.m_parsing_classdef) + lexer.m_maybe_classdef_get_set_method = true; + $$ = $2; - if (lexer.m_reading_classdef_file - || lexer.m_parsing_classdef) - lexer.m_maybe_classdef_get_set_method = true; } ; @@ -1600,9 +1575,7 @@ ; function_body : at_first_executable_stmt opt_list - { - $$ = $2; - } + { $$ = $2; } | function_body1 opt_sep at_first_executable_stmt opt_list { OCTAVE_YYUSE ($2); @@ -1613,9 +1586,7 @@ at_first_executable_stmt : // empty - { - lexer.m_arguments_is_keyword = false; - } + { lexer.m_arguments_is_keyword = false; } ; function_body1 : arguments_block @@ -1793,32 +1764,23 @@ attr_list1 : attr { $$ = parser.make_classdef_attribute_list ($1); } | attr_list1 ',' attr - { - $$ = parser.append_classdef_attribute ($1, $2, $3); - } + { $$ = parser.append_classdef_attribute ($1, $2, $3); } ; attr : identifier { $$ = parser.make_classdef_attribute ($1); } | identifier '=' expression - { - $$ = parser.make_classdef_attribute ($1, $2, $3); - } + { $$ = parser.make_classdef_attribute ($1, $2, $3); } | '~' identifier - { - $$ = parser.make_not_classdef_attribute ($1, $2); - } + { $$ = parser.make_not_classdef_attribute ($1, $2); } | '!' identifier - { - $$ = parser.make_not_classdef_attribute ($1, $2); - } + { $$ = parser.make_not_classdef_attribute ($1, $2); } ; superclass_list : // empty { lexer.m_parsing_classdef_decl = false; lexer.m_parsing_classdef_superclass = false; - $$ = nullptr; } | superclass_list1 opt_sep @@ -1827,20 +1789,15 @@ lexer.m_parsing_classdef_decl = false; lexer.m_parsing_classdef_superclass = false; - $$ = $1; } ; superclass_list1 : EXPR_LT superclass - { - $$ = parser.make_classdef_superclass_list ($1, $2); - } + { $$ = parser.make_classdef_superclass_list ($1, $2); } | superclass_list1 EXPR_AND superclass - { - $$ = parser.append_classdef_superclass ($1, $2, $3); - } + { $$ = parser.append_classdef_superclass ($1, $2, $3); } ; superclass : FQ_IDENT @@ -1999,9 +1956,7 @@ ; method_decl : method_decl1 - { - $$ = parser.finish_classdef_external_method ($1); - } + { $$ = parser.finish_classdef_external_method ($1); } | return_list '=' { lexer.m_defining_fcn++; @@ -2137,9 +2092,7 @@ ; class_enum : identifier '(' expression ')' - { - $$ = parser.make_classdef_enum ($1, $2, $3, $4); - } + { $$ = parser.make_classdef_enum ($1, $2, $3, $4); } ; // ============= @@ -5075,6 +5028,22 @@ return list_append (list, stmt); } + tree_statement_list * + base_parser::make_function_def_list (tree_function_def *fcn_def) + { + tree_statement *stmt = make_statement (fcn_def); + + return new tree_statement_list (stmt); + } + + tree_statement_list * + base_parser::append_function_def_list (tree_statement_list *list, char, tree_function_def *fcn_def) + { + tree_statement *stmt = make_statement (fcn_def); + + return list_append (list, stmt); + } + tree_argument_list * base_parser::make_argument_list (tree_expression *expr) {
--- a/libinterp/parse-tree/parse.h Thu Apr 04 00:59:46 2024 -0400 +++ b/libinterp/parse-tree/parse.h Thu Apr 04 03:26:19 2024 -0400 @@ -607,8 +607,16 @@ // Append a statement to an existing statement list. OCTINTERP_API tree_statement_list * - append_statement_list (tree_statement_list *list, char sep, - tree_statement *stmt, bool warn_missing_semi); + append_statement_list (tree_statement_list *list, char sep, tree_statement *stmt, bool warn_missing_semi); + + // Create a statement list containing only function_def commands. + OCTINTERP_API tree_statement_list * + make_function_def_list (tree_function_def *fcn_def); + + // Append a function_def command to an existing statement list (that + // should contain only other function_def commands). + OCTINTERP_API tree_statement_list * + append_function_def_list (tree_statement_list *list, char sep, tree_function_def *fcn_def); OCTINTERP_API tree_argument_list * make_argument_list (tree_expression *expr);