1 % (c) 2018-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(regexp, [regexp_init/0,
6 regexp_match/2, regexp_match/4,
7 is_regexp/1,
8 regexp_replace/4, regexp_replace/5,
9 regexp_search_first/3, regexp_search_first/4,
10 regexp_search_first_detailed/4, regexp_search_first_detailed/5,
11 regexp_search_all/3, regexp_isearch_all/3, regexp_search_all/4,
12 get_cpp_version/1]).
13
14
15 /* the interface to a heap global variable stored and maintained in C */
16
17
18 foreign(cpp_regex_match,cpp_regex_match(+string,+string,+integer,[-integer])).
19 foreign(cpp_regex_imatch,cpp_regex_imatch(+string,+string,+integer,[-integer])). % i for ignore case
20 foreign(cpp_regex_replace,cpp_regex_replace(+string,+string,+string,[-atom])).
21 foreign(cpp_regex_ireplace,cpp_regex_ireplace(+string,+string,+string,[-atom])). % i for ignore case
22 foreign(cpp_regex_search_all_str,cpp_regex_search_all_str(+string,+string,[-term])).
23 foreign(cpp_regex_isearch_all_str,cpp_regex_isearch_all_str(+string,+string,[-term])). % i for ignore case
24 foreign(cpp_regex_search_first_str,cpp_regex_search_first_str(+string,+string,[-atom])).
25 foreign(cpp_regex_isearch_first_str,cpp_regex_isearch_first_str(+string,+string,[-atom])). % i for ignore case
26 foreign(cpp_regex_search_first,cpp_regex_search_first(+string,+integer,+string,[-term])).
27 foreign(cpp_regex_isearch_first,cpp_regex_isearch_first(+string,+integer,+string,[-term])). % i for ignore case
28 foreign(cpp_version,cpp_version([-term])).
29
30 foreign_resource(regexp,
31 [
32 cpp_regex_match,
33 cpp_regex_imatch,
34 cpp_regex_replace,
35 cpp_regex_ireplace,
36 cpp_regex_search_first_str,
37 cpp_regex_isearch_first_str,
38 cpp_regex_search_first,
39 cpp_regex_isearch_first,
40 cpp_regex_search_all_str,
41 cpp_regex_isearch_all_str,
42 cpp_version
43 ]).
44
45 :- use_module(probsrc(error_manager)).
46
47 regexp_match(String,Pattern) :- regexp_match(String,Pattern,match_case,unknown).
48
49 % match an atom agains a regular expression in ECMA syntax also represented as an atom
50 regexp_match(String,Pattern,IgnoreCase,Span) :-
51 atom(String), atom(Pattern),
52 !,
53 regexp_init,
54 %format('regexp_match: Checking String ~w for Pattern ~w with IngoreCase=~w~n',[String,Pattern,IgnoreCase]),
55 (ignore_case(IgnoreCase)
56 -> cpp_regex_imatch(String,Pattern,1,Res)
57 ; cpp_regex_match(String,Pattern,1,Res)
58 ),
59 %format('String ~w for Pattern ~w: Result=~w~n',[String,Pattern,Res]),
60 (Res=1 -> true
61 ; Res=3
62 -> add_error(regexp,'Regular expression match too complex (backtracking limit of C++ regex library exceeded) for pattern: ',Pattern,Span),
63 fail
64 ; Res>1
65 -> add_error(regexp,'Illegal Regular Expression Pattern (use ECMAScript syntax): ',Pattern,Span),
66 fail).
67 regexp_match(String,Pattern,IgnoreCase,_) :-
68 format(user_error,'Illegal call: ~w~n',[regexp_match(String,Pattern,IgnoreCase)]),
69 fail.
70
71 % check if an atom is a valid regular expression pattern
72 is_regexp(Pattern) :- atom(Pattern),
73 !,
74 regexp_init,
75 cpp_regex_match('',Pattern,0,Res), % 0: do not print error messages for an illegal pattern
76 Res \== 2. % 0 or 1 means pattern was ok, 3 means match was too complex but pattern is ok
77 is_regexp(Pattern) :-
78 format(user_error,'Illegal call: ~w~n',[is_regexp(Pattern)]),
79 fail.
80
81 regexp_replace(String,Pattern,ReplStr,Result) :-
82 regexp_replace(String,Pattern,match_case,ReplStr,Result).
83
84 % match an atom agains a regular expression in ECMA syntax also represented as an atom
85 regexp_replace(String,Pattern,IgnoreCase,ReplStr,Result) :-
86 atom(String), atom(Pattern), atom(ReplStr),
87 !,
88 regexp_init,
89 (ignore_case(IgnoreCase)
90 -> cpp_regex_ireplace(String,Pattern,ReplStr,Result)
91 ; cpp_regex_replace(String,Pattern,ReplStr,Result)
92 ).
93 %,format('Replace ~w for Pattern ~w with ~w: Result=~w~n',[String,Pattern,ReplStr,Result]).
94 regexp_replace(String,Pattern,IC,ReplStr,Result) :-
95 format(user_error,'Illegal call: ~w~n',[regexp_replace(String,Pattern,IC,ReplStr,Result)]),
96 fail.
97
98 regexp_search_first(String,Pattern,Result) :- regexp_search_first(String,Pattern,match_case,Result).
99
100 % search for a first match of a pattern; returns empty string if no match found
101 regexp_search_first(String,Pattern,IgnoreCase,Result) :-
102 atom(String), atom(Pattern),
103 !,
104 regexp_init,
105 (ignore_case(IgnoreCase)
106 -> cpp_regex_isearch_first_str(String,Pattern,Result)
107 ; cpp_regex_search_first_str(String,Pattern,Result)
108 ).
109 %,format('Search ~w for Pattern ~w: Result=~w~n',[String,Pattern,Result]).
110 regexp_search_first(String,Pattern,IgnoreCase,Result) :-
111 format(user_error,'Illegal call: ~w~n',[regexp_search_first(String,Pattern,IgnoreCase,Result)]),
112 fail.
113
114 regexp_search_first_detailed(String,FromPosition,Pattern,Result) :-
115 regexp_search_first_detailed(String,FromPosition,Pattern,match_case,Result).
116 % search for a first match of a pattern;
117 % returns either match(Pos,Len,[MatchAtom|SubMatches]) or no-match
118 regexp_search_first_detailed(String,FromPosition,Pattern,IgnoreCase,Result) :-
119 atom(String), atom(Pattern), FromPosition>0,
120 !,
121 regexp_init,
122 Offset is FromPosition-1,
123 (ignore_case(IgnoreCase)
124 -> cpp_regex_isearch_first(String,Offset,Pattern,Result)
125 ; cpp_regex_search_first(String,Offset,Pattern,Result)
126 ).
127 %, format('Search ~w for Pattern ~w: Result=~w~n',[String,Pattern,Result]).
128 regexp_search_first_detailed(String,From,Pattern,IgnoreCase,Result) :- From<1,!,
129 format(user_error,'From index must be at least 1: ~w~n',[regexp_search_first_detailed(String,From,Pattern,IgnoreCase,Result)]),
130 fail.
131 regexp_search_first_detailed(String,From,Pattern,IgnoreCase,Result) :-
132 format(user_error,'Illegal call: ~w~n',[regexp_search_first_detailed(String,From,Pattern,IgnoreCase,Result)]),
133 fail.
134
135 % ---------------------
136
137 :- use_module(library(lists),[reverse/2]).
138 % search for a first match of a pattern; returns empty string if no match found
139 regexp_search_all(String,Pattern,Result) :- regexp_search_all(String,Pattern,match_case,Result).
140 regexp_isearch_all(String,Pattern,Result) :- regexp_search_all(String,Pattern,ignore_case,Result).
141
142 regexp_search_all(String,Pattern,IgnoreCase,Result) :-
143 atom(String), atom(Pattern),
144 !,
145 regexp_init,
146 (ignore_case(IgnoreCase)
147 -> cpp_regex_isearch_all_str(String,Pattern,CRes)
148 ; cpp_regex_search_all_str(String,Pattern,CRes)
149 ),
150 reverse(CRes,Result).
151 %,format('Search ~w for Pattern ~w: Result=~w~n',[String,Pattern,Result]).
152 regexp_search_all(String,Pattern,IC,Result) :-
153 format(user_error,'Illegal call: ~w~n',[regexp_search_all(String,Pattern,IC,Result)]),
154 fail.
155
156 ignore_case(ignore_case) :- !.
157 ignore_case(match_case) :- !,fail.
158 ignore_case(IgnoreCase) :- format(user_error,'Illegal case: ~w~n',[IgnoreCase]),fail.
159
160 % ---------------------
161
162 %get_cpp_version(unknown) :- !.
163 get_cpp_version(Version) :- regexp_init,
164 cpp_version(Version).
165
166 % ---------------------
167
168 :- dynamic loaded/1.
169
170 :- use_module(probsrc(pathes_lib),[safe_load_foreign_resource/2]).
171
172 % loads the library once; if loading fails, the error is reported once by safe_load_foreign_resource
173 % and regexp_init fails from then on (without trying to load the library again)
174 regexp_init :-
175 (loaded(Res) -> true
176 ; %assert_dir,
177 %assertz(user:library_directory(prob_home('.'))),
178 (safe_load_foreign_resource(regexp,regexp) -> Res = success ; Res = failure),
179 assertz(loaded(Res))
180 ),
181 Res = success.
182
183 %assert_dir :- (user:library_directory('.') -> true ; assertz(user:library_directory('.'))).
184
185