35
36:- module(doc_util,
37 [ insert_alias/2, 38 expand_alias/2, 39 ensure_slash_end/2, 40 atom_pi/2, 41 atom_to_object/2, 42 normalise_white_space//1 43 ]). 44:- use_module(library(dcg/basics)).
51
61insert_alias(Path0, Path) :-
62 path_alias(Alias, Prefix),
63 atom_concat(Prefix, PostFix, Path0),
64 !,
65 atom_concat(Alias, PostFix, Path).
66insert_alias(Path, Path).
73expand_alias(Path0, Path) :-
74 path_alias(Alias, Prefix),
75 atom_concat(Alias, Postfix, Path0),
76 !,
77 atom_concat(Prefix, Postfix, Path).
78expand_alias(Path, Path).
87path_alias('/_SWI_/', Dir) :-
88 current_prolog_flag(home, Dir0),
89 ensure_slash_end(Dir0, Dir).
90path_alias('/_CWD_/', Dir) :-
91 working_directory(Dir0, Dir0),
92 ensure_slash_end(Dir0, Dir).
93
94
95
103ensure_slash_end(Dir, Dir) :-
104 sub_atom(Dir, _, _, 0, /),
105 !.
106ensure_slash_end(Dir0, Dir) :-
107 atom_concat(Dir0, /, Dir).
108
109
121atom_pi(Atom, Module:PI) :-
122 atomic_list_concat([Module, PIAtom], :, Atom),
123 Module \== '',
124 forall(sub_atom(Module, _,1,_,C), char_type(C,alnum)),
125 !,
126 atom_pi2(PIAtom, PI).
127atom_pi(Atom, PI) :-
128 atom_pi2(Atom, PI).
129
130atom_pi2(Atom, Name//Arity) :-
131 sub_atom(Atom, B, _, A, //),
132 sub_atom(Atom, _, A, 0, ArityA),
133 atom_number(ArityA, Arity),
134 sub_atom(Atom, 0, B, _, Name),
135 Name \== '',
136 !.
137atom_pi2(Atom, Name/Arity) :-
138 sub_atom(Atom, B, _, A, /),
139 sub_atom(Atom, _, A, 0, ArityA),
140 atom_number(ArityA, Arity),
141 !,
142 sub_atom(Atom, 0, B, _, Name).
143atom_pi2(Name, Name/_).
149atom_to_object(Atom, Object) :-
150 atom(Atom),
151 atom_concat('f-', Atom0, Atom),
152 !,
153 PI = _/_, 154 atom_pi(Atom0, PI),
155 Object = f(PI).
156atom_to_object(Atom, Object) :-
157 atom(Atom),
158 atom_pi(Atom, PI),
159 ground(PI),
160 !,
161 ( PI = Name/Arity,
162 integer(Arity),
163 atom_concat('f-', FuncName, Name)
164 -> Object = f(FuncName/Arity)
165 ; Object = PI
166 ).
167atom_to_object(Atom, c(Function)) :-
168 atom(Atom),
169 ( sub_atom(Atom, 0, _, _, 'PL_')
170 -> true
171 ; sub_atom(Atom, 0, _, _, 'S')
172 ),
173 sub_atom(Atom, B, _, _, '('),
174 !,
175 sub_atom(Atom, 0, B, _, Function).
182normalise_white_space(Text) -->
183 blanks,
184 normalise_white_space2(Text).
185
186normalise_white_space2(Text) -->
187 non_ws(Text, Tail),
188 blanks,
189 ( eos
190 -> { Tail = [] }
191 ; { Tail = [0'\s|T2] },
192 normalise_white_space2(T2)
193 ).
200non_ws([H|T0], T) -->
201 [H],
202 { \+ code_type(H, space) },
203 !,
204 non_ws(T0, T).
205non_ws(T, T) -->
206 []
PlDoc utilities