1 USING: xmode.loader xmode.utilities xmode.rules namespaces
2 strings splitting assocs sequences kernel io.files xml memoize
3 words globs combinators io.encodings.utf8 sorting accessors ;
6 TUPLE: mode file file-name-glob first-line-glob ;
8 <TAGS: parse-mode-tag ( modes tag -- )
14 { "FILE_NAME_GLOB" f (>>file-name-glob) }
15 { "FIRST_LINE_GLOB" f (>>first-line-glob) }
22 : parse-modes-tag ( tag -- modes )
24 swap child-tags [ parse-mode-tag ] with each
27 MEMO: modes ( -- modes )
28 "resource:basis/xmode/modes/catalog"
29 file>xml parse-modes-tag ;
31 MEMO: mode-names ( -- modes )
32 modes keys natural-sort ;
34 : reset-catalog ( -- )
35 \ modes reset-memoized ;
37 MEMO: (load-mode) ( name -- rule-sets )
40 "resource:basis/xmode/modes/" prepend
41 utf8 <file-reader> parse-mode
48 : no-such-rule-set ( name -- * )
49 "No such rule set: " prepend throw ;
51 : get-rule-set ( name -- rule-sets rules )
52 dup "::" split1 [ swap (load-mode) ] [ rule-sets get ] if*
53 dup -roll at* [ nip ] [ drop no-such-rule-set ] if ;
55 : resolve-delegate ( rule -- )
56 dup delegate>> dup string?
57 [ get-rule-set nip swap (>>delegate) ] [ 2drop ] if ;
59 : each-rule ( rule-set quot -- )
60 [ rules>> values concat ] dip each ; inline
62 : resolve-delegates ( ruleset -- )
63 [ resolve-delegate ] each-rule ;
65 : ?update ( keyword-map/f keyword-map -- keyword-map )
66 over [ dupd update ] [ nip clone ] if ;
68 : import-keywords ( parent child -- )
69 over [ [ keywords>> ] bi@ ?update ] dip (>>keywords) ;
71 : import-rules ( parent child -- )
72 swap [ add-rule ] curry each-rule ;
74 : resolve-imports ( ruleset -- )
76 get-rule-set swap rule-sets [
83 ERROR: mutually-recursive-rulesets ruleset ;
84 : finalize-rule-set ( ruleset -- )
88 [ 1 >>finalized? drop ]
91 [ t >>finalized? drop ]
95 { 1 [ mutually-recursive-rulesets ] }
98 : finalize-mode ( rulesets -- )
100 dup [ nip finalize-rule-set ] assoc-each
103 : load-mode ( name -- rule-sets )
104 (load-mode) dup finalize-mode ;
107 \ (load-mode) reset-memoized ;
109 : ?glob-matches ( string glob/f -- ? )
110 dup [ glob-matches? ] [ 2drop f ] if ;
112 : suitable-mode? ( file-name first-line mode -- ? )
113 tuck first-line-glob>> ?glob-matches
114 [ 2drop t ] [ file-name-glob>> ?glob-matches ] if ;
116 : find-mode ( file-name first-line -- mode )
118 [ nip [ 2dup ] dip suitable-mode? ] assoc-find
119 2drop [ 2drop ] dip [ "text" ] unless* ;