2013-05-14 71 views
0

如何提取模块的名称和文件中存在的可选谓词?如何在文件中提取模块的名称?

如果我有一个file.pl包含对一个或多个模块的调用,如何在模块声明中提取这些模块的名称和谓词的名称?

例如:如果我的文件中包含调用模块

:- use_module(library(lists), [ member/2, 
           append/2 as list_concat 
           ]). 
:- use_module(library(option). 

我想创建一个predicate extract(file.pl)

输出List=[[list,member,append],[option]]

感谢。

回答

1

假定SWI-Prolog(如已标记)。你可以写类似的东西,以我在这个Prolog的编译器Logtalk适配器文件做:

list_of_exports(File, Module, Exports) :- 
    absolute_file_name(File, Path, [file_type(prolog), access(read), file_errors(fail)]), 
    module_property(Module, file(Path)), % only succeeds for loaded modules 
    module_property(Module, exports(Exports)), 
    !. 
list_of_exports(File, Module, Exports) :- 
    absolute_file_name(File, Path, [file_type(prolog), access(read), file_errors(fail)]), 
    open(Path, read, In), 
    ( peek_char(In, #) ->     % deal with #! script; if not present 
     skip(In, 10)      % assume that the module declaration 
    ; true        % is the first directive on the file 
    ), 
    setup_call_cleanup(true, read(In, ModuleDecl), close(In)), 
    ModuleDecl = (:- module(Module, Exports)), 
    ( var(Module) -> 
     file_base_name(Path, Base), 
     file_name_extension(Module, _, Base) 
    ; true 
    ). 

注意这个代码不涉及编码/ 1指令可能存在作为文件的第一项。该代码也是在SWI-Prolog作者的帮助下很久以前编写的。

相关问题