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 [[ 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@ IF r@ ]] r@ Literal u< IF r> 1+ drops dup LEAVE THEN [[ THEN
190: r@ ]] r> 1+ Literal U+DO FORK BUT [[
191: ]] IF I' I - [[ r@ 1- ]] Literal + drops true UNLOOP ;S THEN LOOP [[
192: r@ IF r@ ]] Literal drops [[ THEN
193: rdrop ]] dup LEAVE JOIN [[ ; immediate
194: : **} ( sys -- ) \ regexp-pattern
195: \G end of greedy zero-or-more pattern
196: 0 postpone n*} ; immediate
197: : ++} ( sys -- ) \ regexp-pattern
198: \G end of greedy zero-or-more pattern
199: 1 postpone n*} ; immediate
200:
201: \ non-greedy loops
202:
203: \ Idea: Try to match rest of the regexp, and if that fails, try match
204: \ first expr and then try again rest of regexp.
205:
206: : {+ ( addr -- addr addr ) \ regexp-pattern
207: \G non-greedy one-or-more pattern
208: ]] BEGIN [[ BEGIN, ; immediate
209: : {* ( addr -- addr addr ) \ regexp-pattern
210: \G non-greedy zero-or-more pattern
211: ]] {+ dup FORK BUT IF drop true ;S THEN [[ ; immediate
212: : *} ( addr addr' -- addr' ) \ regexp-pattern
213: \G end of non-greedy zero-or-more pattern
214: ]] dup end$ u> UNTIL [[
215: DONE, ]] drop false ;S JOIN [[ ; immediate
216: : +} ( addr addr' -- addr' ) \ regexp-pattern
217: \G end of non-greedy one-or-more pattern
218: ]] dup FORK BUT IF drop true ;S [[
219: DONE, ]] drop dup LEAVE THEN *} [[ ; immediate
220:
221: : // ( -- ) \ regexp-pattern
222: \G search for string
223: ]] {* 1+ *} [[ ; immediate
224:
225: \ alternatives
226:
227: \ idea: try to match one alternative and then the rest of regexp.
228: \ if that fails, jump back to second alternative
229:
230: : JOINs ( sys -- ) BEGIN dup WHILE ]] JOIN [[ REPEAT drop ;
231:
232: : {{ ( addr -- addr addr ) \ regexp-pattern
233: \G Start of alternatives
234: 0 ]] dup BEGIN [[ vars @ ; immediate
235: : || ( addr addr -- addr addr ) \ regexp-pattern
236: \G separator between alternatives
237: vars @ varsmax @ max varsmax !
238: ]] dup FORK IF 2drop true ;S THEN [[ >r >r >r vars !
239: ]] DONE drop dup [[ r> r> r> ]] BEGIN [[ vars @ ; immediate
240: : }} ( addr addr -- addr ) \ regexp-pattern
241: \G end of alternatives
242: vars @ varsmax @ max vars !
243: ]] dup FORK IF 2drop true ;S THEN drop dup [[ >r >r >r drop
244: ]] DONE drop LEAVE ;S [[ r> r> r> JOINs ; immediate
245:
246: \ match variables
247:
248: : \( ( addr -- addr ) \ regexp-pattern
249: \G start of matching variable; variables are referred as \\1--9
250: ]] dup [[
251: >var ]] ALiteral ! [[ ; immediate
252: : \) ( addr -- addr ) \ regexp-pattern
253: \G end of matching variable
254: ]] dup [[
255: var> ]] ALiteral ! [[ ; immediate
256: : \0 ( -- addr u ) \ regexp-pattern
257: \G the whole string
258: start$ end$ over - ;
259: : \: ( i -- )
260: Create 2* 1+ cells vars + ,
261: DOES> ( -- addr u ) @ 2@ tuck - ;
262: : \:s ( n -- ) 0 ?DO I \: LOOP ;
263: 9 \:s \1 \2 \3 \4 \5 \6 \7 \8 \9
264:
265: \ replacements, needs string.fs
266:
267: require string.fs
268:
269: 0 Value >>ptr
270: 0 Value <<ptr
271: Variable >>string
272: : s>> ( addr -- addr ) \ regexp-replace
273: \G Start replace pattern region
274: dup to >>ptr ;
275: : << ( run-addr addr u -- run-addr ) \ regexp-replace
276: \G Replace string from start of replace pattern region with
277: \G @var{addr} @var{u}
278: <<ptr >>ptr over - >>string $+!
279: >>string $+! dup to <<ptr ;
280: : <<" ( "string<">" -- ) \ regexp-replace
281: \G Replace string from start of replace pattern region with
282: \G @var{string}
283: '" parse postpone SLiteral postpone << ; immediate
284: : >>string@ ( -- addr u )
285: >>string $@ ;
286: : >>string0 ( addr u -- addr u ) s" " >>string $!
287: 0 to >>ptr over to <<ptr ;
288: : >>next ( -- addr u ) <<ptr end$ over - ;
289: : >>rest ( -- ) >>next >>string $+! ;
290: : s// ( addr u -- ptr )
291: \G start search/replace loop
292: ]] >>string0 (( // s>> [[ ; immediate
293: : >> ( addr -- addr )
294: ]] <<ptr >>ptr u> ?LEAVE ?end [[ ; immediate
295: : //s ( ptr -- )
296: \G search end
297: ]] )) drop >>rest >>string@ [[ ; immediate
298: : //o ( ptr addr u -- addr' u' )
299: \G end search/replace single loop
300: ]] << //s [[ ; immediate
301: : //g ( ptr addr u -- addr' u' )
302: \G end search/replace all loop
303: ]] << LEAVE //s [[ ; immediate
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>