| 1 | # RON grammar |
| 2 | |
| 3 | This file describes the structure of a RON file in [EBNF notation][ebnf]. |
| 4 | If extensions are enabled, some rules will be replaced. For that, see the |
| 5 | [extensions document][exts] which describes all extensions and what they override. |
| 6 | |
| 7 | [ebnf]: https://en.wikipedia.org/wiki/Extended_Backus–Naur_form |
| 8 | [exts]: ./extensions.md |
| 9 | |
| 10 | ## RON file |
| 11 | |
| 12 | ```ebnf |
| 13 | RON = [extensions], ws, value, ws; |
| 14 | ``` |
| 15 | |
| 16 | ## Whitespace and comments |
| 17 | |
| 18 | ```ebnf |
| 19 | ws = { ws_single | comment }; |
| 20 | ws_single = "\n" | "\t" | "\r" | " " | U+000B | U+000C | U+0085 | U+200E | U+200F | U+2028 | U+2029; |
| 21 | comment = ["//", { no_newline }, "\n"] | ["/*", nested_block_comment, "*/"]; |
| 22 | nested_block_comment = { ? any characters except "/*" or "*/" ? }, [ "/*", nested_block_comment, "*/", nested_block_comment ]; |
| 23 | ``` |
| 24 | |
| 25 | ## Commas |
| 26 | |
| 27 | ```ebnf |
| 28 | comma = ws, ",", ws; |
| 29 | ``` |
| 30 | |
| 31 | ## Extensions |
| 32 | |
| 33 | ```ebnf |
| 34 | extensions = { "#", ws, "!", ws, "[", ws, extensions_inner, ws, "]", ws }; |
| 35 | extensions_inner = "enable", ws, "(", extension_name, { comma, extension_name }, [comma], ws, ")"; |
| 36 | ``` |
| 37 | |
| 38 | For the extension names see the [`extensions.md`][exts] document. |
| 39 | |
| 40 | ## Value |
| 41 | |
| 42 | ```ebnf |
| 43 | value = integer | byte | float | string | byte_string | char | bool | option | list | map | tuple | struct | enum_variant; |
| 44 | ``` |
| 45 | |
| 46 | ## Numbers |
| 47 | |
| 48 | ```ebnf |
| 49 | digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"; |
| 50 | digit_binary = "0" | "1"; |
| 51 | digit_octal = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7"; |
| 52 | digit_hexadecimal = digit | "A" | "a" | "B" | "b" | "C" | "c" | "D" | "d" | "E" | "e" | "F" | "f"; |
| 53 | |
| 54 | integer = ["+" | "-"], unsigned, [integer_suffix]; |
| 55 | integer_suffix = ("i", "u"), ("8", "16", "32", "64", "128"); |
| 56 | |
| 57 | unsigned = unsigned_binary | unsigned_octal | unsigned_hexadecimal | unsigned_decimal; |
| 58 | unsigned_binary = "0b", digit_binary, { digit_binary | "_" }; |
| 59 | unsigned_octal = "0o", digit_octal, { digit_octal | "_" }; |
| 60 | unsigned_hexadecimal = "0x", digit_hexadecimal, { digit_hexadecimal | "_" }; |
| 61 | unsigned_decimal = digit, { digit | "_" }; |
| 62 | |
| 63 | byte = "b", "'", byte_content, "'"; |
| 64 | byte_content = ascii | ("\\", (escape_ascii | escape_byte)); |
| 65 | |
| 66 | float = ["+" | "-"], ("inf" | "NaN" | float_num), [float_suffix]; |
| 67 | float_num = (float_int | float_std | float_frac), [float_exp]; |
| 68 | float_int = digit, { digit | "_" }; |
| 69 | float_std = digit, { digit | "_" }, ".", [digit, { digit | "_" }]; |
| 70 | float_frac = ".", digit, { digit | "_" }; |
| 71 | float_exp = ("e" | "E"), ["+" | "-"], { digit | "_" }, digit, { digit | "_" }; |
| 72 | float_suffix = "f", ("32", "64"); |
| 73 | ``` |
| 74 | |
| 75 | > Note: `ascii` refers to any ASCII character, i.e. any byte in range `0x00 ..= 0x7F`. |
| 76 | |
| 77 | ## String |
| 78 | |
| 79 | ```ebnf |
| 80 | string = string_std | string_raw; |
| 81 | string_std = "\"", { no_double_quotation_marks | string_escape }, "\""; |
| 82 | string_escape = "\\", (escape_ascii | escape_byte | escape_unicode); |
| 83 | string_raw = "r", string_raw_content; |
| 84 | string_raw_content = ("#", string_raw_content, "#") | "\"", { unicode_non_greedy }, "\""; |
| 85 | |
| 86 | escape_ascii = "'" | "\"" | "\\" | "n" | "r" | "t" | "0"; |
| 87 | escape_byte = "x", digit_hexadecimal, digit_hexadecimal; |
| 88 | escape_unicode = "u", digit_hexadecimal, [digit_hexadecimal, [digit_hexadecimal, [digit_hexadecimal, [digit_hexadecimal, [digit_hexadecimal]]]]]; |
| 89 | ``` |
| 90 | |
| 91 | > Note: Raw strings start with an `r`, followed by n `#`s and a quotation mark |
| 92 | `"`. They may contain any characters or escapes (except the end sequence). |
| 93 | A raw string ends with a quotation mark (`"`), followed by n `#`s. n may be |
| 94 | any number, including zero. |
| 95 | Example: |
| 96 | ```rust |
| 97 | r##"This is a "raw string". It can contain quotations or |
| 98 | backslashes (\)!"## |
| 99 | ``` |
| 100 | Raw strings cannot be written in EBNF, as they are context-sensitive. |
| 101 | Also see [the Rust document] about context-sensitivity of raw strings. |
| 102 | |
| 103 | [the Rust document]: https://github.com/rust-lang/rust/blob/d046ffddc4bd50e04ffc3ff9f766e2ac71f74d50/src/grammar/raw-string-literal-ambiguity.md |
| 104 | |
| 105 | ## Byte String |
| 106 | |
| 107 | ```ebnf |
| 108 | byte_string = byte_string_std | byte_string_raw; |
| 109 | byte_string_std = "b\"", { no_double_quotation_marks | string_escape }, "\""; |
| 110 | byte_string_raw = "br", string_raw_content; |
| 111 | ``` |
| 112 | |
| 113 | > Note: Byte strings are similar to normal strings but are not required to |
| 114 | contain only valid UTF-8 text. RON's byte strings follow the updated Rust |
| 115 | byte string literal rules as proposed in [RFC #3349], i.e. byte strings |
| 116 | allow the exact same characters and escape codes as normal strings. |
| 117 | |
| 118 | [RFC #3349](https://github.com/rust-lang/rfcs/pull/3349) |
| 119 | |
| 120 | > Note: Raw byte strings start with an `br` prefix and follow the same rules |
| 121 | as raw strings, which are outlined above. |
| 122 | |
| 123 | ## Char |
| 124 | |
| 125 | ```ebnf |
| 126 | char = "'", (no_apostrophe | "\\\\" | "\\'"), "'"; |
| 127 | ``` |
| 128 | |
| 129 | ## Boolean |
| 130 | |
| 131 | ```ebnf |
| 132 | bool = "true" | "false"; |
| 133 | ``` |
| 134 | |
| 135 | ## Optional |
| 136 | |
| 137 | ```ebnf |
| 138 | option = "None" | option_some; |
| 139 | option_some = "Some", ws, "(", ws, value, ws, ")"; |
| 140 | ``` |
| 141 | |
| 142 | ## List |
| 143 | |
| 144 | ```ebnf |
| 145 | list = "[", [value, { comma, value }, [comma]], "]"; |
| 146 | ``` |
| 147 | |
| 148 | ## Map |
| 149 | |
| 150 | ```ebnf |
| 151 | map = "{", [map_entry, { comma, map_entry }, [comma]], "}"; |
| 152 | map_entry = value, ws, ":", ws, value; |
| 153 | ``` |
| 154 | |
| 155 | ## Tuple |
| 156 | |
| 157 | ```ebnf |
| 158 | tuple = "(", [value, { comma, value }, [comma]], ")"; |
| 159 | ``` |
| 160 | |
| 161 | ## Struct |
| 162 | |
| 163 | ```ebnf |
| 164 | struct = unit_struct | tuple_struct | named_struct; |
| 165 | unit_struct = ident | "()"; |
| 166 | tuple_struct = [ident], ws, tuple; |
| 167 | named_struct = [ident], ws, "(", ws, [named_field, { comma, named_field }, [comma]], ")"; |
| 168 | named_field = ident, ws, ":", ws, value; |
| 169 | ``` |
| 170 | |
| 171 | ## Enum |
| 172 | |
| 173 | ```ebnf |
| 174 | enum_variant = enum_variant_unit | enum_variant_tuple | enum_variant_named; |
| 175 | enum_variant_unit = ident; |
| 176 | enum_variant_tuple = ident, ws, tuple; |
| 177 | enum_variant_named = ident, ws, "(", [named_field, { comma, named_field }, [comma]], ")"; |
| 178 | ``` |
| 179 | |
| 180 | ## Identifier |
| 181 | |
| 182 | ```ebnf |
| 183 | ident = ident_std | ident_raw; |
| 184 | ident_std = ident_std_first, { ident_std_rest }; |
| 185 | ident_std_first = XID_Start | "_"; |
| 186 | ident_std_rest = XID_Continue; |
| 187 | ident_raw = "r", "#", ident_raw_rest, { ident_raw_rest }; |
| 188 | ident_raw_rest = ident_std_rest | "." | "+" | "-"; |
| 189 | ``` |
| 190 | |
| 191 | > Note: [XID_Start](http://unicode.org/cldr/utility/list-unicodeset.jsp?a=%5B%3AXID_Start%3A%5D&abb=on&g=&i=) and [XID_Continue](http://unicode.org/cldr/utility/list-unicodeset.jsp?a=%5B%3AXID_Continue%3A%5D&abb=on&g=&i=) refer to Unicode character sets. |