| 
									
										
										
										
											2009-03-08 19:50:41 -04:00
										 |  |  | ! Copyright (C) 2008 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-06-30 17:06:14 -04:00
										 |  |  | USING: accessors xmode.tokens xmode.keyword-map kernel | 
					
						
							| 
									
										
										
										
											2016-03-31 02:29:48 -04:00
										 |  |  | sequences vectors assocs strings memoize unicode | 
					
						
							| 
									
										
										
										
											2009-03-08 19:50:41 -04:00
										 |  |  | regexp ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | IN: xmode.rules | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-08 03:23:14 -05:00
										 |  |  | TUPLE: string-matcher string ignore-case? ;
 | 
					
						
							| 
									
										
										
										
											2007-12-06 00:23:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-08 03:23:14 -05:00
										 |  |  | C: <string-matcher> string-matcher | 
					
						
							| 
									
										
										
										
											2007-12-06 00:23:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | ! Based on org.gjt.sp.jedit.syntax.ParserRuleSet | 
					
						
							|  |  |  | TUPLE: rule-set | 
					
						
							|  |  |  | name | 
					
						
							|  |  |  | props | 
					
						
							|  |  |  | keywords | 
					
						
							|  |  |  | rules | 
					
						
							|  |  |  | imports | 
					
						
							|  |  |  | terminate-char | 
					
						
							|  |  |  | ignore-case? | 
					
						
							|  |  |  | default | 
					
						
							|  |  |  | escape-rule | 
					
						
							|  |  |  | highlight-digits? | 
					
						
							|  |  |  | digit-re | 
					
						
							|  |  |  | no-word-sep | 
					
						
							| 
									
										
										
										
											2007-12-10 02:20:36 -05:00
										 |  |  | finalized? | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <rule-set> ( -- ruleset )
 | 
					
						
							| 
									
										
										
										
											2008-06-30 06:22:05 -04:00
										 |  |  |     rule-set new
 | 
					
						
							|  |  |  |         H{ } clone >>rules | 
					
						
							|  |  |  |         H{ } clone >>props | 
					
						
							|  |  |  |         V{ } clone >>imports ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | MEMO: standard-rule-set ( id -- ruleset )
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:32:26 -04:00
										 |  |  |     <rule-set> swap >>default ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : import-rule-set ( import ruleset -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:32:26 -04:00
										 |  |  |     imports>> push ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : inverted-index ( hashes key index -- )
 | 
					
						
							| 
									
										
										
										
											2008-06-13 03:09:16 -04:00
										 |  |  |     [ swapd push-at ] 2curry each ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : ?push-all ( seq1 seq2 -- seq1+seq2 )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-10-28 16:29:01 -04:00
										 |  |  |         over [ [ V{ } like ] dip append! ] [ nip ] if
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  |     ] when* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : rule-set-no-word-sep* ( ruleset -- str )
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:32:26 -04:00
										 |  |  |     [ no-word-sep>> ] | 
					
						
							|  |  |  |     [ keywords>> ] bi
 | 
					
						
							|  |  |  |     dup [ keyword-map-no-word-sep* ] when
 | 
					
						
							| 
									
										
										
										
											2007-12-06 00:23:18 -05:00
										 |  |  |     "_" 3append ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Match restrictions | 
					
						
							|  |  |  | TUPLE: matcher text at-line-start? at-whitespace-end? at-word-start? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <matcher> matcher | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Based on org.gjt.sp.jedit.syntax.ParserRule | 
					
						
							|  |  |  | TUPLE: rule | 
					
						
							|  |  |  | no-line-break? | 
					
						
							|  |  |  | no-word-break? | 
					
						
							|  |  |  | no-escape? | 
					
						
							|  |  |  | start
 | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | match-token | 
					
						
							|  |  |  | body-token | 
					
						
							|  |  |  | delegate | 
					
						
							|  |  |  | chars | 
					
						
							|  |  |  | ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-27 20:27:06 -04:00
										 |  |  | TUPLE: seq-rule < rule ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-27 20:27:06 -04:00
										 |  |  | TUPLE: span-rule < rule ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-27 20:27:06 -04:00
										 |  |  | TUPLE: eol-span-rule < rule ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : init-span ( rule -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-30 22:10:02 -04:00
										 |  |  |     dup delegate>> [ drop ] [ | 
					
						
							|  |  |  |         dup body-token>> standard-rule-set | 
					
						
							| 
									
										
										
										
											2010-05-05 16:52:54 -04:00
										 |  |  |         swap delegate<< | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : init-eol-span ( rule -- )
 | 
					
						
							|  |  |  |     dup init-span | 
					
						
							| 
									
										
										
										
											2008-08-30 22:10:02 -04:00
										 |  |  |     t >>no-line-break? drop ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-27 20:27:06 -04:00
										 |  |  | TUPLE: mark-following-rule < rule ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-27 20:27:06 -04:00
										 |  |  | TUPLE: mark-previous-rule < rule ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-27 20:27:06 -04:00
										 |  |  | TUPLE: escape-rule < rule ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <escape-rule> ( string -- rule )
 | 
					
						
							| 
									
										
										
										
											2007-12-08 03:23:14 -05:00
										 |  |  |     f <string-matcher> f f f <matcher> | 
					
						
							| 
									
										
										
										
											2008-08-27 20:27:06 -04:00
										 |  |  |     escape-rule new swap >>start ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-06 00:23:18 -05:00
										 |  |  | GENERIC: text-hash-char ( text -- ch )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: f text-hash-char ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-02 04:01:19 -04:00
										 |  |  | M: string-matcher text-hash-char string>> first ;
 | 
					
						
							| 
									
										
										
										
											2007-12-06 00:23:18 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: regexp text-hash-char drop f ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | : rule-chars* ( rule -- string )
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:32:26 -04:00
										 |  |  |     [ chars>> ] [ start>> ] bi text>> | 
					
						
							| 
									
										
										
										
											2008-03-31 20:18:05 -04:00
										 |  |  |     text-hash-char [ suffix ] when* ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : add-rule ( rule ruleset -- )
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:17:37 -05:00
										 |  |  |     [ dup rule-chars* >upper swap ] dip rules>> inverted-index ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 23:34:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : add-escape-rule ( string ruleset -- )
 | 
					
						
							| 
									
										
										
										
											2007-12-08 03:23:14 -05:00
										 |  |  |     over [ | 
					
						
							| 
									
										
										
										
											2008-08-30 21:32:26 -04:00
										 |  |  |         [ <escape-rule> ] dip
 | 
					
						
							| 
									
										
										
										
											2010-05-05 16:52:54 -04:00
										 |  |  |         2dup escape-rule<< | 
					
						
							| 
									
										
										
										
											2007-12-08 03:23:14 -05:00
										 |  |  |         add-rule | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         2drop
 | 
					
						
							|  |  |  |     ] if ;
 |