(self)
| 1320 | return node |
| 1321 | |
| 1322 | def import_statement(self) -> Optional[Node]: |
| 1323 | if Def.fun_name != '': |
| 1324 | print_error('import_statement', |
| 1325 | 'Local imports are not allowed', self) |
| 1326 | |
| 1327 | module = '' |
| 1328 | while not self.no_more_tokens(): |
| 1329 | tok = self.curr_token() |
| 1330 | if tok.kind == TokenKind.PERIOD: |
| 1331 | print_error('import_statement', |
| 1332 | 'Invalid period in module name', parser=self) |
| 1333 | self.next_token() |
| 1334 | |
| 1335 | part = tok.value |
| 1336 | module = os.path.join(module, part) |
| 1337 | |
| 1338 | if not self.no_more_tokens(): |
| 1339 | self.match_token(TokenKind.PERIOD) |
| 1340 | |
| 1341 | if self.no_more_tokens(): |
| 1342 | print_error('import_statement', |
| 1343 | 'Invalid trailing period in import', parser=self) |
| 1344 | |
| 1345 | # Multi-file import |
| 1346 | if module.endswith('*'): |
| 1347 | module_roots = [] |
| 1348 | module = module[:-1] |
| 1349 | |
| 1350 | module_source = module |
| 1351 | if not exists(module_source): |
| 1352 | for module_dir in Def.include_list: |
| 1353 | other_source = os.path.join(module_dir, module_source) |
| 1354 | if exists(other_source): |
| 1355 | module_source = other_source |
| 1356 | |
| 1357 | def is_ml_source(f): return f.lower().endswith('.ml') |
| 1358 | modules = list(filter(is_ml_source, [f for f in listdir( |
| 1359 | module_source) if isfile(os.path.join(module_source, f))])) |
| 1360 | |
| 1361 | for module in modules: |
| 1362 | module = os.path.join(module_source, module) |
| 1363 | |
| 1364 | if not exists(module): |
| 1365 | print_error('import_statement', |
| 1366 | f'Module \'{module}\' does not exist.', self) |
| 1367 | |
| 1368 | if module in Def.included: |
| 1369 | continue |
| 1370 | else: |
| 1371 | Def.included.add(module) |
| 1372 | |
| 1373 | try: |
| 1374 | module_root = Parser().parse(module) |
| 1375 | module_roots.append(module_root) |
| 1376 | except RecursionError: |
| 1377 | print_error('import_statement', |
| 1378 | f'Cannot import module "{module}" (circular import)', self) |
| 1379 |
no test coverage detected