Esempio n. 1
0
 def __init__(self, source, node=None, does_match=None):
     if node is None:
         try:
             node = ast.parse(source)
         except SyntaxError:
             # needed to parse expression containing := operator
             node = ast.parse("(" + source + ")")
     if does_match is None:
         self.does_match = self._simple_does_match
     else:
         self.does_match = does_match
     self._init_using_ast(node, source)
Esempio n. 2
0
 def check_syntax(self, code):
     try:
         tree = parse(code)
     except (SyntaxError, IndentationError, ValueError) as e:
         return {"lineno": e.lineno, "offset": e.offset, "text": e.text}
     else:
         return Checker(tree).messages
Esempio n. 3
0
def get_patched_ast(source, sorted_children=False):
    """Adds ``region`` and ``sorted_children`` fields to nodes

    Adds ``sorted_children`` field only if `sorted_children` is True.

    """
    return patch_ast(ast.parse(source), source, sorted_children)
Esempio n. 4
0
 def check_syntax(self, code):
     try:
         tree = parse(code)
     except (SyntaxError, IndentationError, ValueError) as e:
         return {"lineno": e.lineno, "offset": e.offset, "text": e.text}
     else:
         return Checker(tree).messages
Esempio n. 5
0
def eval_str2(holding_scope, name):
    try:
        # parenthesizing for handling cases like 'a_var.\nattr'
        node = ast.parse("(%s)" % name)
    except SyntaxError:
        raise BadIdentifierError("Not a resolvable python identifier selected.")
    return eval_node2(holding_scope, node)
Esempio n. 6
0
def eval_str2(holding_scope, name):
    try:
        # parenthesizing for handling cases like 'a_var.\nattr'
        node = ast.parse('(%s)' % name)
    except SyntaxError:
        raise BadIdentifierError('Not a resolvable python identifier selected.')
    return eval_node2(holding_scope, node)
Esempio n. 7
0
def get_primary_and_pyname_in_scope(holding_scope, name):
    try:
        # parenthesizing for handling cases like 'a_var.\nattr'
        node = ast.parse('(%s)' % name)
    except SyntaxError:
        raise BadIdentifierError('Not a resolvable python identifier selected.')
    return get_primary_and_result(holding_scope, node)
Esempio n. 8
0
def get_patched_ast(source, sorted_children=False):
    """Adds ``region`` and ``sorted_children`` fields to nodes

    Adds ``sorted_children`` field only if `sorted_children` is True.

    """
    return patch_ast(ast.parse(source), source, sorted_children)
Esempio n. 9
0
 def warnings(self, source):
     result = []
     try:
         node = ast.parse(source)
     except SyntaxError:
         return []
     except SyntaxWarning, e:
         result.append((e.lineno, e.msg))
Esempio n. 10
0
 def __init__(self, pycore, resource=None, force_errors=False):
     self.resource = resource
     init_dot_py = self._get_init_dot_py()
     if init_dot_py is not None:
         ast_node = pycore.project.get_pymodule(init_dot_py, force_errors=force_errors).get_ast()
     else:
         ast_node = ast.parse("\n")
     super(PyPackage, self).__init__(pycore, ast_node, resource)
Esempio n. 11
0
 def __init__(self, source, node=None, does_match=None):
     if node is None:
         node = ast.parse(source)
     if does_match is None:
         self.does_match = self._simple_does_match
     else:
         self.does_match = does_match
     self._init_using_ast(node, source)
Esempio n. 12
0
 def __init__(self, source, node=None, does_match=None):
     if node is None:
         node = ast.parse(source)
     if does_match is None:
         self.does_match = self._simple_does_match
     else:
         self.does_match = does_match
     self._init_using_ast(node, source)
Esempio n. 13
0
    def run(self):
        errors = []

        try:
            tree = parse(self.code, filename=self.filename)
        except (SyntaxError, IndentationError, ValueError), e:
            self.syntax_error = e
            sublime.set_timeout(self.handle_syntax_error, 0)
            return
Esempio n. 14
0
    def run(self):
        errors = []

        try:
            tree = parse(self.code, filename=self.filename)
        except (SyntaxError, IndentationError, ValueError), e:
            self.syntax_error = e
            sublime.set_timeout(self.handle_syntax_error, 0)
            return
 def __init__(self, pycore, resource=None, force_errors=False):
     self.resource = resource
     init_dot_py = self._get_init_dot_py()
     if init_dot_py is not None:
         ast_node = pycore.project.get_pymodule(
             init_dot_py, force_errors=force_errors).get_ast()
     else:
         ast_node = ast.parse('\n')
     super(PyPackage, self).__init__(pycore, ast_node, resource)
