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