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