]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/help/syntax/syntax.factor
help.syntax: only allow help.markup $words
[factor.git] / basis / help / syntax / syntax.factor
index 6f5968b9726f456094bdd91bdc7e4f18b9303787..b45bfd3ee7904d070d9e67e4e4195902feb42d61 100644 (file)
@@ -1,27 +1,45 @@
 ! 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 strings.parser 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 ( end -- str/obj/f )
-    ?scan-token dup search {
+    ?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* ;
 
-DEFER: help-block?
+: help-block? ( word -- ? )
+    {
+        $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? ;
 
 : push-help-space ( accum sbuf -- accum sbuf )
     dup empty? [
@@ -66,23 +84,32 @@ DEFER: help-block?
         ] if
     ] produce nip ;
 
-: example-lines ( seq -- seq' )
-    dup string? [
-        string-lines [ [ blank? ] trim ] map harvest
-        dup length 1 - over [ unescape-string ] change-nth
-    ] 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 ;
 
-: make-example ( seq -- seq )
-    dup string? [
-        example-lines dup length 1 > [ \ $example prefix ] when
+: code-lines ( str -- seq )
+    split-lines trim-whitespace [ [ blank? ] all? ] trim ;
+
+: 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 [ 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 ] [ >array ] if ;
+    [ first code-lines ] [ >array ] if ;
 
 : help-text? ( word -- ? )
     {
@@ -93,13 +120,8 @@ DEFER: help-block?
         $slot $image $warning
     } member-eq? ;
 
-: help-block? ( word -- ? )
-    {
-        $description $heading $subheading $syntax
-        $class-description $error-description $var-description
-        $contract $notes $curious $deprecated $errors
-        $side-effects $content $warning $subsections $nl
-    } member-eq? ;
+: help-code? ( word -- ? )
+    { $example $unchecked-example $code } member-eq? ;
 
 : help-values? ( word -- ? )
     { $values $inputs $outputs } member-eq? ;
@@ -107,17 +129,14 @@ DEFER: help-block?
 : help-examples? ( word -- ? )
     { $examples } member-eq? ;
 
-: help-example? ( word -- ? )
-    { $example $unchecked-example $code } member-eq? ;
-
 PRIVATE>
 
 SYNTAX: HELP{
     scan-object dup \ } eq? [ drop { } ] [
         {
             { [ 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