def parse_module(d): if d is None: return None return symbols.Module( d['name'], d.get('exports'), [parse_import(i) for i in d['imports']] if 'imports' in d else [], dict((decl['name'], parse_declaration(decl)) for decl in d['declarations']) if 'declarations' in d else {}, parse_location(d.get('location')))
def ghcmod_browse_module(module_name, cabal = None, use_ghc_package = False): """ Returns symbols.Module with all declarations """ contents = None if use_ghc_package: contents = call_ghcmod_and_wait(['-g', '-package ghc','browse', '-d', module_name], cabal = cabal).splitlines() else: contents = call_ghcmod_and_wait(['browse', '-d', module_name], cabal = cabal).splitlines() # Adding empty modules into cache greatly speeds up loading standard modules cache # if not contents: # return None m = symbols.Module(module_name, cabal = cabal) functionRegex = r'(?P<name>\w+)\s+::\s+(?P<type>.*)' typeRegex = r'(?P<what>(class|type|data|newtype))\s+(?P<name>\w+)(\s+(?P<args>\w+(\s+\w+)*))?' def toDecl(line): matched = re.search(functionRegex, line) if matched: return symbols.Function(matched.group('name'), matched.group('type')) else: matched = re.search(typeRegex, line) if matched: decl_type = matched.group('what') decl_name = matched.group('name') decl_args = matched.group('args') decl_args = decl_args.split() if decl_args else [] if decl_type == 'class': return symbols.Class(decl_name, None, decl_args) elif decl_type == 'data': return symbols.Data(decl_name, None, decl_args) elif decl_type == 'type': return symbols.Type(decl_name, None, decl_args) elif decl_type == 'newtype': return symbols.Newtype(decl_name, None, decl_args) else: return symbols.Declaration(line) decls = map(toDecl, contents) for decl in decls: m.add_declaration(decl) return m
def parse_module_id(d): if d is None: return None return symbols.Module(d['name'], [], [], {}, parse_location(d.get('location')))