ecmwf-ifs / loki

Freely programmable source-to-source translation for Fortran

Home Page:https://sites.ecmwf.int/docs/loki/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Empty source file fails in regex-frontend

mlange05 opened this issue · comments

In [1]: from loki import Sourcefile

In [2]: Sourcefile.from_regex(raw_source='\n', filepath='./')
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[2], line 1
----> 1 Sourcefile.from_regex(raw_source='\n', filepath='./')

File /lus/h2resw01/hpcperm/naml/loki/loki/sourcefile.py:278, in Sourcefile.from_regex(cls, raw_source, filepath, parser_classes)
    273 """
    274 Parse a given source string using the REGEX frontend
    275 """
    276 source, _ = sanitize_input(source=raw_source, frontend=REGEX)
--> 278 ir = parse_regex_source(source, parser_classes=parser_classes)
    279 lines = (1, raw_source.count('\n') + 1)
    280 source = Source(lines, string=raw_source, file=filepath)

File /usr/local/apps/python3/3.8.8-01/lib/python3.8/contextlib.py:75, in ContextDecorator.__call__.<locals>.inner(*args, **kwds)
     72 @wraps(func)
     73 def inner(*args, **kwds):
     74     with self._recreate_cm():
---> 75         return func(*args, **kwds)

File /lus/h2resw01/hpcperm/naml/loki/loki/frontend/regex.py:372, in parse_regex_source(source, parser_classes, scope)
    370 timeout_message = f'REGEX frontend timeout of {config["regex-frontend-timeout"]} s exceeded'
    371 with timeout(config['regex-frontend-timeout'], message=timeout_message):
--> 372     ir_ = Pattern.match_block_candidates(reader, candidates, parser_classes=parser_classes, scope=scope)
    373 return ir.Section(body=as_tuple(ir_), source=source)

File /lus/h2resw01/hpcperm/naml/loki/loki/frontend/regex.py:148, in Pattern.match_block_candidates(cls, reader, candidates, parser_classes, scope)
    145         ir_ += [match]
    147 if reader:
--> 148     source = reader.to_source(include_padding=True)
    149     ir_ += [ir.RawSource(text=source.string, source=source)]
    150 if head is not None and (not ir_ or ir_[0].source.lines[0] > head.lines[1]):
    151     # Insert the header bit only if the recursion hasn't already taken care of it

File /lus/h2resw01/hpcperm/naml/loki/loki/frontend/source.py:253, in FortranReader.to_source(self, include_padding)
    251     index = (lines[0] - self.line_offset - 1, lines[1] - self.line_offset)
    252     string = '\n'.join(self.source_lines[index[0]:index[1]])
--> 253 return Source(lines=lines, string=string)

File /lus/h2resw01/hpcperm/naml/loki/loki/frontend/source.py:44, in Source.__init__(self, lines, string, file)
     43 def __init__(self, lines, string=None, file=None):
---> 44     assert lines and len(lines) == 2 and (lines[1] is None or lines[1] >= lines[0])
     45     self.lines = lines
     46     self.string = string

AssertionError: 

This has a mildly annoying side effect on traversing large source bases via the scheduler, but there are easy workarounds, not not high priority.

Confirmed and fix filed in #187