]> gitweb.factorcode.org Git - factor.git/blob - basis/help/lint/checks/checks.factor
Merge branch 'bags' of git://github.com/littledan/Factor
[factor.git] / basis / help / lint / checks / checks.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes combinators
4 combinators.short-circuit definitions effects eval fry grouping
5 help help.markup help.topics io.streams.string kernel macros
6 namespaces sequences sequences.deep sets sorting splitting
7 strings unicode.categories values vocabs vocabs.loader words
8 words.symbol summary debugger io ;
9 FROM: sets => members ;
10 IN: help.lint.checks
11
12 ERROR: simple-lint-error message ;
13
14 M: simple-lint-error summary message>> ;
15
16 M: simple-lint-error error. summary print ;
17
18 SYMBOL: vocabs-quot
19 SYMBOL: all-vocabs
20 SYMBOL: vocab-articles
21
22 : check-example ( element -- )
23     '[
24         _ rest [
25             but-last "\n" join
26             [ (eval>string) ] call( code -- output )
27             "\n" ?tail drop
28         ] keep
29         last assert=
30     ] vocabs-quot get call( quot -- ) ;
31
32 : check-examples ( element -- )
33     \ $example swap elements [ check-example ] each ;
34
35 : extract-values ( element -- seq )
36     \ $values swap elements dup empty? [
37         first rest [ first ] map
38     ] unless ;
39
40 : extract-value-effects ( element -- seq )
41     \ $values swap elements dup empty? [
42         first rest [ 
43             \ $quotation swap elements dup empty? [ drop f ] [
44                 first second
45             ] if
46         ] map
47     ] unless ;
48
49 : effect-values ( word -- seq )
50     stack-effect
51     [ in>> ] [ out>> ] bi append
52     [ dup pair? [ first ] when effect>string ] map members ;
53
54 : effect-effects ( word -- seq )
55     stack-effect in>> [
56         dup pair?
57         [ second dup effect? [ effect>string ] [ drop f ] if ]
58         [ drop f ] if
59     ] map ;
60
61 : contains-funky-elements? ( element -- ? )
62     {
63         $shuffle
64         $complex-shuffle
65         $values-x/y
66         $predicate
67         $class-description
68         $error-description
69     } swap '[ _ elements empty? not ] any? ;
70
71 : don't-check-word? ( word -- ? )
72     {
73         [ macro? ]
74         [ symbol? ]
75         [ value-word? ]
76         [ parsing-word? ]
77         [ "declared-effect" word-prop not ]
78     } 1|| ;
79
80 : check-values ( word element -- )
81     {
82         [
83             [ don't-check-word? ]
84             [ contains-funky-elements? ]
85             bi* or
86         ] [
87             [ effect-values ]
88             [ extract-values ]
89             bi* sequence=
90         ] 
91     } 2|| [ "$values don't match stack effect" simple-lint-error ] unless ;
92
93 : check-value-effects ( word element -- )
94     [ effect-effects ]
95     [ extract-value-effects ]
96     bi* [ 2dup and [ = ] [ 2drop t ] if ] 2all?
97     [ "$quotation documentation in $values don't match stack effect" simple-lint-error ]
98     unless ;
99
100 : check-nulls ( element -- )
101     \ $values swap elements
102     null swap deep-member?
103     [ "$values should not contain null" simple-lint-error ] when ;
104
105 : check-see-also ( element -- )
106     \ $see-also swap elements [
107         rest all-unique? t assert=
108     ] each ;
109
110 : vocab-exists? ( name -- ? )
111     [ vocab ] [ all-vocabs get member? ] bi or ;
112
113 : check-modules ( element -- )
114     \ $vocab-link swap elements [
115         second
116         vocab-exists? [
117             "$vocab-link to non-existent vocabulary"
118             simple-lint-error
119         ] unless
120     ] each ;
121
122 : check-rendering ( element -- )
123     [ print-content ] with-string-writer drop ;
124
125 : check-strings ( str -- )
126     [
127         "\n\t" intersects? [
128             "Paragraph text should not contain \\n or \\t"
129             simple-lint-error
130         ] when
131     ] [
132         "  " swap subseq? [
133             "Paragraph text should not contain double spaces"
134             simple-lint-error
135         ] when
136     ] bi ;
137
138 : check-whitespace ( str1 str2 -- )
139     [ " " tail? ] [ " " head? ] bi* or
140     [ "Missing whitespace between strings" simple-lint-error ] unless ;
141
142 : check-bogus-nl ( element -- )
143     { { $nl } { { $nl } } } [ head? ] with any? [
144         "Simple element should not begin with a paragraph break"
145         simple-lint-error
146     ] when ;
147
148 : check-class-description ( word element -- )
149     [ class? not ]
150     [ { $class-description } swap elements empty? not ] bi* and
151     [ "A word that is not a class has a $class-description" simple-lint-error ] when ;
152
153 : check-article-title ( article -- )
154     article-title first LETTER?
155     [ "Article title must begin with a capital letter" simple-lint-error ] unless ;
156
157 : check-elements ( element -- )
158     {
159         [ check-bogus-nl ]
160         [ [ string? ] filter [ check-strings ] each ]
161         [ [ simple-element? ] filter [ check-elements ] each ]
162         [ 2 <clumps> [ [ string? ] all? ] filter [ first2 check-whitespace ] each ]
163     } cleave ;
164
165 : check-descriptions ( element -- )
166     { $description $class-description $var-description }
167     swap '[
168         _ elements [
169             rest { { } { "" } } member?
170             [ "Empty $description" simple-lint-error ] when
171         ] each
172     ] each ;
173
174 : check-markup ( element -- )
175     {
176         [ check-elements ]
177         [ check-rendering ]
178         [ check-examples ]
179         [ check-modules ]
180         [ check-descriptions ]
181     } cleave ;
182
183 : files>vocabs ( -- assoc )
184     vocabs
185     [ [ [ vocab-docs-path ] keep ] H{ } map>assoc ]
186     [ [ [ vocab-source-path ] keep ] H{ } map>assoc ]
187     bi assoc-union ;
188
189 : group-articles ( -- assoc )
190     articles get keys
191     files>vocabs
192     H{ } clone [
193         '[
194             dup >link where dup
195             [ first _ at _ push-at ] [ 2drop ] if
196         ] each
197     ] keep ;
198
199 : all-word-help ( words -- seq )
200     [ word-help ] filter ;