_seps: "(){}[],.:;=<>*·+-/%^?~|&∧∨!¬∑∃∀\n\"\\"

200 multi_line_comment = ["/*" ..."*/"? .r?({
    [!"*/" "*" ..."*/"?] [multi_line_comment ..."*/"?] ["/" ..."*/"?]
}) "*/"]
201 comment = {multi_line_comment ["//" ..."\n"?]}
202 w = .r!({.w! comment})

0 ns = ["ns" .w! .s!("::" .._seps!:"name")]
1 uses = .l({[.w? use:"use"] comment})
2 use = ["use" .w! .s!(["::" !"{"] .._seps!:"name")
    ?["::" .w? "{" .w? .s?.(,
        [.._seps!:"use_fn" ?[.w! "as" .w! .._seps!:"use_fn_alias"]]
    ) .w? "}"]
    .w! "as" .w! .._seps!:"alias"]
3 fn = {
    ["fn" .w! .."("!:"name" ?w "(" ?w args ?w ")" ?w ?currents ?w {
            ["->":"returns" ?w ?type:"ret_type"]
            !"->":!"returns"
        } ?w block:"block"]
    [.."("!:"name" ?w "(" ?w args ?w ")" ?w ?currents ?w "=" ?w expr:"expr"]
}
4 args = .s?.(, arg:"arg")
5 arg = [?"mut":"mut" ?w .._seps!:"name" ?[?w ":" ?w
         ?["'" ?w .._seps!:"lifetime"] ?w ?type:"type"]]
6 imm_arg = [!"mut " .._seps!:"name" ?[?w ":" ?w !"'" ?type:"type"]]
7 closure = ["\\(" ?w .s?.(, imm_arg:"arg") ?w ")" ?w ?currents
             ?w "=" ?w expr:"expr"]
8 call_closure = ["\\" item:"item" ?w "(" .s?.(, arg_expr:"call_arg") ?w ")"]
9 named_call_closure = ["\\" item:"item" ?w "(" ?w
    .s?.(, [.._seps!:"word" ?w ":" ?w arg_expr:"call_arg" ?w]) ")"]
10 currents = ["~" ?w .s!.(, current:"current")]
11 current = [?"mut":"mut" ?w .._seps!:"name" ?[?w ":" ?w type:"type"]]
// Support both multi-line expressions and single line.
12 block = ["{" ?w {.l([?w expr:"expr" ?w]) [?w expr:"expr"]} ?w "}"]
13 expr = [{
    in:"in"
    closure:"closure"
    object:"object"
    arr
    ["return" wn expr:"return"]
    for_in:"for_in"
    for_n:"for_n"
    for:"for"
    loop:"loop"
    if:"if"
    break:"break"
    continue:"continue"
    block:"block"
    assign:"assign"
    compare:"compare"
    ["return":"return_void"]
    add:"add"
    in_loops
    short_loops
    items
} try]
// Interprets "return" as variable, does not expect loops or assignment.
14 arg_expr = {
    ["mut":"mut" ?w item:"item"]
    swizzle:"swizzle"
    [{
        in:"in"
        closure:"closure"
        object:"object"
        arr
        if:"if"
        block:"block"
        compare:"compare"
        add:"add"
        in_loops
        short_loops
        items
    } try]
}
15 lexpr = [{
    closure:"closure"
    object:"object"
    arr
    in_loops
    short_loops
    block:"block"
    items
} try]
16 object = ["{" ?w .s?.(, key_value:"key_value") ?w "}"]
17 array = ["[" ?w .s?.(, expr:"array_item") ?w "]"]
18 array_fill = ["[" ?w expr:"fill" ?w ";" ?w expr:"n" ?w "]"]
19 key_value = [{.t?:"key" .._seps!:"key"} ?w ":" ?w expr:"val"]
20 num = .$_:"num"
21 vec4 = ["(" ?w arg_expr:"x" , ?arg_expr:"y"
           ?[, arg_expr:"z" ?[, arg_expr:"w"]] ?, ?w ")"]
