Paste: HEREDOC: draft

Author: mncharity
Mode: factor
Date: Thu, 16 Jul 2009 17:59:50
Plain Text |

extra/heredoc/heredoc-docs.factor

! Copyright (C) 2009 Mitchell N Charity.
! See http://factorcode.org/license.txt for BSD license.
USING: help.markup help.syntax multiline ;
IN: heredoc

HELP: HEREDOC:
{ $syntax "HEREDOC: marker\n...text...marker" }
{ $values { "marker" "a word (token)" } { "text" "arbitrary text" } { "" "a string" } }
{ $description "A multiline string syntax with a user-specified terminating delimiter.  HEREDOC: reads the next word, and uses it as the 'close quote'.  All input from the beginning of the HEREDOC:'s next line, until the first appearance of the word's name, becomes a string.  The terminating word does _not_ need to be at the beginning of a line.\n\nThe HEREDOC: line should not have anything after the delimiting word.  The delimiting word should be an alphanumeric token.  It should not be, for example, a \"quoted string\"." }
{ $examples
    { $example "USING: heredoc ;" "HEREDOC: END\nx\nEND" "! \"x\\n\"" }
    { $example "HEREDOC: END\nxEND" "! \"x\"" }
    { $example "2 5 HEREDOC: zap\nfoo\nbarzap subseq" "! \"o\\nb\"" }
}
{ $notes "HEREDOC: is built on multiline.  A private word in " { $link "multiline" } " is currently used, as the public " { $link parse-multiline-string } " skips forward over one character." }
;

ARTICLE: "heredoc" "Here-document syntax"
"Code can contain \"here documents\" - multiline strings with arbitrary termination."
{ $code "USE: heredoc" "2 5 HEREDOC: zap\nfoo\nbarzap subseq ! \"o\\nb\"" }
"See " { $vocab-link "heredoc" } "."
;

ABOUT: "heredoc"


extra/heredoc/heredoc.factor

! Copyright (C) 2009 Mitchell N Charity.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel lexer locals make math multiline.private namespaces parser ;
IN: heredoc

<PRIVATE

:: (make-multiline-string) ( end-text skip-n-chars -- str )
    ! a copy of multiline's parse-multiline-string ,
    ! but without skip-n-chars hardwired to 1.
    [
        lexer get
        [ skip-n-chars + end-text (parse-multiline-string) ]
        change-column drop
    ] "" make ;

PRIVATE>

SYNTAX: HEREDOC:
    scan
    lexer get next-line ! ( -- )
    0 (make-multiline-string)
    parsed ;


extra/heredoc/heredoc-tests.factor

! Copyright (C) 2009 Mitchell N Charity.
! See http://factorcode.org/license.txt for BSD license.
USING: heredoc tools.test ;
IN: heredoc.tests

[ "foo\nbar\n" ] [ HEREDOC: END
foo
bar
END ] unit-test

[ "foo\nbar" ] [ HEREDOC: END
foo
barEND ] unit-test

[ "" ] [ HEREDOC: END
END ] unit-test

[ " " ] [ HEREDOC: END
 END ] unit-test

[ "\n" ] [ HEREDOC: END

END ] unit-test

[ "x" ] [ HEREDOC: END
xEND ] unit-test

[ "xyz " ] [ HEREDOC: END
xyz END ] unit-test

[ "} ! * # \" «\n" ] [ HEREDOC: END
} ! * # " «
END ] unit-test

[ 21 "foo\nbar" " HEREDOC: FOO\n FOO\n" 22 ] [ 21 HEREDOC: X
foo
barX HEREDOC: END ! mumble
 HEREDOC: FOO
 FOO
END 22 ] unit-test

New Annotation

Summary:
Author:
Mode:
Body: