| \ Regexp compile |
\ Regexp compile |
| |
|
| \ Copyright (C) 2005,2006 Free Software Foundation, Inc. |
\ Copyright (C) 2005,2006,2007,2008 Free Software Foundation, Inc. |
| |
|
| \ This file is part of Gforth. |
\ This file is part of Gforth. |
| |
|
| \ Gforth is free software; you can redistribute it and/or |
\ Gforth is free software; you can redistribute it and/or |
| \ modify it under the terms of the GNU General Public License |
\ modify it under the terms of the GNU General Public License |
| \ as published by the Free Software Foundation; either version 2 |
\ as published by the Free Software Foundation, either version 3 |
| \ of the License, or (at your option) any later version. |
\ of the License, or (at your option) any later version. |
| |
|
| \ This program is distributed in the hope that it will be useful, |
\ This program is distributed in the hope that it will be useful, |
| \ GNU General Public License for more details. |
\ GNU General Public License for more details. |
| |
|
| \ You should have received a copy of the GNU General Public License |
\ You should have received a copy of the GNU General Public License |
| \ along with this program; if not, write to the Free Software |
\ along with this program. If not, see http://www.gnu.org/licenses/. |
| \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
|
| |
|
| \ The idea of the parser is the following: |
\ The idea of the parser is the following: |
| \ As long as there's a match, continue |
\ As long as there's a match, continue |
| : ` ( "char" -- ) \ regexp-pattern |
: ` ( "char" -- ) \ regexp-pattern |
| \G check for particular char |
\G check for particular char |
| ]] count [[ char ]] Literal <> ?LEAVE [[ ; immediate |
]] count [[ char ]] Literal <> ?LEAVE [[ ; immediate |
| |
: -` ( "char" -- ) \ regexp-pattern |
| \ A word for string comparison |
\G check for particular char |
| |
]] count [[ char ]] Literal = ?LEAVE [[ ; immediate |
| : $= ( addr1 addr2 u -- f ) tuck compare ; |
|
| : ,=" ( addr u -- ) tuck ]] dup SLiteral $= ?LEAVE Literal + noop [[ ; |
|
| : =" ( <string>" -- ) \ regexp-pattern |
|
| \G check for string |
|
| '" parse ,=" ; immediate |
|
| |
|
| \ loop stack |
\ loop stack |
| |
|
| 0 Value end$ |
0 Value end$ |
| 0 Value start$ |
0 Value start$ |
| : !end ( addr u -- addr ) over + to end$ dup to start$ ; |
: !end ( addr u -- addr ) over + to end$ dup to start$ ; |
| : $? ( addr -- addr flag ) dup end$ u< ; |
: end-rex? ( addr -- addr flag ) dup end$ u< ; |
| : ^? ( addr -- addr flag ) dup start$ u> ; |
: start-rex? ( addr -- addr flag ) dup start$ u> ; |
| : ?end ( addr -- addr ) ]] dup end$ u> ?LEAVE [[ ; immediate |
: ?end ( addr -- addr ) ]] dup end$ u> ?LEAVE [[ ; immediate |
| |
|
| \ start and end |
\ start and end |
| |
|
| : \^ ( addr -- addr ) \ regexp-pattern |
: \^ ( addr -- addr ) \ regexp-pattern |
| \G check for string start |
\G check for string start |
| ]] ^? ?LEAVE [[ ; immediate |
]] start-rex? ?LEAVE [[ ; immediate |
| : \$ ( addr -- addr ) \ regexp-pattern |
: \$ ( addr -- addr ) \ regexp-pattern |
| \G check for string end |
\G check for string end |
| ]] $? ?LEAVE [[ ; immediate |
]] end-rex? ?LEAVE [[ ; immediate |
| |
|
| |
\ A word for string comparison |
| |
|
| |
: ,=" ( addr u -- ) tuck ]] dup SLiteral tuck compare ?LEAVE Literal + noop [[ ; |
| |
: =" ( <string>" -- ) \ regexp-pattern |
| |
\G check for string |
| |
'" parse ,=" ; immediate |
| |
|
| \ regexp block |
\ regexp block |
| |
|
| immediate |
immediate |
| : n*} ( sys n -- ) \ regexp-pattern |
: n*} ( sys n -- ) \ regexp-pattern |
| \G At least @var{n} pattern |
\G At least @var{n} pattern |
| >r ]] r> 1+ >r $? 0= UNTIL dup [[ DONE, ]] drop [[ |
>r ]] r> 1+ >r end-rex? 0= UNTIL dup [[ DONE, ]] drop [[ |
| r@ IF r@ ]] r@ Literal u< IF r> 1+ drops false EXIT THEN [[ THEN |
r@ IF r@ ]] r@ Literal u< IF r> 1+ drops false EXIT THEN [[ THEN |
| r@ ]] r> 1+ Literal U+DO FORK BUT [[ |
r@ ]] r> 1+ Literal U+DO FORK BUT [[ |
| ]] IF I' I - [[ r@ 1- ]] Literal + drops true UNLOOP EXIT THEN LOOP [[ |
]] IF I' I - [[ r@ 1- ]] Literal + drops true UNLOOP EXIT THEN LOOP [[ |
| \ idea: try to match one alternative and then the rest of regexp. |
\ idea: try to match one alternative and then the rest of regexp. |
| \ if that fails, jump back to second alternative |
\ if that fails, jump back to second alternative |
| |
|
| : THENs ( sys -- ) BEGIN dup WHILE ]] THEN [[ REPEAT drop ; |
: JOINs ( sys -- ) BEGIN dup WHILE ]] JOIN [[ REPEAT drop ; |
| |
|
| : {{ ( addr -- addr addr ) \ regexp-pattern |
: {{ ( addr -- addr addr ) \ regexp-pattern |
| \G Start of alternatives |
\G Start of alternatives |
| : || ( addr addr -- addr addr ) \ regexp-pattern |
: || ( addr addr -- addr addr ) \ regexp-pattern |
| \G separator between alternatives |
\G separator between alternatives |
| vars @ varsmax @ max varsmax ! |
vars @ varsmax @ max varsmax ! |
| ]] nip AHEAD [[ >r >r >r vars ! |
]] dup FORK IF 2drop true EXIT THEN drop dup [[ >r >r >r vars ! |
| ]] DONE drop dup [[ r> r> r> ]] BEGIN [[ vars @ ; immediate |
]] DONE drop dup [[ r> r> r> ]] BEGIN [[ vars @ ; immediate |
| : }} ( addr addr -- addr addr ) \ regexp-pattern |
: }} ( addr addr -- addr addr ) \ regexp-pattern |
| \G end of alternatives |
\G end of alternatives |
| vars @ varsmax @ max vars ! |
vars @ varsmax @ max vars ! |
| ]] nip AHEAD [[ >r >r >r drop |
]] dup FORK IF 2drop true EXIT THEN drop dup [[ >r >r >r drop |
| ]] DONE drop LEAVE [[ r> r> r> THENs ; immediate |
]] DONE drop LEAVE [[ r> r> r> JOINs ; immediate |
| |
|
| \ match variables |
\ match variables |
| |
|
| 0 Value >>ptr |
0 Value >>ptr |
| 0 Value <<ptr |
0 Value <<ptr |
| Variable >>string |
Variable >>string |
| : >> ( addr -- addr ) \ regexp-replace |
: s>> ( addr -- addr ) \ regexp-replace |
| \G Start replace pattern region |
\G Start replace pattern region |
| dup to >>ptr ; |
dup to >>ptr ; |
| : << ( run-addr addr u -- run-addr ) \ regexp-replace |
: << ( run-addr addr u -- run-addr ) \ regexp-replace |
| \G Replace string from start of replace pattern region with |
\G Replace string from start of replace pattern region with |
| \G @var{addr} @var{u} |
\G @var{addr} @var{u} |
| <<ptr 0= IF start$ to <<ptr THEN |
|
| >>string @ 0= IF s" " >>string $! THEN |
|
| <<ptr >>ptr over - >>string $+! |
<<ptr >>ptr over - >>string $+! |
| >>string $+! dup to <<ptr ; |
>>string $+! dup to <<ptr ; |
| : <<" ( "string<">" -- ) \ regexp-replace |
: <<" ( "string<">" -- ) \ regexp-replace |
| \G @var{string} |
\G @var{string} |
| '" parse postpone SLiteral postpone << ; immediate |
'" parse postpone SLiteral postpone << ; immediate |
| : >>string@ ( -- addr u ) |
: >>string@ ( -- addr u ) |
| >>string $@ >>string off |
>>string $@ ; |
| 0 to >>ptr 0 to <<ptr ; |
: >>string0 ( addr u -- addr u ) s" " >>string $! |
| |
0 to >>ptr over to <<ptr ; |
| : >>next ( -- addr u ) <<ptr end$ over - ; |
: >>next ( -- addr u ) <<ptr end$ over - ; |
| : s// ( -- sys ) \ regexp-replace |
: >>rest ( -- ) >>next >>string $+! ; |
| |
: s// ( addr u -- ptr ) |
| \G start search/replace loop |
\G start search/replace loop |
| ]] BEGIN [[ ; immediate |
]] >>string0 (( // s>> [[ ; immediate |
| : //g ( sys -- ) \ regexp-replace |
: >> ( addr -- addr ) |
| \G end search/replace loop |
]] <<ptr >>ptr u> ?LEAVE ?end [[ ; immediate |
| ]] WHILE >>next REPEAT end$ [[ |
: //s ( ptr -- ) |
| s" " ]] SLiteral << >>string@ rot drop [[ ; immediate |
\G search end |
| |
]] )) drop >>rest >>string@ [[ ; immediate |
| |
: //o ( ptr addr u -- addr' u' ) |
| |
\G end search/replace single loop |
| |
]] << //s [[ ; immediate |
| |
: //g ( ptr addr u -- addr' u' ) |
| |
\G end search/replace all loop |
| |
]] << LEAVE //s [[ ; immediate |