22 color = ["#" .._seps!:"color"]
23 text = .t?:"text"
24 bool = [{"true":"bool" "false":!"bool"} !.._seps!]
25 unop_not = [{"!":"!" "¬":"!"} ?w lexpr:"expr"]
26 unop_neg = ["-":"-" ?w mul_expr:"expr"]
27 norm = ["|" ?w expr:"expr" ?w "|"]
28 item = [?"~":"current" ?w .._seps!:"name" ?[?w "?":"try_item"]
    ?item_extra:"item_extra"]
29 item_extra = .r!([{
           [?w "[" ?w {.t?:"id" .$_:"id" expr:"id"} ?w "]"]
           [?w "." ?w .._seps!:"id"]} ?[?w "?":"try_id"]])
30 link = ["link" ?w "{" ?w link_body "}"]
31 link_body = .s?.(?w expr:"link_item")
// Generate link block for body.
32 link_for = [label "link" .w! .s!.(, [.._seps!:"name" ?w
    ?{
        ["[" ?w expr:"start" , expr:"end" ?w ")"]
        [!"{" expr:"end"]
    }]) ?w "{" ?w link_body_block:"block" "}"]
33 link_body_block = link_body_expr:"expr"
34 link_body_expr = link_body:"link"
35 for = [label "for" .w!
    expr:"init" ?w ";" ?w
    expr:"cond" ?w ";" ?w
    expr:"step" ?w block:"block"]
36 for_n = [label "for" short_body]
37 for_in = [label "for" in_body]
38 loop = [label "loop" .w!  block:"block"]
39 break = ["break" ?w ?["'" .._seps!:"label"]]
40 continue = ["continue" ?w ?["'" .._seps!:"label"]]
41 if = ["if" .w! expr:"cond" ?w block:"true_block"
         .r?([?w "else" w "if" ?w expr:"else_if_cond" ?w block:"else_if_block"])
         ?[?w "else" ?w block:"else_block"]]
42 call = [?[.._seps!:"alias" "::"] .._seps!:"name" wn "(" ?w
    .s?.(, arg_expr:"call_arg") ?w ")"]
43 named_call = [?[.._seps!:"alias" "::"] .._seps!:"word" wn "(" ?w
    .s?.(, [.._seps!:"word" ?w ":" ?w arg_expr:"call_arg" ?w]) ")"]
44 go = ["go " ?w {call:"call" named_call:"named_call"}]
45 assign = [lexpr:"left" ?w assign_op ?w expr:"right"]
46 assign_op = {":=":":=" "=":"=" "+=":"+=" "-=":"-=" "*=":"*=" "/=":"/=" "%=":"%="}
47 compare = [lexpr:"left" ?w compare_op ?w expr:"right"]
48 compare_op = {"==":"==" "!=":"!=" "¬=":"!=" "<=":"<=" "<":"<" ">=":">=" ">":">"}
49 grab = ["grab" ?[w "'" .$:"grab_level"] w expr:"expr"]
50 try_expr = ["try" w expr:"expr"]
51 in = ["in" w ?[.._seps!:"alias" "::"] .._seps!:"name"]

60 label = ?["'" .._seps!:"label" ?w ":" ?w]
61 short_body = [.w! .s!.(, [.._seps!:"name" ?w
    ?{
        ["[" ?w expr:"start" , expr:"end" ?w ")"]
        [!"{" expr:"end"]
    }]) ?w block:"block"]
62 in_body = [.w! .._seps!:"name" .w! "in" .w! expr:"iter" ?w block:"block"]
63 try = ?[?w "?":"try"]
64 , = [?w "," ?w]
65 arr = {array:"array" array_fill:"array_fill"}
66 items = {vec4:"vec4" link:"link" grab:"grab" try_expr:"try_expr"
            ["(" ?w expr ?w ")"] unop_not:"unop" norm:"norm"
            text go:"go"
            call_closure:"call_closure" named_call_closure:"named_call_closure"
            call:"call" named_call:"named_call"
            num bool color item:"item"}
