Release coccinelle-0.2.0
[bpt/coccinelle.git] / popl / insert_befaft.ml
1 (*
2 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
5 *
6 * Coccinelle is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, according to version 2 of the License.
9 *
10 * Coccinelle is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
20 *)
21
22
23 module Past = Ast_popl
24
25 (* --------------------------------------------------------------------- *)
26
27 let rec get_before a = function
28 Past.Seq(elem,seq) ->
29 let (elem,ea) = get_before_element a elem in
30 let (seq,sla) = get_before ea seq in
31 (Past.Seq(elem,seq),sla)
32 | Past.Empty -> (Past.Empty,a)
33 | Past.SExists(var,seq) -> failwith "not possible"
34
35 and get_before_element a = function
36 Past.Term(term) as s -> (s,[s])
37 | Past.Or(seq1,seq2) ->
38 let (seq1,seq1a) = get_before a seq1 in
39 let (seq2,seq2a) = get_before a seq2 in
40 (Past.Or(seq1,seq2),Common.union_set seq1a seq2a)
41 | Past.DInfo(dots,_,seq_aft) ->
42 let dots = get_before_dots a dots in
43 (Past.DInfo(dots,a,seq_aft),a)
44 | Past.EExists(var,seq) -> failwith "not possible"
45
46 and get_before_dots a = function
47 Past.Dots -> Past.Dots
48 | Past.Nest(seq) ->
49 let (seq,_) = get_before a seq in
50 Past.Nest(seq)
51 | Past.When(dots,seq) ->
52 let dots = get_before_dots a dots in
53 let (seq,_) = get_before [] seq in
54 Past.When(dots,seq)
55 | Past.DExists(var,dots) -> failwith "not possible"
56
57 (* --------------------------------------------------------------------- *)
58
59 let rec get_after a = function
60 Past.Seq(elem,seq) ->
61 let (seq,sla) = get_after a seq in
62 let (elem,ea) = get_after_element sla elem in
63 (Past.Seq(elem,seq),ea)
64 | Past.Empty -> (Past.Empty,a)
65 | Past.SExists(var,seq) -> failwith "not possible"
66
67 and get_after_element a = function
68 Past.Term(term) as s -> (s,[s])
69 | Past.Or(seq1,seq2) ->
70 let (seq1,seq1a) = get_after a seq1 in
71 let (seq2,seq2a) = get_after a seq2 in
72 (Past.Or(seq1,seq2),Common.union_set seq1a seq2a)
73 | Past.DInfo(dots,seq_bef,_) ->
74 let dots = get_after_dots a dots in
75 (Past.DInfo(dots,seq_bef,a),a)
76 | Past.EExists(var,seq) -> failwith "not possible"
77
78 and get_after_dots a = function
79 Past.Dots -> Past.Dots
80 | Past.Nest(seq) ->
81 let (seq,_) = get_after (Common.union_set (get_first [] seq) a) seq in
82 Past.Nest(seq)
83 | Past.When(dots,seq) ->
84 let dots = get_after_dots a dots in
85 let (seq,_) = get_after [] seq in
86 Past.When(dots,seq)
87 | Past.DExists(var,dots) -> failwith "not possible"
88
89 (* --------------------------------------------------------------------- *)
90 (* like get_after, but just returns the a component; doesn't modify the term *)
91
92 and get_first a = function
93 Past.Seq(elem,seq) ->
94 let sla = get_first a seq in
95 let ea = get_first_element sla elem in
96 ea
97 | Past.Empty -> a
98 | Past.SExists(var,seq) -> failwith "not possible"
99
100 and get_first_element a = function
101 Past.Term(term) as s -> [s]
102 | Past.Or(seq1,seq2) ->
103 Common.union_set (get_first a seq1) (get_first a seq2)
104 | Past.DInfo(dots,_,_) -> a
105 | Past.EExists(var,seq) -> failwith "not possible"
106
107 (* --------------------------------------------------------------------- *)
108 (* Entry point *)
109
110 let insert_befaft sl =
111 let (sl,_) = get_before [] sl in
112 let (sl,_) = get_after [] sl in
113 sl