MCPcopy Create free account
hub / github.com/NICUP14/MiniLang / import_statement

Method import_statement

src/Parser.py:1322–1404  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

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

Callers 1

statementMethod · 0.95

Calls 8

no_more_tokensMethod · 0.95
curr_tokenMethod · 0.95
next_tokenMethod · 0.95
match_tokenMethod · 0.95
print_errorFunction · 0.90
glue_statementsFunction · 0.90
ParserClass · 0.85
parseMethod · 0.80

Tested by

no test coverage detected