]> gitweb.factorcode.org Git - factor.git/commitdiff
fix HEREDOC:s, add DELIMITED: which is like a HEREDOC: where the terminator can appea...
authorDoug Coleman <doug.coleman@gmail.com>
Tue, 18 Aug 2009 08:44:54 +0000 (03:44 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Tue, 18 Aug 2009 08:44:54 +0000 (03:44 -0500)
basis/multiline/multiline-docs.factor
basis/multiline/multiline-tests.factor
basis/multiline/multiline.factor

index 0977acd1cd1a214283c48308f8be88bb2dda456b..fd91c440d73c782d44d4ab5efb7fa67a01122647 100644 (file)
@@ -1,4 +1,4 @@
-USING: help.markup help.syntax ;
+USING: help.markup help.syntax strings ;
 IN: multiline
 
 HELP: STRING:
@@ -19,24 +19,33 @@ HELP: /*
 } ;
 
 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, as in some other languages, a \"quoted string\"." }
+{ $syntax "HEREDOC: marker\n...text...\nmarker" }
+{ $values { "marker" "a word (token)" } { "text" "arbitrary text" } { "value" string } }
+{ $description "Returns a string delimited by an arbitrary user-defined token. This delimiter must be exactly the text beginning at the first non-blank character after the " { $link POSTPONE: HEREDOC: } " until the end of the line containing the " { $link POSTPONE: HEREDOC: } ". Text is captured until a line is found conatining exactly this delimter string." }
+{ $warning "Whitespace is significant." }
 { $examples
     { $example "USING: multiline prettyprint ;"
-               "HEREDOC: END\nx\nEND ."
+               "HEREDOC: END\nx\nEND\n."
                "\"x\\n\""
     }
-    { $example "USING: multiline prettyprint ;"
-               "HEREDOC: END\nxEND ."
-               "\"x\""
-    }
     { $example "USING: multiline prettyprint sequences ;"
-               "2 5 HEREDOC: zap\nfoo\nbarzap subseq ."
+               "2 5 HEREDOC: zap\nfoo\nbar\nzap\nsubseq ."
                "\"o\\nb\""
     }
 } ;
 
+HELP: DELIMITED:
+{ $syntax "DELIMITED: marker\n...text...\nmarker" }
+{ $values { "marker" "a word (token)" } { "text" "arbitrary text" } { "value" string } }
+{ $description "Returns a string delimited by an arbitrary user-defined token. This delimiter must be exactly the text beginning at the first non-blank character after the " { $link POSTPONE: DELIMITED: } " until the end of the line containing the " { $link POSTPONE: DELIMITED: } ". Text is captured until the exact delimiter string is found, regardless of where." }
+{ $examples
+    { $example "USING: multiline prettyprint ;"
+               "DELIMITED: factor blows my mind"
+"whoafactor blows my mind ."
+                "\"whoa\""
+    }
+} ;
+
 { POSTPONE: <" POSTPONE: STRING: } related-words
 
 HELP: parse-multiline-string
@@ -49,6 +58,7 @@ ARTICLE: "multiline" "Multiline"
 { $subsection POSTPONE: STRING: }
 { $subsection POSTPONE: <" }
 { $subsection POSTPONE: HEREDOC: }
+{ $subsection POSTPONE: DELIMITED: }
 "Multiline comments:"
 { $subsection POSTPONE: /* }
 "Writing new multiline parsing words:"
index 2458589d27d5c2484aad2b5ed7dc3e6f01d33bb7..25610ed6601bd391a5a335e81e179a7aa4ed207b 100644 (file)
@@ -1,4 +1,4 @@
-USING: multiline tools.test ;
+USING: accessors eval multiline tools.test ;
 IN: multiline.tests
 
 STRING: test-it
@@ -26,36 +26,66 @@ hi"> ] unit-test
 [ "foo\nbar\n" ] [ HEREDOC: END
 foo
 bar
-END ] unit-test
-
-[ "foo\nbar" ] [ HEREDOC: END
-foo
-barEND ] unit-test
+END
+] unit-test
 
 [ "" ] [ HEREDOC: END
-END ] unit-test
+END
+] unit-test
 
-[ " " ] [ HEREDOC: END
- END ] unit-test
+[ " END\n" ] [ HEREDOC: END
+ END
+END
+] unit-test
 
 [ "\n" ] [ HEREDOC: END
 
-END ] unit-test
+END
+] unit-test
 
-[ "x" ] [ HEREDOC: END
-xEND ] unit-test
+[ "x\n" ] [ HEREDOC: END
+x
+END
+] unit-test
 
-[ "xyz " ] [ HEREDOC: END
-xyz END ] unit-test
+[ "x\n" ] [ HEREDOC:       END
+x
+END
+] unit-test
+
+[ "xyz \n" ] [ HEREDOC: END
+xyz 
+END
+] unit-test
 
 [ "} ! * # \" «\n" ] [ HEREDOC: END
 } ! * # " «
-END ] unit-test
+END
+] unit-test
 
-[ 21 "foo\nbar" " HEREDOC: FOO\n FOO\n" 22 ] [ 21 HEREDOC: X
+[ 21 "foo\nbar\n" " HEREDOC: FOO\n FOO\n" 22 ] [ 21 HEREDOC: X
 foo
-barX HEREDOC: END ! mumble
+bar
+X
+HEREDOC: END
  HEREDOC: FOO
  FOO
-END 22 ] unit-test
+END
+22 ] unit-test
+
+[ "lol\n xyz\n" ]
+[
+HEREDOC: xyz
+lol
+ xyz
+xyz
+] unit-test
+
+
+[ "lol" ]
+[ DELIMITED: aol
+lolaol ] unit-test
 
+[ "whoa" ]
+[ DELIMITED: factor blows my mind
+whoafactor blows my mind ] unit-test
index e4334f1201101ca93bbc9c77cf879472e71d6ed0..4eaafe1f188c73d77d9210aca17d0feaf8e78ab4 100644 (file)
@@ -4,6 +4,8 @@ USING: namespaces make parser lexer kernel sequences words
 quotations math accessors locals ;
 IN: multiline
 
+ERROR: bad-heredoc identifier ;
+
 <PRIVATE
 : next-line-text ( -- str )
     lexer get dup next-line line-text>> ;
@@ -46,6 +48,28 @@ SYNTAX: STRING:
         change-column drop
     ] "" make ;
 
+: rest-of-line ( -- seq )
+    lexer get [ line-text>> ] [ column>> ] bi tail ;
+
+:: advance-same-line ( text -- )
+    lexer get [ text length + ] change-column drop ;
+
+:: (parse-til-line-begins) ( begin-text -- )
+    lexer get still-parsing? [
+        lexer get line-text>> begin-text sequence= [
+            begin-text advance-same-line
+        ] [
+            lexer get line-text>> % "\n" %
+            lexer get next-line
+            begin-text (parse-til-line-begins)
+        ] if
+    ] [
+        begin-text bad-heredoc
+    ] if ;
+
+: parse-til-line-begins ( begin-text -- seq )
+    [ (parse-til-line-begins) ] "" make ;
+
 PRIVATE>
 
 : parse-multiline-string ( end-text -- str )
@@ -66,7 +90,13 @@ SYNTAX: {"
 SYNTAX: /* "*/" parse-multiline-string drop ;
 
 SYNTAX: HEREDOC:
-    scan
+    lexer get skip-blank
+    rest-of-line
+    lexer get next-line
+    parse-til-line-begins parsed ;
+
+SYNTAX: DELIMITED:
+    lexer get skip-blank
+    rest-of-line
     lexer get next-line
-    0 (parse-multiline-string)
-    parsed ;
+    0 (parse-multiline-string) parsed ;