authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2021-03-03 14:30:46-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-03-03 14:30:46-05:00
log7fbe9e7d60fa17e49177f52df922bbf295ecc619
tree2c0b79b5a1659d3700c43298bcfb5bb76ff5d67d
parent5bd9a6451685ad056628e312aadaf80e66a4c004
signature Signed by PGP key 4AEE18F83AFDEB23

update docs and grammar to allow CRLF line endings (#8063)


1 files changed, 51 insertions(+), 23 deletions(-)

doc/langref.html.in+51-23
......@@ -10447,13 +10447,40 @@ fn readU32Be() u32 {}
1044710447 {#header_close#}
1044810448 {#header_open|Source Encoding#}
1044910449 <p>Zig source code is encoded in UTF-8. An invalid UTF-8 byte sequence results in a compile error.</p>
10450 <p>Throughout all zig source code (including in comments), some codepoints are never allowed:</p>
10450 <p>Throughout all zig source code (including in comments), some code points are never allowed:</p>
1045110451 <ul>
10452 <li>Ascii control characters, except for U+000a (LF): U+0000 - U+0009, U+000b - U+0001f, U+007f. (Note that Windows line endings (CRLF) are not allowed, and hard tabs are not allowed.)</li>
10452 <li>Ascii control characters, except for U+000a (LF), U+000d (CR), and U+0009 (HT): U+0000 - U+0008, U+000b - U+000c, U+000e - U+0001f, U+007f.</li>
1045310453 <li>Non-Ascii Unicode line endings: U+0085 (NEL), U+2028 (LS), U+2029 (PS).</li>
1045410454 </ul>
10455 <p>The codepoint U+000a (LF) (which is encoded as the single-byte value 0x0a) is the line terminator character. This character always terminates a line of zig source code (except possibly the last line of the file).</p>
10456 <p>For some discussion on the rationale behind these design decisions, see <a href="https://github.com/ziglang/zig/issues/663">issue #663</a></p>
10455 <p>
10456 LF (byte value 0x0a, code point U+000a, {#syntax#}'\n'{#endsyntax#}) is the line terminator in Zig source code.
10457 This byte value terminates every line of zig source code except the last line of the file.
10458 It is recommended that non-empty source files end with an empty line, which means the last byte would be 0x0a (LF).
10459 </p>
10460 <p>
10461 Each LF may be immediately preceded by a single CR (byte value 0x0d, code point U+000d, {#syntax#}'\r'{#endsyntax#})
10462 to form a Windows style line ending, but this is discouraged.
10463 A CR in any other context is not allowed.
10464 </p>
10465 <p>
10466 HT hard tabs (byte value 0x09, code point U+0009, {#syntax#}'\t'{#endsyntax#}) are interchangeable with
10467 SP spaces (byte value 0x20, code point U+0020, {#syntax#}' '{#endsyntax#}) as a token separator,
10468 but use of hard tabs is discouraged. See {#link|Grammar#}.
10469 </p>
10470 <p>
10471 Note that running <code>zig fmt</code> on a source file will implement all recommendations mentioned here.
10472 Note also that the stage1 compiler does <a href="https://github.com/ziglang/zig/wiki/FAQ#why-does-zig-force-me-to-use-spaces-instead-of-tabs">not yet support CR or HT</a> control characters.
10473 </p>
10474 <p>
10475 Note that a tool reading Zig source code can make assumptions if the source code is assumed to be correct Zig code.
10476 For example, when identifying the ends of lines, a tool can use a naive search such as <code>/\n/</code>,
10477 or an <a href="https://msdn.microsoft.com/en-us/library/dd409797.aspx">advanced</a>
10478 search such as <code>/\r\n?|[\n\u0085\u2028\u2029]/</code>, and in either case line endings will be correctly identified.
10479 For another example, when identifying the whitespace before the first token on a line,
10480 a tool can either use a naive search such as <code>/[ \t]/</code>,
10481 or an <a href="https://tc39.es/ecma262/#sec-characterclassescape">advanced</a> search such as <code>/\s/</code>,
10482 and in either case whitespace will be correctly identified.
10483 </p>
1045710484 {#header_close#}
1045810485
1045910486 {#header_open|Keyword Reference#}
......@@ -11373,6 +11400,7 @@ ExprList &lt;- (Expr COMMA)* Expr?
1137311400
1137411401# *** Tokens ***
1137511402eof &lt;- !.
11403eol &lt;- ('\r'? '\n') | eof
1137611404hex &lt;- [0-9a-fA-F]
1137711405hex_ &lt;- ('_'/hex)
1137811406dec &lt;- [0-9]
......@@ -11382,39 +11410,39 @@ dec_int &lt;- dec (dec_* dec)?
1138211410hex_int &lt;- hex (hex_* dec)?
1138311411
1138411412char_escape
11385 &lt;- &quot;\\x&quot; hex hex
11386 / &quot;\\u{&quot; hex+ &quot;}&quot;
11387 / &quot;\\&quot; [nr\\t'&quot;]
11413 &lt;- '\\x' hex hex
11414 / '\\u{' hex+ '}'
11415 / '\\' [nr\\t'&quot;]
1138811416char_char
1138911417 &lt;- char_escape
11390 / [^\\'\n]
11418 / [^\\'\r\n]
1139111419string_char
1139211420 &lt;- char_escape
11393 / [^\\&quot;\n]
11421 / [^\\&quot;\r\n]
1139411422
11395line_comment &lt;- '//'[^\n]*
11396line_string &lt;- (&quot;\\\\&quot; [^\n]* [ \n]*)+
11397skip &lt;- ([ \n] / line_comment)*
11423line_comment &lt;- '//'[^\r\n]* eol
11424line_string &lt;- ('\\\\' [^\r\n]* eol skip)+
11425skip &lt;- ([ \t] / eol / line_comment)*
1139811426
1139911427CHAR_LITERAL &lt;- &quot;'&quot; char_char &quot;'&quot; skip
1140011428FLOAT
11401 &lt;- &quot;0x&quot; hex_* hex &quot;.&quot; hex_int ([pP] [-+]? hex_int)? skip
11402 / dec_int &quot;.&quot; dec_int ([eE] [-+]? dec_int)? skip
11403 / &quot;0x&quot; hex_* hex &quot;.&quot;? [pP] [-+]? hex_int skip
11404 / dec_int &quot;.&quot;? [eE] [-+]? dec_int skip
11429 &lt;- '0x' hex_* hex '.' hex_int ([pP] [-+]? hex_int)? skip
11430 / dec_int '.' dec_int ([eE] [-+]? dec_int)? skip
11431 / '0x' hex_* hex '.'? [pP] [-+]? hex_int skip
11432 / dec_int '.'? [eE] [-+]? dec_int skip
1140511433INTEGER
11406 &lt;- &quot;0b&quot; [_01]* [01] skip
11407 / &quot;0o&quot; [_0-7]* [0-7] skip
11408 / &quot;0x&quot; hex_* hex skip
11434 &lt;- '0b' [_01]* [01] skip
11435 / '0o' [_0-7]* [0-7] skip
11436 / '0x' hex_* hex skip
1140911437 / dec_int skip
11410STRINGLITERALSINGLE &lt;- &quot;\&quot;&quot; string_char* &quot;\&quot;&quot; skip
11438STRINGLITERALSINGLE &lt;- '&quot;' string_char* '&quot;' skip
1141111439STRINGLITERAL
1141211440 &lt;- STRINGLITERALSINGLE
11413 / line_string skip
11441 / line_string skip
1141411442IDENTIFIER
1141511443 &lt;- !keyword [A-Za-z_] [A-Za-z0-9_]* skip
11416 / &quot;@\&quot;&quot; string_char* &quot;\&quot;&quot; skip
11417BUILTINIDENTIFIER &lt;- &quot;@&quot;[A-Za-z_][A-Za-z0-9_]* skip
11444 / '@&quot;' string_char* '&quot;' skip
11445BUILTINIDENTIFIER &lt;- '@'[A-Za-z_][A-Za-z0-9_]* skip
1141811446
1141911447
1142011448AMPERSAND &lt;- '&amp;' ![=] skip