34
35:- module(subpos_utils,
36 [subpos_location/3,
37 subterm_location/3,
38 location_subterm/3,
39 location_subterm/4,
40 location_subterm_un/3,
41 location_subterm_un/4,
42 location_subterm_eq/3
43 ]). 44
45:- use_module(library(lists)). 46:- use_module(library(neck)). 47:- init_expansors. 48
49:- meta_predicate
50 subterm_location(1,+,?),
51 location_subterm(+,1,+),
52 location_subterm(+,1,1,+),
53 location_subterm_un(+,+,1,+). 54
55location_subpos(PPos, N, SPos) :-
56 nonvar(PPos),
57 PPos = parentheses_term_position(_, _, Pos), !,
58 location_subpos(Pos, N, SPos).
59location_subpos(term_position(_, _, _, _, PosL), N, Pos) :-
60 nth1(N, PosL, Pos).
61location_subpos(PPos, N, Pos) :-
62 member(IniP-PPos, [inip(Pos1)-list_position(From, To, PosL, Tail),
63 frto(BTo)-sub_list_position(From, To, BTo, _, _, PosL, Tail)
64 ]),
65 neck,
66 ( N = 1
67 ->( PosL = [Pos|_]
68 ->true
69 ; PosL = []
70 ->Pos = Tail
71 )
72 ; N = 2
73 ->( PosL = [_]
74 ->Pos = Tail
75 ; PosL = [Pos1|PosL1],
76 lspi(IniP, BTo1),
77 PosL1 = [Pos2|_],
78 arg(2, Pos1, PTo1),
79 arg(1, Pos2, PTo2),
80
81 82 83 84 85 86
87 Pos = sub_list_position(From, To, BTo1, PTo1, PTo2, PosL1, Tail)
88 )
89 ).
90location_subpos(brace_term_position(_, _, Pos), 1, Pos).
91
92lspi(inip(Pos), BTo) :- arg(1, Pos, BTo).
93lspi(frto(BTo), BTo).
94
95subpos_location([], Pos, Pos).
96subpos_location([N|L], SubPos, Pos) :-
97 location_subpos(SubPos, N, Pos1),
98 subpos_location(L, Pos1, Pos).
99
100location_subterm_un(L, Term, Find) :- location_subterm(L, =(Find), Term).
101
102location_subterm_un(L, Term, Tester, Find) :- location_subterm(L, =(Find), Tester, Term).
103
104location_subterm_eq(L, Term, Find) :- subterm_location(==(Find), Term, L).
105
106subterm_location(Comparator, Term, []) :- call(Comparator, Term), !.
107subterm_location(Comparator, Term, [N|L]) :-
108 compound(Term),
109 arg(N, Term, SubTerm),
110 subterm_location(Comparator, SubTerm, L).
111
112location_subterm(L, Comparator, Term) :-
113 location_subterm(L, Comparator, compound, Term).
114
115location_subterm([], Comparator, _, Term) :- call(Comparator, Term).
116location_subterm([N|L], Comparator, Tester, Term) :-
117 call(Tester, Term),
118 arg(N, Term, SubTerm),
119 location_subterm(L, Comparator, Tester, SubTerm)