home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
pcre34.zip
/
PCRE34L.ZIP
/
rexxpcre.rexx
< prev
next >
Wrap
OS/2 REXX Batch file
|
2000-11-22
|
8KB
|
250 lines
/*---------------------------------------------------------------------
Copyright (c) 2000-2001, Vadim Yegorov
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------*/
call RxFuncAdd 'PCRELoad', 'REXXPCRE', 'PCRELoad'
call PCRELoad
/*-------------------------------------------------------------------------*/
/**
* è½áßß RxPCRE »αÑñßΓáó½∩ÑΓ «ß¡«ó¡δÑ ¼ÑΓ«ñδ ñ½∩ αáí«Γδ ß αÑúπ½∩α¡δ¼¿
* óδαáªÑ¡¿∩¼¿ ¿ ∩ó½∩ÑΓß∩ πñ«í¡«⌐ "«í±αΓ¬«⌐" ñ½∩ í¿í½¿«ΓѬ¿ libpcre.
*/
::class RxPCRE public
/* ¿¡¿µ¿á½¿º¿απÑΓ «íΩѬΓ. »áαá¼ÑΓαδ: ½«¬á½∞*/
::method init
expose curLocale curMask pcre lasterr pairs
use arg loc
if VAR('LOC') then
curLocale = PCREInit(loc);
else
curLocale = PCREInit("");
curMask = ""; /* current mask */
pcre = ""; /* «íΩѬΓ, σαá¡∩Θ¿⌐ «Γ¬«¼»½¿α«óá¡¡«Ñ óδαáªÑ¡¿Ñ */
lasterr = 0 ; /* ¡ÑΓ «Φ¿í«¬ ?! ;)*/
pairs = 0 ;
return self;
/* ó«ºóαáΘáÑΓ ½«¬á½∞, »α¿¼Ñ¡±¡¡πε ñ½∩ PCRE */
::method getLocale
expose curLocale
return curLocale;
/* ó«ºóαáΘáÑΓ ¿ß»«½∞ºπѼπε ¼á߬π*/
::method getMask
expose curMask
return curMask;
/** ¬«¼»¿½¿απÑΓ ¼á߬π.
* @syntax rc = setMask(mask)
* @param mask αÑúπ½∩α¡«Ñ óδαáªÑ¡¿Ñ ó ¡«Γᵿ¿ PERL'a: /regexp/[options]
* @return ¬«ñ «Φ¿í¬¿, 0 -- «Φ¿í«¬ ¡ÑΓ
*/
::method setMask
expose curMask pcre lasterr pairs subs
use arg mask
lasterr = PCRECompile('pcre_tmp', mask);
if lasterr = 0 then do
curMask = mask;
pcre = pcre_tmp;
/*-- mask analyzing --*/
n = 0; pairs = 1; l = mask~length
do i = 1 to l
ch = mask~substr(i,1);
if ch = '(' then
n = n + 1;
if ch = ')' then do
n = n - 1;
if n = 0 then
pairs = pairs + 1;
end
end
subs = .array~new(pairs);
end
return lasterr
/** »«¿ß¬ Σαáú¼Ñ¡Γ«ó
* @syntax rc = find(ofs_stem., str, [start_pos [,opts]])
* @param ofs_stem ßΓѼ ß¼ÑΘÑ¡¿⌐, ofs_stem.0 -- τ¿ß½« φ½Ñ¼Ñ¡Γ«ó
* @param str ßΓ᫬á
* @param start_pos ßΓáαΓ«óá∩ »«º¿µ¿∩, ¡Ñ«í∩ºáΓѽѡ
* @param opt «»µ¿¿, ¡Ñ«í∩ºáΓѽѡ, αáºαÑΦÑ¡δ Γ«½∞¬«:
* A - Anchored
* b - Not begin of line
* e - Not end of line
* g - Not empty
*
* @return ¬«ñ «Φ¿í¬¿
*/
::method find
expose pcre ofs lasterr
use arg L., str, pos, opts
if \VAR('POS') then
pos = 1;
if \VAR('OPTS') then
opts = "";
if \VAR('STR') then
str = "";
lasterr = PCREExec(L,pcre,str,pos,opts);
if lasterr = -1 then
L.0 = 0;
return lasterr;
/** »«¿ß¬ Σαáú¼Ñ¡Γ«ó
* @syntax rc = findSubs(str, [start_pos [,opts]])
* @param str ßΓ᫬á
* @param start_pos ßΓáαΓ«óá∩ »«º¿µ¿∩, ¡Ñ«í∩ºáΓѽѡ
* @param opt «»µ¿¿, ¡Ñ«í∩ºáΓѽѡ, αáºαÑΦÑ¡δ Γ«½∞¬«:
* A - Anchored
* b - Not begin of line
* e - Not end of line
* g - Not empty
*
* @return ¼áßß¿ó ßΓ᫬, ß«ñÑαªáΘ¿σ Σαáú¼Ñ¡Γδ ¿½¿ nil
*
*/
::method findSubs
expose pcre ofs lasterr subs
use arg str, pos, opts
if \VAR('POS') then
pos = 1;
if \VAR('OPTS') then
opts = "";
lasterr = PCREExec('LO',pcre,str,pos,opts);
if lasterr = 0 then do
n = 1;
do j = 1 to lo.0 by 2
k = j; kk = k + 1;
subs[n] = str~substr(lo.k, lo.kk - lo.k);
n = n + 1;
end
end; else
return .nil;
return subs;
/**
* ó«ºóαáΘáÑΓ ¬«ñ ºáóÑαΦÑ¡¿∩ »«ß½Ññ¡Ñ⌐ «»Ñαᵿ¿
*
* @return RC, 0 -- «Φ¿í«¬ ¡ÑΓ, -1 -- »«¿ß¬ ¡Ñπß»ÑΦÑ¡
*
*/
::method lastError
expose lasterr
return lasterr
/**
* ó«ºóαáΘáÑΓ óÑαß¿ε libpcre
*
* @return ßΓ᫬á óÑαß¿¿
*/
::method getVersion
return PCREVersion();
/**
* ó«ºóαáΘáÑΓ óÑαß¿ε rxlibpcre
*
* @return ßΓ᫬á óÑαß¿¿
*/
::method getRxVersion
return PCRERxVersion();
/**
* ó«ºóαáΘáÑΓ ßΓ᫬π ß ¿¼Ñ¡Ñ¼ áóΓ«αá í¿í½¿«ΓѬ¿ libpcre
*
*/
::method getAuthor
return PCREAuthor();
/**
* ó«ºóαáΘáÑΓ ßΓ᫬π ß ¿¼Ñ¡Ñ¼ áóΓ«αá »«αΓá.
*/
::method getPortAuthor
return PCREPortAuthor();
/*-------------------------------------------------------------------------*/
/** è½áßß »αÑñßΓáó½∩ÑΓ ß«í«⌐ ¡Ñ¬¿⌐ á¡á½«ú ¬«ßΓαπ¬µ¿¿ Perl'a:
* ($a, $b, $c) = /mask/ or do smt
* »α¿¼Ñα¡δ⌐ ¬«ñ ñ½∩ αáí«Γδ Γᬫ⌐:
* if re~parse(string_for_parsing) then
* call re[1], re[2], re[3];
* ¬áªñá∩ úαπ»»á '()' ó ¼á᪥ ñá±Γ «ñ¿¡ φ½Ñ¼Ñ¡Γ
*/
::class RxStrParser subclass RxPCRE public
::method init
expose ofs. slots instr
use arg l
super~new(l); /* set locale*/
ofs.0 = 0;
slots = 0;
instr = "";
return self;
/** αáºí¿αáÑΓ ßΓ᫬π
* @syntax rc = re~parse(string_for_parsing,[pos,[opts]])
* @return 1 -- Ñß½¿ ßΓ᫬á íδ½á αẫíαá¡á, ¿¡áτÑ 0 ¿
* «τÑ¡∞ αѬ«¼Ñ¡ñπÑΓß∩ »«ß¼«ΓαÑΓ∞ ¡á «Φ¿í¬¿.
*/
::method parse
expose ofs. slots instr
use arg str , pos, opts
if \VAR('POS') then
pos = 1;
if \VAR('OPTS') then
opts = "";
if self~find(ofs.,str,pos,opts) = 0 then do
/* found */
slots = ofs.0/2;
instr = str
return 1;
end
/* not found or error occured, see lastError message */
ofs.0 = 0;
return 0;
/**
* ó«ºóαáΘáÑΓ »«ñßΓ᫬π ñ½∩ ß««Γó. φ½Ñ¼Ñ¡Γá ¼á߬¿.
* »πßΓá∩ »«ñßΓ᫬á ΓᬠªÑ ¼«ªÑΓ «º¡áτΓ∞ «Φ¿í¬¿ (߬áªÑ¼
* 'index out of range')
*
* @syntax str = re[5];
* @return »«ñßΓ᫬á
*/
::method '[]'
expose ofs. slots instr
use arg idx
if ofs.0 = 0 | idx >= slots then
return ""; /* out of range */
/*calc real offsets*/
L = idx * 2 + 1;
H = L + 1;
return instr~substr(ofs.L, ofs.H - ofs.L);
/*-------------------------------------------------------------------------*/