Example #1
0
    def _visit_Import(self, stmt):
        expr = []
        for name in stmt.names:
            imp = self._visit(name)
            if isinstance(imp, AsName):
                source = AsName(self._treat_import_source(imp.name, 0), imp.target)
            else:
                source = self._treat_import_source(imp, 0)
            import_line = Import(source)
            import_line.set_fst(stmt)
            self.insert_import(import_line)
            expr.append(import_line)

        if len(expr)==1:
            return expr[0]
        else:
            expr = CodeBlock(expr)
            expr.set_fst(stmt)
            return expr
Example #2
0
    def _visit_CommentMultiLine(self, stmt):

        exprs = []
        # if annotated comment
        for com in stmt.s.split('\n'):
            if com.startswith('#$'):
                env = com[2:].lstrip()
                if env.startswith('omp'):
                    exprs.append(omp_parse(stmts=com))
                elif env.startswith('acc'):
                    exprs.append(acc_parse(stmts=com))
                elif env.startswith('header'):
                    expr = hdr_parse(stmts=com)
                    if isinstance(expr, MetaVariable):

                        # a metavar will not appear in the semantic stage.
                        # but can be used to modify the ast

                        self._metavars[str(expr.name)] = str(expr.value)
                        expr = EmptyNode()
                    else:
                        expr.set_fst(stmt)

                    exprs.append(expr)
                else:
                    errors.report(PYCCEL_INVALID_HEADER,
                                  symbol=stmt,
                                  severity='error')
            else:

                txt = com[1:].lstrip()
                exprs.append(Comment(txt))

        if len(exprs) == 1:
            return exprs[0]
        else:
            return CodeBlock(exprs)
Example #3
0
    def _visit_Module(self, stmt):
        """ Visits the ast and splits the result into elements relevant for the module or the program"""
        prog = []
        mod = []
        start = []
        current_file = start
        targets = []
        n_empty_lines = 0
        is_prog = False
        body = [self._visit(v) for v in stmt.body]

        # Define the names of the module and program
        # The module name allows it to be correctly referenced from an import command
        current_mod_name = os.path.splitext(os.path.basename(
            self._filename))[0]
        prog_name = 'prog_' + current_mod_name

        new_body = []
        for i in body:
            if isinstance(i, CodeBlock):
                new_body += list(i.body)
            else:
                new_body.append(i)

        body = new_body
        for v in body:

            if n_empty_lines > 3:
                current_file = start
            if isinstance(v, (FunctionDef, ClassDef)):
                # Functions and classes are always defined in a module
                n_empty_lines = 0
                mod.append(v)
                targets.append(v.name)
                current_file = mod
                im = Import(source=current_mod_name, target=[v.name])
                prog.append(im)
            elif isinstance(v, (Header, Comment, CommentBlock)):
                # Headers and Comments are defined in the same block as the following object
                n_empty_lines = 0
                current_file = start
                current_file.append(v)
            elif isinstance(v, (NewLine, EmptyNode)):
                # EmptyNodes are defined in the same block as the previous line
                current_file.append(v)
                n_empty_lines += 1
            elif isinstance(v, Import):
                # Imports are defined in both the module and the program
                n_empty_lines = 0
                mod.append(v)
                prog.append(v)
            else:
                # Everything else is defined in a module
                is_prog = True
                n_empty_lines = 0
                prog.append(v)
                current_file = prog

            # If the current file is now a program or a module. Add headers and comments before the line we just read
            if len(start) > 0 and current_file is not start:
                current_file[-1:-1] = start
                start = []
        if len(start) > 0:
            mod.extend(start)

        mod_code = CodeBlock(mod) if len(targets) > 0 else None
        if is_prog:
            prog_code = CodeBlock(prog)
            prog_code.set_fst(stmt)
        else:
            prog_code = None
            # If the file only contains headers
            if mod_code is None:
                mod_code = CodeBlock(mod)
        assert (mod_code is not None or prog_code is not None)
        code = ParserResult(program=prog_code,
                            module=mod_code,
                            prog_name=prog_name,
                            mod_name=current_mod_name)
        code.set_fst(stmt)
        code._fst.lineno = 1
        code._fst.col_offset = 1
        return code