Feature Raw/verbatim string literals in ASH

gulaschkanone

New member
Some languages have raw/verbatim/whatever string literals, in which escape sequences are not evaluated, for example in C# the literal @"\nneat\n" is equivalent to what one otherwise would write as "\\nneat\\n", escaping the backslashes to doublebackslashes to get a literal "\n" into the string rather than a newline character. It's minor, but would still be nice to have - mainly for regexps that often end up being littered with hard to read and get correct double-triple-quintuple-backslashes, which I often find a frustrating source of error - and hopefully not too hard to implement.

Not related, but thanks for your prompt reactions to previous requests. I thought bumping the threads with nothing but "thanks" would be spammy, so I didn't.
 

heeheehee

Developer
Staff member
Similarly, Python has r"text". I do like the premise of @"text" more, mainly because Python-style creates potential for typos such as f(r"text") instead of f(r,"text") (@ is already used in the context of calling the main functions of imported scripts, but I don't think that should conflict). Python deals with raw strings strangely.
Code:
r"
"
throws an error (eol while scanning string literal), but
Code:
r"\
"
is parsed as "\\\n" (i.e. a backslash followed by a newline). Similarly, r""" gets parsed into a backslash followed by a quotation mark.

Perl and some other languages (JavaScript, Ruby come to mind) also allow for the creation of regular expressions via /pattern/flags (e.g. /<img\b.*?>/i to match a single image tag in a case-insensitive manner), although I'm not sure if it makes sense to expose Java's Pattern class just for that.

For reasons that were better explained in the multi-line strings thread, I'd rather not allow for multi-line raw strings (maybe only if explicitly escaped?).
 

Crowther

Active member
Regular expressions are certainly hard to read in ASH, especially since we are often parsing HTML which shares important characters with regex. I always liked the way csh differentiated between single and double quotes, but I think that syntax is pretty rare today.
 

heeheehee

Developer
Staff member
Oh, of course, I forgot about the single vs double quote in various shell scripting languages (bash, csh, etc), but that's an especially bad option, since it breaks backward compatibility.
 

Veracity

Developer
Staff member
Yeah, you know. I wished for this today. I wrote a little test program in which I copied in combat macro. I ended up doing this:

Code:
static
{
    buffer buf;
    buf.append( "abort hppercentbelow 15\n" );
    buf.append( "sub mafiaround\n" );
    buf.append( "endsub#mafiaround\n" );
    buf.append( "sub mafiamp\n" );
    buf.append( "    call mafiaround; use 518\n" );
    buf.append( "endsub#mafiamp\n" );
    buf.append( "#mafiaheader\n" );
    buf.append( "if hasskill Chronic Indigestion\n" );
    buf.append( "    while mpbelow 5\n" );
    buf.append( "        call mafiamp\n" );
    buf.append( "    endwhile\n" );
    buf.append( "    if hasskill 2\n" );
    buf.append( "        call mafiaround; skill 2\n" );
    buf.append( "    endif\n" );
    buf.append( "endif\n" );
    string macro = buf.to_string();
}
Simply because I wanted it to be readable. I could have done it like this:

Code:
string macro = "abort hppercentbelow 15\nsub mafiaround\nendsub#mafiaround\nsub mafiamp\n    call mafiaround; use 518\nendsub#mafiamp\n#mafiaheader\nif hasskill Chronic Indigestion\n    while mpbelow 5\n        call mafiamp\n    endwhile\n    if hasskill 2\n        call mafiaround; skill 2\n    endif\nendif\n""
but I would have preferred to do it like this:

Code:
string macro = "
abort hppercentbelow 15
sub mafiaround
endsub#mafiaround
sub mafiamp
    call mafiaround; use 518
endsub#mafiamp
#mafiaheader
if hasskill Chronic Indigestion
    while mpbelow 5
        call mafiamp
    endwhile
    if hasskill 2
        call mafiaround; skill 2
    endif
endif";
Or something.
 

heeheehee

Developer
Staff member
I thought I added multiline strings, so you could do something along the lines of
Code:
string macro = "abort hppercentbelow 15\
sub mafiaround\
endsub#mafiaround\
sub mafiamp\
    call mafiaround; use 518\
endsub#mafiamp\
#mafiaheader\
if hasskill Chronic Indigestion\
    while mpbelow 5\
        call mafiamp\
    endwhile\
    if hasskill 2\
        call mafiaround; skill 2\
    endif\
endif";

Not quite as concise, I admit, but the originally suggested use case (to avoid escaping backslashes in regex strings) is (to me, at least) a much more compelling argument.
 

Veracity

Developer
Staff member
You added it? Well, I'll be. I need to find the "multi line string thread" that you referenced; I THOUGHT I had seen it, but forum search only showed me this one.
 

Bale

Minion
For reference: http://kolmafia.us/showthread.php?19867-Multiline-strings-in-ASH-(potential-feature-)

And:
r16800: Add multi-line string support to ASH. In order to specify a multi-line string,
you must explicitly escape the end-of-line by placing a backslash (\) at the end of the line.

Note that as a consequence of how the parser is written, it will trim all
leading and trailing whitespace for each of these lines via Java's
String.trim() method. Previously, this was irrelevant in the context of ASH
strings, but now that ASH strings can span multiple lines, it is worth
mentioning. If you want to have whitespace at the beginning of the line,
just escape the first character of the line.

Marking this implemented.
 

heeheehee

Developer
Staff member
I didn't personally mark it as implemented back then, since raw string literals are not the same as multiline strings.
 
Top