Double quotation marks for quoted strings: Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (Created page with "As of")
 
mNo edit summary
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
As of
As of <var class="product">Sirius Mods</var> version 7.8, the <var class="product">[[SOUL]]</var> and <var class="product">[[Janus SOAP]]</var> compiler accepts either a single-quotation-mark character (<tt>'</tt>) or a double-quotation-mark character (<tt>"</tt>) as a quoted-string delimiter.
Prior to this version, only a single-quotation-mark character (also called an
''apostrophe'') could be such a delimiter.
These are examples of the feature:
<ol>
<li>The statements below are equivalent as of version 7.8:
<p class="code">%n = 'abc':stringToHex
%n = "abc":stringToHex
</p>
<li>The hex string produced by the statements below as of version 7.8 is
<code>818283</code>:
<p class="code">printText {'abc':stringToHex}
printText {"abc":stringToHex}
</p>
<li>The first and second statements below are equivalent as of version 7.8,
producing the string <code>IT'S COOL!</code>:
<p class="code"><nowiki>printText {'It''s cool!':toUpper}
printText {"It's cool!":toUpper}
printText {"It''s cool!":toUpper}</nowiki>
</p>
The last statement in the example above produces the string <code><nowiki>IT''S COOL!</nowiki></code>,
which demonstrates that repeating a
single quotation mark does not escape it if the quoted string is delimited with double quotation marks.
</ol>
<p class="note">'''Note:'''
Since this double quotation mark feature is restricted to the compiler, it does not affect command parsing, and the following command is still '''''invalid''''': </p>
<p class="code"> RESET FUNCOPTS X"00"
</p>
As a rule, the double quotation mark character can be used to enclose, or bracket,
a quoted region that begins a string literal, or to bracket a
"continuation" of a string literal that begins with a double quotation mark,
but it may not otherwise be used to bracket a quoted region.
The "continuation of a string literal" refers to the peculiar <var class="product">User Language</var> concept that a quoted token does not end until it reaches a space or separator character after the closing quotation mark character.
So, the following statement prints <code>Hello</code>:
<p class="code">print 'Hel'lo
</p>
You can also continue the quoted region:
<p class="code">print 'Hel'lo' World'
</p>
This prints <code>Hello World</code>.
In addition, you can quote parts of unquoted tokens:
<p class="code">pr'int' 'Hello World'
</p>
This also prints <code>Hello World</code>.
<var class="product">User Language</var> quoted string continuation and the new functionality of double quotation marks
are shown in the following annotated request, which is valid in <var class="product">Sirius Mods</var> version 7.8.
The letter labels on the left
are not part of the request but are for the commentary that follows:
<p class="code">      Begin
(A)  %b'y' string len 255
      %b'y' = 'abc'
      print  %b'y'
(B)  print  %by
      call foobar
(C)  subroutine foo'bar'
(D)      print "here I am"
(E)      print 'come and 'g"et"
(F)      print "say ""uncle"""
(G)      print "Bob's"Your"Uncle"
      end subroutine
      end
</p>
<table>
<tr><th>(A)
</th><td>'''%b'y' string len 255'''
<code>'y'</code> is a quoted region that is a continuation of the %variable name.
</td></tr>
<tr><th>(B)
</th><td>'''print %by'''
The %variable <code>%by</code> is the same as the %variable <code>%b'y'</code>.
</td></tr>
<tr><th>(C)
</th><td>'''subroutine foo'bar''''
<code>'bar'</code> is a quoted region that is a continuation of the subroutine name; the name is the same as the name <code>foobar</code>.
</td></tr>
<tr><th>(D)
</th><td>'''print "here I am"'''
The result is: <code>here I am</code>, because of the new feature. Prior to this version, this statement was invalid.
</td></tr>
<tr><th>(E)
</th><td>'''print 'come and 'g"et"'''
The result is <code>come and g"et"</code>, because the double quotation marks are ordinary characters
(not brackets for a quoted region) in a string literal that starts with a single quotation mark. The string literal extends beyond the second single quotation mark until ended by a blank or the end of the string. Since this result also occurred prior to version 7.8, backward compatibility dictates the new rule that the opening quotation mark in a quoted string determines the quote bracketing character through the rest of the quoted
string.
</td></tr>
<tr><th>(F)
</th><td>'''print "say ""uncle"""'''
The result is: <code>say "uncle"</code>, because within a quoted region started by a double quotation mark, two consecutive double quotation marks are folded into one.
</td></tr>
<tr><th>(G)
</th><td>'''print "Bob's"Your"Uncle"'''
The result is: <code>Bob'sYourUncle</code>, because <code>"Uncle"</code> is a quoted region which continues the string literal that was started by the initial double quotation mark.
</td></tr></table>
The following statement would produce a compilation error:
<p class="code">print 'say'Uncle" Bob"
</p>
<code>" Bob"</code> is ''not'' a quoted region that continues the
string literal (because the string literal is started by the initial
single quotation mark and not by an initial double quotation mark),
so the blank before <code>Bob"</code> ends the quoted string and makes the
final four characters extraneous and invalid for the <var>Print</var> statement.
A final note: in a string that does not start with a quotation mark, only a single
quotation mark character is allowed to produce a quoted piece of the token.
For example, the following statement is valid:
<p class="code">%x = foo'BAR'
</p>
This statement causes <code>%x</code> to be set to the value of field FOOBAR.
However, the following statement results in <code>%x</code> being set to the
value of field FOO"BAR":
<p class="code">%x = foo"BAR"
</p>
This is necessary to preserve backward compatibility.
 
[[Category:User Language syntax enhancements]]

Latest revision as of 16:07, 26 June 2014

As of Sirius Mods version 7.8, the SOUL and Janus SOAP compiler accepts either a single-quotation-mark character (') or a double-quotation-mark character (") as a quoted-string delimiter. Prior to this version, only a single-quotation-mark character (also called an apostrophe) could be such a delimiter.

These are examples of the feature:

  1. The statements below are equivalent as of version 7.8:

    %n = 'abc':stringToHex %n = "abc":stringToHex

  2. The hex string produced by the statements below as of version 7.8 is 818283:

    printText {'abc':stringToHex} printText {"abc":stringToHex}

  3. The first and second statements below are equivalent as of version 7.8, producing the string IT'S COOL!:

    printText {'It''s cool!':toUpper} printText {"It's cool!":toUpper} printText {"It''s cool!":toUpper}

    The last statement in the example above produces the string IT''S COOL!, which demonstrates that repeating a single quotation mark does not escape it if the quoted string is delimited with double quotation marks.

Note: Since this double quotation mark feature is restricted to the compiler, it does not affect command parsing, and the following command is still invalid:

RESET FUNCOPTS X"00"

As a rule, the double quotation mark character can be used to enclose, or bracket, a quoted region that begins a string literal, or to bracket a "continuation" of a string literal that begins with a double quotation mark, but it may not otherwise be used to bracket a quoted region.

The "continuation of a string literal" refers to the peculiar User Language concept that a quoted token does not end until it reaches a space or separator character after the closing quotation mark character. So, the following statement prints Hello:

print 'Hel'lo

You can also continue the quoted region:

print 'Hel'lo' World'

This prints Hello World. In addition, you can quote parts of unquoted tokens:

pr'int' 'Hello World'

This also prints Hello World.

User Language quoted string continuation and the new functionality of double quotation marks are shown in the following annotated request, which is valid in Sirius Mods version 7.8. The letter labels on the left are not part of the request but are for the commentary that follows:

Begin (A) %b'y' string len 255 %b'y' = 'abc' print %b'y' (B) print %by call foobar (C) subroutine foo'bar' (D) print "here I am" (E) print 'come and 'g"et" (F) print "say ""uncle""" (G) print "Bob's"Your"Uncle" end subroutine end

(A) %b'y' string len 255

'y' is a quoted region that is a continuation of the %variable name.

(B) print %by

The %variable %by is the same as the %variable %b'y'.

(C) subroutine foo'bar'

'bar' is a quoted region that is a continuation of the subroutine name; the name is the same as the name foobar.

(D) print "here I am"

The result is: here I am, because of the new feature. Prior to this version, this statement was invalid.

(E) print 'come and 'g"et"

The result is come and g"et", because the double quotation marks are ordinary characters (not brackets for a quoted region) in a string literal that starts with a single quotation mark. The string literal extends beyond the second single quotation mark until ended by a blank or the end of the string. Since this result also occurred prior to version 7.8, backward compatibility dictates the new rule that the opening quotation mark in a quoted string determines the quote bracketing character through the rest of the quoted string.

(F) print "say ""uncle"""

The result is: say "uncle", because within a quoted region started by a double quotation mark, two consecutive double quotation marks are folded into one.

(G) print "Bob's"Your"Uncle"

The result is: Bob'sYourUncle, because "Uncle" is a quoted region which continues the string literal that was started by the initial double quotation mark.

The following statement would produce a compilation error:

print 'say'Uncle" Bob"

" Bob" is not a quoted region that continues the string literal (because the string literal is started by the initial single quotation mark and not by an initial double quotation mark), so the blank before Bob" ends the quoted string and makes the final four characters extraneous and invalid for the Print statement.

A final note: in a string that does not start with a quotation mark, only a single quotation mark character is allowed to produce a quoted piece of the token. For example, the following statement is valid:

%x = foo'BAR'

This statement causes %x to be set to the value of field FOOBAR. However, the following statement results in %x being set to the value of field FOO"BAR":

%x = foo"BAR"

This is necessary to preserve backward compatibility.