def parse_declaration(decl): try: what = decl['decl']['what'] loc = parse_location(None, decl.get('pos')) docs = decl.get('docs') name = decl['name'] if what == 'function': return symbols.Function(name, decl['decl'].get('type'), docs, loc) elif what == 'type': return symbols.Type(name, decl['decl']['info'].get('ctx'), decl['decl']['info'].get('args'), decl['decl']['info'].get('def'), docs, loc) elif what == 'newtype': return symbols.Newtype(name, decl['decl']['info'].get('ctx'), decl['decl']['info'].get('args'), decl['decl']['info'].get('def'), docs, loc) elif what == 'data': return symbols.Data(name, decl['decl']['info'].get('ctx'), decl['decl']['info'].get('args'), decl['decl']['info'].get('def'), docs, loc) elif what == 'class': return symbols.Class(name, decl['decl']['info'].get('ctx'), decl['decl']['info'].get('args'), decl['decl']['info'].get('def'), docs, loc) else: return None except Exception as e: log('Error pasring declaration: {0}'.format(e)) return None
def parse_declaration(decl): try: what = decl['decl']['what'] docs = crlf2lf(decl.get('docs')) name = decl['name'] pos = parse_position(decl.get('pos')) imported = [] if 'imported' in decl and decl['imported']: imported = [parse_import(d) for d in decl['imported']] defined = None if 'defined' in decl and decl['defined']: defined = parse_module_id(decl['defined']) if what == 'function': return symbols.Function(name, decl['decl'].get('type'), docs, imported, defined, pos) elif what == 'type': return symbols.Type(name, decl['decl']['info'].get('ctx'), decl['decl']['info'].get('args', []), decl['decl']['info'].get('def'), docs, imported, defined, pos) elif what == 'newtype': return symbols.Newtype(name, decl['decl']['info'].get('ctx'), decl['decl']['info'].get('args', []), decl['decl']['info'].get('def'), docs, imported, defined, pos) elif what == 'data': return symbols.Data(name, decl['decl']['info'].get('ctx'), decl['decl']['info'].get('args', []), decl['decl']['info'].get('def'), docs, imported, defined, pos) elif what == 'class': return symbols.Class(name, decl['decl']['info'].get('ctx'), decl['decl']['info'].get('args', []), decl['decl']['info'].get('def'), docs, imported, defined, pos) else: return None except Exception as e: log('Error pasring declaration: {0}'.format(e), log_error) return None
def parse_info(name, contents): """ Parses result of :i <name> command of ghci and returns derived symbols.Declaration """ functionRegex = '{0}\s+::\s+(?P<type>.*?)(\s+--(.*))?$'.format(name) dataRegex = '(?P<what>(newtype|type|data))\s+((?P<ctx>(.*))=>\s+)?(?P<name>\S+)\s+(?P<args>(\w+\s+)*)=(\s*(?P<def>.*)\s+-- Defined)?' classRegex = '(?P<what>class)\s+((?P<ctx>(.*))=>\s+)?(?P<name>\S+)\s+(?P<args>(\w+\s+)*)(.*)where$' if name[0].isupper(): # data, class, type or newtype matched = re.search(dataRegex, contents, re.MULTILINE) or re.search( classRegex, contents, re.MULTILINE) if matched: what = matched.group('what') args = matched.group('args').strip().split(' ') if matched.group( 'args') else [] ctx = matched.group('ctx') definition = matched.group('def') if definition: definition.strip() if what == 'class': return symbols.Class(name, ctx, args) elif what == 'data': return symbols.Data(name, ctx, args, definition) elif what == 'type': return symbols.Type(name, ctx, args, definition) elif what == 'newtype': return symbols.Newtype(name, ctx, args, definition) else: raise RuntimeError('Unknown type of symbol: {0}'.format(what)) else: # function matched = re.search(functionRegex, contents, re.MULTILINE) if matched: return symbols.Function(name, matched.group('type')) return None
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)