2013-05-14 67 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作者的幫助下很久以前編寫的。

相關問題