Esempio n. 16
0
 def _create_pattern(self, expression):
     expression = self._replace_wildcards(expression)
     node = ast.parse(expression)
     # Getting Module.Stmt.nodes
     nodes = node.body
     if len(nodes) == 1 and isinstance(nodes[0], ast.Expr):
         # Getting Discard.expr
         wanted = nodes[0].value
     else:
         wanted = nodes
     return wanted
Esempio n. 17
0
 def _create_pattern(self, expression):
     expression = self._replace_wildcards(expression)
     node = ast.parse(expression)
     # Getting Module.Stmt.nodes
     nodes = node.body
     if len(nodes) == 1 and isinstance(nodes[0], ast.Expr):
         # Getting Discard.expr
         wanted = nodes[0].value
     else:
         wanted = nodes
     return wanted
Esempio n. 18
0
 def __init__(self, pycore, source=None, resource=None, force_errors=False):
     ignore = pycore.project.prefs.get("ignore_syntax_errors", False)
     syntax_errors = force_errors or not ignore
     try:
         source, node = self._init_source(pycore, source, resource)
     except exceptions.ModuleSyntaxError:
         if syntax_errors:
             raise
         else:
             source = "\n"
             node = ast.parse("\n")
     self.source_code = source
     self.star_imports = []
     self.visitor_class = _GlobalVisitor
     self.coding = fscommands.read_str_coding(self.source_code)
     super(PyModule, self).__init__(pycore, node, resource)
Esempio n. 19
0
 def __init__(self, pycore, source=None,
              resource=None, force_errors=False):
     ignore = pycore.project.prefs.get('ignore_syntax_errors', False)
     syntax_errors = force_errors or not ignore
     try:
         source, node = self._init_source(pycore, source, resource)
     except exceptions.ModuleSyntaxError:
         if syntax_errors:
             raise
         else:
             source = '\n'
             node = ast.parse('\n')
     self.source_code = source
     self.star_imports = []
     self.visitor_class = _GlobalVisitor
     super(PyModule, self).__init__(pycore, node, resource)
Esempio n. 20
0
 def _init_source(self, pycore, source_code, resource):
     filename = 'string'
     if resource:
         filename = resource.path
     try:
         if source_code is None:
             source_bytes = resource.read_bytes()
             source_code = fscommands.file_data_to_unicode(source_bytes)
         else:
             if isinstance(source_code, unicode):
                 source_bytes = fscommands.unicode_to_file_data(source_code)
             else:
                 source_bytes = source_code
         ast_node = ast.parse(source_bytes, filename=filename)
     except SyntaxError, e:
         raise exceptions.ModuleSyntaxError(filename, e.lineno, e.msg)
Esempio n. 21
0
 def _init_source(self, pycore, source_code, resource):
     filename = 'string'
     if resource:
         filename = resource.path
     try:
         if source_code is None:
             source_bytes = resource.read_bytes()
             source_code = fscommands.file_data_to_unicode(source_bytes)
         else:
             if isinstance(source_code, unicode):
                 source_bytes = fscommands.unicode_to_file_data(source_code)
             else:
                 source_bytes = source_code
         ast_node = ast.parse(source_bytes, filename=filename)
     except SyntaxError, e:
         raise exceptions.ModuleSyntaxError(filename, e.lineno, e.msg)
Esempio n. 22
0
 def __init__(self, pycore, source=None, resource=None, force_errors=False):
     ignore = pycore.project.prefs.get('ignore_syntax_errors', False)
     syntax_errors = force_errors or not ignore
     try:
         source, node = self._init_source(pycore, source, resource)
     except exceptions.ModuleSyntaxError:
         if syntax_errors:
             raise
         else:
             source = '\n'
             node = ast.parse('\n')
     self.source_code = source
     self.star_imports = []
     self.visitor_class = _GlobalVisitor
     self.coding = fscommands.read_str_coding(self.source_code)
     super(PyModule, self).__init__(pycore, node, resource)
Esempio n. 23
0
def source_suite_tree(source):
    return suites.ast_suite_tree(ast.parse(source))
Esempio n. 24
0
def _parse_text(body):
    body = sourceutils.fix_indentation(body, 0)
    node = ast.parse(body)
    return node
Esempio n. 25
0
def get_string_result(scope, string):
    """use `get_pyname_in_scope` instead"""
    evaluator = StatementEvaluator(scope)
    node = ast.parse(string)
    ast.walk(node, evaluator)
    return evaluator.result
Esempio n. 26
0
 def get_root_nodes(self, source_code):
     if isinstance(source_code, unicode):
         source_code = source_code.encode('utf-8')
     ast_node = ast.parse(source_code)
     return _get_ast_children(ast_node)
Esempio n. 27
0
def source_suite_tree(source):
    return suites.ast_suite_tree(ast.parse(source))
Esempio n. 28
0
 def errors(self, source):
     try:
         ast.parse(source)
     except SyntaxError, e:
         return [(e.lineno, e.msg)]