]> gitweb.factorcode.org Git - factor.git/blob - basis/xmode/marker/marker.factor
2b09ffdd5536b4bb1aa2dc3a26eeb839378a9697
[factor.git] / basis / xmode / marker / marker.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3
4 USING: accessors ascii assocs combinators
5 combinators.short-circuit formatting kernel make math namespaces
6 regexp regexp.parser sequences splitting strings
7 xmode.marker.state xmode.rules xmode.tokens xmode.utilities ;
8
9 IN: xmode.marker
10
11 ! Next two words copied from parser-combinators
12 ! Just like head?, but they optionally ignore case
13
14 : string= ( str1 str2 ignore-case -- ? )
15     [ [ >upper ] bi@ ] when sequence= ;
16
17 : string-head? ( str1 str2 ignore-case -- ? )
18     2over shorter?
19     [ 3drop f ] [
20         [
21             [ nip ]
22             [ length head-slice ] 2bi
23         ] dip string=
24     ] if ;
25
26 ! Based on org.gjt.sp.jedit.syntax.TokenMarker
27
28 : current-keyword ( -- string )
29     last-offset get position get line get subseq ;
30
31 : keyword-number? ( keyword -- ? )
32     {
33         [ current-rule-set highlight-digits?>> ]
34         [ dup [ digit? ] any? ]
35         [
36             dup [ digit? ] all? [
37                 current-rule-set digit-re>>
38                 [ dupd matches? ] [ f ] if*
39             ] unless*
40         ]
41     } 0&& nip ;
42
43 : mark-number ( keyword -- id )
44     keyword-number? DIGIT and ;
45
46 : mark-keyword ( keyword -- id )
47     current-rule-set keywords>> at ;
48
49 : add-remaining-token ( -- )
50     current-rule-set default>> prev-token, ;
51
52 : mark-token ( -- )
53     current-keyword
54     dup mark-number [ ] [ mark-keyword ] ?if
55     [ prev-token, ] when* ;
56
57 : current-char ( -- char )
58     position get line get nth ;
59
60 GENERIC: match-position ( rule -- n )
61
62 M: mark-previous-rule match-position drop last-offset get ;
63
64 M: rule match-position drop position get ;
65
66 : can-match-here? ( matcher rule -- ? )
67     match-position {
68         [ over ]
69         [ over at-line-start?>>     over zero?                implies ]
70         [ over at-whitespace-end?>> over whitespace-end get = implies ]
71         [ over at-word-start?>>     over last-offset get =    implies ]
72     } 0&& 2nip ;
73
74 : rest-of-line ( -- str )
75     line get position get tail-slice ;
76
77 : match-start ( string regexp -- slice/f )
78     first-match dup [ dup from>> 0 = [ drop f ] unless ] when ;
79
80 GENERIC: text-matches? ( string text -- match-count/f )
81
82 M: f text-matches?
83     2drop f ;
84
85 M: string-matcher text-matches?
86     [ string>> ] [ ignore-case?>> ] bi
87     [ string-head? ] keepd length and ;
88
89 M: regexp text-matches?
90     [ >string ] dip match-start dup [ to>> ] when ;
91
92 <PRIVATE
93
94 ! XXX: Terrible inefficient regexp match group support
95
96 : #match-groups ( regexp -- n/f )
97     raw>> [ CHAR: ( = ] count [ f ] when-zero ;
98
99 : nth-index ( n obj seq -- i )
100     [ = dup [ drop 1 - dup 0 < ] when ] with find drop nip ;
101
102 : match-group-regexp ( regexp n -- skip-regexp match-regexp )
103     [ [ options>> options>string ] [ raw>> ] bi ] dip
104     CHAR: ( pick nth-index cut CHAR: ) over index 1 + head
105     rot '[ _ H{ } [ <optioned-regexp> ] 2cache ] bi@ ;
106
107 : skip-first-match ( match regexp -- tailseq )
108     first-match [ seq>> ] [ to>> ] bi tail ;
109
110 : nth-match ( match regexp n -- slice/f )
111     match-group-regexp [ skip-first-match ] [ first-match ] bi* ;
112
113 :: update-match-group ( str match regexp n -- str' )
114     n H{ } [ 1 + CHAR: $ swap "" 2sequence ] cache :> x
115     x str subseq? [
116         x match "" like regexp n nth-match str replace
117     ] [ str ] if ;
118
119 : update-match-groups ( str match regexp -- str' )
120     dup #match-groups [ update-match-group ] 2with each-integer ;
121
122 GENERIC: fixup-end ( match regexp end -- end' )
123
124 M: string-matcher fixup-end
125     [ string>> -rot update-match-groups ]
126     [ ignore-case?>> ] bi <string-matcher> ;
127
128 MEMO: <fixup-regexp> ( raw matched options -- regexp )
129     <optioned-regexp> {
130         [ parse-tree>> ] [ options>> ] [ dfa>> ] [ next-match>> ]
131     } cleave regexp boa ;
132
133 M: regexp fixup-end
134     [ raw>> [ -rot update-match-groups ] keep swap ]
135     [ options>> options>string ] bi <fixup-regexp> ;
136
137 : fixup-end? ( text -- ? )
138     { [ regexp? ] [ #match-groups ] } 1&& ;
139
140 : fixup-end/text-matches? ( string regexp rule -- match-count/f )
141     [ >string ] 2dip [ [ match-start dup ] keep ] dip pick [
142         end>> [ [ fixup-end ] change-text drop ] [ 2drop ] if*
143     ] [
144         3drop
145     ] if dup [ to>> ] when ;
146
147 PRIVATE>
148
149 :: rule-start-matches? ( rule -- match-count/f )
150     rule start>> dup rule can-match-here? [
151         rest-of-line swap text>>
152         dup fixup-end? [
153             rule fixup-end/text-matches?
154         ] [
155             text-matches?
156         ] if
157     ] [
158         drop f
159     ] if ;
160
161 : rule-end-matches? ( rule -- match-count/f )
162     dup mark-following-rule? [
163         [ start>> ] keep can-match-here? 0 and
164     ] [
165         [ end>> dup ] keep can-match-here? [
166             rest-of-line
167             swap text>> context get end>> or
168             text-matches?
169         ] [
170             drop f
171         ] if
172     ] if ;
173
174 DEFER: get-rules
175
176 : get-always-rules ( ruleset -- vector/f )
177     f swap rules>> at ;
178
179 : get-char-rules ( char ruleset -- vector/f )
180     [ ch>upper ] dip rules>> at ;
181
182 : get-rules ( char ruleset -- seq )
183     [ get-char-rules ] [ get-always-rules ] bi [ append ] when* ;
184
185 GENERIC: handle-rule-start ( match-count rule -- )
186
187 GENERIC: handle-rule-end ( match-count rule -- )
188
189 : find-escape-rule ( -- rule )
190     context get dup
191     in-rule-set>> escape-rule>> [ ] [
192         parent>> in-rule-set>>
193         dup [ escape-rule>> ] when
194     ] ?if ;
195
196 : check-escape-rule ( rule -- ? )
197     escape-rule>> [ find-escape-rule ] unless*
198     dup [
199         dup rule-start-matches? [
200             swap handle-rule-start
201             delegate-end-escaped? toggle
202             t
203         ] [
204             drop f
205         ] if*
206     ] when ;
207
208 : check-every-rule ( -- ? )
209     current-char current-rule-set get-rules
210     [ rule-start-matches? ] map-find
211     [ handle-rule-start t ] [ drop f ] if* ;
212
213 : ?end-rule ( -- )
214     current-rule [
215         dup rule-end-matches?
216         [ swap handle-rule-end ] [ drop ] if*
217     ] when* ;
218
219 : rule-match-token* ( rule -- id )
220     dup match-token>> {
221         { f [ dup body-token>> ] }
222         { t [ current-rule-set default>> ] }
223         [ ]
224     } case nip ;
225
226 M: escape-rule handle-rule-start
227     drop
228     ?end-rule
229     process-escape? get [
230         escaped? toggle
231         position [ + ] change
232     ] [ drop ] if ;
233
234 M: seq-rule handle-rule-start
235     ?end-rule
236     mark-token
237     add-remaining-token
238     [ body-token>> next-token, ] keep
239     delegate>> [ push-context ] when* ;
240
241 UNION: abstract-span-rule span-rule eol-span-rule ;
242
243 M: abstract-span-rule handle-rule-start
244     ?end-rule
245     mark-token
246     add-remaining-token
247     [ rule-match-token* next-token, ] keep
248     ! ... end subst ...
249     dup context get in-rule<<
250     delegate>> push-context ;
251
252 M: span-rule handle-rule-end
253     2drop ;
254
255 M: mark-following-rule handle-rule-start
256     ?end-rule
257     mark-token add-remaining-token
258     [ rule-match-token* next-token, ] keep
259     f context get end<<
260     context get in-rule<< ;
261
262 M: mark-following-rule handle-rule-end
263     nip rule-match-token* prev-token,
264     f context get in-rule<< ;
265
266 M: mark-previous-rule handle-rule-start
267     ?end-rule
268     mark-token
269     dup body-token>> prev-token,
270     rule-match-token* next-token, ;
271
272 : do-escaped ( -- )
273     escaped? get [
274         escaped? off
275         ! ...
276     ] when ;
277
278 : check-end-delegate ( -- ? )
279     context get parent>> [
280         in-rule>> [
281             dup rule-end-matches? [
282                 [
283                     swap handle-rule-end
284                     ?end-rule
285                     mark-token
286                     add-remaining-token
287                 ] keep context get parent>> in-rule>>
288                 rule-match-token* next-token,
289                 pop-context
290                 seen-whitespace-end? on t
291             ] [ check-escape-rule ] if*
292         ] [ f ] if*
293     ] [ f ] if* ;
294
295 : handle-no-word-break ( -- )
296     context get parent>> [
297         in-rule>> [
298             dup no-word-break?>> [
299                 rule-match-token* prev-token,
300                 pop-context
301             ] [ drop ] if
302         ] when*
303     ] when* ;
304
305 : check-rule ( -- )
306     ?end-rule
307     handle-no-word-break
308     mark-token
309     add-remaining-token ;
310
311 : (check-word-break) ( -- )
312     check-rule
313
314     1 current-rule-set default>> next-token, ;
315
316 : rule-set-empty? ( ruleset -- ? )
317     [ rules>> ] [ keywords>> ] bi
318     [ assoc-empty? ] both? ;
319
320 : check-word-break ( -- ? )
321     current-char dup blank? [
322         drop
323
324         seen-whitespace-end? get [
325             position get 1 + whitespace-end set
326         ] unless
327
328         (check-word-break)
329
330     ] [
331         ! Micro-optimization with incorrect semantics; we keep
332         ! it here because jEdit mode files depend on it now...
333         current-rule-set rule-set-empty? [
334             drop
335         ] [
336             dup alpha? [
337                 drop
338             ] [
339                 current-rule-set rule-set-no-word-sep* member? [
340                     (check-word-break)
341                 ] unless
342             ] if
343         ] if
344
345         seen-whitespace-end? on
346     ] if
347     escaped? off
348     delegate-end-escaped? off t ;
349
350
351 : mark-token-loop ( -- )
352     position get line get length < [
353         {
354             [ check-end-delegate ]
355             [ check-every-rule ]
356             [ check-word-break ]
357         } 0|| drop
358
359         position inc
360         mark-token-loop
361     ] when ;
362
363 : mark-remaining ( -- )
364     line get length position set
365     check-rule ;
366
367 : unwind-no-line-break ( -- )
368     context get parent>> [
369         in-rule>> [
370             no-line-break?>> [
371                 pop-context
372                 unwind-no-line-break
373             ] when
374         ] when*
375     ] when* ;
376
377 : tokenize-line ( line-context line rules -- line-context' seq )
378     [
379         "MAIN" of -rot
380         init-token-marker
381         mark-token-loop
382         mark-remaining
383         unwind-no-line-break
384         context get
385     ] { } make ;