Exemplo n.º 1
0
def parse_type_dec(line, lineno, var_name, type_spec):
    """Constructs a L{ast_extensions.TypeDec} from the type declaration in the
    provided line. The name of the variable and the name of the type are passed
    since they are stored when matching the typedec regex against the line and
    it would be wasteful to discard that information.

    @type line: str
    @param line: the line of source code containing the type declaration.
    @type lineno: int
    @param lineno: the index of this line in the orginial source code file.
    @type var_name: str
    @param var_name: the name of the identifier whose type is being declared.
    @type type_name: str
    @param type_name: the name of the type which is being declared.
    @rtype: L{ast_extensions.TypeDec}
    @return: a L{ast_extensions.TypeDec} node for the declaration in the given
        line.
    """

    col = line.index(var_name)
    name_node = ast.Name(ctx=TypeStore(), id=var_name, lineno=lineno,
                         col_offset=col)

    col_offset = line.index("#:")

    return TypeDec([name_node], TypeSpecParser.parse(type_spec), lineno,
                   col_offset)
Exemplo n.º 2
0
    def __init__(self, targets, t, line, col = None):
        """Creates a L{TypeDec} node with the supplied parameters.

        @type targets: list of ast.Name
        @param targets: The list of variables which are having their types
            declared. 
        @type t: PytyType
        @param t: The type which the C{targets} are declared as. If a string is
            provided, then a PytyType object will be created based on this
            string.
        @type line: int
        @param line: The line number of the source code for the declaration.
        @type col: int
        @param col: The column number of the source code for the declaration.
        """
    
        self.targets = targets
        self.lineno = line
        if col is not None:
            self.col_offset = col

        if type(t) == str:
            self.t = TypeSpecParser.parse(t)
        else:
            self.t = t

        # these are instance variables provided by AST nodes to allow traversal
        # / parsing of the nodes.
        self._fields = ("targets", "t")
        self._attributes = ("lineno", "col_offset")
Exemplo n.º 3
0
def make_function_tdec(partypes, rtype, lineno, func_name):
    # FIXME: this should be somehow built into the parser; this is a hack.
    t = TypeSpecParser.parse("(" + ', '.join(partype) + ") -> " + rtype)

    return TypeDec([func_name], t, lineno)