Pro West
Pro West

Reputation: 409

Extract function arguments in PLSQL calls with Antlr4/Python4

I am trying to extract the arguments in function/procedures calls in Oracle PLSQL using Antlr4 and Python3.

I trap the enterFunction_argument event (is the right term?) and I think the context could have the arguments under arguments, argument, function_argument or function_arguments depending of if the function has no, one or more than one argument. So I am trying to cater for all these bit I keep getting the error

def enterFunction_argument(self, ctx:PlSqlParser.Function_argumentContext):

        print(ctx.toStringTree(recog=parser))
        print(dir(ctx))
        print("")

        args = None

        if hasattr(ctx, 'arguments'):
            args = ctx.arguments()
        elif hasattr(ctx, 'argument'):
            args = [ ctx.argument() ]
        elif hasattr( ctx, 'function_argument'):
            fa = ctx.function_argument()
            if hasattr( fa, 'arguments'):
                args = ctx.function_argument().arguments()
            elif hasattr( fa, 'argument'):
                args = [ ctx.function_argument().argument() ]

        for arg in args:
            argName = arg.expression().getChild(0).getText().lower()  # Errors
            argName = arg.getText().lower()  # Errors

I get errors trying to get the arg name:

argName = arg.getText()
AttributeError: 'list' object has no attribute 'getText'
argName = arg.expression().getChild(0).getText().lower()
AttributeError: 'list' object has no attribute 'expression'

The tree view of the source code pertaining to this code is:

   ║     ║     ║  ║  ║     ╚═ function_argument
   ║     ║     ║  ║  ║        ╠═ "(" (LEFT_PAREN)
   ║     ║     ║  ║  ║        ╠═ argument
   ║     ║     ║  ║  ║        ║  ╚═ expression
   ║     ║     ║  ║  ║        ║     ╚═ logical_expression
   ║     ║     ║  ║  ║        ║        ╚═ unary_logical_expression
   ║     ║     ║  ║  ║        ║           ╚═ multiset_expression
   ║     ║     ║  ║  ║        ║              ╚═ relational_expression
   ║     ║     ║  ║  ║        ║                 ╚═ compound_expression
   ║     ║     ║  ║  ║        ║                    ╚═ concatenation
   ║     ║     ║  ║  ║        ║                       ╚═ model_expression
   ║     ║     ║  ║  ║        ║                          ╚═ unary_expression
   ║     ║     ║  ║  ║        ║                             ╚═ atom
   ║     ║     ║  ║  ║        ║                                ╚═ general_element
   ║     ║     ║  ║  ║        ║                                   ╚═ general_element_part
   ║     ║     ║  ║  ║        ║                                      ╚═ id_expression
   ║     ║     ║  ║  ║        ║                                         ╚═ regular_id
   ║     ║     ║  ║  ║        ║                                            ╚═ "l_name" (REGULAR_ID)
   ║     ║     ║  ║  ║        ╠═ "," (COMMA)
   ║     ║     ║  ║  ║        ╠═ argument
   ║     ║     ║  ║  ║        ║  ╚═ expression
   ║     ║     ║  ║  ║        ║     ╚═ logical_expression
   ║     ║     ║  ║  ║        ║        ╚═ unary_logical_expression
   ║     ║     ║  ║  ║        ║           ╚═ multiset_expression
   ║     ║     ║  ║  ║        ║              ╚═ relational_expression
   ║     ║     ║  ║  ║        ║                 ╚═ compound_expression
   ║     ║     ║  ║  ║        ║                    ╚═ concatenation
   ║     ║     ║  ║  ║        ║                       ╚═ model_expression
   ║     ║     ║  ║  ║        ║                          ╚═ unary_expression
   ║     ║     ║  ║  ║        ║                             ╚═ atom
   ║     ║     ║  ║  ║        ║                                ╚═ constant
   ║     ║     ║  ║  ║        ║                                   ╚═ quoted_string
   ║     ║     ║  ║  ║        ║                                      ╚═ "'Started'" (CHAR_STRING)
   ║     ║     ║  ║  ║        ╚═ ")" (RIGHT_PAREN)

The PLSQL source code:

CREATE OR REPLACE
PACKAGE BODY pa_monthly_sales_upd  IS

   PROCEDURE pr_upd_comp_monthly_sales ( p_date IN DATE DEFAULT TRUNC(SYSDATE-1) ) IS
      l_name varchar2(30) := 'pr_upd_comp_monthly_sales';
      l_fin_year NUMBER;
      l_fin_month NUMBER;
   BEGIN

      pa_logging.pr_log_info( l_name, 'Started' );  --< This line is highlighted in this question

      pa_calendar.pr_get_cal_month ( p_date, l_fin_year, l_fin_month );

      pa_logging.pr_log_info( l_name, 'Completed' );

    END pr_upd_comp_monthly_sales;

END pa_monthly_sales_upd;
/

The output I want is l_name and "Started".

Upvotes: 0

Views: 42

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170278

In Python, .getText() and .getType() are simply .text and .type. At least, when the node is a terminal node, I believe. Not sure if this works on all nodes.

Upvotes: 1

Related Questions