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)
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
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)
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)
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)
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)
def warnings(self, source): result = [] try: node = ast.parse(source) except SyntaxError: return [] except SyntaxWarning, e: result.append((e.lineno, e.msg))
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)
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)
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)
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
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)
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)
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)
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)
def source_suite_tree(source): return suites.ast_suite_tree(ast.parse(source))
def _parse_text(body): body = sourceutils.fix_indentation(body, 0) node = ast.parse(body) return node
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
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)
def errors(self, source): try: ast.parse(source) except SyntaxError, e: return [(e.lineno, e.msg)]