]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/help/syntax/syntax.factor
help.syntax: treat $url differently
[factor.git] / basis / help / syntax / syntax.factor
index bd0cfd0d460af0902171a8dffcf5a60cdcd68db1..3e6256acbb834dbcc534bf0980b727cde7e4fc9d 100644 (file)
@@ -1,42 +1,70 @@
 ! Copyright (C) 2005, 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
+
 USING: accessors arrays ascii combinators
 combinators.short-circuit compiler.units definitions help
-help.markup help.topics kernel lexer math namespaces parser
-sequences splitting strings vocabs.parser words ;
+help.markup help.topics kernel lexer math math.order namespaces
+parser sequences splitting strings strings.parser vocabs.parser
+words ;
+
 IN: help.syntax
 
+DEFER: HELP{
+
 <PRIVATE
 
-:: parse-help-token ( -- str/obj/f )
-    ?scan-token dup search {
-        { [ dup \ } eq? ] [ 2drop f ] }
+:: parse-help-token ( end -- str/obj/f )
+    ?scan-token dup {
+        [ "{" = [ \ HELP{ ] [ f ] if ]
+        [ "syntax" lookup-word ]
+        [ { [ "$" head? ] [ "help.markup" lookup-word ] } 1&& ]
+        [ dup ?last ":{[(/\"" member-eq? [ search ] [ drop f ] if ]
+    } 1|| {
+        { [ dup not ] [ drop ] }
+        { [ dup end eq? ] [ 2drop f ] }
         { [ dup parsing-word? ] [
             nip V{ } clone swap execute-parsing first
             dup wrapper? [ wrapped>> \ $link swap 2array ] when ] }
-        [ drop ]
+        { [ dup ] [ nip ] }
     } cond ;
 
 : push-help-text ( accum sbuf obj -- accum sbuf' )
     [ dup empty? [ >string suffix! SBUF" " clone ] unless ]
     [ [ suffix! ] curry dip ] bi* ;
 
-: push-help-space ( accum sbuf -- accum sbuf )
+: help-block? ( word -- ? )
     {
-        [ dup empty? not ]
-        [ over empty? not pick ?last \ $nl eq? not and ]
-    } 0|| [ CHAR: \s suffix! ] when ;
+        $description $heading $subheading $syntax
+        $class-description $error-description $var-description
+        $contract $notes $curious $deprecated $errors
+        $side-effects $content $warning $subsections $nl
+        $list $table $example $unchecked-example $code
+    } member-eq? ;
 
-:: parse-help-text ( -- seq )
+: push-help-space ( accum sbuf -- accum sbuf )
+    dup empty? [
+        over empty? not
+        pick ?last dup array? [ ?first ] when
+        help-block? not and
+    ] [
+        dup last CHAR: \s eq? not
+    ] if [ CHAR: \s suffix! ] when ;
+
+:: parse-help-text ( end -- seq )
     V{ } clone SBUF" " clone [
-        lexer get line>> parse-help-token [
-            lexer get line>> swap - 1 > [
-                \ $nl push-help-text
-            ] when
-        ] dip [
+        lexer get line>> :> m
+        end parse-help-token :> obj
+        lexer get line>> :> n
+
+        obj string? n m - 1 > and [
+            { [ dup empty? not ] [ over ?last string? ] } 0||
+            [ \ $nl push-help-text ] when
+        ] when
+
+        obj [
             [
                 dup string? [
-                    dup ?first ".,;:" member? [
+                    dup ?first " .,;:" member? [
                         [ push-help-space ] dip
                     ] unless append!
                 ] [
@@ -50,54 +78,65 @@ IN: help.syntax
 : parse-help-values ( -- seq )
     [ scan-token dup "}" = not ] [
         dup "{" = [
-            parse-datum dup parsing-word?
-            [ V{ } clone swap execute-parsing first ] when
+            drop \ } parse-until >array
         ] [
             ":" ?tail drop scan-object 2array
         ] if
     ] produce nip ;
 
-: example-lines ( seq -- seq' )
-    dup string? [ string-lines [ [ blank? ] trim ] map harvest ] when ;
+: whitespace ( seq -- n )
+    [ [ blank? ] all? ] reject [ 0 ] [
+        [ [ blank? not ] find drop ] [ min ] map-reduce
+    ] if-empty ;
+
+: trim-whitespace ( seq -- seq' )
+    dup rest-slice dup whitespace
+    [ '[ _ index-or-length tail ] map! ] unless-zero drop
+    0 over [ [ blank? ] trim-head ] change-nth ;
+
+: code-lines ( str -- seq )
+    split-lines trim-whitespace [ [ blank? ] all? ] trim ;
 
-: make-example ( str type -- seq )
-    over string? [
-        [ example-lines ] [ prefix ] bi*
-    ] [ drop ] if ;
+: make-example ( str -- seq )
+    code-lines dup { [ array? ] [ length 1 > ] } 1&& [
+        dup length 1 - over [ unescape-string ] change-nth
+        \ $example prefix
+    ] when ;
 
 : parse-help-examples ( -- seq )
-    \ } parse-until [ \ $example make-example ] { } map-as ;
+    \ } parse-until dup [ string? ] all?
+    [ [ make-example ] { } map-as ] [ >array ] if ;
 
-: parse-help-example ( -- seq )
+: parse-help-code ( -- seq )
     \ } parse-until dup { [ length 1 = ] [ first string? ] } 1&&
-    [ first example-lines ] when ;
+    [ first code-lines ] [ >array ] if ;
 
 : help-text? ( word -- ? )
     {
-        $description $snippet $emphasis $strong $url $heading
-        $subheading $code $syntax $class-description
+        $description $snippet $emphasis $strong $heading
+        $subheading $syntax $class-description
         $error-description $var-description $contract $notes
         $curious $deprecated $errors $side-effects $content
-        $slot $image
+        $slot $image $warning
     } member-eq? ;
 
+: help-code? ( word -- ? )
+    { $example $unchecked-example $code $url } member-eq? ;
+
 : help-values? ( word -- ? )
     { $values $inputs $outputs } member-eq? ;
 
 : help-examples? ( word -- ? )
     { $examples } member-eq? ;
 
-: help-example? ( word -- ? )
-    { $example $unchecked-example } member-eq? ;
-
 PRIVATE>
 
 SYNTAX: HELP{
-    scan-word dup \ } eq? [ drop { } ] [
+    scan-object dup \ } eq? [ drop { } ] [
         {
-            { [ dup help-text? ] [ parse-help-text ] }
+            { [ dup help-text? ] [ \ } parse-help-text ] }
+            { [ dup help-code? ] [ parse-help-code ] }
             { [ dup help-values? ] [ parse-help-values ] }
-            { [ dup help-example? ] [ parse-help-example ] }
             { [ dup help-examples? ] [ parse-help-examples ] }
             [ \ } parse-until >array ]
         } cond swap prefix
@@ -111,13 +150,10 @@ SYNTAX: HELP:
         bi
     ] with-words ;
 
-ERROR: article-expects-name-and-title got ;
-
 SYNTAX: ARTICLE:
     location [
-        parse-array-def
-        dup length 2 < [ article-expects-name-and-title ] when
-        [ first2 ] [ 2 tail ] bi <article>
+        scan-object scan-object
+        \ ; parse-help-text <article>
         over add-article >link
     ] dip remember-definition ;