This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,4 @@
{{omit from|BBC BASIC}}
A here document (or "heredoc") is a way of specifying a text block, preserving the line breaks, indentation and other whitespace within the text. Depending on the language being used a here document is constructed using a command followed by "<<" (or some other symbol) followed by a token string. The text block will then start on the next line, and will be followed by the chosen token at the beginning of the following line, which is used to mark the end of the textblock.
The task is to demonstrate the use of here documents within the language.

View file

@ -0,0 +1,17 @@
#!/usr/local/bin/a68g --script #
[]STRING help = (
"Usage: thingy [OPTIONS]",
" -h Display this usage message",
" -H hostname Hostname to connect to"
);
printf(($gl$,help,$l$));
printf(($gl$,
"The river was deep but I swam it, Janet.",
"The future is ours so let's plan it, Janet.",
"So please don't tell me to can it, Janet.",
"I've one thing to say and that's ...",
"Dammit. Janet, I love you."
))

View file

@ -0,0 +1,20 @@
with Ada.Containers.Indefinite_Vectors, Ada.Text_IO;
procedure Here_Doc is
package String_Vec is new Ada.Containers.Indefinite_Vectors
(Index_Type => Positive,
Element_Type => String);
use type String_Vec.Vector;
Document: String_Vec.Vector := String_Vec.Empty_Vector
& "This is a vector of strings with the following properties:"
& " - indention is preserved, and"
& " - a quotation mark '""' must be ""escaped"" by a double-quote '""""'.";
begin
Document := Document & "Have fun!";
for I in Document.First_Index .. Document.Last_Index loop
Ada.Text_IO.Put_Line(Document.Element(I));
end loop;
end Here_Doc;

View file

@ -0,0 +1,9 @@
MyVar = "This is the text inside MyVar"
MyVariable =
(
Note that whitespace is preserved
As well as newlines.
The LTrim option can be present to remove left whitespace.
Variable references such as %MyVar% are expanded.
)
MsgBox % MyVariable

View file

@ -0,0 +1,13 @@
using System;
class Program
{
static void Main(string[] args)
{
Console.Write(@"
multiline
strings are easy
to put together
in C#");
}
}

View file

@ -0,0 +1,11 @@
myDoc = '''
Single-quoted heredocs allows no '#{foo}' interpolation.
This behavior is similar to single-quoted strings.
'''
doc2 = """
However, double-quoted heredocs *do* allow these.
See it in action:
Content: "#{myDoc}"
"""
console.log doc2

View file

@ -0,0 +1 @@
auto s = q"[a string that you "don't" have to escape]";

View file

@ -0,0 +1,6 @@
writefln(q"EOS
This
is a multi-line
heredoc string
EOS"
);

View file

@ -0,0 +1,4 @@
PrintLn("This is
a multiline
""string""
sample");

View file

@ -0,0 +1,49 @@
\ GForth specific words:
\ under+ ( a b c -- a+c b) , latest ( -- nt ) , name>string ( nt -- ca u )
\ Should not be a problem to modify it to work with other Forth implementation:
: $! ( ca u -- a )
dup >R dup , here swap move R> allot ;
: $@ ( a -- ca u )
dup @ 1 cells under+ ;
: c!+ ( c ca - ca+1 )
tuck c! 1+ ;
: $!+ ( a u a' -- a'+u ; string-store-plus )
2dup + >R swap move R> ;
\ --- UNIX end-of-line adapted words
10 constant EOL
: input-fix ( -- ; for interactive use ! )
source-id 0= IF cr THEN ;
: get_line ( -- ca u ) EOL parse input-fix ;
: EOL!+ ( a -- a' ; eol-store-plus ) EOL swap c!+ ;
: EOD ( -- ca u ; end-of-doc )
latest name>string ;
: >> ( "doc-name" "doc-body" -- ) input-fix
CREATE 0 , \ post body length
here dup >R
BEGIN refill >R
get_line 2dup EOD compare
R> AND \ notEOD && input-stream ->
WHILE rot $!+ EOL!+
REPEAT 2drop
R> tuck - dup allot
swap -1 cells + ! \ fixup body length
DOES> ( -- ca u ) $@ ;
\ TEST ; excerpt from Project Gutenberg 'Alice in Wonderland'
>> ALICE
CHAPTER I. Down the Rabbit-Hole
Alice was beginning to get very tired of sitting by her sister on the
bank, and of having nothing to do: once or twice she had peeped into the
book her sister was reading, but it had no pictures or conversations in
it, 'and what is the use of a book,' thought Alice 'without pictures or
conversation?'
ALICE
>> RABBIT
RABBIT
ALICE type ." --" cr RABBIT type

View file

@ -0,0 +1,3 @@
lyrics = """Oh, Danny Boy,
The pipes, the pipes are calling
From glen to glen and down the mountainside"""

View file

@ -0,0 +1,3 @@
var m = ` leading spaces
and blank lines`

View file

@ -0,0 +1,8 @@
println '''
Time's a strange fellow;
more he gives than takes
(and he takes all) nor any marvel finds
quite disappearance but some keener makes
losing, gaining
--love! if a world ends
'''

View file

@ -0,0 +1,18 @@
def expired='defunct'
def horse='stallion'
def christ='Jesus'
println """
Buffalo Bill's
${expired}
who used to
ride a watersmooth-silver
${horse}
and break onetwothreefourfive pigeonsjustlikethat
${christ}
he was a handsome man
and what i want to know is
how do you like your blueeyed boy
Mister Death
"""

View file

@ -0,0 +1,40 @@
here=:0 :0
0 :0 will be replaced by the text on the following lines.
This is three tokens: two instances of the number 0 and
one instance of the explicit definition token ':'.
Any indentation in the here document will be retained in the result.
There must be a space to the left of : or it will combine with the
0 on its left to form the token 0: which is something completely
different.
The here document is terminated by a line which contains only a
single right parenthesis ')' and optional white space. In J's
documentation the family of entities which include here documents
(and verb definitions and so on) are called 'scripts'.
When several scripts are referenced on the same line, they are used
sequentially in an order determined by their appearance on the line.
The leftmost 'script' reference gets the last script and the rightmost
reference gets the first script. But this is a rare usage.
Typically, such values are assigned a name so that they can be
used later. However, they may also be discarded and/or ignored, in
which case they are logically equivalent to multi-line comments.
)
and_here=:noun define
'noun define' is an alternative and perhaps more "user friendly"
way of declaring a here document. It achieves the same thing as
0 :0 and in fact 'noun' has the value 0 and 'define' has the value :0
And, of course, there must be a space between the word 'noun' and
the word 'define'.
Other useful alternatives include verb (which has the value 3)
and dyad (which has the value 4), and adverb (which has the value 1).
In other words 'verb define' (if unquoted) would be replaced by a
verb whose definition is provided in the following 'script'.
However, all of these names are normal variables which can
be declared to have different values by the developer. And, of course,
note that this mechanism is significantly more verbose than using
the underlying 0 :0 mechanism directly.
)

View file

@ -0,0 +1,14 @@
print([[
This is a long paragraph of text
it is the simplest while using it
with lua, however it will have the
same line breaks and spacing as
you set in this block.
]])
local msg = [[this is a message that spans
multiple lines and will have the next lines
preserved as they were entered, so be careful
when using this]]
print(msg)

View file

@ -0,0 +1,6 @@
$address = <<<END
1, High Street,
$town_name,
West Midlands.
WM4 5HD.
END;

View file

@ -0,0 +1,5 @@
$x = <<<'FOO'
No
$interpolation
here
FOO;

View file

@ -0,0 +1,6 @@
$address = <<END;
1, High Street,
$town_name,
West Midlands.
WM4 5HD.
END

View file

@ -0,0 +1,5 @@
$pancake = <<"NO MORE INGREDIENTS";
egg
milk
flour
NO MORE INGREDIENTS

View file

@ -0,0 +1,5 @@
$x = <<'FOO';
No
$interpolation
here
FOO

View file

@ -0,0 +1,3 @@
$output = <<`BAR`;
ls /home
BAR

View file

@ -0,0 +1,4 @@
print(<<EOF . "lamb\n");
Mary had
a little
EOF

View file

@ -0,0 +1,5 @@
print(<<EOF
Mary had
a little
EOF
. "lamb\n");

View file

@ -0,0 +1,9 @@
(out "file.txt" # Write to "file.txt"
(prinl "### This is before the text ###")
(here "TEXT-END")
(prinl "### This is after the text ###") )
"There must be some way out of here", said the joker to the thief
"There's too much confusion, I can't get no relief"
TEXT-END
(in "file.txt" (echo)) # Show "file.txt"

View file

@ -0,0 +1,5 @@
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")

View file

@ -0,0 +1,61 @@
/*REXX program demonstrates a method to use "here" documents in REXX. */
parse arg doc . /*"here" name is case sensitive. */
do j=1 for sourceline()
if sourceline(j)\==''doc then iterate
do !=j+1 to sourceline() while sourceline(!)\=='.'
say sourceline(!)
end /*!*/
exit /*stick a fork in it, we're done.*/
end /*j*/
say doc '"here" document not found.'
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────start of "here" docs──────────────────
rs-232
RS232 Signals and Pinouts
13 12 11 10 9 8 7 6 5 4 3 2 1
Interface between data 25 24 23 22 21 20 19 18 17 16 15 14
terminal equipment (DTE/male)
and data communication equipment
[DCE/female] employing serial
binary data interchange. 12secondary carrier detect [SCD] DCE
13secondary clear to send [SCS] DCE
1protective ground [PG, GND] 14secondary transmitted data [STD] DTE
2transmitted data [TD] DTE 15transmit clock [TC] DCE
3received data [RD] DCE 16secondary received data [SRD] DCE
4request to send [RTS] DTE 17receiver clock [RC] DCE
5clear to send [CTS] DCE 18unassigned
6data set ready [DSR] DCE 19secondary request to send [SRS] DTE
7signal ground [SG] 20data terminal ready [DTR] DTE
(common return) 21signal quality detector [SQD] DCE
8carrier detect [CD] DCE 22ring indicator [RI] DCE
9positive voltage [-] 23data rate select [DRS] DCE/DTE
10negative voltate [-] 24external clock [XTC] DTE
11unassigned 25unassigned
.
can
.
end of "here" docs*/

View file

@ -0,0 +1,8 @@
#lang racket/base
(displayln #<<EOF
Blah blah blah
with indentation intact
and "free" \punctuations\
EOF
)

View file

@ -0,0 +1,17 @@
#lang at-exp racket/base
(require scribble/text)
(define excited "!!!")
(define (shout . text) @list{>>> @text <<<})
(output
@list{Blah blah blah
with indentation intact
but respecting the indentation of
the whole code
and "free" \punctuations\
and even string interpolation-like @excited
but really @shout{any code}
})

View file

@ -0,0 +1,6 @@
address = <<END
1, High Street,
#{town_name},
West Midlands.
WM4 5HD.
END

View file

@ -0,0 +1,5 @@
pancake = <<"NO MORE INGREDIENTS"
egg
milk
flour
NO MORE INGREDIENTS

View file

@ -0,0 +1,5 @@
x = <<'FOO'
No
#{interpolation}
here
FOO

View file

@ -0,0 +1,3 @@
output = <<`BAR`
ls /home
BAR

View file

@ -0,0 +1,4 @@
puts <<EOF + "lamb"
Mary had
a little
EOF

View file

@ -0,0 +1,11 @@
set hereDocExample {
In Tcl, the {curly brace} notation is strictly a here-document style notation
as it permits arbitrary content inside it *except* for an unbalanced brace.
That is typically not a problem as seen in reality, as almost all content that
might be placed in a here-doc is either brace-free or balanced. The content
of the braces is not interpreted at all; no substitutions are performed on it.
The sole exception is that there is limited processing of backslashes; a single
backslash at the end of a line causes the end-of-line plus all whitespace at
the start of the next line to be compressed to a single space.
}