1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548 | --------------------------------------------------------------------
--
-- Unit Name: Tash.Regexp body
--
-- File Name: tash-regexp.adb
--
-- Purpose: Provides regular expression pattern matching
-- on Tash strings and lists.
--
-- Copyright (c) 1999-2000 Terry J. Westley
--
-- Tash is free software; you can redistribute it and/or modify it under
-- terms of the GNU General Public License as published by the Free
-- Software Foundation; either version 2, or (at your option) any later
-- version. Tash is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
-- for more details. You should have received a copy of the GNU General
-- Public License distributed with Tash; see file COPYING. If not, write to
--
-- Free Software Foundation
-- 59 Temple Place - Suite 330
-- Boston, MA 02111-1307, USA
--
-- As a special exception, if other files instantiate generics from this
-- unit, or you link this unit with other files to produce an executable,
-- this unit does not by itself cause the resulting executable to be
-- covered by the GNU General Public License. This exception does not
-- however invalidate any other reasons why the executable file might be
-- covered by the GNU Public License.
--
-- Tash is maintained at http://tcladashell.sourceforge.net/.
--
--------------------------------------------------------------------
with CHelper;
with Interfaces.C.Strings;
with System;
with Tcl.Ada;
package body Tash.Regexp is
function To_CString
(TString : in Tash_Regexp) return Interfaces.C.Strings.chars_ptr;
function CFlags
(Mode : in Regexp_Mode;
Expanded : in Boolean;
Ignore_Case : in Boolean;
Line : in Boolean;
Line_Stop : in Boolean;
Line_Anchor : in Boolean) return Interfaces.C.int;
function Match
(Source : in Tcl.Tcl_Obj;
Pattern : in Tash_Regexp;
Mode : in Regexp_Mode := Advanced;
Expanded : in Boolean := False;
Ignore_Case : in Boolean := False;
Line : in Boolean := False;
Line_Stop : in Boolean := False;
Line_Anchor : in Boolean := False) return Boolean;
use type Ada.Strings.Direction;
use type Interfaces.C.int;
use type Interfaces.C.long;
use type Interfaces.C.size_t;
use type Interfaces.C.Strings.chars_ptr;
use type Tcl.Tcl_RegExp;
function To_CString
(TString : in Tash_Regexp)
return Interfaces.C.Strings.chars_ptr
is
--
Length : aliased Interfaces.C.int;
begin -- To_CString
if Is_Null (TString) then
return Interfaces.C.Strings.Null_Ptr;
else
return Tcl.Tcl_GetStringFromObj (TString.Obj, Length'Access);
end if;
end To_CString;
function CFlags
(Mode : in Regexp_Mode;
Expanded : in Boolean;
Ignore_Case : in Boolean;
Line : in Boolean;
Line_Stop : in Boolean;
Line_Anchor : in Boolean)
return Interfaces.C.int
is
--
Flags : Interfaces.C.int;
begin -- CFlags
case Mode is
when Basic =>
Flags := Tcl.TCL_REG_BASIC;
when Extended =>
Flags := Tcl.TCL_REG_EXTENDED;
when Advanced =>
Flags := Tcl.TCL_REG_ADVANCED;
when Quote =>
Flags := Tcl.TCL_REG_QUOTE;
end case;
if Expanded then
Flags := Flags + Tcl.TCL_REG_EXPANDED;
end if;
if Ignore_Case then
Flags := Flags + Tcl.TCL_REG_NOCASE;
end if;
if Line then
Flags := Flags + Tcl.TCL_REG_NEWLINE;
else
if Line_Stop then
Flags := Flags + Tcl.TCL_REG_NLSTOP;
end if;
if Line_Anchor then
Flags := Flags + Tcl.TCL_REG_NLANCH;
end if;
end if;
return Flags;
end CFlags;
procedure RegDump (R : Tcl.Tcl_RegExp);
pragma Import (C, RegDump, "regdump");
pragma Unreferenced (RegDump); -- XXX what is it for, then?
-------------------------------------------------
-- Converting a string to a Tash_Regexp is
-- merely converting it to a Tcl object. Later,
-- when the object is first used as a pattern,
-- the pattern will be compiled and cached in
-- the object.
-------------------------------------------------
function To_Tash_Regexp (Str : in String) return Tash_Regexp is
--
C_TString : aliased Interfaces.C.char_array := Interfaces.C.To_C (Str);
New_Obj : Tcl.Tcl_Obj;
begin -- To_Tash_Regexp
New_Obj :=
Tcl.Tcl_NewStringObj
(Interfaces.C.Strings.To_Chars_Ptr (C_TString'Unchecked_Access),
Interfaces.C.int (Str'Length));
Tcl.Tcl_IncrRefCount (New_Obj);
return (Ada.Finalization.Controlled with Obj => New_Obj);
end To_Tash_Regexp;
function "+" (Str : in String) return Tash_Regexp is
begin -- "+"
return To_Tash_Regexp (Str);
end "+";
pragma Inline ("+");
function To_String (Regexp : in Tash_Regexp) return String is
begin -- To_String
if Is_Null (Regexp) then
return "";
else
return CHelper.Value (To_CString (Regexp));
end if;
end To_String;
function Match
(Source : in Tcl.Tcl_Obj;
Pattern : in Tash_Regexp;
Mode : in Regexp_Mode := Advanced;
Expanded : in Boolean := False;
Ignore_Case : in Boolean := False;
Line : in Boolean := False;
Line_Stop : in Boolean := False;
Line_Anchor : in Boolean := False)
return Boolean
is
--
Flags : Interfaces.C.int;
Regexp : Tcl.Tcl_RegExp;
Result : Interfaces.C.int;
Interp : Tcl.Tcl_Interp;
begin -- Match
Flags :=
CFlags
(Mode => Mode,
Expanded => Expanded,
Ignore_Case => Ignore_Case,
Line => Line,
Line_Stop => Line_Stop,
Line_Anchor => Line_Anchor);
Tash_Interp.Get (Interp);
Regexp := Tcl.Tcl_GetRegExpFromObj (Interp, Pattern.Obj, Flags);
if Regexp = null then
Tash_Interp.Raise_Exception (Interp, Regexp_Error'Identity);
end if;
Result := Tcl.Tcl_RegExpExecObj (Interp, Regexp, Source, 0, 0, 0);
if Result < 0 then
Tash_Interp.Raise_Exception (Interp, Regexp_Error'Identity);
end if;
Tash_Interp.Release (Interp);
return Result = 1;
end Match;
function Match
(Source : in String;
Pattern : in String;
Mode : in Regexp_Mode := Advanced;
Expanded : in Boolean := False;
Ignore_Case : in Boolean := False;
Line : in Boolean := False;
Line_Stop : in Boolean := False;
Line_Anchor : in Boolean := False)
return Boolean
is
begin -- Match
return Match
(Source => Tash.To_Tcl_Obj (Source),
Pattern => To_Tash_Regexp (Pattern),
Mode => Mode,
Expanded => Expanded,
Ignore_Case => Ignore_Case,
Line => Line,
Line_Stop => Line_Stop,
Line_Anchor => Line_Anchor);
end Match;
function Match
(Source : in String;
Pattern : in Tash_Regexp;
Mode : in Regexp_Mode := Advanced;
Expanded : in Boolean := False;
Ignore_Case : in Boolean := False;
Line : in Boolean := False;
Line_Stop : in Boolean := False;
Line_Anchor : in Boolean := False)
return Boolean
is
begin -- Match
return Match
(Source => Tash.To_Tcl_Obj (Source),
Pattern => Pattern,
Mode => Mode,
Expanded => Expanded,
Ignore_Case => Ignore_Case,
Line => Line,
Line_Stop => Line_Stop,
Line_Anchor => Line_Anchor);
end Match;
function Match_Element
(Source : in Tash.Lists.Tash_List;
Pattern : in String;
Going : in Ada.Strings.Direction := Ada.Strings.Forward;
Mode : in Regexp_Mode := Advanced;
Expanded : in Boolean := False;
Ignore_Case : in Boolean := False;
Line : in Boolean := False;
Line_Stop : in Boolean := False;
Line_Anchor : in Boolean := False)
return Natural
is
begin -- Match_Element
return Match_Element
(Source => Source,
Pattern => To_Tash_Regexp (Pattern),
Going => Going,
Mode => Mode,
Expanded => Expanded,
Ignore_Case => Ignore_Case,
Line => Line,
Line_Stop => Line_Stop,
Line_Anchor => Line_Anchor);
end Match_Element;
function Match_Element
(Source : in Tash.Lists.Tash_List;
Pattern : in Tash_Regexp;
Going : in Ada.Strings.Direction := Ada.Strings.Forward;
Mode : in Regexp_Mode := Advanced;
Expanded : in Boolean := False;
Ignore_Case : in Boolean := False;
Line : in Boolean := False;
Line_Stop : in Boolean := False;
Line_Anchor : in Boolean := False)
return Natural
is
begin -- Match_Element
if Going = Ada.Strings.Forward then
for I in 1 .. Tash.Lists.Length (Source) loop
if Match
(Tash.Lists.Get_Element (Source, I),
Pattern,
Mode,
Expanded,
Ignore_Case,
Line,
Line_Stop,
Line_Anchor)
then
return I;
end if;
end loop;
else
for I in reverse 1 .. Tash.Lists.Length (Source) loop
if Match
(Tash.Lists.Get_Element (Source, I),
Pattern,
Mode,
Expanded,
Ignore_Case,
Line,
Line_Stop,
Line_Anchor)
then
return I;
end if;
end loop;
end if;
return 0;
end Match_Element;
function Match
(Source : in String;
Pattern : in Tash_Regexp;
Mode : in Regexp_Mode := Advanced;
Expanded : in Boolean := False;
Ignore_Case : in Boolean := False;
Line : in Boolean := False;
Line_Stop : in Boolean := False;
Line_Anchor : in Boolean := False)
return Tash.Lists.Tash_List
is
Flags : Interfaces.C.int;
Interp : Tcl.Tcl_Interp;
Regexp : Tcl.Tcl_RegExp;
Result : Interfaces.C.int;
InfoRec : aliased Tcl.Tcl_RegExpInfo_Rec;
Matches : Tcl.Tcl_RegExpIndices;
Substrings : Tash.Lists.Tash_List;
begin -- Match
Flags :=
CFlags
(Mode => Mode,
Expanded => Expanded,
Ignore_Case => Ignore_Case,
Line => Line,
Line_Stop => Line_Stop,
Line_Anchor => Line_Anchor);
Tash_Interp.Get (Interp);
-- get regular expression
-------------------------
Regexp := Tcl.Tcl_GetRegExpFromObj (Interp, Pattern.Obj, Flags);
if Regexp = null then
Tash_Interp.Raise_Exception (Interp, Regexp_Error'Identity);
end if;
-- perform regexp match
-----------------------
Result :=
Tcl.Tcl_RegExpExecObj
(Interp,
Regexp,
Tash.To_Tcl_Obj (Source),
0,
-1,
0);
if Result < 0 then
Tash_Interp.Raise_Exception (Interp, Regexp_Error'Identity);
else
Tash_Interp.Release (Interp);
end if;
-- get information about this match
-----------------------------------
Tcl.Tcl_RegExpGetInfo (Regexp, InfoRec'Unchecked_Access);
Matches := InfoRec.matches;
-- Each iteration of this loop gets the next matching
-- substring and appends it to Substrings.
-----------------------------------------------------
for I in 1 .. InfoRec.nsubs + 1 loop
declare
Substring : constant String :=
Source (
Positive (Matches.Start + 1) .. Positive (Matches.E_n_d));
begin
Tash.Lists.Append (Substrings, Substring);
end;
Tcl.Tcl_RegExpIndices_Pointers.Increment (Matches);
end loop;
return Substrings;
end Match;
function Match
(Source : in String;
Pattern : in String;
Mode : in Regexp_Mode := Advanced;
Expanded : in Boolean := False;
Ignore_Case : in Boolean := False;
Line : in Boolean := False;
Line_Stop : in Boolean := False;
Line_Anchor : in Boolean := False)
return Tash.Lists.Tash_List
is
begin -- Match
return Match
(Source => Source,
Pattern => To_Tash_Regexp (Pattern),
Mode => Mode,
Expanded => Expanded,
Ignore_Case => Ignore_Case,
Line => Line,
Line_Stop => Line_Stop,
Line_Anchor => Line_Anchor);
end Match;
function Tcl_RegsubObjCmd
(dummy : in Tcl.ClientData;
interp : in Tcl.Tcl_Interp;
objc : in Interfaces.C.int;
objv : in Tcl.Tcl_Obj_Array)
return Interfaces.C.int;
pragma Import (C, Tcl_RegsubObjCmd, "Tcl_RegsubObjCmd");
function Substitute
(Source : in String;
Pattern : in String;
Sub_Spec : in String;
Sub_All : in Boolean := False;
Expanded : in Boolean := False;
Ignore_Case : in Boolean := False;
Line : in Boolean := False;
Line_Stop : in Boolean := False;
Line_Anchor : in Boolean := False)
return String
is
Objc : Interfaces.C.int;
Interp : Tcl.Tcl_Interp;
Result : Interfaces.C.int;
begin -- Substitute
-- Count how many object arguments we'll
-- have when calling Tcl_RegsubObjCmd.
----------------------------------------
Objc := 6;
if Sub_All then
Objc := Objc + 1;
end if;
if Expanded then
Objc := Objc + 1;
end if;
if Ignore_Case then
Objc := Objc + 1;
end if;
if Line then
Objc := Objc + 1;
end if;
if Line_Stop then
Objc := Objc + 1;
end if;
if Line_Anchor then
Objc := Objc + 1;
end if;
declare
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
I : Interfaces.C.int := 1;
begin
-- Create object arguments for
-- call to Tcl_RegesubObjCmd.
------------------------------
Objv (I) := Tash.To_Tcl_Obj ("regsub");
I := I + 1;
if Sub_All then
Objv (I) := Tash.To_Tcl_Obj ("-all");
I := I + 1;
end if;
if Expanded then
Objv (I) := Tash.To_Tcl_Obj ("-expanded");
I := I + 1;
end if;
if Ignore_Case then
Objv (I) := Tash.To_Tcl_Obj ("-nocase");
I := I + 1;
end if;
if Line then
Objv (I) := Tash.To_Tcl_Obj ("-line");
I := I + 1;
end if;
if Line_Stop then
Objv (I) := Tash.To_Tcl_Obj ("-linestop");
I := I + 1;
end if;
if Line_Anchor then
Objv (I) := Tash.To_Tcl_Obj ("-lineanchor");
I := I + 1;
end if;
Objv (I) := Tash.To_Tcl_Obj ("--");
I := I + 1;
Objv (I) := Tash.To_Tcl_Obj (Pattern);
I := I + 1;
Objv (I) := Tash.To_Tcl_Obj (Source);
I := I + 1;
Objv (I) := Tash.To_Tcl_Obj (Sub_Spec);
I := I + 1;
-- Create a variable in which regsub will store the
-- modified string. Store the name of this variable
-- in the last object parameter for regsub.
----------------------------------------------------
Tash_Interp.Get (Interp);
Tcl.Ada.Tcl_SetVar (Interp, "_TASH_Regsub_Result", "");
Objv (I) := Tash.To_Tcl_Obj ("_TASH_Regsub_Result");
-- Perform the substitution(s) and check for errors
---------------------------------------------------
Tcl.Tcl_ResetResult (Interp);
Result :=
Tcl_RegsubObjCmd
(dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tash_Interp.Assert (Interp, Result, Regexp_Error'Identity);
-- Get and return the regsub'd string
-------------------------------------
declare
Regsub_Result : constant String :=
Tcl.Ada.Tcl_GetVar (Interp, "_TASH_Regsub_Result");
begin
Tcl.Ada.Tcl_UnsetVar (Interp, "_TASH_Regsub_Result");
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
return Regsub_Result;
end;
end;
end Substitute;
end Tash.Regexp;
|