Ejemplo n.º 1
0
def p_error(p):
    if p is None:
        raise_exception(SyntaxError, "Unexpected EOF", new_lexer)
    if p.type == "COMMENT":
        # print "Discarded comment", p.value
        parser.errok()
        return
    raise_exception(SyntaxError, ('Unexpected "%s" (parser)' % p.value),
                    new_lexer)
Ejemplo n.º 2
0
def p_error(p):
    if p is None:
        raise_exception(SyntaxError, "Unexpected EOF", new_lexer)
    if p.type == "COMMENT":
        # print "Discarded comment", p.value
        parser.errok()
        return
    raise_exception(SyntaxError,
                    ('Unexpected "%s" (parser)' % p.value),
                    new_lexer)
Ejemplo n.º 3
0
def p_for_stmt(p):
    """
    for_stmt : FOR ident EQ expr SEMI stmt_list END_STMT
             | FOR LPAREN ident EQ expr RPAREN SEMI stmt_list END_STMT
             | FOR matrix EQ expr SEMI stmt_list END_STMT
    """
    if len(p) == 8:
        if not isinstance(p[2], node.ident):
            raise_exception(SyntaxError, "Not implemented: for loop", new_lexer)
        p[2].props = "I"  # I= for-loop iteration variable
        p[0] = node.for_stmt(ident=p[2], expr=p[4], stmt_list=p[6])
Ejemplo n.º 4
0
def p_for_stmt(p):
    """
    for_stmt : FOR ident EQ expr SEMI stmt_list END_STMT
             | FOR LPAREN ident EQ expr RPAREN SEMI stmt_list END_STMT
             | FOR matrix EQ expr SEMI stmt_list END_STMT
    """
    if len(p) == 8:
        if not isinstance(p[2], node.ident):
            raise_exception(SyntaxError, "Not implemented: for loop", new_lexer)
        p[2].props = "I"  # I= for-loop iteration variable
        p[0] = node.for_stmt(ident=p[2], expr=p[4], stmt_list=p[6])
Ejemplo n.º 5
0
def p_expr2(p):
    """expr2 : expr AND expr
             | expr ANDAND expr
             | expr BACKSLASH expr
             | expr COLON expr
             | expr DIV expr
             | expr DOT expr
             | expr DOTDIV expr
             | expr DOTDIVEQ expr
             | expr DOTEXP expr
             | expr DOTMUL expr
             | expr DOTMULEQ expr
             | expr EQEQ expr
             | expr POW expr
             | expr EXP expr
             | expr EXPEQ expr
             | expr GE expr
             | expr GT expr
             | expr LE expr
             | expr LT expr
             | expr MINUS expr
             | expr MUL expr
             | expr NE expr
             | expr OR expr
             | expr OROR expr
             | expr PLUS expr
             | expr EQ expr
             | expr MULEQ expr
             | expr DIVEQ expr
             | expr MINUSEQ expr
             | expr PLUSEQ expr
             | expr OREQ expr
             | expr ANDEQ expr
    """
    if p[2] == "=":
        if p[1].__class__ is node.let:
            raise_exception(SyntaxError,
                            "Not implemented assignment as expression",
                            new_lexer)
        # The algorithm, which decides if an
        # expression F(X)
        # is arrayref or funcall, is implemented in
        # resolve.py, except the following lines up
        # to XXX. These lines handle the case where
        # an undefined array is updated:
        #    >>> clear a
        #    >>> a[1:10]=123
        # Though legal in matlab, these lines
        # confuse the algorithm, which thinks that
        # the undefined variable is a function name.
        # To prevent the confusion, we mark these
        # nodes arrayref as early as during the parse
        # phase.
        if p[1].__class__ is node.funcall:
            # A(B) = C
            p[1].__class__ = node.arrayref
        elif p[1].__class__ is node.matrix:
            # [A1(B1) A2(B2) ...] = C
            for e in p[1].args:
                if e.__class__ is node.funcall:
                    e.__class__ = node.arrayref
        # XXX

        if isinstance(p[1], node.getfield):
            # import pdb;pdb.set_trace()
            # A.B=C  setfield(A,B,C)
            p[0] = node.setfield(p[1].args[0], p[1].args[1], p[3])
        else:
            # assert len(p[1].args) > 0
            ret = p[1].args if isinstance(p[1], node.matrix) else p[1]
            p[0] = node.let(ret=ret,
                            args=p[3],
                            lineno=p.lineno(2),
                            lexpos=p.lexpos(2))

            if isinstance(p[1], node.matrix):
                # TBD: mark idents as "P" - persistent
                if p[3].__class__ not in (node.ident,
                                          node.funcall):  #, p[3].__class__
                    raise_exception(SyntaxError, "multi-assignment", new_lexer)
                if p[3].__class__ is node.ident:
                    # [A1(B1) A2(B2) ...] = F     implied F()
                    # import pdb; pdb.set_trace()
                    p[3] = node.funcall(func_expr=p[3], args=node.expr_list())
                # [A1(B1) A2(B2) ...] = F(X)
                p[3].nargout = len(p[1].args[0])
    elif p[2] == "*":
        p[0] = node.funcall(func_expr=node.ident("dot"),
                            args=node.expr_list([p[1], p[3]]))
    elif p[2] == ".*":
        p[0] = node.funcall(func_expr=node.ident("multiply"),
                            args=node.expr_list([p[1], p[3]]))


