Skip to content

Commit 1dba783

Browse files
committed
Split standalone comments and empty lines to separate statements
Also add a separate EOL token
1 parent d36e72a commit 1dba783

File tree

4 files changed

+123
-86
lines changed

4 files changed

+123
-86
lines changed

src/robot/parsing/lexer/__init__.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
from itertools import chain
17+
1618
from .context import TestCaseFileContext, ResourceFileContext
1719
from .lexers import FileLexer
1820
from .splitter import Splitter
@@ -45,7 +47,13 @@ def get_tokens(self):
4547
Token.OLD_FOR_INDENT}
4648
else:
4749
ignore = {Token.IGNORE}
48-
for statement in self._handle_old_for(self.statements):
50+
statements = self._handle_old_for(self.statements)
51+
if not self._data_only:
52+
statements = chain.from_iterable(
53+
self._split_trailing_comment_and_empty_lines(s)
54+
for s in statements
55+
)
56+
for statement in statements:
4957
name_token = last_token = None
5058
for token in statement:
5159
if token.type in ignore:
@@ -82,6 +90,33 @@ def _get_first_data_token(self, statement):
8290
return token
8391
return None
8492

93+
def _split_trailing_comment_and_empty_lines(self, statement):
94+
lines = list(self._split_to_lines(statement))
95+
split_statements = []
96+
for line in reversed(lines):
97+
is_split = False
98+
for token in line:
99+
if token.type not in (token.IGNORE, token.SEPARATOR):
100+
is_split = token.type in (token.EOL, token.COMMENT)
101+
break
102+
if not is_split:
103+
break
104+
split_statements.append(line)
105+
lines.pop()
106+
yield list(chain.from_iterable(lines))
107+
for split in reversed(split_statements):
108+
yield split
109+
110+
def _split_to_lines(self, statement):
111+
current = []
112+
for tok in statement:
113+
current.append(tok)
114+
if tok.type == tok.EOL:
115+
yield current
116+
current = []
117+
if current:
118+
yield current
119+
85120

86121
class TestCaseFileLexer(BaseLexer):
87122
context_class = TestCaseFileContext

src/robot/parsing/lexer/splitter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _split_line(self, line, lineno, data_only=False):
5050
if not data_only:
5151
trailing_whitespace = re.search(r'\s+$', line, flags=re.UNICODE)
5252
if trailing_whitespace:
53-
yield Token(sepa, trailing_whitespace.group(), lineno, columnno)
53+
yield Token(Token.EOL, trailing_whitespace.group(), lineno, columnno)
5454

5555
def _split_from_spaces(self, line):
5656
for index, value in enumerate(self._space_splitter.split(line)):

src/robot/parsing/lexer/tokens.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class Token(object):
5353
END = 'END'
5454

5555
SEPARATOR = 'SEPARATOR'
56+
EOL = 'EOL'
5657
COMMENT = 'COMMENT'
5758
CONTINUATION = 'CONTINUATION'
5859
IGNORE = 'IGNORE'

0 commit comments

Comments
 (0)