Robot Framework
blockparsers.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from ..lexer import Token
17 from ..model import TestCase, Keyword, For, If, Try, While
18 
19 
20 
21 class Parser:
22 
23  def __init__(self, model):
24  self.modelmodel = model
25 
26  def handles(self, statement):
27  raise NotImplementedError
28 
29  def parse(self, statement):
30  raise NotImplementedError
31 
32 
34  unhandled_tokens = Token.HEADER_TOKENS | frozenset((Token.TESTCASE_NAME,
35  Token.KEYWORD_NAME))
36 
37  def __init__(self, model):
38  Parser.__init__(self, model)
39  self.nested_parsersnested_parsers = {
40  Token.FOR: ForParser,
41  Token.IF: IfParser,
42  Token.INLINE_IF: IfParser,
43  Token.TRY: TryParser,
44  Token.WHILE: WhileParser
45  }
46 
47  def handles(self, statement):
48  return statement.type not in self.unhandled_tokensunhandled_tokens
49 
50  def parse(self, statement):
51  parser_class = self.nested_parsersnested_parsers.get(statement.type)
52  if parser_class:
53  parser = parser_class(statement)
54  self.modelmodel.body.append(parser.model)
55  return parser
56  self.modelmodel.body.append(statement)
57  return None
58 
59 
61 
62  def __init__(self, header):
63  BlockParser.__init__(self, TestCase(header))
64 
65 
67 
68  def __init__(self, header):
69  BlockParser.__init__(self, Keyword(header))
70 
71 
73 
74  def handles(self, statement):
75  return BlockParser.handles(self, statement) and \
76  not getattr(self.modelmodel, 'end', False)
77 
78  def parse(self, statement):
79  if statement.type == Token.END:
80  self.modelmodel.end = statement
81  return None
82  return BlockParser.parse(self, statement)
83 
84 
86 
87  def __init__(self, header):
88  NestedBlockParser.__init__(self, For(header))
89 
90 
92 
93  def __init__(self, header, handle_end=True):
94  super().__init__(If(header))
95  self.handle_endhandle_end = handle_end
96 
97  def parse(self, statement):
98  if statement.type in (Token.ELSE_IF, Token.ELSE):
99  parser = IfParser(statement, handle_end=False)
100  self.modelmodel.orelse = parser.model
101  return parser
102  return NestedBlockParser.parse(self, statement)
103 
104  def handles(self, statement):
105  if statement.type == Token.END and not self.handle_endhandle_end:
106  return False
107  return super().handles(statement)
108 
109 
111 
112  def __init__(self, header, handle_end=True):
113  super().__init__(Try(header))
114  self.handle_endhandle_end = handle_end
115 
116  def parse(self, statement):
117  if statement.type in (Token.EXCEPT, Token.ELSE, Token.FINALLY):
118  parser = TryParser(statement, handle_end=False)
119  self.modelmodel.next = parser.model
120  return parser
121  return super().parse(statement)
122 
123  def handles(self, statement):
124  if statement.type == Token.END and not self.handle_endhandle_end:
125  return False
126  return super().handles(statement)
127 
128 
130 
131  def __init__(self, header):
132  super().__init__(While(header))
Represents IF structures in the model.
Definition: blocks.py:194
def __init__(self, header, handle_end=True)
Definition: blockparsers.py:93
def __init__(self, header, handle_end=True)