[gforth] / gforth / regexp.fs  

gforth: gforth/regexp.fs

Diff for /gforth/regexp.fs between version 1.19 and 1.25

version 1.19, Sun Sep 12 16:18:31 2010 UTC version 1.25, Sun Oct 10 19:39:34 2010 UTC
Line 113 
Line 113 
 : loops> ( -- addr ) -3 loops +!  loops @+ swap cells + 3@ ;  : loops> ( -- addr ) -3 loops +!  loops @+ swap cells + 3@ ;
 : >loops ( addr -- ) loops @+ swap cells + 3! 3 loops +! ;  : >loops ( addr -- ) loops @+ swap cells + 3! 3 loops +! ;
 : BEGIN, ( -- )  ]] BEGIN [[ >loops ;  : BEGIN, ( -- )  ]] BEGIN [[ >loops ;
 : DONE, ( -- )  loops @ IF  loops> ]] DONE [[ THEN ]] noop [[ ;  : DONE, ( -- )  loops @ IF  loops> ]] DONE [[ ELSE ." no done left!" cr THEN ;
   
 \ variables  \ variables
   
Line 135 
Line 135 
 : end-rex? ( addr -- addr flag ) dup end$ u< ;  : end-rex? ( addr -- addr flag ) dup end$ u< ;
 : start-rex? ( 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
   : rest$ ( addr -- addr addr u ) dup end$ over - ;
   
 \ start and end  \ start and end
   
Line 147 
Line 148 
   
 \ A word for string comparison  \ A word for string comparison
   
 : ,=" ( addr u -- ) tuck ]] dup SLiteral tuck compare ?LEAVE Literal + noop [[ ;  : (str=?) ( addr1 addr u -- addr2 )
       dup >r 2>r rest$ r@ umin 2r> compare IF rdrop true ELSE r> + false THEN ;
   : str=? ( addr1 addr u -- addr2 ) ]] (str=?) ?LEAVE [[ ; immediate
   : ,=" ( addr u -- ) tuck dup ]] rest$ Literal umin SLiteral compare ?LEAVE Literal + [[ ;
 : =" ( <string>" -- ) \ regexp-pattern  : =" ( <string>" -- ) \ regexp-pattern
     \G check for string      \G check for string
     '" parse ,=" ; immediate      '" parse ,=" ; immediate
Line 182 
Line 186 
 : 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 end-rex? 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  ;S  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 ;S  THEN  LOOP [[      ]] IF  I' I - [[ r@ 1- ]] Literal + drops true UNLOOP ;S  THEN  LOOP [[
     r@ IF  r@ ]] Literal drops [[ THEN      r@ IF  r@ ]] Literal drops [[ THEN
     rdrop ]] false  ;S  JOIN [[ ; immediate      rdrop ]]  dup LEAVE  JOIN [[ ; immediate
 : **} ( sys -- ) \ regexp-pattern  : **} ( sys -- ) \ regexp-pattern
     \G end of greedy zero-or-more pattern      \G end of greedy zero-or-more pattern
     0 postpone n*} ; immediate      0 postpone n*} ; immediate
Line 212 
Line 215 
 : +} ( addr addr' -- addr' ) \ regexp-pattern  : +} ( addr addr' -- addr' ) \ regexp-pattern
     \G end of non-greedy one-or-more pattern      \G end of non-greedy one-or-more pattern
     ]] dup FORK BUT  IF  drop true  ;S [[      ]] dup FORK BUT  IF  drop true  ;S [[
     DONE, ]] drop false  ;S  THEN *} [[ ; immediate      DONE, ]] drop dup  LEAVE [[ BEGIN, ]] THEN *} [[ ; immediate
   
 : // ( -- ) \ regexp-pattern  : // ( -- ) \ regexp-pattern
     \G search for string      \G search for string
Line 223 
Line 226 
 \ 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
   
 : JOINs ( sys -- )  BEGIN  dup  WHILE  ]] JOIN [[  REPEAT  drop ;  : THENs ( sys -- )  BEGIN  dup  WHILE  ]] THEN [[  REPEAT  drop ;
   
 : {{ ( addr -- addr addr ) \ regexp-pattern  : {{ ( addr -- addr addr ) \ regexp-pattern
     \G Start of alternatives      \G Start of alternatives
     0 ]] dup BEGIN [[  vars @ ; immediate      0 ]] dup dup FORK  IF  2drop true ;S  BUT  JOIN [[ vars @ ; immediate
 : || ( 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 !  vars !
     ]] dup FORK  IF  2drop true  ;S THEN  drop dup [[ >r >r >r vars !      ]] AHEAD  BUT  THEN  drop [[
     ]] DONE drop dup [[ r> r> r> ]] BEGIN [[ vars @ ; immediate      ]] dup dup FORK  IF  2drop true ;S  BUT  JOIN [[ vars @ ; immediate
 : }} ( addr addr -- addr addr ) \ regexp-pattern  : }} ( addr addr -- addr ) \ regexp-pattern
     \G end of alternatives      \G end of alternatives
     vars @ varsmax @ max vars !      vars @ varsmax @ max vars !  drop
     ]] dup FORK  IF  2drop true  ;S THEN  drop dup [[ >r >r >r drop      ]] AHEAD  BUT  THEN  drop LEAVE [[  THENs ; immediate
     ]] DONE drop LEAVE [[ r> r> r> JOINs ; immediate  
   
 \ match variables  \ match variables
   


Generate output suitable for use with a patch program
Legend:
Removed from v.1.19  
changed lines
  Added in v.1.25

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help