Repository navigation
#4079 Continue and Break - #4136
Conversation
Break and Continue
Continue and Break Lexers modified
Break and Continue Lexer added in blocklexers.py
Continue and Break definitions added in Builtin.py
Implementation continue and break. Initial commit
Initial commit for the Continue and Break statements.
Initial level review comments incorporated.
Exit and Continue for loop
Changes in BuiltIn file removed.
KeywordCallLexer was removed by mistake
White space removed. Tried to add unit tests for the Break and Continue. [not done completely.]
|
|
||
|
|
||
| @Body.register | ||
| class Continue(model.Return): |
There was a problem hiding this comment.
There should be Continue and Break classes in the model package that are extended here. They are also needed in the result package.
There was a problem hiding this comment.
added in result pkg but need you guidance a not able to understand this pkg fully.
| __slots__ = ['lineno'] | ||
|
|
||
| def __init__(self, values=(), parent=None, lineno=None): | ||
| model.Return.__init__(self, values, parent) |
There was a problem hiding this comment.
Neither CONTINUE nor BREAK accepts values in data (RETURN does) so model objects shouldn't have values arguments either.
There was a problem hiding this comment.
Removed following code blocked and directly raised... Is this correct or should I just remove values .... ?
with StatusReporter(self, ReturnResult(self.values), context, run):
if run:
raise ExitForLoop(self.values)
New Code:
def run(self, context, run=True, templated=False):
raise ContinueForLoop()
def run(self, context, run=True, templated=False):
raise ExitForLoop()
| self._verify(data, expected) | ||
|
|
||
| def test_in_test(self): | ||
| # This is not valid usage but that's not recognized during lexing. |
There was a problem hiding this comment.
It's true using CONTINUE and BREAK directly under a test isn't valid, but using them directly inside a user keyword isn't valid either. It should be possible to use them only inside FOR either directly or inside IF and TRY structures. This is different to RETURN that can be used anywhere inside a user keyword, also directly under them. In other words, this comment applies also to the above test.
Notice that it's possible to use Continue For Loop and Exit For Loop keywords directly inside user keywords as long as those user keywords are used inside a loop. That's bad design and not something I want to repeat with CONTINUE and BREAK. Although those keywords will be eventually deprecated and removed, I wouldn't like to change their behavior to avoid backwards incompatible change. We need to see can that be handled nicely when we get further with CONTINUE/BREAK.
Notice also that I have an idea how these error situations could be detected already during lexing/parsing. I can look at that after this PR has been merged and update tests then.
There was a problem hiding this comment.
I am not sure I am following all your points mentioned here however I have modified test little.
There was a problem hiding this comment.
This ought to be fine. Can be changed later if parser is enhanced to detect RETURN and others when used in a wrong place. That's not something this PR needs to care.
| __slots__ = ['lineno'] | ||
|
|
||
| def __init__(self, values=(), parent=None, lineno=None): | ||
| model.Return.__init__(self) |
There was a problem hiding this comment.
Should be model.Continue, not model.Return here! Even better would be using super() that we have now started to use when we don't need to care about Python 2 (super() syntax was different with it). Also need to pass parent to the parent class. In other words, this should be super().__init__(parent).
| __slots__ = ['lineno'] | ||
|
|
||
| def __init__(self, values=(), parent=None, lineno=None): | ||
| model.Return.__init__(self) |
There was a problem hiding this comment.
Same problems as with Continue here as well.
|
|
||
| def test_in_keyword(self): | ||
| data = ' BREAK' | ||
| expected = [(T.CONTINUE, 'BREAK', 3, 4), |
There was a problem hiding this comment.
Should be T.BREAK here and in tests below.
| ), | ||
| body=[ | ||
| KeywordCall([Token(Token.KEYWORD, 'Break', 3, 4)]), | ||
| ReturnStatement([Token(Token.BREAK, 'BREAK', 4, 4)]) |
There was a problem hiding this comment.
Should be Continue, not ReturnStatement here!
| self._verify(data, expected) | ||
|
|
||
| def test_in_test(self): | ||
| # This is not valid usage but that's not recognized during lexing. |
There was a problem hiding this comment.
This ought to be fine. Can be changed later if parser is enhanced to detect RETURN and others when used in a wrong place. That's not something this PR needs to care.
|
|
||
| EndLexer, ReturnLexer, ContinueLexer, BreakLexer) | ||
|
|
||
| class BlockLexer(Lexer): |
There was a problem hiding this comment.
An empty line has been accidentally removed. PEP-8 says there should be two such lines between imports and the class declaration.
| def lexer_classes(self): | ||
| return (InlineIfLexer, IfHeaderLexer, ElseIfHeaderLexer, ElseHeaderLexer, | ||
| ForLexer, TryLexer, EndLexer, ReturnLexer, KeywordCallLexer) | ||
| ForLexer, TryLexer, EndLexer, ReturnLexer, KeywordCallLexer, ContinueLexer, BreakLexer) |
There was a problem hiding this comment.
I'm not absolutely sure about this, but I believe these new lexers should be before KeywordCallLexer here and in other lexer_classes. Otherwise BREAK and CONTINUE are likely to be lexed as keyword calls.
| __slots__ = ['status', 'starttime', 'endtime'] | ||
|
|
||
| def __init__(self, values=(), status='FAIL', starttime=None, endtime=None, parent=None): | ||
| super().__init__(values, parent) |
There was a problem hiding this comment.
Continue shouldn't accept values. Same also with Break.
There was a problem hiding this comment.
I had checked this, and I was thinking that it will be a empty, so did not remove.
Removing now.
| def visit_Break(self, node): | ||
| self.test.body.create_keyword(name='BREAK',lineno=node.lineno) | ||
|
|
||
| class KeywordBuilder(NodeVisitor): |
There was a problem hiding this comment.
- When mapping CONTINUE and BREAK used outside a loop to a keyword call, we should pass also their possible arguments. That's done with RETURN above, but using
node.valuesisn't possible here because CONTINUE and BREAK nodes don't currently have such attribute. ProbablyNoArgumentsHeadershould get this property to allow that:@property def values(self): return self.get_values(Token.ARGUMENT)
- There should be two empty lines between classes.
There was a problem hiding this comment.
Added above property in the NoArgumentsHeader.
In this case, I am adding back args=node.values to the method.
def visit_Continue(self, node):
self.test.body.create_keyword(name='CONTINUE', args=node.values,
lineno=node.lineno)
| from robot import model | ||
| from robot.conf import RobotSettings | ||
| from robot.errors import ReturnFromKeyword | ||
| from robot.errors import ContinueForLoop, ExitForLoop |
There was a problem hiding this comment.
Combine the above two import lines together.
| __slots__ = ['lineno'] | ||
|
|
||
| def __init__(self, values=(), parent=None, lineno=None): | ||
| super().__init__(parent) |
There was a problem hiding this comment.
Continue, and Break, shouldn't accept values.
There was a problem hiding this comment.
Corrected. Thought it will be empty so had kept.
Removed
Moved ContinueLexer, BreakLexer before KeywordCallLexer
…, we should pass also their possible arguments. That's done with RETURN above, but using node.values isn't possible here because CONTINUE and BREAK nodes don't currently have such attribute.
pekkaklarck
left a comment
There was a problem hiding this comment.
Good progress again! Small change still needed to get BREAK and CONTINUE visible in log.
|
|
||
| def __init__(self, parent=None): | ||
| self.parent = parent | ||
| #self.body = None |
There was a problem hiding this comment.
Remove the commented code here and in Break. These structures don't have body.
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # limitations under the License. |
There was a problem hiding this comment.
Remove accidentally added whitespace.
|
|
||
| def start(self, elem, result): | ||
| return result.body.create_break() | ||
|
|
There was a problem hiding this comment.
In addition to creating these handlers for <break> and <continue>, they need to be listed in children for IterationHandler and BranchHandler. After this change BREAK and CONTINUE should be visible in log as well.
<return> is also used with KeywordHandler, but these statements aren't usable directly under keywords so that shouldn't be needed.
No description provided.