vallettea / koala

Transpose your Excel calculations into python for better performances and scaling.
GNU General Public License v3.0
143 stars 60 forks source link

Double negative/minus operator causes IndexError #255

Open jpp-0 opened 4 years ago

jpp-0 commented 4 years ago

Formulae such as --("A"="B") cause IndexError in koala/ast/__init__.py:build_ast due to "double operator" Workaround until fix: replace -- with 1* in workbook

Example minimum reproduction: double_minus.xlsx

    def build_ast(expression, debug = False):
        """build an AST from an Excel formula expression in reverse polish notation"""
        #use a directed graph to store the tree
        G = DiGraph()
        stack = []

        for n in expression:
            # Since the graph does not maintain the order of adding nodes/edges
            # add an extra attribute 'pos' so we can always sort to the correct order
            if isinstance(n,OperatorNode):
                if n.ttype == "operator-infix":
                    arg2 = stack.pop()
                    arg1 = stack.pop()
                    # Hack to write the name of sheet in 2argument address
                    if(n.tvalue == ':'):
                        if '!' in arg1.tvalue and arg2.ttype == 'operand' and '!' not in arg2.tvalue:
                            arg2.tvalue = arg1.tvalue.split('!')[0] + '!' + arg2.tvalue

                    G.add_node(arg1,pos = 1)
                    G.add_node(arg2,pos = 2)
                    G.add_edge(arg1, n)
                    G.add_edge(arg2, n)
                else:
>                   arg1 = stack.pop()
E                   IndexError: pop from empty list

.venv/lib/python3.7/site-packages/koala/ast/__init__.py:295: IndexError
victorjmarin commented 3 years ago

The same issue is discussed here https://github.com/dgorissen/pycel/issues/105.

Setting unary negation to be 'right' associative instead of 'left' should fix the problem. https://github.com/vallettea/koala/blob/master/koala/tokenizer.py#L710