Inline multiline string literals

release
Daniel Ehrenberg 2007-12-11 17:23:56 -05:00
parent e1346ced8b
commit b397c369d7
3 changed files with 35 additions and 3 deletions

View File

@ -4,5 +4,18 @@ HELP: STRING:
{ $syntax "STRING: name\nfoo\n;" }
{ $description "Forms a multiline string literal, or 'here document' stored in the word called name. A semicolon is used to signify the end, and that semicolon must be on a line by itself, not preceeded or followed by any whitespace. The string will have newlines in between lines but not at the end, unless there is a blank line before the semicolon." } ;
IN: multiline
ABOUT: POSTPONE: STRING:
HELP: <"
{ $syntax "<\" text \">" }
{ $description "This forms a multiline string literal ending in \">. Unlike the " { $link POSTPONE: STRING: } " form, you can end it in the middle of a line. This construct is non-nesting. In the example above, the string would be parsed as \"text\"." } ;
{ POSTPONE: <" POSTPONE: STRING: } related-words
HELP: parse-here
{ $values { "str" "a string" } }
{ $description "Parses a multiline string literal, as used by " { $link POSTPONE: STRING: } "." } ;
HELP: parse-literal
{ $values { "end-text" "a string delineating the end" } { "str" "the parsed string" } }
{ $description "Parses a multiline string literal, as used by " { $link POSTPONE: <" } ". The end-text is the delimiter for the end." } ;
{ parse-here parse-literal } related-words

View File

@ -7,3 +7,6 @@ bar
;
[ "foo\nbar\n" ] [ test-it ] unit-test
[ "foo\nbar\n" ] [ <" foo
bar
"> ] unit-test

View File

@ -1,6 +1,6 @@
! Copyright (C) 2007 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
USING: namespaces parser kernel sequences words quotations ;
USING: namespaces parser kernel sequences words quotations math ;
IN: multiline
: next-line-text ( -- str )
@ -17,3 +17,19 @@ IN: multiline
: STRING:
CREATE dup reset-generic
parse-here 1quotation define-compound ; parsing
: (parse-literal) ( start-index end-text -- end-index )
lexer get line-text 2dup start
[ rot dupd >r >r swap subseq % r> r> length + ] [
rot tail % "\n" % 0
lexer get next-line swap (parse-literal)
] if* ;
: parse-literal ( end-text -- str )
[
lexer get lexer-column swap (parse-literal)
lexer get set-lexer-column
] "" make 1 tail 1 head* ;
: <"
"\">" parse-literal parsed ; parsing