1/*  Part of Extended Libraries for SWI-Prolog
    2
    3    Author:        Edison Mera
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xlibrary
    6    Copyright (C): 2014, Process Design Center, Breda, The Netherlands.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   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        % Example: sublist_position for [c, d]:
   82        %
   83        % [    a,   b,    c,  d ]
   84        % ^    ^     ^    ^     ^
   85        % From BTo1  PTo1 PTo2  To
   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)