From f055ba198fe836e9440423be014b761e6e115548 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Thu, 17 Aug 2017 10:38:45 +0200 Subject: [PATCH] Move more stuff and fix tests. --- parso/normalizer.py | 10 +++++----- parso/python/errors.py | 21 +++++++++++++-------- parso/python/pep8.py | 5 +++-- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/parso/normalizer.py b/parso/normalizer.py index fb7f703..26b3ca0 100644 --- a/parso/normalizer.py +++ b/parso/normalizer.py @@ -76,12 +76,12 @@ class Normalizer(use_metaclass(_NormalizerMeta)): @classmethod def register_rule(cls, **kwargs): """ - Use it as a class decorator: + Use it as a class decorator:: - >>> normalizer = Normalizer('grammar', 'config') - >>> @normalizer.register_rule(value='foo') - ... class MyRule(Rule): - ... error_code = 42 + normalizer = Normalizer('grammar', 'config') + @normalizer.register_rule(value='foo') + class MyRule(Rule): + error_code = 42 """ return cls._register_rule(**kwargs) diff --git a/parso/python/errors.py b/parso/python/errors.py index e854fe3..aeab303 100644 --- a/parso/python/errors.py +++ b/parso/python/errors.py @@ -655,13 +655,6 @@ class ErrorFinder(Normalizer): if not in_loop: message = "'continue' not properly in loop" self._add_syntax_error(message, leaf) - elif leaf.value == 'break': - in_loop = False - for block in self.context.blocks: - if block.type in ('for_stmt', 'while_stmt'): - in_loop = True - if not in_loop: - self._add_syntax_error("'break' outside loop", leaf) elif leaf.value in ('yield', 'return'): if self.context.node.type != 'funcdef': self._add_syntax_error("'%s' outside function" % leaf.value, leaf.parent) @@ -797,7 +790,7 @@ class IndentationRule(Rule): @ErrorFinder.register_rule(value='await') -class AwaitOutsideAsync(SyntaxRule): +class _AwaitOutsideAsync(SyntaxRule): message = "'await' outside async function" def is_issue(self, leaf): @@ -806,3 +799,15 @@ class AwaitOutsideAsync(SyntaxRule): def get_error_node(self, node): # Return the whole await statement. return node.parent + + +@ErrorFinder.register_rule(value='break') +class _BreakOutsideLoop(SyntaxRule): + message = "'break' outside loop" + + def is_issue(self, leaf): + in_loop = False + for block in self._normalizer.context.blocks: + if block.type in ('for_stmt', 'while_stmt'): + in_loop = True + return not in_loop diff --git a/parso/python/pep8.py b/parso/python/pep8.py index fafcc0a..ba11e10 100644 --- a/parso/python/pep8.py +++ b/parso/python/pep8.py @@ -712,10 +712,11 @@ class PEP8NormalizerConfig(ErrorFinderConfig): self.spaces_before_comment = spaces_before_comment -@PEP8Normalizer.register_rule(type='endmarker') +# TODO this is not yet ready. +#@PEP8Normalizer.register_rule(type='endmarker') class BlankLineAtEnd(Rule): code = 392 message = 'Blank line at end of file' - def check(self, leaf): + def is_issue(self, leaf): return self._newline_count >= 2