Exemple #1
0
def visit_future(node):
    """Accumulates a set of compiler flags for the compiler __future__ imports.

  Returns an instance of FutureFeatures which encapsulates the flags and the
  line number of the last valid future import parsed. A downstream parser can
  use the latter to detect invalid future imports that appear too late in the
  file.
  """
    # If this is the module node, do an initial pass through the module body's
    # statements to detect future imports and process their directives (i.e.,
    # set compiler flags), and detect ones that don't appear at the beginning of
    # the file. The only things that can proceed a future statement are other
    # future statements and/or a doc string.
    assert isinstance(node, ast.Module)
    ff = FutureFeatures()
    done = False
    found_docstring = False
    for node in node.body:
        if isinstance(node, ast.ImportFrom):
            modname = node.module
            if modname == '__future__':
                if done:
                    raise util.ParseError(node, late_future)
                ff.parser_flags |= import_from_future(node)
                ff.future_lineno = node.lineno
            else:
                done = True
        elif isinstance(node, ast.Expr) and not found_docstring:
            e = node.value
            if not isinstance(e, ast.Str):  # pylint: disable=simplifiable-if-statement
                done = True
            else:
                found_docstring = True
        else:
            done = True
    return ff
Exemple #2
0
 def _node_not_implemented(self, node):
   msg = 'node not yet implemented: ' + type(node).__name__
   raise util.ParseError(node, msg)
Exemple #3
0
 def generic_visit(self, node):
   msg = 'expression node not yet implemented: ' + type(node).__name__
   raise util.ParseError(node, msg)
Exemple #4
0
 def visit_Continue(self, node):
   if not self.block.loop_stack:
     raise util.ParseError(node, "'continue' not in loop")
   self._write_py_context(node.lineno)
   self.writer.write('goto Label{}'.format(self.block.top_loop().start_label))
Exemple #5
0
 def generic_visit(self, node):
   msg = 'node not yet implemented: {}'.format(type(node).__name__)
   raise util.ParseError(node, msg)
Exemple #6
0
 def visit_Continue(self, node):
   if not self.block.loop_stack:
     raise util.ParseError(node, "'continue' not in loop")
   self._write_py_context(node.lineno)
   self.writer.write('continue')