diff --git a/doc/langref.html.in b/doc/langref.html.in index 936254958cb67e9260a5578c8e2e920deb412ef0..83dee670a124ffb2e99621645722f4a5e3997dbf 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -1894,20 +1894,77 @@ const B = error{Two}; {#header_close#} {#header_open|Precedence#} +

+ Zig features partial operator precedence: not every operator has a defined precedence with + every other operator. Mixing operators with ambiguous precedence is prohibited and parentheses + are mandatory. Consequently, valid code is typically auto-descriptive; when reading and + writing Zig code, there are very few precedence rules you'll need to remember. +

+ + {#code|operator_precedence.zig#} + + {#code|ambiguous_precedence_orelse.zig#} + +

+ There is a set of operators (prefix operators, suffix operators, etc) that have unambiguously + higher precedence than all other operators. That set of operators has the following precedence table: +

+
{#syntax#}x() x[] x.y x.* x.?
 a!b
 x{}
 !x -x -%x ~x &x ?x try
-* / % *% *| ||
-+ - ++ +% -% +| -|
-<< >> <<|
-& ^ | orelse catch
+// lower precedence operators omitted{#endsyntax#}
+ +

+ For the other operators, simplified operator precedence tables are provided below, each containing only + the information that you'll need to know when reading/writing code that uses that particular category of operators. + The operators that have ambiguous precedence or semantic errors with the relevant operators have been omitted, + as have the operators with higher precedence (those shown above). +

+ +

+ For arithmetic operators: +

+ +
{#syntax#}* / *% *| %
++ - +% -% +| -|
 == != < > <= >=
-and
-or
-= *= *%= *|= /= %= += +%= +|= -= -%= -|= <<= <<|= >>= &= ^= |={#endsyntax#}
+and or{#endsyntax#} + +

+ For bitwise operators: +

+ +
{#syntax#}<< <<| >> & ^ |
+== != < > <= >=
+and or{#endsyntax#}
+ +

+ For null and error recovery operators: +

+ +
{#syntax#}catch orelse{#endsyntax#}
+ +

+ For error set operators: +

+ +
{#syntax#}||{#endsyntax#}
+ +

+ For array concatenation operators: +

+ +
{#syntax#}++{#endsyntax#}
+ +

+ The full precedence rules can be found in the {#link|Grammar#}, which also includes an {#link|Operator Precedence Flowchart#}. +

+ {#header_close#} {#header_close#} + {#header_open|Arrays#} {#code|test_arrays.zig#} @@ -7879,6 +7936,42 @@ fn readU32Be() u32 {} {#header_open|Appendix#} {#header_open|Grammar#} {#grammar#} + {#header_open|Operator Precedence Flowchart#} +

+ To help understand the grammar, this flowchart is provided for the operator precedence. + The bottom represents any location where an expression may occur and the topmost row represents + a parenthesized expression. An operator will have a higher precedence (and therefore be the parent) to + another node if it is strictly lower on the tree (having a directed path from one to another). This is inverted from classical precedence, but + is closer to how a recursive descent parser views the grammar. +

+

+                     ┏━━━━━━━━━━━━━━━━━━━┓   
+                     ┃(expr)             ┃                  
+                     ┃x() x[] x.y x.* x.?┃                  
+                     ┃E!T                ┃                  
+                     ┃x{}                ┃                  
+                     ┃!x -x -%x ~x &x ?x ┃                  
+                     ┗━━━━━━━━━▲━━━━━━━━━┛                  
+          ┌─────────────┬──────┴───────┬───────┬──┬──┬─┐         
+          │             │              │       │  │  │ │         
+   ┏━━━━━━┷━━━━━┓╭Non╌Chaining╮ ╭Non╌Chaining╮┏┷┓┏┷┓┏┷┓│         
+   ┃* *% *| / ||┃╎      %     ╎ ╎ << <<|  >> ╎┃&┃┃^┃┃|┃│         
+   ┗━━━━━━▲━━━━━┛╰╌╌╌╌╌╌▲╌╌╌╌╌╯ ╰╌╌╌╌╌╌▲╌╌╌╌╌╯┗▲┛┗▲┛┗▲┛│         
+       ┏━━┷━━━━━━━━━━━━━┷━┓       ┌────┴───────┴──┴──┘ │         
+       ┃+ - +% -% +| -| ++┃       │                    │         
+       ┗━━━━━━━━━━━▲━━━━━━┛       │                    │         
+                  ╭┴╌Non-Chaining╌┴╮                   │         
+                  ╎== !=  < <= >= >╎                   │         
+                  ╰╌╌╌▲╌╌╌╌▲╌╌╌╌╌╌╌╯      ┌──────┬─────┘                
+                    ┏━┷━┓┏━┷━┓        ┏━━━┷━━┓┏━━┷━━┓                  
+                    ┃and┃┃or ┃        ┃orelse┃┃catch┃                  
+                    ┗━▲━┛┗━▲━┛        ┗━━━▲━━┛┗━━▲━━┛                  
+                   ╭╌╌┴╌╌╌╌┴╌╌╌╌╌╌╌╌╌╌╌╌╌╌┴╌╌╌╌╌╌┴╌╮ 
+                   ╎    Any expression location    ╎ 
+                   ╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╯ 
+                                                              
+      
+ {#header_close#} {#header_close#} {#header_open|Zen#}