]> gitweb.factorcode.org Git - factor.git/blob - basis/help/syntax/syntax.factor
Revert "help.syntax: prevent unnecessary ambiguous-use-errors."
[factor.git] / basis / help / syntax / syntax.factor
1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays ascii combinators
4 combinators.short-circuit compiler.units definitions help
5 help.markup help.topics kernel lexer math namespaces parser
6 sequences splitting strings strings.parser vocabs.parser words ;
7 IN: help.syntax
8
9 <PRIVATE
10
11 :: parse-help-token ( end -- str/obj/f )
12     ?scan-token dup search {
13         { [ dup not ] [ drop ] }
14         { [ dup end eq? ] [ 2drop f ] }
15         { [ dup parsing-word? ] [
16             nip V{ } clone swap execute-parsing first
17             dup wrapper? [ wrapped>> \ $link swap 2array ] when ] }
18         { [ dup vocabulary>> "help.markup" = ] [ nip ] }
19         [ drop ]
20     } cond ;
21
22 : push-help-text ( accum sbuf obj -- accum sbuf' )
23     [ dup empty? [ >string suffix! SBUF" " clone ] unless ]
24     [ [ suffix! ] curry dip ] bi* ;
25
26 DEFER: help-block?
27
28 : push-help-space ( accum sbuf -- accum sbuf )
29     dup empty? [
30         over empty? not
31         pick ?last dup array? [ ?first ] when
32         help-block? not and
33     ] [
34         dup last CHAR: \s eq? not
35     ] if [ CHAR: \s suffix! ] when ;
36
37 :: parse-help-text ( end -- seq )
38     V{ } clone SBUF" " clone [
39         lexer get line>> :> m
40         end parse-help-token :> obj
41         lexer get line>> :> n
42
43         obj string? n m - 1 > and [
44             { [ dup empty? not ] [ over ?last string? ] } 0||
45             [ \ $nl push-help-text ] when
46         ] when
47
48         obj [
49             [
50                 dup string? [
51                     dup ?first " .,;:" member? [
52                         [ push-help-space ] dip
53                     ] unless append!
54                 ] [
55                     [ push-help-space ]
56                     [ push-help-text ] bi*
57                 ] if
58             ] when*
59         ] keep
60     ] loop [ >string suffix! ] unless-empty >array ; inline
61
62 : parse-help-values ( -- seq )
63     [ scan-token dup "}" = not ] [
64         dup "{" = [
65             drop \ } parse-until >array
66         ] [
67             ":" ?tail drop scan-object 2array
68         ] if
69     ] produce nip ;
70
71 : code-lines ( str -- seq )
72     string-lines [ [ blank? ] trim ] map harvest ;
73
74 : make-example ( seq -- seq )
75     dup string? [
76         code-lines
77         dup { [ array? ] [ length 1 > ] } 1&& [
78             dup length 1 - over [ unescape-string ] change-nth
79             \ $example prefix
80         ] when
81     ] when ;
82
83 : parse-help-examples ( -- seq )
84     \ } parse-until [ make-example ] { } map-as ;
85
86 : parse-help-code ( -- seq )
87     \ } parse-until dup { [ length 1 = ] [ first string? ] } 1&&
88     [ first code-lines ] [ >array ] if ;
89
90 : help-text? ( word -- ? )
91     {
92         $description $snippet $emphasis $strong $url $heading
93         $subheading $syntax $class-description
94         $error-description $var-description $contract $notes
95         $curious $deprecated $errors $side-effects $content
96         $slot $image $warning
97     } member-eq? ;
98
99 : help-code? ( word -- ? )
100     { $example $unchecked-example $code } member-eq? ;
101
102 : help-block? ( word -- ? )
103     {
104         $description $heading $subheading $syntax
105         $class-description $error-description $var-description
106         $contract $notes $curious $deprecated $errors
107         $side-effects $content $warning $subsections $nl
108     } member-eq? ;
109
110 : help-values? ( word -- ? )
111     { $values $inputs $outputs } member-eq? ;
112
113 : help-examples? ( word -- ? )
114     { $examples } member-eq? ;
115
116 PRIVATE>
117
118 SYNTAX: HELP{
119     scan-object dup \ } eq? [ drop { } ] [
120         {
121             { [ dup help-text? ] [ \ } parse-help-text ] }
122             { [ dup help-code? ] [ parse-help-code ] }
123             { [ dup help-values? ] [ parse-help-values ] }
124             { [ dup help-examples? ] [ parse-help-examples ] }
125             [ \ } parse-until >array ]
126         } cond swap prefix
127     ] if suffix! ;
128
129 SYNTAX: HELP:
130     H{ { "{" POSTPONE: HELP{ } } [
131         scan-word bootstrap-word
132         [ >link save-location ]
133         [ [ parse-array-def ] dip set-word-help ]
134         bi
135     ] with-words ;
136
137 SYNTAX: ARTICLE:
138     location [
139         scan-object scan-object
140         \ ; parse-help-text <article>
141         over add-article >link
142     ] dip remember-definition ;
143
144 SYNTAX: ABOUT:
145     current-vocab scan-object >>help changed-definition ;