Skip to content

Commit 53492cc

Browse files
Merge pull request serge-sans-paille#1107 from serge-sans-paille/fix/spec-lines
Rework pythran directive preprocessing
2 parents 7530e2d + 9010c2d commit 53492cc

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

pythran/spec.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,6 @@ class SpecParser(object):
212212
('left', 'LIST', 'DICT', 'SET'),
213213
)
214214

215-
# regexp to extract pythran specs from comments
216-
# the first part matches lines with a comment and the pythran keyword
217-
# the second part matches lines with comments following the pythran ones
218-
FILTER = re.compile(r'^\s*#\s*pythran[^\n\r]*[\n\r]*'
219-
r'^(?:\s*#[^\n\r]*[\n\r]*)*', re.MULTILINE)
220-
221215
def t_IDENTIFER(self, t):
222216
r'\#?[a-zA-Z_][a-zA-Z_0-9]*'
223217
t.type = SpecParser.reserved.get(t.value, 'IDENTIFIER')
@@ -414,11 +408,24 @@ def __call__(self, path_or_text):
414408
self.input_file = None
415409

416410
data = self.read_path_or_text(path_or_text)
411+
lines = []
412+
in_pythran_export = False
413+
for line in data.split("\n"):
414+
if re.match(r'\s*#\s*pythran', line):
415+
in_pythran_export = True
416+
lines.append(re.sub(r'\s*#\s*pythran', '#pythran', line))
417+
elif in_pythran_export:
418+
stripped = line.strip()
419+
if stripped.startswith('#'):
420+
lines.append(line.replace('#', ''))
421+
else:
422+
in_pythran_export = not stripped
423+
lines.append('')
424+
else:
425+
in_pythran_export &= not line.strip()
426+
lines.append('')
417427

418-
raw = "\n".join(SpecParser.FILTER.findall(data))
419-
pythran_data = (re.sub(r'#\s*pythran', '\_o< pythran >o_/', raw)
420-
.replace('#', '')
421-
.replace('\_o< pythran >o_/', '#pythran'))
428+
pythran_data = '\n'.join(lines)
422429
self.parser.parse(pythran_data, lexer=self.lexer, debug=False)
423430

424431
for key, overloads in self.native_exports.items():

0 commit comments

Comments
 (0)