File:  [gforth] / gforth / regexp.fs
Revision 1.23: download - view: text, annotated - select for diffs
Sun Sep 12 22:04:06 2010 UTC (13 years, 7 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
More fixes

    1: \ Regexp compile
    2: 
    3: \ Copyright (C) 2005,2006,2007,2008 Free Software Foundation, Inc.
    4: 
    5: \ This file is part of Gforth.
    6: 
    7: \ Gforth is free software; you can redistribute it and/or
    8: \ modify it under the terms of the GNU General Public License
    9: \ as published by the Free Software Foundation, either version 3
   10: \ of the License, or (at your option) any later version.
   11: 
   12: \ This program is distributed in the hope that it will be useful,
   13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15: \ GNU General Public License for more details.
   16: 
   17: \ You should have received a copy of the GNU General Public License
   18: \ along with this program. If not, see http://www.gnu.org/licenses/.
   19: 
   20: \ The idea of the parser is the following:
   21: \ As long as there's a match, continue
   22: \ On a mismatch, LEAVE.
   23: \ Insert appropriate control structures on alternative branches
   24: \ Keep the old pointer (backtracking) on the stack
   25: \ I try to keep the syntax as close to a real regexp system as possible
   26: \ All regexp stuff is compiled into one function as forward branching
   27: \ state machine
   28: 
   29: \ special control structure
   30: 
   31: : FORK ( compilation -- orig ; run-time f -- ) \ gforth
   32:     \G AHEAD-like control structure: calls the code after JOIN.
   33:     POSTPONE call >mark ; immediate restrict
   34: : JOIN ( orig -- ) \ gforth
   35:     \G THEN-like control structure for FORK
   36:     postpone THEN ; immediate restrict
   37: 
   38: \ Charclasses
   39: 
   40: : +bit ( addr n -- )  + 1 swap c! ;
   41: : -bit ( addr n -- )  + 0 swap c! ;
   42: : @+ ( addr -- n addr' )  dup @ swap cell+ ;
   43: 
   44: 0 Value cur-class
   45: : charclass ( -- ) \ regexp-cg
   46:     \G Create a charclass
   47:     Create here dup to cur-class $100 dup allot erase ;
   48: : +char ( char -- ) \ regexp-cg
   49:     \G add a char to the current charclass
   50:     cur-class swap +bit ;
   51: : -char ( char -- ) \ regexp-cg
   52:     \G remove a char from the current charclass
   53:     cur-class swap -bit ;
   54: : ..char ( start end -- ) \ regexp-cg
   55:     \G add a range of chars to the current charclass
   56:     1+ swap ?DO  I +char  LOOP ;
   57: : or! ( n addr -- )  dup @ rot or swap ! ;
   58: : and! ( n addr -- )  dup @ rot and swap ! ;
   59: : +class ( class -- ) \ regexp-cg
   60:     \G union of charclass @var{class} and the current charclass
   61:     $100 0 ?DO  @+ swap
   62:     cur-class I + or!  cell +LOOP  drop ;
   63: : -class ( class -- ) \ regexp-cg
   64:     \G subtract the charclass @var{class} from the current charclass
   65:     $100 0 ?DO  @+ swap invert
   66:     cur-class I + and!  cell +LOOP  drop ;
   67: 
   68: : char? ( addr class -- addr' flag )
   69:     >r count r> + c@ ;
   70: 
   71: \ Charclass tests
   72: 
   73: : c? ( addr class -- ) \ regexp-pattern
   74:     \G check @var{addr} for membership in charclass @var{class}
   75:     ]] char? 0= ?LEAVE [[ ; immediate
   76: : -c? ( addr class -- ) \ regexp-pattern
   77:     \G check @var{addr} for not membership in charclass @var{class}
   78:     ]] char?    ?LEAVE [[ ; immediate
   79: 
   80: charclass digit  '0 '9 ..char
   81: charclass blanks 0 bl ..char
   82: \ bl +char #tab +char #cr +char #lf +char ctrl L +char
   83: charclass letter 'a 'z ..char 'A 'Z ..char
   84: charclass any    0 $FF ..char #lf -char
   85: 
   86: : \d ( addr -- addr' ) \ regexp-pattern
   87:     \G check for digit
   88:     ]] digit c?        [[ ; immediate
   89: : \s ( addr -- addr' ) \ regexp-pattern
   90:     \G check for blanks
   91:     ]] blanks c?       [[ ; immediate
   92: : .? ( addr -- addr' ) \ regexp-pattern
   93:     \G check for any single charachter
   94:     ]] any c?          [[ ; immediate
   95: : -\d ( addr -- addr' ) \ regexp-pattern
   96:     \G check for not digit
   97:     ]] digit -c?       [[ ; immediate
   98: : -\s ( addr -- addr' ) \ regexp-pattern
   99:     \G check for not blank
  100:     ]] blanks -c?      [[ ; immediate
  101: : ` ( "char" -- ) \ regexp-pattern
  102:     \G check for particular char
  103:     ]] count [[  char ]] Literal <> ?LEAVE [[ ;  immediate
  104: : -` ( "char" -- ) \ regexp-pattern
  105:     \G check for particular char
  106:     ]] count [[  char ]] Literal = ?LEAVE [[ ;  immediate
  107: 
  108: \ loop stack
  109: 
  110: Variable loops  $40 3 * cells allot
  111: : 3@ ( addr -- a b c )  dup >r 2 cells + @ r> 2@ ;
  112: : 3! ( a b c addr -- )  dup >r 2! r> 2 cells + ! ;
  113: : loops> ( -- addr ) -3 loops +!  loops @+ swap cells + 3@ ;
  114: : >loops ( addr -- ) loops @+ swap cells + 3! 3 loops +! ;
  115: : BEGIN, ( -- )  ]] BEGIN [[ >loops ;
  116: : DONE, ( -- )  loops @ IF  loops> ]] DONE [[ ELSE ." no done left!" cr THEN ;
  117: 
  118: \ variables
  119: 
  120: Variable vars   &18 cells allot
  121: Variable varstack 9 cells allot
  122: Variable varsmax
  123: : >var ( -- addr ) vars @+ swap 2* cells +
  124:     vars @ varstack @+ swap cells + !
  125:     1 vars +! 1 varstack +! ;
  126: : var> ( -- addr ) -1 varstack +!
  127:     varstack @+ swap cells + @
  128:     1+ 2* cells vars + ;
  129: 
  130: \ start end
  131: 
  132: 0 Value end$
  133: 0 Value start$
  134: : !end ( addr u -- addr )  over + to end$ dup to start$ ;
  135: : end-rex? ( addr -- addr flag ) dup end$ u< ;
  136: : start-rex? ( addr -- addr flag ) dup start$ u> ;
  137: : ?end ( addr -- addr ) ]] dup end$ u> ?LEAVE [[ ; immediate
  138: : rest$ ( addr -- addr addr u ) dup end$ over - ;
  139: 
  140: \ start and end
  141: 
  142: : \^ ( addr -- addr ) \ regexp-pattern
  143:     \G check for string start
  144:     ]] start-rex? ?LEAVE [[ ; immediate
  145: : \$ ( addr -- addr ) \ regexp-pattern
  146:     \G check for string end
  147:     ]] end-rex? ?LEAVE [[ ; immediate
  148: 
  149: \ A word for string comparison
  150: 
  151: : (str=?) ( addr1 addr u -- addr2 )
  152:     dup >r 2>r rest$ r@ umin 2r> compare IF rdrop true ELSE r> + false THEN ;
  153: : str=? ( addr1 addr u -- addr2 ) ]] (str=?) ?LEAVE [[ ; immediate
  154: : ,=" ( addr u -- ) tuck dup ]] rest$ Literal umin SLiteral compare ?LEAVE Literal + [[ ;
  155: : =" ( <string>" -- ) \ regexp-pattern
  156:     \G check for string
  157:     '" parse ,=" ; immediate
  158: 
  159: \ regexp block
  160: 
  161: \ FORK/JOIN are like AHEAD THEN, but producing a call on AHEAD
  162: \ instead of a jump.
  163: 
  164: : (( ( addr u -- ) \ regexp-pattern
  165:     \G start regexp block
  166:     vars off varsmax off loops off
  167:     ]] FORK  AHEAD BUT JOIN !end [[ BEGIN, ; immediate
  168: : )) ( -- addr f ) \ regexp-pattern
  169:     \G end regexp block
  170:     ]] ?end drop true ;S [[
  171:     DONE, ]] drop false ;S THEN [[ ; immediate
  172: 
  173: \ greedy loops
  174: 
  175: \ Idea: scan as many characters as possible, try the rest of the pattern
  176: \ and then back off one pattern at a time
  177: 
  178: : drops ( n -- ) 1+ cells sp@ + sp! ;
  179: 
  180: : {** ( addr -- addr addr ) \ regexp-pattern
  181:     \G greedy zero-or-more pattern
  182:     0 ]] Literal >r BEGIN dup [[ BEGIN, ; immediate
  183: ' {** Alias {++ ( addr -- addr addr ) \ regexp-pattern
  184:     \G greedy one-or-more pattern
  185:     immediate
  186: : n*} ( sys n -- ) \ regexp-pattern
  187:     \G At least @var{n} pattern
  188:     >r ]] r> 1+ >r end-rex? 0= UNTIL dup [[ DONE, ]] drop [[
  189:     r@ ]] r> 1+ Literal U+DO FORK BUT [[
  190:     ]] IF  I' I - [[ r@ 1- ]] Literal + drops true UNLOOP ;S  THEN  LOOP [[
  191:     r@ IF  r@ ]] Literal drops [[ THEN
  192:     rdrop ]]  dup LEAVE  JOIN [[ ; immediate
  193: : **} ( sys -- ) \ regexp-pattern
  194:     \G end of greedy zero-or-more pattern
  195:     0 postpone n*} ; immediate
  196: : ++} ( sys -- ) \ regexp-pattern
  197:     \G end of greedy zero-or-more pattern
  198:     1 postpone n*} ; immediate
  199: 
  200: \ non-greedy loops
  201: 
  202: \ Idea: Try to match rest of the regexp, and if that fails, try match
  203: \ first expr and then try again rest of regexp.
  204: 
  205: : {+ ( addr -- addr addr ) \ regexp-pattern
  206:     \G non-greedy one-or-more pattern
  207:     ]] BEGIN  [[ BEGIN, ; immediate
  208: : {* ( addr -- addr addr ) \ regexp-pattern
  209:     \G non-greedy zero-or-more pattern
  210:     ]] {+ dup FORK BUT  IF  drop true  ;S THEN [[ ; immediate
  211: : *} ( addr addr' -- addr' ) \ regexp-pattern
  212:     \G end of non-greedy zero-or-more pattern
  213:     ]] dup end$ u>  UNTIL [[
  214:     DONE, ]] drop false  ;S  JOIN [[ ; immediate
  215: : +} ( addr addr' -- addr' ) \ regexp-pattern
  216:     \G end of non-greedy one-or-more pattern
  217:     ]] dup FORK BUT  IF  drop true  ;S [[
  218:     DONE, ]] drop dup  LEAVE [[ BEGIN, ]] THEN *} [[ ; immediate
  219: 
  220: : // ( -- ) \ regexp-pattern
  221:     \G search for string
  222:     ]] {* 1+ *} [[ ; immediate
  223: 
  224: \ alternatives
  225: 
  226: \ idea: try to match one alternative and then the rest of regexp.
  227: \ if that fails, jump back to second alternative
  228: 
  229: : JOINs ( sys -- )  BEGIN  dup  WHILE  ]] JOIN [[  REPEAT  drop ;
  230: 
  231: : {{ ( addr -- addr addr ) \ regexp-pattern
  232:     \G Start of alternatives
  233:     0 ]] dup BEGIN [[  vars @ ; immediate
  234: : || ( addr addr -- addr addr ) \ regexp-pattern
  235:     \G separator between alternatives
  236:     vars @ varsmax @ max varsmax !
  237:     ]] dup FORK  IF  2drop true  ;S THEN  [[ >r >r >r vars !
  238:     ]] DONE drop dup [[ r> r> r> ]] BEGIN [[ vars @ ; immediate
  239: : }} ( addr addr -- addr ) \ regexp-pattern
  240:     \G end of alternatives
  241:     vars @ varsmax @ max vars !
  242:     ]] dup FORK  IF  2drop true  ;S THEN drop dup [[ >r >r >r drop
  243:     ]] DONE drop LEAVE ;S  [[ r> r> r> JOINs ; immediate
  244: 
  245: \ match variables
  246: 
  247: : \( ( addr -- addr ) \ regexp-pattern
  248:     \G start of matching variable; variables are referred as \\1--9
  249:     ]] dup [[
  250:     >var ]] ALiteral ! [[ ; immediate
  251: : \) ( addr -- addr ) \ regexp-pattern
  252:     \G end of matching variable
  253:     ]] dup [[
  254:     var> ]] ALiteral ! [[ ; immediate
  255: : \0 ( -- addr u ) \ regexp-pattern
  256:     \G the whole string
  257:     start$ end$ over - ;
  258: : \: ( i -- )
  259:     Create 2* 1+ cells vars + ,
  260:   DOES> ( -- addr u ) @ 2@ tuck - ;
  261: : \:s ( n -- ) 0 ?DO  I \:  LOOP ;
  262: 9 \:s \1 \2 \3 \4 \5 \6 \7 \8 \9
  263: 
  264: \ replacements, needs string.fs
  265: 
  266: require string.fs
  267: 
  268: 0 Value >>ptr
  269: 0 Value <<ptr
  270: Variable >>string
  271: : s>>  ( addr -- addr ) \ regexp-replace
  272:     \G Start replace pattern region
  273:     dup to >>ptr ;
  274: : << ( run-addr addr u -- run-addr ) \ regexp-replace
  275:     \G Replace string from start of replace pattern region with
  276:     \G @var{addr} @var{u}
  277:     <<ptr >>ptr over - >>string $+!
  278:     >>string $+! dup to <<ptr ;
  279: : <<" ( "string<">" -- ) \ regexp-replace
  280:     \G Replace string from start of replace pattern region with
  281:     \G @var{string}
  282:     '" parse postpone SLiteral postpone << ; immediate
  283: : >>string@ ( -- addr u )
  284:     >>string $@ ;
  285: : >>string0 ( addr u -- addr u )  s" " >>string $!
  286:     0 to >>ptr  over to <<ptr ;
  287: : >>next ( -- addr u ) <<ptr end$ over - ;
  288: : >>rest ( -- ) >>next >>string $+! ;
  289: : s// ( addr u -- ptr )
  290:     \G start search/replace loop
  291:     ]] >>string0 (( // s>> [[ ; immediate
  292: : >> ( addr -- addr )
  293:     ]] <<ptr >>ptr u> ?LEAVE ?end [[ ; immediate
  294: : //s ( ptr -- )
  295:     \G search end
  296:     ]] )) drop >>rest >>string@ [[ ; immediate
  297: : //o ( ptr addr u -- addr' u' )
  298:     \G end search/replace single loop
  299:     ]] << //s [[ ; immediate
  300: : //g ( ptr addr u -- addr' u' )
  301:     \G end search/replace all loop
  302:     ]] << LEAVE //s [[ ; immediate

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>