]> gitweb.factorcode.org Git - factor.git/commitdiff
Inline multiline string literals
authorDaniel Ehrenberg <ehrenbed@carleton.edu>
Tue, 11 Dec 2007 22:23:56 +0000 (17:23 -0500)
committerDaniel Ehrenberg <ehrenbed@carleton.edu>
Tue, 11 Dec 2007 22:23:56 +0000 (17:23 -0500)
extra/multiline/multiline-docs.factor
extra/multiline/multiline-tests.factor
extra/multiline/multiline.factor

index d2d9c7967fde07e06d2926a06ef12a8471592669..05ccdefe6ffe7cdc6a8cf71fd88b4d196765800d 100644 (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
index 5ef289f4c3eca21243773f74639b5944cce0806d..a9b9ee232294de0176404b6a602d5828f69bb607 100644 (file)
@@ -7,3 +7,6 @@ bar
 ;
 
 [ "foo\nbar\n" ] [ test-it ] unit-test
+[ "foo\nbar\n" ] [ <" foo
+bar
+ "> ] unit-test
index 1229dcc6899ec51228b41c6d8926404f81cb3493..e808b7899738b1a2fc81363e97a5bdbbedef87ba 100644 (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