// Allow whitespace, but no new line.
67 wn = .r?({" " "\t" "\r"})

70 in_loops = {sum_in:"sum_in" prod_in:"prod_in"
    min_in:"min_in" max_in:"max_in" any_in:"any_in" all_in:"all_in"
    sift_in:"sift_in" link_in:"link_in"}
71 sum_in = [label {"sum" "∑"} in_body]
72 prod_in = [label {"prod" "∏"} in_body]
73 min_in = [label "min" in_body]
74 max_in = [label "max" in_body]
75 any_in = [label {"any" "∃"} in_body]
76 all_in = [label {"all" "∀"} in_body]
77 sift_in = [label "sift" in_body]
78 link_in = [label "link" .w! .._seps!:"name" .w! "in" .w! expr:"iter" ?w
  "{" ?w link_body_block:"block" "}"]

80 short_loops = {sum:"sum" prod:"prod" sum_vec4:"sum_vec4"
    prod_vec4:"prod_vec4" min:"min" max:"max" sift:"sift"
    any:"any" all:"all" vec4_un_loop:"vec4_un_loop" link_for:"link_for"}
81 sum = [label {"sum" "∑"} short_body]
82 prod = [label {"prod" "∏"} short_body]
83 min = [label "min" short_body]
84 max = [label "max" short_body]
85 sift = [label "sift" short_body]
86 any = [label {"any" "∃"} short_body]
87 all = [label {"all" "∀"} short_body]
88 sum_vec4 = [label {"sum_vec4" "∑vec4"} short_body]
89 prod_vec4 = [label {"prod_vec4" "∏vec4"} short_body]
90 vec4_un_loop = ["vec" {"4":"4" "3":"3" "2":"2"}
                   w .._seps!:"name" w expr:"expr"]
91 swizzle = [sw:"sw0" sw:"sw1" ?sw:"sw2" ?sw:"sw3" w expr:"expr"]
92 sw = {"x":"x" "y":"y" "z":"z" "w":"w"}

100 type = {
    "any":"any"
    "bool":"bool"
    "f64":"f64"
    "str":"str"
    "vec4":"vec4"
    "link":"link"
    ["opt" ?w "[" ?w type:"opt" ?w "]"]
    "opt":"opt_any"
    ["res" ?w "[" ?w type:"res" ?w "]"]
    "res":"res_any"
    ["sec" ?w "[" ?w {"bool":"sec_bool" "f64":"sec_f64"} ?w "]"]
    "[]":"arr_any"
    ["[" ?w type:"arr" ?w "]"]
    "{}":"obj_any"
    ["thr" ?w "[" ?w type:"thr" ?w "]"]
    "thr":"thr_any"
    ["in" ?w "[" ?w type:"in" ?w "]"]
    "in":"in_any"
    closure_type:"closure_type"
    [.._seps!:"ad_hoc" ?[?w type:"ad_hoc_ty"]]
}
101 closure_type = ["\\(" ?w .s?.(, type:"cl_arg") ?w ")"
    ?w "->" ?w type:"cl_ret"]

200 + = [?w {"+":"+" "||":"||" "∨":"+" ["or":"+" w]} ?w]
201 - = [wn "-":"-" ?w]
// Allow whitespace before multiplication sign, but no new line.
// This prevents `x` on a new line from being interpreted as multiplication sign.
202 * = [wn {
    "*.":"*." "·":"*."
    ["x":"x" w] "⨯":"x"
    "*":"*" "&&":"&&" "∧":"*" ["and":"*" w]
} ?w]
203 / = [?w "/":"/" ?w]
204 % = [?w "%":"%" ?w]
205 pow = [lexpr:"base" ?w "^" ?w lexpr:"exp"]
206 mul = .s!({* / %} {unop_neg:"unop" pow:"pow" lexpr:"val"})
207 mul_expr = {mul:"mul"}
208 add = .s!({+ -} mul_expr:"expr")

1000 document = [?ns:"ns" ?w ?uses:"uses" ?w .l({[.w? fn:"fn"] comment})]