#    elif p[2] == "." and isinstance(p[3],node.expr) and p[3].op=="parens":
#        p[0] = node.getfield(p[1],p[3].args[0])
#        raise SyntaxError(p[3],p.lineno(3),p.lexpos(3))
    elif p[2] == ":" and isinstance(p[1], node.expr) and p[1].op == ":":
        # Colon expression means different things depending on the
        # context.  As an array subscript, it is a slice; otherwise,
        # it is a call to the "range" function, and the parser can't
        # tell which is which.  So understanding of colon expressions
        # is put off until after "resolve".
        p[0] = p[1]
        p[0].args.insert(1, p[3])
    else:
        p[0] = node.expr(op=p[2], args=node.expr_list([p[1], p[3]]))
Ejemplo n.º 6
0
def p_expr2(p):
    """expr2 : expr AND expr
             | expr ANDAND expr
             | expr BACKSLASH expr
             | expr COLON expr
             | expr DIV expr
             | expr DOT expr
             | expr DOTDIV expr
             | expr DOTDIVEQ expr
             | expr DOTEXP expr
             | expr DOTMUL expr
             | expr DOTMULEQ expr
             | expr EQEQ expr
             | expr POW expr
             | expr EXP expr
             | expr EXPEQ expr
             | expr GE expr
             | expr GT expr
             | expr LE expr
             | expr LT expr
             | expr MINUS expr
             | expr MUL expr
             | expr NE expr
             | expr OR expr
             | expr OROR expr
             | expr PLUS expr
             | expr EQ expr
             | expr MULEQ expr
             | expr DIVEQ expr
             | expr MINUSEQ expr
             | expr PLUSEQ expr
             | expr OREQ expr
             | expr ANDEQ expr
    """
    if p[2] == "=":
        if p[1].__class__ is node.let:
            raise_exception(SyntaxError,
                            "Not implemented assignment as expression",
                            new_lexer)
        # The algorithm, which decides if an
        # expression F(X)
        # is arrayref or funcall, is implemented in
        # resolve.py, except the following lines up
        # to XXX. These lines handle the case where
        # an undefined array is updated:
        #    >>> clear a
        #    >>> a[1:10]=123
        # Though legal in matlab, these lines
        # confuse the algorithm, which thinks that
        # the undefined variable is a function name.
        # To prevent the confusion, we mark these
        # nodes arrayref as early as during the parse
        # phase.
        if p[1].__class__ is node.funcall:
            # A(B) = C
            p[1].__class__ = node.arrayref
        elif p[1].__class__ is node.matrix:
            # [A1(B1) A2(B2) ...] = C
            for e in p[1].args:
                if e.__class__ is node.funcall:
                    e.__class__ = node.arrayref
        # XXX

        if isinstance(p[1], node.getfield):
            # import pdb;pdb.set_trace()
            # A.B=C  setfield(A,B,C)
            p[0] = node.setfield(p[1].args[0], p[1].args[1], p[3])
        else:
            # assert len(p[1].args) > 0
            ret = p[1].args if isinstance(p[1], node.matrix) else p[1]
            p[0] = node.let(ret=ret,
                            args=p[3],
                            lineno=p.lineno(2),
                            lexpos=p.lexpos(2))

            if isinstance(p[1], node.matrix):
                # TBD: mark idents as "P" - persistent
                if p[3].__class__ not in (node.ident, node.funcall
                                          ):  #, p[3].__class__
                    raise_exception(SyntaxError,
                                    "multi-assignment",
                                    new_lexer)
                if p[3].__class__ is node.ident:
                    # [A1(B1) A2(B2) ...] = F     implied F()
                    # import pdb; pdb.set_trace()
                    p[3] = node.funcall(func_expr=p[3], args=node.expr_list())
                # [A1(B1) A2(B2) ...] = F(X)
                p[3].nargout = len(p[1].args[0])
    elif p[2] == "*":
        p[0] = node.funcall(
            func_expr=node.ident("dot"), args=node.expr_list([p[1], p[3]]))
    elif p[2] == ".*":
        p[0] = node.funcall(
            func_expr=node.ident("multiply"),
            args=node.expr_list([p[1], p[3]]))

#    elif p[2] == "." and isinstance(p[3],node.expr) and p[3].op=="parens":
#        p[0] = node.getfield(p[1],p[3].args[0])
#        raise SyntaxError(p[3],p.lineno(3),p.lexpos(3))
    elif p[2] == ":" and isinstance(p[1], node.expr) and p[1].op == ":":
        # Colon expression means different things depending on the
        # context.  As an array subscript, it is a slice; otherwise,
        # it is a call to the "range" function, and the parser can't
        # tell which is which.  So understanding of colon expressions
        # is put off until after "resolve".
        p[0] = p[1]
        p[0].args.insert(1, p[3])
    else:
        p[0] = node.expr(op=p[2], args=node.expr_list([p[1], p[3]]))