Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 1 | <!-- |
| 2 | Copyright 2018 The CUE Authors |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | --> |
| 16 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 17 | # The CUE Language Specification |
| 18 | |
| 19 | ## Introduction |
| 20 | |
Marcel van Lohuizen | 5953c66 | 2019-01-26 13:26:04 +0100 | [diff] [blame] | 21 | This is a reference manual for the CUE data constraint language. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 22 | CUE, pronounced cue or Q, is a general-purpose and strongly typed |
Marcel van Lohuizen | 5953c66 | 2019-01-26 13:26:04 +0100 | [diff] [blame] | 23 | constraint-based language. |
| 24 | It can be used for data templating, data validation, code generation, scripting, |
| 25 | and many other applications involving structured data. |
| 26 | The CUE tooling, layered on top of CUE, provides |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 27 | a general purpose scripting language for creating scripts as well as |
Marcel van Lohuizen | 5953c66 | 2019-01-26 13:26:04 +0100 | [diff] [blame] | 28 | simple servers, also expressed in CUE. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 29 | |
| 30 | CUE was designed with cloud configuration, and related systems, in mind, |
| 31 | but is not limited to this domain. |
| 32 | It derives its formalism from relational programming languages. |
| 33 | This formalism allows for managing and reasoning over large amounts of |
Marcel van Lohuizen | 5953c66 | 2019-01-26 13:26:04 +0100 | [diff] [blame] | 34 | data in a straightforward manner. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 35 | |
| 36 | The grammar is compact and regular, allowing for easy analysis by automatic |
| 37 | tools such as integrated development environments. |
| 38 | |
| 39 | This document is maintained by mpvl@golang.org. |
| 40 | CUE has a lot of similarities with the Go language. This document draws heavily |
Marcel van Lohuizen | 73f14eb | 2019-01-30 17:11:17 +0100 | [diff] [blame] | 41 | from the Go specification as a result. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 42 | |
| 43 | CUE draws its influence from many languages. |
| 44 | Its main influences were BCL/ GCL (internal to Google), |
| 45 | LKB (LinGO), Go, and JSON. |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 46 | Others are Swift, Typescript, Javascript, Prolog, NCL (internal to Google), |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 47 | Jsonnet, HCL, Flabbergast, Nix, JSONPath, Haskell, Objective-C, and Python. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 48 | |
| 49 | |
| 50 | ## Notation |
| 51 | |
| 52 | The syntax is specified using Extended Backus-Naur Form (EBNF): |
| 53 | |
| 54 | ``` |
| 55 | Production = production_name "=" [ Expression ] "." . |
| 56 | Expression = Alternative { "|" Alternative } . |
| 57 | Alternative = Term { Term } . |
| 58 | Term = production_name | token [ "…" token ] | Group | Option | Repetition . |
| 59 | Group = "(" Expression ")" . |
| 60 | Option = "[" Expression "]" . |
| 61 | Repetition = "{" Expression "}" . |
| 62 | ``` |
| 63 | |
| 64 | Productions are expressions constructed from terms and the following operators, |
| 65 | in increasing precedence: |
| 66 | |
| 67 | ``` |
| 68 | | alternation |
| 69 | () grouping |
| 70 | [] option (0 or 1 times) |
| 71 | {} repetition (0 to n times) |
| 72 | ``` |
| 73 | |
| 74 | Lower-case production names are used to identify lexical tokens. Non-terminals |
| 75 | are in CamelCase. Lexical tokens are enclosed in double quotes "" or back quotes |
| 76 | ``. |
| 77 | |
| 78 | The form a … b represents the set of characters from a through b as |
| 79 | alternatives. The horizontal ellipsis … is also used elsewhere in the spec to |
| 80 | informally denote various enumerations or code snippets that are not further |
| 81 | specified. The character … (as opposed to the three characters ...) is not a |
Roger Peppe | ded0e1d | 2019-09-24 16:39:36 +0100 | [diff] [blame] | 82 | token of the CUE language. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 83 | |
| 84 | |
| 85 | ## Source code representation |
| 86 | |
| 87 | Source code is Unicode text encoded in UTF-8. |
| 88 | Unless otherwise noted, the text is not canonicalized, so a single |
| 89 | accented code point is distinct from the same character constructed from |
| 90 | combining an accent and a letter; those are treated as two code points. |
| 91 | For simplicity, this document will use the unqualified term character to refer |
| 92 | to a Unicode code point in the source text. |
| 93 | |
| 94 | Each code point is distinct; for instance, upper and lower case letters are |
| 95 | different characters. |
| 96 | |
| 97 | Implementation restriction: For compatibility with other tools, a compiler may |
| 98 | disallow the NUL character (U+0000) in the source text. |
| 99 | |
| 100 | Implementation restriction: For compatibility with other tools, a compiler may |
| 101 | ignore a UTF-8-encoded byte order mark (U+FEFF) if it is the first Unicode code |
| 102 | point in the source text. A byte order mark may be disallowed anywhere else in |
| 103 | the source. |
| 104 | |
| 105 | |
| 106 | ### Characters |
| 107 | |
| 108 | The following terms are used to denote specific Unicode character classes: |
| 109 | |
| 110 | ``` |
| 111 | newline = /* the Unicode code point U+000A */ . |
| 112 | unicode_char = /* an arbitrary Unicode code point except newline */ . |
| 113 | unicode_letter = /* a Unicode code point classified as "Letter" */ . |
| 114 | unicode_digit = /* a Unicode code point classified as "Number, decimal digit" */ . |
| 115 | ``` |
| 116 | |
| 117 | In The Unicode Standard 8.0, Section 4.5 "General Category" defines a set of |
| 118 | character categories. |
| 119 | CUE treats all characters in any of the Letter categories Lu, Ll, Lt, Lm, or Lo |
| 120 | as Unicode letters, and those in the Number category Nd as Unicode digits. |
| 121 | |
| 122 | |
| 123 | ### Letters and digits |
| 124 | |
| 125 | The underscore character _ (U+005F) is considered a letter. |
| 126 | |
| 127 | ``` |
| 128 | letter = unicode_letter | "_" . |
| 129 | decimal_digit = "0" … "9" . |
| 130 | octal_digit = "0" … "7" . |
| 131 | hex_digit = "0" … "9" | "A" … "F" | "a" … "f" . |
| 132 | ``` |
| 133 | |
| 134 | |
| 135 | ## Lexical elements |
| 136 | |
| 137 | ### Comments |
Marcel van Lohuizen | 7fc421b | 2019-09-11 09:24:03 +0200 | [diff] [blame] | 138 | Comments serve as program documentation. |
| 139 | CUE supports line comments that start with the character sequence // |
| 140 | and stop at the end of the line. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 141 | |
Marcel van Lohuizen | 7fc421b | 2019-09-11 09:24:03 +0200 | [diff] [blame] | 142 | A comment cannot start inside a string literal or inside a comment. |
| 143 | A comment acts like a newline. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 144 | |
| 145 | |
| 146 | ### Tokens |
| 147 | |
| 148 | Tokens form the vocabulary of the CUE language. There are four classes: |
| 149 | identifiers, keywords, operators and punctuation, and literals. White space, |
| 150 | formed from spaces (U+0020), horizontal tabs (U+0009), carriage returns |
| 151 | (U+000D), and newlines (U+000A), is ignored except as it separates tokens that |
| 152 | would otherwise combine into a single token. Also, a newline or end of file may |
| 153 | trigger the insertion of a comma. While breaking the input into tokens, the |
| 154 | next token is the longest sequence of characters that form a valid token. |
| 155 | |
| 156 | |
| 157 | ### Commas |
| 158 | |
| 159 | The formal grammar uses commas "," as terminators in a number of productions. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 160 | CUE programs may omit most of these commas using the following two rules: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 161 | |
| 162 | When the input is broken into tokens, a comma is automatically inserted into |
| 163 | the token stream immediately after a line's final token if that token is |
| 164 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 165 | - an identifier |
| 166 | - null, true, false, bottom, or an integer, floating-point, or string literal |
| 167 | - one of the characters ), ], or } |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 168 | |
| 169 | |
| 170 | Although commas are automatically inserted, the parser will require |
| 171 | explicit commas between two list elements. |
| 172 | |
| 173 | To reflect idiomatic use, examples in this document elide commas using |
| 174 | these rules. |
| 175 | |
| 176 | |
| 177 | ### Identifiers |
| 178 | |
| 179 | Identifiers name entities such as fields and aliases. |
Marcel van Lohuizen | 8a2df96 | 2019-11-10 00:14:24 +0100 | [diff] [blame] | 180 | An identifier is a sequence of one or more letters (which includes `_` and `$`) |
Marcel van Lohuizen | b7083ff | 2020-05-12 11:38:19 +0200 | [diff] [blame] | 181 | and digits, optionally preceded by `#` or `_#`. |
Marcel van Lohuizen | dbf1c00 | 2020-05-16 14:19:34 +0200 | [diff] [blame] | 182 | It may not be `_` or `$`. |
| 183 | The first character in an identifier, or after an `#` if it contains one, |
| 184 | must be a letter. |
Marcel van Lohuizen | b7083ff | 2020-05-12 11:38:19 +0200 | [diff] [blame] | 185 | Identifiers starting with a `#` or `_` are reserved for definitions and hidden |
| 186 | fields. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 187 | |
| 188 | <!-- |
| 189 | TODO: allow identifiers as defined in Unicode UAX #31 |
| 190 | (https://unicode.org/reports/tr31/). |
| 191 | |
| 192 | Identifiers are normalized using the NFC normal form. |
| 193 | --> |
| 194 | |
| 195 | ``` |
Marcel van Lohuizen | b7083ff | 2020-05-12 11:38:19 +0200 | [diff] [blame] | 196 | identifier = [ "#" | "_#" ] letter { letter | unicode_digit } . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 197 | ``` |
| 198 | |
| 199 | ``` |
| 200 | a |
| 201 | _x9 |
| 202 | fieldName |
| 203 | αβ |
| 204 | ``` |
| 205 | |
| 206 | <!-- TODO: Allow Unicode identifiers TR 32 http://unicode.org/reports/tr31/ --> |
| 207 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 208 | Some identifiers are [predeclared](#predeclared-identifiers). |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 209 | |
| 210 | |
| 211 | ### Keywords |
| 212 | |
| 213 | CUE has a limited set of keywords. |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 214 | In addition, CUE reserves all identifiers starting with `__`(double underscores) |
| 215 | as keywords. |
| 216 | These are typically targets of pre-declared identifiers. |
| 217 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 218 | All keywords may be used as labels (field names). |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 219 | Unless noted otherwise, they can also be used as identifiers to refer to |
| 220 | the same name. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 221 | |
| 222 | |
| 223 | #### Values |
| 224 | |
| 225 | The following keywords are values. |
| 226 | |
| 227 | ``` |
| 228 | null true false |
| 229 | ``` |
| 230 | |
| 231 | These can never be used to refer to a field of the same name. |
| 232 | This restriction is to ensure compatibility with JSON configuration files. |
| 233 | |
| 234 | |
| 235 | #### Preamble |
| 236 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 237 | The following keywords are used at the preamble of a CUE file. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 238 | After the preamble, they may be used as identifiers to refer to namesake fields. |
| 239 | |
| 240 | ``` |
| 241 | package import |
| 242 | ``` |
| 243 | |
| 244 | |
| 245 | #### Comprehension clauses |
| 246 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 247 | The following keywords are used in comprehensions. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 248 | |
| 249 | ``` |
| 250 | for in if let |
| 251 | ``` |
| 252 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 253 | <!-- |
| 254 | TODO: |
| 255 | reduce [to] |
| 256 | order [by] |
| 257 | --> |
| 258 | |
| 259 | |
| 260 | #### Arithmetic |
| 261 | |
| 262 | The following pseudo keywords can be used as operators in expressions. |
| 263 | |
| 264 | ``` |
| 265 | div mod quo rem |
| 266 | ``` |
| 267 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 268 | ### Operators and punctuation |
| 269 | |
| 270 | The following character sequences represent operators and punctuation: |
| 271 | |
| 272 | ``` |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 273 | + div && == < = ( ) |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 274 | - mod || != > : { } |
| 275 | * quo & =~ <= ? [ ] , |
| 276 | / rem | !~ >= ! _|_ ... . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 277 | ``` |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 278 | <!-- |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 279 | Free tokens: ; ~ ^ |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 280 | // To be used: |
| 281 | @ at: associative lists. |
| 282 | |
| 283 | // Idea: use # instead of @ for attributes and allow then at declaration level. |
| 284 | // This will open up the possibility of defining #! at the start of a file |
| 285 | // without requiring special syntax. Although probably not quite. |
| 286 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 287 | |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 288 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 289 | ### Integer literals |
| 290 | |
| 291 | An integer literal is a sequence of digits representing an integer value. |
Marcel van Lohuizen | b2703c6 | 2019-09-29 18:20:01 +0200 | [diff] [blame] | 292 | An optional prefix sets a non-decimal base: 0o for octal, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 293 | 0x or 0X for hexadecimal, and 0b for binary. |
| 294 | In hexadecimal literals, letters a-f and A-F represent values 10 through 15. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 295 | All integers allow interstitial underscores "_"; |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 296 | these have no meaning and are solely for readability. |
| 297 | |
| 298 | Decimal integers may have a SI or IEC multiplier. |
| 299 | Multipliers can be used with fractional numbers. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 300 | When multiplying a fraction by a multiplier, the result is truncated |
| 301 | towards zero if it is not an integer. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 302 | |
| 303 | ``` |
Marcel van Lohuizen | afb4db6 | 2019-05-31 00:23:24 +0200 | [diff] [blame] | 304 | int_lit = decimal_lit | si_lit | octal_lit | binary_lit | hex_lit . |
| 305 | decimal_lit = ( "1" … "9" ) { [ "_" ] decimal_digit } . |
| 306 | decimals = decimal_digit { [ "_" ] decimal_digit } . |
| 307 | si_it = decimals [ "." decimals ] multiplier | |
| 308 | "." decimals multiplier . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 309 | binary_lit = "0b" binary_digit { binary_digit } . |
| 310 | hex_lit = "0" ( "x" | "X" ) hex_digit { [ "_" ] hex_digit } . |
Marcel van Lohuizen | b2703c6 | 2019-09-29 18:20:01 +0200 | [diff] [blame] | 311 | octal_lit = "0o" octal_digit { [ "_" ] octal_digit } . |
Marcel van Lohuizen | 6eefcd0 | 2019-10-04 13:32:06 +0200 | [diff] [blame] | 312 | multiplier = ( "K" | "M" | "G" | "T" | "P" ) [ "i" ] |
Marcel van Lohuizen | afb4db6 | 2019-05-31 00:23:24 +0200 | [diff] [blame] | 313 | |
| 314 | float_lit = decimals "." [ decimals ] [ exponent ] | |
| 315 | decimals exponent | |
| 316 | "." decimals [ exponent ]. |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 317 | exponent = ( "e" | "E" ) [ "+" | "-" ] decimals . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 318 | ``` |
Marcel van Lohuizen | 6eefcd0 | 2019-10-04 13:32:06 +0200 | [diff] [blame] | 319 | <!-- |
| 320 | TODO: consider allowing Exo (and up), if not followed by a sign |
| 321 | or number. Alternatively one could only allow Ei, Yi, and Zi. |
| 322 | --> |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 323 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 324 | ``` |
| 325 | 42 |
| 326 | 1.5Gi |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 327 | 170_141_183_460_469_231_731_687_303_715_884_105_727 |
Marcel van Lohuizen | fc6303c | 2019-02-07 17:49:04 +0100 | [diff] [blame] | 328 | 0xBad_Face |
| 329 | 0o755 |
| 330 | 0b0101_0001 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 331 | ``` |
| 332 | |
| 333 | ### Decimal floating-point literals |
| 334 | |
| 335 | A decimal floating-point literal is a representation of |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 336 | a decimal floating-point value (a _float_). |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 337 | It has an integer part, a decimal point, a fractional part, and an |
| 338 | exponent part. |
| 339 | The integer and fractional part comprise decimal digits; the |
| 340 | exponent part is an `e` or `E` followed by an optionally signed decimal exponent. |
| 341 | One of the integer part or the fractional part may be elided; one of the decimal |
| 342 | point or the exponent may be elided. |
| 343 | |
| 344 | ``` |
| 345 | decimal_lit = decimals "." [ decimals ] [ exponent ] | |
| 346 | decimals exponent | |
| 347 | "." decimals [ exponent ] . |
| 348 | exponent = ( "e" | "E" ) [ "+" | "-" ] decimals . |
| 349 | ``` |
| 350 | |
| 351 | ``` |
| 352 | 0. |
| 353 | 72.40 |
| 354 | 072.40 // == 72.40 |
| 355 | 2.71828 |
| 356 | 1.e+0 |
| 357 | 6.67428e-11 |
| 358 | 1E6 |
| 359 | .25 |
| 360 | .12345E+5 |
| 361 | ``` |
| 362 | |
| 363 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 364 | ### String and byte sequence literals |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 365 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 366 | A string literal represents a string constant obtained from concatenating a |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 367 | sequence of characters. |
| 368 | Byte sequences are a sequence of bytes. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 369 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 370 | String and byte sequence literals are character sequences between, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 371 | respectively, double and single quotes, as in `"bar"` and `'bar'`. |
| 372 | Within the quotes, any character may appear except newline and, |
| 373 | respectively, unescaped double or single quote. |
| 374 | String literals may only be valid UTF-8. |
| 375 | Byte sequences may contain any sequence of bytes. |
| 376 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 377 | Several escape sequences allow arbitrary values to be encoded as ASCII text. |
| 378 | An escape sequence starts with an _escape delimiter_, which is `\` by default. |
| 379 | The escape delimiter may be altered to be `\` plus a fixed number of |
| 380 | hash symbols `#` |
| 381 | by padding the start and end of a string or byte sequence literal |
| 382 | with this number of hash symbols. |
| 383 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 384 | There are four ways to represent the integer value as a numeric constant: `\x` |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 385 | followed by exactly two hexadecimal digits; `\u` followed by exactly four |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 386 | hexadecimal digits; `\U` followed by exactly eight hexadecimal digits, and a |
| 387 | plain backslash `\` followed by exactly three octal digits. |
| 388 | In each case the value of the literal is the value represented by the |
| 389 | digits in the corresponding base. |
| 390 | Hexadecimal and octal escapes are only allowed within byte sequences |
| 391 | (single quotes). |
| 392 | |
| 393 | Although these representations all result in an integer, they have different |
| 394 | valid ranges. |
| 395 | Octal escapes must represent a value between 0 and 255 inclusive. |
| 396 | Hexadecimal escapes satisfy this condition by construction. |
| 397 | The escapes `\u` and `\U` represent Unicode code points so within them |
| 398 | some values are illegal, in particular those above `0x10FFFF`. |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 399 | Surrogate halves are allowed, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 400 | but are translated into their non-surrogate equivalent internally. |
| 401 | |
| 402 | The three-digit octal (`\nnn`) and two-digit hexadecimal (`\xnn`) escapes |
| 403 | represent individual bytes of the resulting string; all other escapes represent |
| 404 | the (possibly multi-byte) UTF-8 encoding of individual characters. |
| 405 | Thus inside a string literal `\377` and `\xFF` represent a single byte of |
| 406 | value `0xFF=255`, while `ÿ`, `\u00FF`, `\U000000FF` and `\xc3\xbf` represent |
| 407 | the two bytes `0xc3 0xbf` of the UTF-8 |
| 408 | encoding of character `U+00FF`. |
| 409 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 410 | ``` |
| 411 | \a U+0007 alert or bell |
| 412 | \b U+0008 backspace |
| 413 | \f U+000C form feed |
| 414 | \n U+000A line feed or newline |
| 415 | \r U+000D carriage return |
| 416 | \t U+0009 horizontal tab |
| 417 | \v U+000b vertical tab |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 418 | \/ U+002f slash (solidus) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 419 | \\ U+005c backslash |
| 420 | \' U+0027 single quote (valid escape only within single quoted literals) |
| 421 | \" U+0022 double quote (valid escape only within double quoted literals) |
| 422 | ``` |
| 423 | |
| 424 | The escape `\(` is used as an escape for string interpolation. |
| 425 | A `\(` must be followed by a valid CUE Expression, followed by a `)`. |
| 426 | |
| 427 | All other sequences starting with a backslash are illegal inside literals. |
| 428 | |
| 429 | ``` |
Marcel van Lohuizen | 39df6c9 | 2019-10-25 20:16:26 +0200 | [diff] [blame] | 430 | escaped_char = `\` { `#` } ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | "/" | `\` | "'" | `"` ) . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 431 | byte_value = octal_byte_value | hex_byte_value . |
| 432 | octal_byte_value = `\` octal_digit octal_digit octal_digit . |
| 433 | hex_byte_value = `\` "x" hex_digit hex_digit . |
| 434 | little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit . |
| 435 | big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit |
| 436 | hex_digit hex_digit hex_digit hex_digit . |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 437 | unicode_value = unicode_char | little_u_value | big_u_value | escaped_char . |
| 438 | interpolation = "\(" Expression ")" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 439 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 440 | string_lit = simple_string_lit | |
| 441 | multiline_string_lit | |
| 442 | simple_bytes_lit | |
| 443 | multiline_bytes_lit | |
| 444 | `#` string_lit `#` . |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 445 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 446 | simple_string_lit = `"` { unicode_value | interpolation } `"` . |
Marcel van Lohuizen | c6e5d17 | 2019-11-22 12:09:25 -0800 | [diff] [blame] | 447 | simple_bytes_lit = `'` { unicode_value | interpolation | byte_value } `'` . |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 448 | multiline_string_lit = `"""` newline |
| 449 | { unicode_value | interpolation | newline } |
| 450 | newline `"""` . |
| 451 | multiline_bytes_lit = "'''" newline |
| 452 | { unicode_value | interpolation | byte_value | newline } |
| 453 | newline "'''" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 454 | ``` |
| 455 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 456 | Carriage return characters (`\r`) inside string literals are discarded from |
Marcel van Lohuizen | db9d25a | 2019-02-21 23:54:43 +0100 | [diff] [blame] | 457 | the string value. |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 458 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 459 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 460 | 'a\000\xab' |
| 461 | '\007' |
| 462 | '\377' |
| 463 | '\xa' // illegal: too few hexadecimal digits |
| 464 | "\n" |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 465 | "\"" |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 466 | 'Hello, world!\n' |
| 467 | "Hello, \( name )!" |
| 468 | "日本語" |
| 469 | "\u65e5本\U00008a9e" |
| 470 | "\xff\u00FF" |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 471 | "\uD800" // illegal: surrogate half (TODO: probably should allow) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 472 | "\U00110000" // illegal: invalid Unicode code point |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 473 | |
| 474 | #"This is not an \(interpolation)"# |
| 475 | #"This is an \#(interpolation)"# |
| 476 | #"The sequence "\U0001F604" renders as \#U0001F604."# |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 477 | ``` |
| 478 | |
| 479 | These examples all represent the same string: |
| 480 | |
| 481 | ``` |
| 482 | "日本語" // UTF-8 input text |
| 483 | '日本語' // UTF-8 input text as byte sequence |
| 484 | `日本語` // UTF-8 input text as a raw literal |
| 485 | "\u65e5\u672c\u8a9e" // the explicit Unicode code points |
| 486 | "\U000065e5\U0000672c\U00008a9e" // the explicit Unicode code points |
| 487 | "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // the explicit UTF-8 bytes |
| 488 | ``` |
| 489 | |
| 490 | If the source code represents a character as two code points, such as a |
| 491 | combining form involving an accent and a letter, the result will appear as two |
| 492 | code points if placed in a string literal. |
| 493 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 494 | Strings and byte sequences have a multiline equivalent. |
| 495 | Multiline strings are like their single-line equivalent, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 496 | but allow newline characters. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 497 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 498 | Multiline strings and byte sequences respectively start with |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 499 | a triple double quote (`"""`) or triple single quote (`'''`), |
| 500 | immediately followed by a newline, which is discarded from the string contents. |
| 501 | The string is closed by a matching triple quote, which must be by itself |
| 502 | on a newline, preceded by optional whitespace. |
Marcel van Lohuizen | c8d6c39 | 2019-12-02 13:30:47 +0100 | [diff] [blame] | 503 | The newline preceding the closing quote is discarded from the string contents. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 504 | The whitespace before a closing triple quote must appear before any non-empty |
| 505 | line after the opening quote and will be removed from each of these |
| 506 | lines in the string literal. |
| 507 | A closing triple quote may not appear in the string. |
| 508 | To include it is suffices to escape one of the quotes. |
| 509 | |
| 510 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 511 | """ |
| 512 | lily: |
| 513 | out of the water |
| 514 | out of itself |
| 515 | |
| 516 | bass |
| 517 | picking bugs |
| 518 | off the moon |
| 519 | — Nick Virgilio, Selected Haiku, 1988 |
| 520 | """ |
| 521 | ``` |
| 522 | |
| 523 | This represents the same string as: |
| 524 | |
| 525 | ``` |
| 526 | "lily:\nout of the water\nout of itself\n\n" + |
| 527 | "bass\npicking bugs\noff the moon\n" + |
| 528 | " — Nick Virgilio, Selected Haiku, 1988" |
| 529 | ``` |
| 530 | |
| 531 | <!-- TODO: other values |
| 532 | |
| 533 | Support for other values: |
| 534 | - Duration literals |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 535 | - regular expessions: `re("[a-z]")` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 536 | --> |
| 537 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 538 | |
| 539 | ## Values |
| 540 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 541 | In addition to simple values like `"hello"` and `42.0`, CUE has _structs_. |
| 542 | A struct is a map from labels to values, like `{a: 42.0, b: "hello"}`. |
| 543 | Structs are CUE's only way of building up complex values; |
| 544 | lists, which we will see later, |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 545 | are defined in terms of structs. |
| 546 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 547 | All possible values are ordered in a lattice, |
| 548 | a partial order where every two elements have a single greatest lower bound. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 549 | A value `a` is an _instance_ of a value `b`, |
| 550 | denoted `a ⊑ b`, if `b == a` or `b` is more general than `a`, |
| 551 | that is if `a` orders before `b` in the partial order |
| 552 | (`⊑` is _not_ a CUE operator). |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 553 | We also say that `b` _subsumes_ `a` in this case. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 554 | In graphical terms, `b` is "above" `a` in the lattice. |
| 555 | |
| 556 | At the top of the lattice is the single ancestor of all values, called |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 557 | _top_, denoted `_` in CUE. |
| 558 | Every value is an instance of top. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 559 | |
| 560 | At the bottom of the lattice is the value called _bottom_, denoted `_|_`. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 561 | A bottom value usually indicates an error. |
| 562 | Bottom is an instance of every value. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 563 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 564 | An _atom_ is any value whose only instances are itself and bottom. |
| 565 | Examples of atoms are `42.0`, `"hello"`, `true`, `null`. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 566 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 567 | A value is _concrete_ if it is either an atom, or a struct all of whose |
| 568 | field values are themselves concrete, recursively. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 569 | |
| 570 | CUE's values also include what we normally think of as types, like `string` and |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 571 | `float`. |
| 572 | But CUE does not distinguish between types and values; only the |
| 573 | relationship of values in the lattice is important. |
| 574 | Each CUE "type" subsumes the concrete values that one would normally think |
| 575 | of as part of that type. |
| 576 | For example, "hello" is an instance of `string`, and `42.0` is an instance of |
| 577 | `float`. |
| 578 | In addition to `string` and `float`, CUE has `null`, `int`, `bool` and `bytes`. |
| 579 | We informally call these CUE's "basic types". |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 580 | |
| 581 | |
| 582 | ``` |
| 583 | false ⊑ bool |
| 584 | true ⊑ bool |
| 585 | true ⊑ true |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 586 | 5.0 ⊑ float |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 587 | bool ⊑ _ |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 588 | _|_ ⊑ _ |
| 589 | _|_ ⊑ _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 590 | |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 591 | _ ⋢ _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 592 | _ ⋢ bool |
| 593 | int ⋢ bool |
| 594 | bool ⋢ int |
| 595 | false ⋢ true |
| 596 | true ⋢ false |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 597 | float ⋢ 5.0 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 598 | 5 ⋢ 6 |
| 599 | ``` |
| 600 | |
| 601 | |
| 602 | ### Unification |
| 603 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 604 | The _unification_ of values `a` and `b` |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 605 | is defined as the greatest lower bound of `a` and `b`. (That is, the |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 606 | value `u` such that `u ⊑ a` and `u ⊑ b`, |
| 607 | and for any other value `v` for which `v ⊑ a` and `v ⊑ b` |
| 608 | it holds that `v ⊑ u`.) |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 609 | Since CUE values form a lattice, the unification of two CUE values is |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 610 | always unique. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 611 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 612 | These all follow from the definition of unification: |
| 613 | - The unification of `a` with itself is always `a`. |
| 614 | - The unification of values `a` and `b` where `a ⊑ b` is always `a`. |
| 615 | - The unification of a value with bottom is always bottom. |
| 616 | |
| 617 | Unification in CUE is a [binary expression](#Operands), written `a & b`. |
| 618 | It is commutative and associative. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 619 | As a consequence, order of evaluation is irrelevant, a property that is key |
| 620 | to many of the constructs in the CUE language as well as the tooling layered |
| 621 | on top of it. |
| 622 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 623 | |
| 624 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 625 | <!-- TODO: explicitly mention that disjunction is not a binary operation |
| 626 | but a definition of a single value?--> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 627 | |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 628 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 629 | ### Disjunction |
| 630 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 631 | The _disjunction_ of values `a` and `b` |
| 632 | is defined as the least upper bound of `a` and `b`. |
| 633 | (That is, the value `d` such that `a ⊑ d` and `b ⊑ d`, |
| 634 | and for any other value `e` for which `a ⊑ e` and `b ⊑ e`, |
| 635 | it holds that `d ⊑ e`.) |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 636 | This style of disjunctions is sometimes also referred to as sum types. |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 637 | Since CUE values form a lattice, the disjunction of two CUE values is always unique. |
| 638 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 639 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 640 | These all follow from the definition of disjunction: |
| 641 | - The disjunction of `a` with itself is always `a`. |
| 642 | - The disjunction of a value `a` and `b` where `a ⊑ b` is always `b`. |
| 643 | - The disjunction of a value `a` with bottom is always `a`. |
| 644 | - The disjunction of two bottom values is bottom. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 645 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 646 | Disjunction in CUE is a [binary expression](#Operands), written `a | b`. |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 647 | It is commutative, associative, and idempotent. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 648 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 649 | The unification of a disjunction with another value is equal to the disjunction |
| 650 | composed of the unification of this value with all of the original elements |
| 651 | of the disjunction. |
| 652 | In other words, unification distributes over disjunction. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 653 | |
| 654 | ``` |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 655 | (a_0 | ... |a_n) & b ==> a_0&b | ... | a_n&b. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 656 | ``` |
| 657 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 658 | ``` |
| 659 | Expression Result |
| 660 | ({a:1} | {b:2}) & {c:3} {a:1, c:3} | {b:2, c:3} |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 661 | (int | string) & "foo" "foo" |
| 662 | ("a" | "b") & "c" _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 663 | ``` |
| 664 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 665 | A disjunction is _normalized_ if there is no element |
| 666 | `a` for which there is an element `b` such that `a ⊑ b`. |
| 667 | |
| 668 | <!-- |
| 669 | Normalization is important, as we need to account for spurious elements |
| 670 | For instance "tcp" | "tcp" should resolve to "tcp". |
| 671 | |
| 672 | Also consider |
| 673 | |
| 674 | ({a:1} | {b:1}) & ({a:1} | {b:2}) -> {a:1} | {a:1,b:1} | {a:1,b:2}, |
| 675 | |
| 676 | in this case, elements {a:1,b:1} and {a:1,b:2} are subsumed by {a:1} and thus |
| 677 | this expression is logically equivalent to {a:1} and should therefore be |
| 678 | considered to be unambiguous and resolve to {a:1} if a concrete value is needed. |
| 679 | |
| 680 | For instance, in |
| 681 | |
| 682 | x: ({a:1} | {b:1}) & ({a:1} | {b:2}) // -> {a:1} | {a:1,b:1} | {a:1,b:2} |
| 683 | y: x.a // 1 |
| 684 | |
| 685 | y should resolve to 1, and not an error. |
| 686 | |
| 687 | For comparison, in |
| 688 | |
| 689 | x: ({a:1, b:1} | {b:2}) & {a:1} // -> {a:1,b:1} | {a:1,b:2} |
| 690 | y: x.a // _|_ |
| 691 | |
| 692 | y should be an error as x is still ambiguous before the selector is applied, |
| 693 | even though `a` resolves to 1 in all cases. |
| 694 | --> |
| 695 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 696 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 697 | #### Default values |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 698 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 699 | Any element of a disjunction can be marked as a default |
Axel Wagner | 8529d77 | 2019-09-24 18:27:12 +0000 | [diff] [blame] | 700 | by prefixing it with an asterisk `*`. |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 701 | Intuitively, when an expression needs to be resolved for an operation other |
| 702 | than unification or disjunctions, |
| 703 | non-starred elements are dropped in favor of starred ones if the starred ones |
| 704 | do not resolve to bottom. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 705 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 706 | More precisely, any value `v` may be associated with a default value `d`, |
| 707 | denoted `(v, d)` (not CUE syntax), |
| 708 | where `d` must be in instance of `v` (`d ⊑ v`). |
| 709 | The rules for unifying and disjoining such values are as follows: |
| 710 | |
| 711 | ``` |
| 712 | U1: (v1, d1) & v2 => (v1&v2, d1&v2) |
| 713 | U2: (v1, d1) & (v2, d2) => (v1&v2, d1&d2) |
| 714 | |
| 715 | D1: (v1, d1) | v2 => (v1|v2, d1) |
| 716 | D2: (v1, d1) | (v2, d2) => (v1|v2, d1|d2) |
| 717 | ``` |
| 718 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 719 | Default values may be introduced within disjunctions |
| 720 | by _marking_ terms of a disjunction with an asterisk `*` |
| 721 | ([a unary expression](#Operators)). |
| 722 | The default value of a disjunction with marked terms is the disjunction |
| 723 | of those marked terms, applying the following rules for marks: |
| 724 | |
| 725 | ``` |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 726 | M1: *v => (v, v) |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 727 | M2: *(v1, d1) => (v1, d1) |
| 728 | ``` |
| 729 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 730 | In general, any operation `f` in CUE involving default values proceeds along the |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 731 | following lines |
| 732 | ``` |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 733 | O1: f((v1, d1), ..., (vn, dn)) => (f(v1, ..., vn), f(d1, ..., dn)) |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 734 | ``` |
| 735 | where, with the exception of disjunction, a value `v` without a default |
| 736 | value is promoted to `(v, v)`. |
| 737 | |
| 738 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 739 | ``` |
| 740 | Expression Value-default pair Rules applied |
| 741 | *"tcp" | "udp" ("tcp"|"udp", "tcp") M1, D1 |
| 742 | string | *"foo" (string, "foo") M1, D1 |
| 743 | |
| 744 | *1 | 2 | 3 (1|2|3, 1) M1, D1 |
| 745 | |
| 746 | (*1|2|3) | (1|*2|3) (1|2|3, 1|2) M1, D1, D2 |
| 747 | (*1|2|3) | *(1|*2|3) (1|2|3, 1|2) M1, D1, M2, D2 |
| 748 | (*1|2|3) | (1|*2|3)&2 (1|2|3, 1|2) M1, D1, U1, D2 |
| 749 | |
| 750 | (*1|2) & (1|*2) (1|2, _|_) M1, D1, U2 |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 751 | |
| 752 | (*1|2) + (1|*2) ((1|2)+(1|2), 3) M1, D1, O1 |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 753 | ``` |
| 754 | |
| 755 | The rules of subsumption for defaults can be derived from the above definitions |
| 756 | and are as follows. |
| 757 | |
| 758 | ``` |
| 759 | (v2, d2) ⊑ (v1, d1) if v2 ⊑ v1 and d2 ⊑ d1 |
| 760 | (v1, d1) ⊑ v if v1 ⊑ v |
| 761 | v ⊑ (v1, d1) if v ⊑ d1 |
| 762 | ``` |
| 763 | |
| 764 | <!-- |
| 765 | For the second rule, note that by definition d1 ⊑ v1, so d1 ⊑ v1 ⊑ v. |
| 766 | |
| 767 | The last one is so restrictive as v could still be made more specific by |
| 768 | associating it with a default that is not subsumed by d1. |
| 769 | |
| 770 | Proof: |
| 771 | by definition for any d ⊑ v, it holds that (v, d) ⊑ v, |
| 772 | where the most general value is (v, v). |
| 773 | Given the subsumption rule for (v2, d2) ⊑ (v1, d1), |
| 774 | from (v, v) ⊑ v ⊑ (v1, d1) it follows that v ⊑ d1 |
| 775 | exactly defines the boundary of this subsumption. |
| 776 | --> |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 777 | |
| 778 | <!-- |
| 779 | (non-normalized entries could also be implicitly marked, allowing writing |
| 780 | int | 1, instead of int | *1, but that can be done in a backwards |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 781 | compatible way later if really desirable, as long as we require that |
| 782 | disjunction literals be normalized). |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 783 | --> |
| 784 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 785 | |
| 786 | ``` |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 787 | Expression Resolves to |
| 788 | "tcp" | "udp" "tcp" | "udp" |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 789 | *"tcp" | "udp" "tcp" |
| 790 | float | *1 1 |
| 791 | *string | 1.0 string |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 792 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 793 | (*1|2|3) | (1|*2|3) 1|2 |
| 794 | (*1|2|3) & (1|*2|3) 1|2|3 // default is _|_ |
| 795 | |
| 796 | (* >=5 | int) & (* <=5 | int) 5 |
| 797 | |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 798 | (*"tcp"|"udp") & ("udp"|*"tcp") "tcp" |
| 799 | (*"tcp"|"udp") & ("udp"|"tcp") "tcp" |
| 800 | (*"tcp"|"udp") & "tcp" "tcp" |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 801 | (*"tcp"|"udp") & (*"udp"|"tcp") "tcp" | "udp" // default is _|_ |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 802 | |
| 803 | (*true | false) & bool true |
| 804 | (*true | false) & (true | false) true |
| 805 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 806 | {a: 1} | {b: 1} {a: 1} | {b: 1} |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 807 | {a: 1} | *{b: 1} {b:1} |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 808 | *{a: 1} | *{b: 1} {a: 1} | {b: 1} |
| 809 | ({a: 1} | {b: 1}) & {a:1} {a:1} // after eliminating {a:1,b:1} by normalization |
| 810 | ({a:1}|*{b:1}) & ({a:1}|*{b:1}) {b:1} // after eliminating {a:1,b:1} by normalization |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 811 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 812 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 813 | |
| 814 | ### Bottom and errors |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 815 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 816 | Any evaluation error in CUE results in a bottom value, respresented by |
Axel Wagner | 8529d77 | 2019-09-24 18:27:12 +0000 | [diff] [blame] | 817 | the token `_|_`. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 818 | Bottom is an instance of every other value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 819 | Any evaluation error is represented as bottom. |
| 820 | |
| 821 | Implementations may associate error strings with different instances of bottom; |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 822 | logically they all remain the same value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 823 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 824 | |
| 825 | ### Top |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 826 | |
Axel Wagner | 8529d77 | 2019-09-24 18:27:12 +0000 | [diff] [blame] | 827 | Top is represented by the underscore character `_`, lexically an identifier. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 828 | Unifying any value `v` with top results `v` itself. |
| 829 | |
| 830 | ``` |
| 831 | Expr Result |
| 832 | _ & 5 5 |
| 833 | _ & _ _ |
| 834 | _ & _|_ _|_ |
| 835 | _ | _|_ _ |
| 836 | ``` |
| 837 | |
| 838 | |
| 839 | ### Null |
| 840 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 841 | The _null value_ is represented with the keyword `null`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 842 | It has only one parent, top, and one child, bottom. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 843 | It is unordered with respect to any other value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 844 | |
| 845 | ``` |
| 846 | null_lit = "null" |
| 847 | ``` |
| 848 | |
| 849 | ``` |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 850 | null & 8 _|_ |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 851 | null & _ null |
| 852 | null & _|_ _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 853 | ``` |
| 854 | |
| 855 | |
| 856 | ### Boolean values |
| 857 | |
| 858 | A _boolean type_ represents the set of Boolean truth values denoted by |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 859 | the keywords `true` and `false`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 860 | The predeclared boolean type is `bool`; it is a defined type and a separate |
| 861 | element in the lattice. |
| 862 | |
| 863 | ``` |
| 864 | boolean_lit = "true" | "false" |
| 865 | ``` |
| 866 | |
| 867 | ``` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 868 | bool & true true |
| 869 | true & true true |
| 870 | true & false _|_ |
| 871 | bool & (false|true) false | true |
| 872 | bool & (true|false) true | false |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 873 | ``` |
| 874 | |
| 875 | |
| 876 | ### Numeric values |
| 877 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 878 | The _integer type_ represents the set of all integral numbers. |
| 879 | The _decimal floating-point type_ represents the set of all decimal floating-point |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 880 | numbers. |
| 881 | They are two distinct types. |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 882 | Both are instances instances of a generic `number` type. |
| 883 | |
| 884 | <!-- |
| 885 | number |
| 886 | / \ |
| 887 | int float |
| 888 | --> |
| 889 | |
| 890 | The predeclared number, integer, decimal floating-point types are |
| 891 | `number`, `int` and `float`; they are defined types. |
| 892 | <!-- |
| 893 | TODO: should we drop float? It is somewhat preciser and probably a good idea |
| 894 | to have it in the programmatic API, but it may be confusing to have to deal |
| 895 | with it in the language. |
| 896 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 897 | |
| 898 | A decimal floating-point literal always has type `float`; |
| 899 | it is not an instance of `int` even if it is an integral number. |
| 900 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 901 | Integer literals are always of type `int` and don't match type `float`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 902 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 903 | Numeric literals are exact values of arbitrary precision. |
| 904 | If the operation permits it, numbers should be kept in arbitrary precision. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 905 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 906 | Implementation restriction: although numeric values have arbitrary precision |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 907 | in the language, implementations may implement them using an internal |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 908 | representation with limited precision. |
| 909 | That said, every implementation must: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 910 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 911 | - Represent integer values with at least 256 bits. |
| 912 | - Represent floating-point values, with a mantissa of at least 256 bits and |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 913 | a signed binary exponent of at least 16 bits. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 914 | - Give an error if unable to represent an integer value precisely. |
| 915 | - Give an error if unable to represent a floating-point value due to overflow. |
| 916 | - Round to the nearest representable value if unable to represent |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 917 | a floating-point value due to limits on precision. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 918 | These requirements apply to the result of any expression except for builtin |
| 919 | functions for which an unusual loss of precision must be explicitly documented. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 920 | |
| 921 | |
| 922 | ### Strings |
| 923 | |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 924 | The _string type_ represents the set of UTF-8 strings, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 925 | not allowing surrogates. |
| 926 | The predeclared string type is `string`; it is a defined type. |
| 927 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 928 | The length of a string `s` (its size in bytes) can be discovered using |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 929 | the built-in function `len`. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 930 | |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 931 | |
| 932 | ### Bytes |
| 933 | |
| 934 | The _bytes type_ represents the set of byte sequences. |
| 935 | A byte sequence value is a (possibly empty) sequence of bytes. |
| 936 | The number of bytes is called the length of the byte sequence |
| 937 | and is never negative. |
| 938 | The predeclared byte sequence type is `bytes`; it is a defined type. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 939 | |
| 940 | |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 941 | ### Bounds |
| 942 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 943 | A _bound_, syntactically a [unary expression](#Operands), defines |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 944 | an infinite disjunction of concrete values than can be represented |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 945 | as a single comparison. |
| 946 | |
| 947 | For any [comparison operator](#Comparison-operators) `op` except `==`, |
| 948 | `op a` is the disjunction of every `x` such that `x op a`. |
| 949 | |
| 950 | ``` |
| 951 | 2 & >=2 & <=5 // 2, where 2 is either an int or float. |
| 952 | 2.5 & >=1 & <=5 // 2.5 |
| 953 | 2 & >=1.0 & <3.0 // 2.0 |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 954 | 2 & >1 & <3.0 // 2.0 |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 955 | 2.5 & int & >1 & <5 // _|_ |
| 956 | 2.5 & float & >1 & <5 // 2.5 |
| 957 | int & 2 & >1.0 & <3.0 // _|_ |
| 958 | 2.5 & >=(int & 1) & <5 // _|_ |
| 959 | >=0 & <=7 & >=3 & <=10 // >=3 & <=7 |
| 960 | !=null & 1 // 1 |
| 961 | >=5 & <=5 // 5 |
| 962 | ``` |
| 963 | |
| 964 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 965 | ### Structs |
| 966 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 967 | A _struct_ is a set of elements called _fields_, each of |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 968 | which has a name, called a _label_, and value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 969 | |
| 970 | We say a label is defined for a struct if the struct has a field with the |
| 971 | corresponding label. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 972 | The value for a label `f` of struct `a` is denoted `a.f`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 973 | A struct `a` is an instance of `b`, or `a ⊑ b`, if for any label `f` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 974 | defined for `b`, label `f` is also defined for `a` and `a.f ⊑ b.f`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 975 | Note that if `a` is an instance of `b` it may have fields with labels that |
| 976 | are not defined for `b`. |
| 977 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 978 | The (unique) struct with no fields, written `{}`, has every struct as an |
| 979 | instance. It can be considered the type of all structs. |
| 980 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 981 | ``` |
| 982 | {a: 1} ⊑ {} |
| 983 | {a: 1, b: 1} ⊑ {a: 1} |
| 984 | {a: 1} ⊑ {a: int} |
| 985 | {a: 1, b: 1} ⊑ {a: int, b: float} |
| 986 | |
| 987 | {} ⋢ {a: 1} |
| 988 | {a: 2} ⋢ {a: 1} |
| 989 | {a: 1} ⋢ {b: 1} |
| 990 | ``` |
| 991 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 992 | A field may be required or optional. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 993 | The successful unification of structs `a` and `b` is a new struct `c` which |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 994 | has all fields of both `a` and `b`, where |
| 995 | the value of a field `f` in `c` is `a.f & b.f` if `f` is in both `a` and `b`, |
| 996 | or just `a.f` or `b.f` if `f` is in just `a` or `b`, respectively. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 997 | If a field `f` is in both `a` and `b`, `c.f` is optional only if both |
| 998 | `a.f` and `b.f` are optional. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 999 | Any [references](#References) to `a` or `b` |
| 1000 | in their respective field values need to be replaced with references to `c`. |
Marcel van Lohuizen | 3022ae9 | 2019-10-15 13:35:58 +0200 | [diff] [blame] | 1001 | The result of a unification is bottom (`_|_`) if any of its non-optional |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1002 | fields evaluates to bottom, recursively. |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 1003 | |
Marcel van Lohuizen | 5134dee | 2019-07-21 14:41:44 +0200 | [diff] [blame] | 1004 | <!--NOTE: About bottom values for optional fields being okay. |
| 1005 | |
| 1006 | The proposition ¬P is a close cousin of P → ⊥ and is often used |
| 1007 | as an approximation to avoid the issues of using not. |
| 1008 | Bottom (⊥) is also frequently used to mean undefined. This makes sense. |
| 1009 | Consider `{a?: 2} & {a?: 3}`. |
| 1010 | Both structs say `a` is optional; in other words, it may be omitted. |
| 1011 | So we can still get a valid result by omitting `a`, even in |
| 1012 | case of a conflict. |
| 1013 | |
| 1014 | Granted, this definition may lead to confusing results, especially in |
| 1015 | definitions, when tightening an optional field leads to unintentionally |
| 1016 | discarding it. |
| 1017 | It could be a role of vet checkers to identify such cases (and suggest users |
| 1018 | to explicitly use `_|_` to discard a field, for instance). |
| 1019 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1020 | |
Marcel van Lohuizen | 21ca371 | 2020-06-04 11:59:12 +0200 | [diff] [blame] | 1021 | Syntactically, a field is marked as optional by following its label with a `?`. |
| 1022 | The question mark is not part of the field name. |
| 1023 | A struct literal may contain multiple fields with |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1024 | the same label, the result of which is a single field with the same properties |
| 1025 | as defined as the unification of two fields resulting from unifying two structs. |
| 1026 | |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1027 | These examples illustrate required fields only. |
| 1028 | Examples with optional fields follow below. |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1029 | |
| 1030 | ``` |
| 1031 | Expression Result (without optional fields) |
| 1032 | {a: int, a: 1} {a: 1} |
| 1033 | {a: int} & {a: 1} {a: 1} |
| 1034 | {a: >=1 & <=7} & {a: >=5 & <=9} {a: >=5 & <=7} |
| 1035 | {a: >=1 & <=7, a: >=5 & <=9} {a: >=5 & <=7} |
| 1036 | |
| 1037 | {a: 1} & {b: 2} {a: 1, b: 2} |
| 1038 | {a: 1, b: int} & {b: 2} {a: 1, b: 2} |
| 1039 | |
| 1040 | {a: 1} & {a: 2} _|_ |
| 1041 | ``` |
| 1042 | |
Marcel van Lohuizen | 21ca371 | 2020-06-04 11:59:12 +0200 | [diff] [blame] | 1043 | A struct may define constraints that apply to fields that are added when unified |
| 1044 | with another struct using pattern or default constraints. |
| 1045 | |
| 1046 | A _pattern constraint_, denoted `[pattern]: value`, defines a pattern, which |
| 1047 | is a value of type string, and a value to unify with fields whose label |
| 1048 | match that pattern. |
| 1049 | When unifying structs `a` and `b`, |
| 1050 | a pattern constraint `[p]: v` declared in `a` |
| 1051 | defines that the value `v` should unify with any field in the resulting struct `c` |
| 1052 | whose label unifies with pattern `p` and for which there exists no |
| 1053 | field in `a` with the same label. |
| 1054 | |
| 1055 | Additionally, a _default constraint_, denoted `...value`, defines a value |
| 1056 | to unify with any field for which there is no other declaration in a struct. |
| 1057 | When unifying structs `a` and `b`, |
| 1058 | a default constraint `...v` declared in `a` |
| 1059 | defines that the value `v` should unify with any field in the resulting struct `c` |
| 1060 | whose label does not unify with any of the patterns of the pattern |
| 1061 | constraints defined for `a` _and_ for which there exists no field in `a` |
| 1062 | with that label. |
| 1063 | The token `...` is a shorthand for `..._`. |
| 1064 | |
| 1065 | |
Marcel van Lohuizen | 0cb140e | 2020-02-10 09:09:43 +0100 | [diff] [blame] | 1066 | ``` |
Marcel van Lohuizen | 21ca371 | 2020-06-04 11:59:12 +0200 | [diff] [blame] | 1067 | a: { |
| 1068 | foo: string // foo is a string |
| 1069 | ["^i"]: int // all other fields starting with i are integers |
| 1070 | ["^b"]: bool // all other fields starting with b are booleans |
| 1071 | ...string // all other fields must be a string |
| 1072 | } |
| 1073 | |
| 1074 | b: a & { |
| 1075 | i3: 3 |
| 1076 | bar: true |
| 1077 | other: "a string" |
| 1078 | } |
Marcel van Lohuizen | 0cb140e | 2020-02-10 09:09:43 +0100 | [diff] [blame] | 1079 | ``` |
Marcel van Lohuizen | 21ca371 | 2020-06-04 11:59:12 +0200 | [diff] [blame] | 1080 | |
| 1081 | <!-- NOTE: pattern and default constraints can be made to apply to all |
| 1082 | fields by embedding them as a struct: |
| 1083 | x: { |
| 1084 | a: 2 |
| 1085 | b: 3 |
| 1086 | {[string]: int} |
| 1087 | } |
| 1088 | or by writing |
| 1089 | x: [string]: int |
| 1090 | x: { |
| 1091 | a: 2 |
| 1092 | b: 3 |
| 1093 | } |
| 1094 | --> |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1095 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1096 | Concrete field labels may be an identifier or string, the latter of which may be |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1097 | interpolated. |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1098 | Fields with identifier labels can be referred to within the scope they are |
| 1099 | defined, string labels cannot. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1100 | References within such interpolated strings are resolved within |
| 1101 | the scope of the struct in which the label sequence is |
| 1102 | defined and can reference concrete labels lexically preceding |
| 1103 | the label within a label sequence. |
| 1104 | <!-- We allow this so that rewriting a CUE file to collapse or expand |
| 1105 | field sequences has no impact on semantics. |
| 1106 | --> |
| 1107 | |
| 1108 | <!--TODO: first implementation round will not yet have expression labels |
| 1109 | |
| 1110 | An ExpressionLabel sets a collection of optional fields to a field value. |
| 1111 | By default it defines this value for all possible string labels. |
| 1112 | An optional expression limits this to the set of optional fields which |
| 1113 | labels match the expression. |
| 1114 | --> |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1115 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1116 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1117 | <!-- NOTE: if we allow ...Expr, as in list, it would mean something different. --> |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1118 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1119 | |
| 1120 | <!-- NOTE: |
| 1121 | A DefinitionDecl does not allow repeated labels. This is to avoid |
| 1122 | any ambiguity or confusion about whether earlier path components |
| 1123 | are to be interpreted as declarations or normal fields (they should |
| 1124 | always be normal fields.) |
| 1125 | --> |
| 1126 | |
| 1127 | <!--NOTE: |
| 1128 | The syntax has been deliberately restricted to allow for the following |
| 1129 | future extensions and relaxations: |
| 1130 | - Allow omitting a "?" in an expression label to indicate a concrete |
| 1131 | string value (but maybe we want to use () for that). |
| 1132 | - Make the "?" in expression label optional if expression labels |
| 1133 | are always optional. |
| 1134 | - Or allow eliding the "?" if the expression has no references and |
| 1135 | is obviously not concrete (such as `[string]`). |
| 1136 | - The expression of an expression label may also indicate a struct with |
| 1137 | integer or even number labels |
| 1138 | (beware of imprecise computation in the latter). |
| 1139 | e.g. `{ [int]: string }` is a map of integers to strings. |
| 1140 | - Allow for associative lists (`foo [@.field]: {field: string}`) |
| 1141 | - The `...` notation can be extended analogously to that of a ListList, |
| 1142 | by allowing it to follow with an expression for the remaining properties. |
| 1143 | In that case it is no longer a shorthand for `[string]: _`, but rather |
| 1144 | would define the value for any other value for which there is no field |
| 1145 | defined. |
| 1146 | Like the definition with List, this is somewhat odd, but it allows the |
| 1147 | encoding of JSON schema's and (non-structural) OpenAPI's |
| 1148 | additionalProperties and additionalItems. |
| 1149 | --> |
| 1150 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1151 | ``` |
Marcel van Lohuizen | 21ca371 | 2020-06-04 11:59:12 +0200 | [diff] [blame] | 1152 | StructLit = "{" { Declaration "," } "}" . |
| 1153 | Declaration = Field | Ellipsis | Embedding | LetClause | attribute . |
| 1154 | Ellipsis = "..." [ Expression ] . |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1155 | Embedding = Comprehension | AliasExpr . |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 1156 | Field = Label ":" { Label ":" } Expression { attribute } . |
Marcel van Lohuizen | 86e1a64 | 2020-05-19 21:42:01 +0200 | [diff] [blame] | 1157 | Label = [ identifier "=" ] LabelExpr . |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1158 | LabelExpr = LabelName [ "?" ] | "[" AliasExpr "]" . |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1159 | LabelName = identifier | simple_string_lit . |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 1160 | |
Marcel van Lohuizen | 4d29dde | 2019-12-02 23:11:30 +0100 | [diff] [blame] | 1161 | attribute = "@" identifier "(" attr_tokens ")" . |
| 1162 | attr_tokens = { attr_token | |
| 1163 | "(" attr_tokens ")" | |
| 1164 | "[" attr_tokens "]" | |
| 1165 | "{" attr_tokens "}" } . |
| 1166 | attr_token = /* any token except '(', ')', '[', ']', '{', or '}' */ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1167 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1168 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1169 | ``` |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1170 | Expression Result (without optional fields) |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1171 | a: { foo?: string } {} |
| 1172 | b: { foo: "bar" } { foo: "bar" } |
| 1173 | c: { foo?: *"bar" | string } {} |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1174 | |
| 1175 | d: a & b { foo: "bar" } |
| 1176 | e: b & c { foo: "bar" } |
| 1177 | f: a & c {} |
| 1178 | g: a & { foo?: number } {} |
| 1179 | h: b & { foo?: number } _|_ |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1180 | i: c & { foo: string } { foo: "bar" } |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1181 | |
| 1182 | intMap: [string]: int |
| 1183 | intMap: { |
| 1184 | t1: 43 |
| 1185 | t2: 2.4 // error: 2.4 is not an integer |
| 1186 | } |
| 1187 | |
| 1188 | nameMap: [string]: { |
| 1189 | firstName: string |
| 1190 | nickName: *firstName | string |
| 1191 | } |
| 1192 | |
| 1193 | nameMap: hank: { firstName: "Hank" } |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1194 | ``` |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1195 | The optional field set defined by `nameMap` matches every field, |
| 1196 | in this case just `hank`, and unifies the associated constraint |
| 1197 | with the matched field, resulting in: |
| 1198 | ``` |
| 1199 | nameMap: hank: { |
| 1200 | firstName: "Hank" |
| 1201 | nickName: "Hank" |
| 1202 | } |
| 1203 | ``` |
| 1204 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1205 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1206 | #### Closed structs |
| 1207 | |
| 1208 | By default, structs are open to adding fields. |
Marcel van Lohuizen | 5134dee | 2019-07-21 14:41:44 +0200 | [diff] [blame] | 1209 | Instances of an open struct `p` may contain fields not defined in `p`. |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1210 | This is makes it easy to add fields, but can lead to bugs: |
| 1211 | |
| 1212 | ``` |
| 1213 | S: { |
| 1214 | field1: string |
| 1215 | } |
| 1216 | |
| 1217 | S1: S & { field2: "foo" } |
| 1218 | |
| 1219 | // S1 is { field1: string, field2: "foo" } |
| 1220 | |
| 1221 | |
| 1222 | A: { |
| 1223 | field1: string |
| 1224 | field2: string |
| 1225 | } |
| 1226 | |
| 1227 | A1: A & { |
| 1228 | feild1: "foo" // "field1" was accidentally misspelled |
| 1229 | } |
| 1230 | |
| 1231 | // A1 is |
| 1232 | // { field1: string, field2: string, feild1: "foo" } |
| 1233 | // not the intended |
| 1234 | // { field1: "foo", field2: string } |
| 1235 | ``` |
| 1236 | |
Marcel van Lohuizen | dd0fa88 | 2020-07-25 16:00:45 +0200 | [diff] [blame] | 1237 | A _closed struct_ `c` is a struct whose instances may not declare any field |
| 1238 | with a name that does not match the name of a regular or optional field, |
Marcel van Lohuizen | 21ca371 | 2020-06-04 11:59:12 +0200 | [diff] [blame] | 1239 | or the pattern of a pattern constraint defined in `c`. |
Marcel van Lohuizen | dd0fa88 | 2020-07-25 16:00:45 +0200 | [diff] [blame] | 1240 | Hidden fields are excluded from this limitation. |
Marcel van Lohuizen | 21ca371 | 2020-06-04 11:59:12 +0200 | [diff] [blame] | 1241 | A struct that is the result of unifying any struct with a [`...`](#Structs) |
| 1242 | declaration is defined for all fields. |
| 1243 | Recursively closing a struct is equivalent to adding `..._|_` to its its root |
| 1244 | and any of its substructures that are not defined for all fields. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1245 | |
Marcel van Lohuizen | 21ca371 | 2020-06-04 11:59:12 +0200 | [diff] [blame] | 1246 | Syntactically, structs are recursively closed explicitly with |
| 1247 | the `close` builtin or implicitly by [definitions](#Definitions). |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1248 | |
| 1249 | |
| 1250 | ``` |
| 1251 | A: close({ |
| 1252 | field1: string |
| 1253 | field2: string |
| 1254 | }) |
| 1255 | |
| 1256 | A1: A & { |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1257 | feild1: string |
| 1258 | } // _|_ feild1 not defined for A |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1259 | |
| 1260 | A2: A & { |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1261 | for k,v in { feild1: string } { |
| 1262 | k: v |
| 1263 | } |
| 1264 | } // _|_ feild1 not defined for A |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1265 | |
| 1266 | C: close({ |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1267 | [_]: _ |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1268 | }) |
| 1269 | |
| 1270 | C2: C & { |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1271 | for k,v in { thisIsFine: string } { |
| 1272 | "\(k)": v |
| 1273 | } |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | D: close({ |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1277 | // Values generated by comprehensions are treated as embeddings. |
| 1278 | for k,v in { x: string } { |
| 1279 | "\(k)": v |
| 1280 | } |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1281 | }) |
| 1282 | ``` |
| 1283 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1284 | <!-- (jba) Somewhere it should be said that optional fields are only |
| 1285 | interesting inside closed structs. --> |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1286 | |
Marcel van Lohuizen | 21ca371 | 2020-06-04 11:59:12 +0200 | [diff] [blame] | 1287 | <!-- TODO: move embedding section to above the previous one --> |
| 1288 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1289 | #### Embedding |
| 1290 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1291 | A struct may contain an _embedded value_, an operand used |
Marcel van Lohuizen | 5134dee | 2019-07-21 14:41:44 +0200 | [diff] [blame] | 1292 | as a declaration, which must evaluate to a struct. |
| 1293 | An embedded value of type struct is unified with the struct in which it is |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1294 | embedded, but disregarding the restrictions imposed by closed structs. |
| 1295 | A struct resulting from such a unification is closed if either of the involved |
| 1296 | structs were closed. |
| 1297 | |
Marcel van Lohuizen | a3c7bef | 2019-10-10 21:50:58 +0200 | [diff] [blame] | 1298 | At the top level, an embedded value may be any type. |
| 1299 | In this case, a CUE program will evaluate to the embedded value |
| 1300 | and the CUE program may not have top-level regular or optional |
| 1301 | fields (definitions and aliases are allowed). |
| 1302 | |
Marcel van Lohuizen | 21ca371 | 2020-06-04 11:59:12 +0200 | [diff] [blame] | 1303 | Syntactically, embeddings may be any expression. |
Marcel van Lohuizen | 1f5a903 | 2019-09-09 23:53:42 +0200 | [diff] [blame] | 1304 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1305 | ``` |
| 1306 | S1: { |
| 1307 | a: 1 |
| 1308 | b: 2 |
| 1309 | { |
| 1310 | c: 3 |
| 1311 | } |
| 1312 | } |
| 1313 | // S1 is { a: 1, b: 2, c: 3 } |
| 1314 | |
| 1315 | S2: close({ |
| 1316 | a: 1 |
| 1317 | b: 2 |
| 1318 | { |
| 1319 | c: 3 |
| 1320 | } |
| 1321 | }) |
| 1322 | // same as close(S1) |
| 1323 | |
| 1324 | S3: { |
| 1325 | a: 1 |
| 1326 | b: 2 |
| 1327 | close({ |
| 1328 | c: 3 |
| 1329 | }) |
| 1330 | } |
| 1331 | // same as S2 |
| 1332 | ``` |
| 1333 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1334 | |
Marcel van Lohuizen | b7083ff | 2020-05-12 11:38:19 +0200 | [diff] [blame] | 1335 | #### Definitions and hidden fields |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1336 | |
Marcel van Lohuizen | b7083ff | 2020-05-12 11:38:19 +0200 | [diff] [blame] | 1337 | A field is a _definition_ if its identifier starts with `#` or `_#`. |
| 1338 | A field is _hidden_ if its starts with a `_`. |
| 1339 | Definitions and hidden fields are not emitted when converting a CUE program |
| 1340 | to data and are never required to be concrete. |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 1341 | |
Marcel van Lohuizen | 21ca371 | 2020-06-04 11:59:12 +0200 | [diff] [blame] | 1342 | Referencing a definition will implicitely [close](#ClosedStructs) it. |
| 1343 | A struct that embeds a referenced definition will itself be closed |
| 1344 | after first allowing any other fields or embedded structs to unify. |
| 1345 | The result of `{ #A }` is `#A` for any `#A`. |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 1346 | |
Marcel van Lohuizen | 21ca371 | 2020-06-04 11:59:12 +0200 | [diff] [blame] | 1347 | If referencing a definition would always result in an error, implementations |
| 1348 | may report this inconsistency at the point of its declaration. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1349 | |
| 1350 | ``` |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 1351 | #MyStruct: { |
| 1352 | sub: field: string |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1353 | } |
| 1354 | |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 1355 | #MyStruct: { |
| 1356 | sub: enabled?: bool |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1357 | } |
| 1358 | |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 1359 | myValue: #MyStruct & { |
| 1360 | sub: feild: 2 // error, feild not defined in #MyStruct |
| 1361 | sub: enabled: true // okay |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1362 | } |
| 1363 | |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 1364 | #D: { |
| 1365 | #OneOf |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1366 | |
| 1367 | c: int // adds this field. |
| 1368 | } |
| 1369 | |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 1370 | #OneOf: { a: int } | { b: int } |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1371 | |
| 1372 | |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 1373 | D1: #D & { a: 12, c: 22 } // { a: 12, c: 22 } |
| 1374 | D2: #D & { a: 12, b: 33 } // _|_ // cannot define both `a` and `b` |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1375 | ``` |
| 1376 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1377 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1378 | <!--- |
| 1379 | JSON fields are usual camelCase. Clashes can be avoided by adopting the |
| 1380 | convention that definitions be TitleCase. Unexported definitions are still |
| 1381 | subject to clashes, but those are likely easier to resolve because they are |
| 1382 | package internal. |
| 1383 | ---> |
| 1384 | |
| 1385 | |
Marcel van Lohuizen | 4dd9630 | 2020-01-13 09:38:00 +0100 | [diff] [blame] | 1386 | #### Attributes |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1387 | |
Marcel van Lohuizen | 4dd9630 | 2020-01-13 09:38:00 +0100 | [diff] [blame] | 1388 | Attributes allow associating meta information with values. |
| 1389 | Their primary purpose is to define mappings between CUE and |
| 1390 | other representations. |
| 1391 | Attributes do not influence the evaluation of CUE. |
| 1392 | |
| 1393 | An attribute associates an identifier with a value, a balanced token sequence, |
| 1394 | which is a sequence of CUE tokens with balanced brackets (`()`, `[]`, and `{}`). |
| 1395 | The sequence may not contain interpolations. |
| 1396 | |
| 1397 | Fields, structs and packages can be associated with a set of attributes. |
| 1398 | Attributes accumulate during unification, but implementations may remove |
| 1399 | duplicates that have the same source string representation. |
| 1400 | The interpretation of an attribute, including the handling of multiple |
| 1401 | attributes for a given identifier, is up to the consumer of the attribute. |
| 1402 | |
| 1403 | Field attributes define additional information about a field, |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1404 | such as a mapping to a protocol buffer <!-- TODO: add link --> tag or alternative |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 1405 | name of the field when mapping to a different language. |
| 1406 | |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 1407 | |
| 1408 | ``` |
Marcel van Lohuizen | 4dd9630 | 2020-01-13 09:38:00 +0100 | [diff] [blame] | 1409 | // Package attribute |
| 1410 | @protobuf(proto3) |
| 1411 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1412 | myStruct1: { |
Marcel van Lohuizen | 4dd9630 | 2020-01-13 09:38:00 +0100 | [diff] [blame] | 1413 | // Struct attribute: |
| 1414 | @jsonschema(id="https://example.org/mystruct1.json") |
| 1415 | |
| 1416 | // Field attributes |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 1417 | field: string @go(Field) |
| 1418 | attr: int @xml(,attr) @go(Attr) |
| 1419 | } |
| 1420 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1421 | myStruct2: { |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 1422 | field: string @go(Field) |
| 1423 | attr: int @xml(a1,attr) @go(Attr) |
| 1424 | } |
| 1425 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1426 | Combined: myStruct1 & myStruct2 |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 1427 | // field: string @go(Field) |
| 1428 | // attr: int @xml(,attr) @xml(a1,attr) @go(Attr) |
| 1429 | ``` |
| 1430 | |
Marcel van Lohuizen | fa7e3ce | 2019-10-10 15:43:34 +0200 | [diff] [blame] | 1431 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1432 | #### Aliases |
| 1433 | |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1434 | Aliases name values that can be referred to |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1435 | within the [scope](#declarations-and-scopes) in which they are declared. |
| 1436 | The name of an alias must be unique within its scope. |
| 1437 | |
| 1438 | ``` |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1439 | AliasExpr = Expression | identifier "=" Expression . |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1440 | ``` |
| 1441 | |
| 1442 | Aliases can appear in several positions: |
| 1443 | |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1444 | <!--- TODO: consider allowing this. It should be considered whether |
| 1445 | having field aliases isn't already sufficient. |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1446 | |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1447 | As a declaration in a struct (`X=value`): |
| 1448 | |
| 1449 | - binds identifier `X` to a value embedded within the struct. |
| 1450 | ---> |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1451 | |
| 1452 | In front of a Label (`X=label: value`): |
| 1453 | |
| 1454 | - binds the identifier to the same value as `label` would be bound |
| 1455 | to if it were a valid identifier. |
| 1456 | - for optional fields (`foo?: bar` and `[foo]: bar`), |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1457 | the bound identifier is only visible within the field value (`bar`). |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1458 | |
| 1459 | Inside a bracketed label (`[X=expr]: value`): |
| 1460 | |
| 1461 | - binds the identifier to the the concrete label that matches `expr` |
| 1462 | within the instances of the field value (`value`). |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1463 | |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1464 | Before a list element (`[ X=value, X+1 ]`) (Not yet implemented) |
| 1465 | |
| 1466 | - binds the identifier to the list element it precedes within the scope of the |
| 1467 | list expression. |
| 1468 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1469 | <!-- TODO: explain the difference between aliases and definitions. |
| 1470 | Now that you have definitions, are aliases really necessary? |
| 1471 | Consider removing. |
| 1472 | --> |
| 1473 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1474 | ``` |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1475 | // An alias declaration |
| 1476 | Alias = 3 |
| 1477 | a: Alias // 3 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1478 | |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1479 | // A field alias |
| 1480 | foo: X // 4 |
| 1481 | X="not an identifier": 4 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1482 | |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1483 | // A label alias |
| 1484 | [Y=string]: { name: Y } |
| 1485 | foo: { value: 1 } // outputs: foo: { name: "foo", value: 1 } |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1486 | ``` |
| 1487 | |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1488 | <!-- TODO: also allow aliases as lists --> |
| 1489 | |
| 1490 | |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1491 | #### Let declarations |
| 1492 | |
| 1493 | _Let declarations_ bind an identifier to an expression. |
| 1494 | The identifier is visible within the [scope](#declarations-and-scopes) |
| 1495 | in which it is declared. |
| 1496 | The identifier must be unique within its scope. |
| 1497 | |
| 1498 | ``` |
| 1499 | let x = expr |
| 1500 | |
| 1501 | a: x + 1 |
| 1502 | b: x + 2 |
| 1503 | ``` |
| 1504 | |
Jonathan Amsterdam | 061bde1 | 2019-09-03 08:28:10 -0400 | [diff] [blame] | 1505 | #### Shorthand notation for nested structs |
| 1506 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1507 | A field whose value is a struct with a single field may be written as |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1508 | a colon-separated sequence of the two field names, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1509 | followed by a colon and the value of that single field. |
| 1510 | |
| 1511 | ``` |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1512 | job: myTask: replicas: 2 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1513 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1514 | expands to |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1515 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1516 | job: { |
| 1517 | myTask: { |
| 1518 | replicas: 2 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1519 | } |
| 1520 | } |
| 1521 | ``` |
| 1522 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1523 | <!-- OPTIONAL FIELDS: |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1524 | |
Marcel van Lohuizen | 08a0ef2 | 2019-03-28 09:12:19 +0100 | [diff] [blame] | 1525 | The optional marker solves the issue of having to print large amounts of |
| 1526 | boilerplate when dealing with large types with many optional or default |
| 1527 | values (such as Kubernetes). |
| 1528 | Writing such optional values in terms of *null | value is tedious, |
| 1529 | unpleasant to read, and as it is not well defined what can be dropped or not, |
| 1530 | all null values have to be emitted from the output, even if the user |
| 1531 | doesn't override them. |
| 1532 | Part of the issue is how null is defined. We could adopt a Typescript-like |
| 1533 | approach of introducing "void" or "undefined" to mean "not defined and not |
| 1534 | part of the output". But having all of null, undefined, and void can be |
| 1535 | confusing. If these ever are introduced anyway, the ? operator could be |
| 1536 | expressed along the lines of |
| 1537 | foo?: bar |
| 1538 | being a shorthand for |
| 1539 | foo: void | bar |
| 1540 | where void is the default if no other default is given. |
| 1541 | |
| 1542 | The current mechanical definition of "?" is straightforward, though, and |
| 1543 | probably avoids the need for void, while solving a big issue. |
| 1544 | |
| 1545 | Caveats: |
| 1546 | [1] this definition requires explicitly defined fields to be emitted, even |
| 1547 | if they could be elided (for instance if the explicit value is the default |
| 1548 | value defined an optional field). This is probably a good thing. |
| 1549 | |
| 1550 | [2] a default value may still need to be included in an output if it is not |
| 1551 | the zero value for that field and it is not known if any outside system is |
| 1552 | aware of defaults. For instance, which defaults are specified by the user |
| 1553 | and which by the schema understood by the receiving system. |
| 1554 | The use of "?" together with defaults should therefore be used carefully |
| 1555 | in non-schema definitions. |
| 1556 | Problematic cases should be easy to detect by a vet-like check, though. |
| 1557 | |
| 1558 | [3] It should be considered how this affects the trim command. |
| 1559 | Should values implied by optional fields be allowed to be removed? |
| 1560 | Probably not. This restriction is unlikely to limit the usefulness of trim, |
| 1561 | though. |
| 1562 | |
| 1563 | [4] There should be an option to emit all concrete optional values. |
| 1564 | ``` |
| 1565 | --> |
| 1566 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1567 | ### Lists |
| 1568 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1569 | A list literal defines a new value of type list. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1570 | A list may be open or closed. |
| 1571 | An open list is indicated with a `...` at the end of an element list, |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1572 | optionally followed by a value for the remaining elements. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1573 | |
| 1574 | The length of a closed list is the number of elements it contains. |
| 1575 | The length of an open list is the its number of elements as a lower bound |
| 1576 | and an unlimited number of elements as its upper bound. |
| 1577 | |
| 1578 | ``` |
Marcel van Lohuizen | 21ca371 | 2020-06-04 11:59:12 +0200 | [diff] [blame] | 1579 | ListLit = "[" [ ElementList [ "," [ Ellipsis ] ] [ "," ] "]" . |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1580 | ElementList = Embedding { "," Embedding } . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1581 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1582 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1583 | Lists can be thought of as structs: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1584 | |
| 1585 | ``` |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1586 | List: *null | { |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1587 | Elem: _ |
| 1588 | Tail: List |
| 1589 | } |
| 1590 | ``` |
| 1591 | |
| 1592 | For closed lists, `Tail` is `null` for the last element, for open lists it is |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1593 | `*null | List`, defaulting to the shortest variant. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1594 | For instance, the open list [ 1, 2, ... ] can be represented as: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1595 | ``` |
| 1596 | open: List & { Elem: 1, Tail: { Elem: 2 } } |
| 1597 | ``` |
| 1598 | and the closed version of this list, [ 1, 2 ], as |
| 1599 | ``` |
| 1600 | closed: List & { Elem: 1, Tail: { Elem: 2, Tail: null } } |
| 1601 | ``` |
| 1602 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1603 | Using this representation, the subsumption rule for lists can |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1604 | be derived from those of structs. |
| 1605 | Implementations are not required to implement lists as structs. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1606 | The `Elem` and `Tail` fields are not special and `len` will not work as |
| 1607 | expected in these cases. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1608 | |
| 1609 | |
| 1610 | ## Declarations and Scopes |
| 1611 | |
| 1612 | |
| 1613 | ### Blocks |
| 1614 | |
| 1615 | A _block_ is a possibly empty sequence of declarations. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1616 | The braces of a struct literal `{ ... }` form a block, but there are |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1617 | others as well: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1618 | |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 1619 | - The _universe block_ encompasses all CUE source text. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1620 | - Each [package](#modules-instances-and-packages) has a _package block_ |
| 1621 | containing all CUE source text in that package. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1622 | - Each file has a _file block_ containing all CUE source text in that file. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1623 | - Each `for` and `let` clause in a [comprehension](#comprehensions) |
| 1624 | is considered to be its own implicit block. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1625 | |
| 1626 | Blocks nest and influence [scoping]. |
| 1627 | |
| 1628 | |
| 1629 | ### Declarations and scope |
| 1630 | |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1631 | A _declaration_ may bind an identifier to a field, alias, or package. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1632 | Every identifier in a program must be declared. |
| 1633 | Other than for fields, |
| 1634 | no identifier may be declared twice within the same block. |
| 1635 | For fields an identifier may be declared more than once within the same block, |
| 1636 | resulting in a field with a value that is the result of unifying the values |
| 1637 | of all fields with the same identifier. |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1638 | String labels do not bind an identifier to the respective field. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1639 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1640 | The _scope_ of a declared identifier is the extent of source text in which the |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1641 | identifier denotes the specified field, alias, or package. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1642 | |
| 1643 | CUE is lexically scoped using blocks: |
| 1644 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1645 | 1. The scope of a [predeclared identifier](#predeclared-identifiers) is the universe block. |
Marcel van Lohuizen | 21f6c44 | 2019-09-26 14:55:23 +0200 | [diff] [blame] | 1646 | 1. The scope of an identifier denoting a field |
| 1647 | declared at top level (outside any struct literal) is the package block. |
| 1648 | 1. The scope of an identifier denoting an alias |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1649 | declared at top level (outside any struct literal) is the file block. |
| 1650 | 1. The scope of the package name of an imported package is the file block of the |
| 1651 | file containing the import declaration. |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1652 | 1. The scope of a field, alias or let identifier declared inside a struct |
| 1653 | literal is the innermost containing block. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1654 | |
| 1655 | An identifier declared in a block may be redeclared in an inner block. |
| 1656 | While the identifier of the inner declaration is in scope, it denotes the entity |
| 1657 | declared by the inner declaration. |
| 1658 | |
| 1659 | The package clause is not a declaration; |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1660 | the package name does not appear in any scope. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1661 | Its purpose is to identify the files belonging to the same package |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 1662 | and to specify the default name for import declarations. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1663 | |
| 1664 | |
| 1665 | ### Predeclared identifiers |
| 1666 | |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1667 | CUE predefines a set of types and builtin functions. |
| 1668 | For each of these there is a corresponding keyword which is the name |
| 1669 | of the predefined identifier, prefixed with `__`. |
| 1670 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1671 | ``` |
| 1672 | Functions |
| 1673 | len required close open |
| 1674 | |
| 1675 | Types |
| 1676 | null The null type and value |
| 1677 | bool All boolean values |
| 1678 | int All integral numbers |
| 1679 | float All decimal floating-point numbers |
| 1680 | string Any valid UTF-8 sequence |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 1681 | bytes Any valid byte sequence |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1682 | |
| 1683 | Derived Value |
| 1684 | number int | float |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1685 | uint >=0 |
| 1686 | uint8 >=0 & <=255 |
| 1687 | int8 >=-128 & <=127 |
| 1688 | uint16 >=0 & <=65536 |
| 1689 | int16 >=-32_768 & <=32_767 |
| 1690 | rune >=0 & <=0x10FFFF |
| 1691 | uint32 >=0 & <=4_294_967_296 |
| 1692 | int32 >=-2_147_483_648 & <=2_147_483_647 |
| 1693 | uint64 >=0 & <=18_446_744_073_709_551_615 |
| 1694 | int64 >=-9_223_372_036_854_775_808 & <=9_223_372_036_854_775_807 |
| 1695 | uint128 >=0 & <=340_282_366_920_938_463_463_374_607_431_768_211_455 |
| 1696 | int128 >=-170_141_183_460_469_231_731_687_303_715_884_105_728 & |
| 1697 | <=170_141_183_460_469_231_731_687_303_715_884_105_727 |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 1698 | float32 >=-3.40282346638528859811704183484516925440e+38 & |
| 1699 | <=3.40282346638528859811704183484516925440e+38 |
| 1700 | float64 >=-1.797693134862315708145274237317043567981e+308 & |
| 1701 | <=1.797693134862315708145274237317043567981e+308 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1702 | ``` |
| 1703 | |
| 1704 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1705 | ### Exported identifiers |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1706 | |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 1707 | <!-- move to a more logical spot --> |
| 1708 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1709 | An identifier of a package may be exported to permit access to it |
| 1710 | from another package. |
Marcel van Lohuizen | b7083ff | 2020-05-12 11:38:19 +0200 | [diff] [blame] | 1711 | All identifiers not starting with `_` (so all regular fields and definitions |
| 1712 | starting with `#`) are exported. |
| 1713 | Any identifier starting with `_` is not visible outside the package and resides |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1714 | in a separate namespace than namesake identifiers of other packages. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1715 | |
| 1716 | ``` |
| 1717 | package mypackage |
| 1718 | |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 1719 | foo: string // visible outside mypackage |
| 1720 | "bar": string // visible outside mypackage |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1721 | |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 1722 | #Foo: { // visible outside mypackage |
| 1723 | a: 1 // visible outside mypackage |
| 1724 | _b: 2 // not visible outside mypackage |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1725 | |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 1726 | #C: { // visible outside mypackage |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1727 | d: 4 // visible outside mypackage |
| 1728 | } |
Marcel van Lohuizen | b7083ff | 2020-05-12 11:38:19 +0200 | [diff] [blame] | 1729 | _#E: foo // not visible outside mypackage |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1730 | } |
| 1731 | ``` |
| 1732 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1733 | |
| 1734 | ### Uniqueness of identifiers |
| 1735 | |
| 1736 | Given a set of identifiers, an identifier is called unique if it is different |
| 1737 | from every other in the set, after applying normalization following |
| 1738 | Unicode Annex #31. |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1739 | Two identifiers are different if they are spelled differently |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1740 | or if they appear in different packages and are not exported. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1741 | Otherwise, they are the same. |
| 1742 | |
| 1743 | |
| 1744 | ### Field declarations |
| 1745 | |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1746 | A field associates the value of an expression to a label within a struct. |
| 1747 | If this label is an identifier, it binds the field to that identifier, |
| 1748 | so the field's value can be referenced by writing the identifier. |
| 1749 | String labels are not bound to fields. |
| 1750 | ``` |
| 1751 | a: { |
| 1752 | b: 2 |
| 1753 | "s": 3 |
| 1754 | |
| 1755 | c: b // 2 |
| 1756 | d: s // _|_ unresolved identifier "s" |
| 1757 | e: a.s // 3 |
| 1758 | } |
| 1759 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1760 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1761 | If an expression may result in a value associated with a default value |
| 1762 | as described in [default values](#default-values), the field binds to this |
| 1763 | value-default pair. |
| 1764 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1765 | |
Marcel van Lohuizen | bcf832f | 2019-04-03 22:50:44 +0200 | [diff] [blame] | 1766 | <!-- TODO: disallow creating identifiers starting with __ |
| 1767 | ...and reserve them for builtin values. |
| 1768 | |
| 1769 | The issue is with code generation. As no guarantee can be given that |
| 1770 | a predeclared identifier is not overridden in one of the enclosing scopes, |
| 1771 | code will have to handle detecting such cases and renaming them. |
| 1772 | An alternative is to have the predeclared identifiers be aliases for namesake |
| 1773 | equivalents starting with a double underscore (e.g. string -> __string), |
| 1774 | allowing generated code (normal code would keep using `string`) to refer |
| 1775 | to these directly. |
| 1776 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1777 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1778 | |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1779 | ### Let declarations |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1780 | |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1781 | Within a struct, a let clause binds an identifier to the given expression. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1782 | |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1783 | Within the scope of the identifier, the identifier refers to the |
| 1784 | _locally declared_ expression. |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 1785 | The expression is evaluated in the scope it was declared. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1786 | |
| 1787 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1788 | ## Expressions |
| 1789 | |
| 1790 | An expression specifies the computation of a value by applying operators and |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1791 | built-in functions to operands. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1792 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1793 | Expressions that require concrete values are called _incomplete_ if any of |
| 1794 | their operands are not concrete, but define a value that would be legal for |
| 1795 | that expression. |
| 1796 | Incomplete expressions may be left unevaluated until a concrete value is |
| 1797 | requested at the application level. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1798 | |
| 1799 | ### Operands |
| 1800 | |
| 1801 | Operands denote the elementary values in an expression. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1802 | An operand may be a literal, a (possibly qualified) identifier denoting |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1803 | field, alias, or let declaration, or a parenthesized expression. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1804 | |
| 1805 | ``` |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 1806 | Operand = Literal | OperandName | "(" Expression ")" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1807 | Literal = BasicLit | ListLit | StructLit . |
| 1808 | BasicLit = int_lit | float_lit | string_lit | |
| 1809 | null_lit | bool_lit | bottom_lit | top_lit . |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1810 | OperandName = identifier | QualifiedIdent . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1811 | ``` |
| 1812 | |
| 1813 | ### Qualified identifiers |
| 1814 | |
| 1815 | A qualified identifier is an identifier qualified with a package name prefix. |
| 1816 | |
| 1817 | ``` |
| 1818 | QualifiedIdent = PackageName "." identifier . |
| 1819 | ``` |
| 1820 | |
| 1821 | A qualified identifier accesses an identifier in a different package, |
| 1822 | which must be [imported]. |
| 1823 | The identifier must be declared in the [package block] of that package. |
| 1824 | |
| 1825 | ``` |
| 1826 | math.Sin // denotes the Sin function in package math |
| 1827 | ``` |
| 1828 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1829 | ### References |
| 1830 | |
| 1831 | An identifier operand refers to a field and is called a reference. |
| 1832 | The value of a reference is a copy of the expression associated with the field |
| 1833 | that it is bound to, |
| 1834 | with any references within that expression bound to the respective copies of |
| 1835 | the fields they were originally bound to. |
| 1836 | Implementations may use a different mechanism to evaluate as long as |
| 1837 | these semantics are maintained. |
| 1838 | |
| 1839 | ``` |
| 1840 | a: { |
| 1841 | place: string |
| 1842 | greeting: "Hello, \(place)!" |
| 1843 | } |
| 1844 | |
| 1845 | b: a & { place: "world" } |
| 1846 | c: a & { place: "you" } |
| 1847 | |
| 1848 | d: b.greeting // "Hello, world!" |
| 1849 | e: c.greeting // "Hello, you!" |
| 1850 | ``` |
| 1851 | |
| 1852 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1853 | |
| 1854 | ### Primary expressions |
| 1855 | |
| 1856 | Primary expressions are the operands for unary and binary expressions. |
| 1857 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1858 | |
| 1859 | ``` |
| 1860 | |
| 1861 | Slice: indices must be complete |
| 1862 | ([0, 1, 2, 3] | [2, 3])[0:2] => [0, 1] | [2, 3] |
| 1863 | |
| 1864 | ([0, 1, 2, 3] | *[2, 3])[0:2] => [0, 1] | [2, 3] |
| 1865 | ([0,1,2,3]|[2,3], [2,3])[0:2] => ([0,1]|[2,3], [2,3]) |
| 1866 | |
| 1867 | Index |
| 1868 | a: (1|2, 1) |
| 1869 | b: ([0,1,2,3]|[2,3], [2,3])[a] => ([0,1,2,3]|[2,3][a], 3) |
| 1870 | |
| 1871 | Binary operation |
| 1872 | A binary is only evaluated if its operands are complete. |
| 1873 | |
| 1874 | Input Maximum allowed evaluation |
| 1875 | a: string string |
| 1876 | b: 2 2 |
| 1877 | c: a * b a * 2 |
| 1878 | |
| 1879 | An error in a struct is if the evaluation of any expression results in |
| 1880 | bottom, where an incomplete expression is not considered bottom. |
| 1881 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 1882 | <!-- TODO(mpvl) |
| 1883 | Conversion | |
| 1884 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1885 | ``` |
| 1886 | PrimaryExpr = |
| 1887 | Operand | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1888 | PrimaryExpr Selector | |
| 1889 | PrimaryExpr Index | |
| 1890 | PrimaryExpr Slice | |
| 1891 | PrimaryExpr Arguments . |
| 1892 | |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1893 | Selector = "." (identifier | simple_string_lit) . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1894 | Index = "[" Expression "]" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1895 | Argument = Expression . |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 1896 | Arguments = "(" [ ( Argument { "," Argument } ) [ "," ] ] ")" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1897 | ``` |
| 1898 | <!--- |
Marcel van Lohuizen | 9ffcbbc | 2019-10-23 18:05:05 +0200 | [diff] [blame] | 1899 | TODO: |
| 1900 | PrimaryExpr Query | |
| 1901 | Query = "." Filters . |
| 1902 | Filters = Filter { Filter } . |
| 1903 | Filter = "[" [ "?" ] AliasExpr "]" . |
| 1904 | |
| 1905 | TODO: maybe reintroduce slices, as they are useful in queries, probably this |
| 1906 | time with Python semantics. |
| 1907 | Slice = "[" [ Expression ] ":" [ Expression ] [ ":" [Expression] ] "]" . |
| 1908 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1909 | Argument = Expression | ( identifer ":" Expression ). |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1910 | |
| 1911 | // & expression type |
| 1912 | // string_lit: same as label. Arguments is current node. |
| 1913 | // If selector is applied to list, it performs the operation for each |
| 1914 | // element. |
| 1915 | |
| 1916 | TODO: considering allowing decimal_lit for selectors. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1917 | ---> |
| 1918 | |
| 1919 | ``` |
| 1920 | x |
| 1921 | 2 |
| 1922 | (s + ".txt") |
| 1923 | f(3.1415, true) |
| 1924 | m["foo"] |
| 1925 | s[i : j + 1] |
| 1926 | obj.color |
| 1927 | f.p[i].x |
| 1928 | ``` |
| 1929 | |
| 1930 | |
| 1931 | ### Selectors |
| 1932 | |
Roger Peppe | ded0e1d | 2019-09-24 16:39:36 +0100 | [diff] [blame] | 1933 | For a [primary expression](#primary-expressions) `x` that is not a [package name](#package-clause), |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1934 | the selector expression |
| 1935 | |
| 1936 | ``` |
| 1937 | x.f |
| 1938 | ``` |
| 1939 | |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1940 | denotes the element of a <!--list or -->struct `x` identified by `f`. |
| 1941 | <!--For structs, -->`f` must be an identifier or a string literal identifying |
| 1942 | any definition or regular non-optional field. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1943 | The identifier `f` is called the field selector. |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1944 | |
| 1945 | <!-- |
| 1946 | Allowing strings to be used as field selectors obviates the need for |
| 1947 | backquoted identifiers. Note that some standards use names for structs that |
| 1948 | are not standard identifiers (such "Fn::Foo"). Note that indexing does not |
| 1949 | allow access to identifiers. |
| 1950 | --> |
| 1951 | |
| 1952 | <!-- |
| 1953 | For lists, `f` must be an integer and follows the same lookup rules as |
| 1954 | for the index operation. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1955 | The type of the selector expression is the type of `f`. |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1956 | --> |
| 1957 | |
Roger Peppe | ded0e1d | 2019-09-24 16:39:36 +0100 | [diff] [blame] | 1958 | If `x` is a package name, see the section on [qualified identifiers](#qualified-identifiers). |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1959 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1960 | <!-- |
| 1961 | TODO: consider allowing this and also for selectors. It needs to be considered |
| 1962 | how defaults are corried forward in cases like: |
| 1963 | |
| 1964 | x: { a: string | *"foo" } | *{ a: int | *4 } |
| 1965 | y: x.a & string |
| 1966 | |
| 1967 | What is y in this case? |
| 1968 | (x.a & string, _|_) |
| 1969 | (string|"foo", _|_) |
| 1970 | (string|"foo", "foo) |
| 1971 | If the latter, then why? |
| 1972 | |
| 1973 | For a disjunction of the form `x1 | ... | xn`, |
| 1974 | the selector is applied to each element `x1.f | ... | xn.f`. |
| 1975 | --> |
| 1976 | |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1977 | Otherwise, if `x` is not a <!--list or -->struct, |
| 1978 | or if `f` does not exist in `x`, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1979 | the result of the expression is bottom (an error). |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1980 | In the latter case the expression is incomplete. |
| 1981 | The operand of a selector may be associated with a default. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1982 | |
| 1983 | ``` |
| 1984 | T: { |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1985 | x: int |
| 1986 | y: 3 |
| 1987 | "x-y": 4 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1988 | } |
| 1989 | |
Marcel van Lohuizen | c7791ac | 2019-10-07 11:29:28 +0200 | [diff] [blame] | 1990 | a: T.x // int |
| 1991 | b: T.y // 3 |
| 1992 | c: T.z // _|_ // field 'z' not found in T |
| 1993 | d: T."x-y" // 4 |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1994 | |
| 1995 | e: {a: 1|*2} | *{a: 3|*4} |
| 1996 | f: e.a // 4 (default value) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1997 | ``` |
| 1998 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1999 | <!-- |
| 2000 | ``` |
| 2001 | (v, d).f => (v.f, d.f) |
| 2002 | |
| 2003 | e: {a: 1|*2} | *{a: 3|*4} |
| 2004 | f: e.a // 4 after selecting default from (({a: 1|*2} | {a: 3|*4}).a, 4) |
| 2005 | |
| 2006 | ``` |
| 2007 | --> |
| 2008 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2009 | |
| 2010 | ### Index expressions |
| 2011 | |
| 2012 | A primary expression of the form |
| 2013 | |
| 2014 | ``` |
| 2015 | a[x] |
| 2016 | ``` |
| 2017 | |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 2018 | denotes the element of a list or struct `a` indexed by `x`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2019 | The value `x` is called the index or field name, respectively. |
| 2020 | The following rules apply: |
| 2021 | |
| 2022 | If `a` is not a struct: |
| 2023 | |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 2024 | - `a` is a list (which need not be complete) |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 2025 | - the index `x` unified with `int` must be concrete. |
| 2026 | - the index `x` is in range if `0 <= x < len(a)`, where only the |
| 2027 | explicitly defined values of an open-ended list are considered, |
| 2028 | otherwise it is out of range |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2029 | |
| 2030 | The result of `a[x]` is |
| 2031 | |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 2032 | for `a` of list type: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2033 | |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 2034 | - the list element at index `x`, if `x` is within range |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2035 | - bottom (an error), otherwise |
| 2036 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2037 | |
| 2038 | for `a` of struct type: |
| 2039 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 2040 | - the index `x` unified with `string` must be concrete. |
Marcel van Lohuizen | d282553 | 2019-09-23 12:44:01 +0100 | [diff] [blame] | 2041 | - the value of the regular and non-optional field named `x` of struct `a`, |
| 2042 | if this field exists |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2043 | - bottom (an error), otherwise |
| 2044 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 2045 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2046 | ``` |
| 2047 | [ 1, 2 ][1] // 2 |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 2048 | [ 1, 2 ][2] // _|_ |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2049 | [ 1, 2, ...][2] // _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2050 | ``` |
| 2051 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 2052 | Both the operand and index value may be a value-default pair. |
| 2053 | ``` |
| 2054 | va[vi] => va[vi] |
| 2055 | va[(vi, di)] => (va[vi], va[di]) |
| 2056 | (va, da)[vi] => (va[vi], da[vi]) |
| 2057 | (va, da)[(vi, di)] => (va[vi], da[di]) |
| 2058 | ``` |
| 2059 | |
| 2060 | ``` |
| 2061 | Fields Result |
| 2062 | x: [1, 2] | *[3, 4] ([1,2]|[3,4], [3,4]) |
| 2063 | i: int | *1 (int, 1) |
| 2064 | |
| 2065 | v: x[i] (x[i], 4) |
| 2066 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2067 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2068 | ### Operators |
| 2069 | |
| 2070 | Operators combine operands into expressions. |
| 2071 | |
| 2072 | ``` |
| 2073 | Expression = UnaryExpr | Expression binary_op Expression . |
| 2074 | UnaryExpr = PrimaryExpr | unary_op UnaryExpr . |
| 2075 | |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 2076 | binary_op = "|" | "&" | "||" | "&&" | "==" | rel_op | add_op | mul_op . |
Marcel van Lohuizen | 2b0e7cd | 2019-03-25 08:28:41 +0100 | [diff] [blame] | 2077 | rel_op = "!=" | "<" | "<=" | ">" | ">=" | "=~" | "!~" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2078 | add_op = "+" | "-" . |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2079 | mul_op = "*" | "/" | "div" | "mod" | "quo" | "rem" . |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 2080 | unary_op = "+" | "-" | "!" | "*" | rel_op . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2081 | ``` |
| 2082 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2083 | Comparisons are discussed [elsewhere](#Comparison-operators). |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 2084 | For any binary operators, the operand types must unify. |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2085 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2086 | <!-- TODO: durations |
| 2087 | unless the operation involves durations. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2088 | |
| 2089 | Except for duration operations, if one operand is an untyped [literal] and the |
| 2090 | other operand is not, the constant is [converted] to the type of the other |
| 2091 | operand. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2092 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2093 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 2094 | Operands of unary and binary expressions may be associated with a default using |
| 2095 | the following |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2096 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 2097 | <!-- |
| 2098 | ``` |
| 2099 | O1: op (v1, d1) => (op v1, op d1) |
| 2100 | |
| 2101 | O2: (v1, d1) op (v2, d2) => (v1 op v2, d1 op d2) |
| 2102 | and because v => (v, v) |
| 2103 | O3: v1 op (v2, d2) => (v1 op v2, v1 op d2) |
| 2104 | O4: (v1, d1) op v2 => (v1 op v2, d1 op v2) |
| 2105 | ``` |
| 2106 | --> |
| 2107 | |
| 2108 | ``` |
| 2109 | Field Resulting Value-Default pair |
| 2110 | a: *1|2 (1|2, 1) |
| 2111 | b: -a (-a, -1) |
| 2112 | |
| 2113 | c: a + 2 (a+2, 3) |
| 2114 | d: a + a (a+a, 2) |
| 2115 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2116 | |
| 2117 | #### Operator precedence |
| 2118 | |
| 2119 | Unary operators have the highest precedence. |
| 2120 | |
| 2121 | There are eight precedence levels for binary operators. |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 2122 | Multiplication operators binds strongest, followed by |
| 2123 | addition operators, comparison operators, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2124 | `&&` (logical AND), `||` (logical OR), `&` (unification), |
| 2125 | and finally `|` (disjunction): |
| 2126 | |
| 2127 | ``` |
| 2128 | Precedence Operator |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2129 | 7 * / div mod quo rem |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2130 | 6 + - |
Marcel van Lohuizen | 2b0e7cd | 2019-03-25 08:28:41 +0100 | [diff] [blame] | 2131 | 5 == != < <= > >= =~ !~ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2132 | 4 && |
| 2133 | 3 || |
| 2134 | 2 & |
| 2135 | 1 | |
| 2136 | ``` |
| 2137 | |
| 2138 | Binary operators of the same precedence associate from left to right. |
| 2139 | For instance, `x / y * z` is the same as `(x / y) * z`. |
| 2140 | |
| 2141 | ``` |
| 2142 | +x |
| 2143 | 23 + 3*x[i] |
| 2144 | x <= f() |
| 2145 | f() || g() |
| 2146 | x == y+1 && y == z-1 |
| 2147 | 2 | int |
| 2148 | { a: 1 } & { b: 2 } |
| 2149 | ``` |
| 2150 | |
| 2151 | #### Arithmetic operators |
| 2152 | |
| 2153 | Arithmetic operators apply to numeric values and yield a result of the same type |
| 2154 | as the first operand. The three of the four standard arithmetic operators |
| 2155 | `(+, -, *)` apply to integer and decimal floating-point types; |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 2156 | `+` and `*` also apply to lists and strings. |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2157 | `/` only applies to decimal floating-point types and |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2158 | `div`, `mod`, `quo`, and `rem` only apply to integer types. |
| 2159 | |
| 2160 | ``` |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 2161 | + sum integers, floats, lists, strings, bytes |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2162 | - difference integers, floats |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 2163 | * product integers, floats, lists, strings, bytes |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2164 | / quotient floats |
| 2165 | div division integers |
| 2166 | mod modulo integers |
| 2167 | quo quotient integers |
| 2168 | rem remainder integers |
| 2169 | ``` |
| 2170 | |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2171 | For any operator that accepts operands of type `float`, any operand may be |
| 2172 | of type `int` or `float`, in which case the result will be `float` if any |
| 2173 | of the operands is `float` or `int` otherwise. |
| 2174 | For `/` the result is always `float`. |
| 2175 | |
| 2176 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2177 | #### Integer operators |
| 2178 | |
| 2179 | For two integer values `x` and `y`, |
| 2180 | the integer quotient `q = x div y` and remainder `r = x mod y ` |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 2181 | implement Euclidean division and |
| 2182 | satisfy the following relationship: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2183 | |
| 2184 | ``` |
| 2185 | r = x - y*q with 0 <= r < |y| |
| 2186 | ``` |
| 2187 | where `|y|` denotes the absolute value of `y`. |
| 2188 | |
| 2189 | ``` |
| 2190 | x y x div y x mod y |
| 2191 | 5 3 1 2 |
| 2192 | -5 3 -2 1 |
| 2193 | 5 -3 -1 2 |
| 2194 | -5 -3 2 1 |
| 2195 | ``` |
| 2196 | |
| 2197 | For two integer values `x` and `y`, |
| 2198 | the integer quotient `q = x quo y` and remainder `r = x rem y ` |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 2199 | implement truncated division and |
| 2200 | satisfy the following relationship: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2201 | |
| 2202 | ``` |
| 2203 | x = q*y + r and |r| < |y| |
| 2204 | ``` |
| 2205 | |
| 2206 | with `x quo y` truncated towards zero. |
| 2207 | |
| 2208 | ``` |
| 2209 | x y x quo y x rem y |
| 2210 | 5 3 1 2 |
| 2211 | -5 3 -1 -2 |
| 2212 | 5 -3 -1 2 |
| 2213 | -5 -3 1 -2 |
| 2214 | ``` |
| 2215 | |
| 2216 | A zero divisor in either case results in bottom (an error). |
| 2217 | |
| 2218 | For integer operands, the unary operators `+` and `-` are defined as follows: |
| 2219 | |
| 2220 | ``` |
| 2221 | +x is 0 + x |
| 2222 | -x negation is 0 - x |
| 2223 | ``` |
| 2224 | |
| 2225 | |
| 2226 | #### Decimal floating-point operators |
| 2227 | |
| 2228 | For decimal floating-point numbers, `+x` is the same as `x`, |
| 2229 | while -x is the negation of x. |
| 2230 | The result of a floating-point division by zero is bottom (an error). |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2231 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2232 | <!-- TODO: consider making it +/- Inf --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2233 | |
| 2234 | An implementation may combine multiple floating-point operations into a single |
| 2235 | fused operation, possibly across statements, and produce a result that differs |
| 2236 | from the value obtained by executing and rounding the instructions individually. |
| 2237 | |
| 2238 | |
| 2239 | #### List operators |
| 2240 | |
| 2241 | Lists can be concatenated using the `+` operator. |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2242 | Opens list are closed to their default value beforehand. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2243 | |
| 2244 | ``` |
| 2245 | [ 1, 2 ] + [ 3, 4 ] // [ 1, 2, 3, 4 ] |
| 2246 | [ 1, 2, ... ] + [ 3, 4 ] // [ 1, 2, 3, 4 ] |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2247 | [ 1, 2 ] + [ 3, 4, ... ] // [ 1, 2, 3, 4 ] |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2248 | ``` |
| 2249 | |
Jonathan Amsterdam | 0500c31 | 2019-02-16 18:04:09 -0500 | [diff] [blame] | 2250 | Lists can be multiplied with a non-negative`int` using the `*` operator |
Marcel van Lohuizen | 13e36bd | 2019-02-01 09:59:18 +0100 | [diff] [blame] | 2251 | to create a repeated the list by the indicated number. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2252 | ``` |
| 2253 | 3*[1,2] // [1, 2, 1, 2, 1, 2] |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2254 | 3*[1, 2, ...] // [1, 2, 1, 2, 1 ,2] |
Marcel van Lohuizen | 13e36bd | 2019-02-01 09:59:18 +0100 | [diff] [blame] | 2255 | [byte]*4 // [byte, byte, byte, byte] |
Jonathan Amsterdam | 0500c31 | 2019-02-16 18:04:09 -0500 | [diff] [blame] | 2256 | 0*[1,2] // [] |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2257 | ``` |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 2258 | |
| 2259 | <!-- TODO(mpvl): should we allow multiplication with a range? |
| 2260 | If so, how does one specify a list with a range of possible lengths? |
| 2261 | |
| 2262 | Suggestion from jba: |
| 2263 | Multiplication should distribute over disjunction, |
| 2264 | so int(1)..int(3) * [x] = [x] | [x, x] | [x, x, x]. |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 2265 | The hard part is figuring out what (>=1 & <=3) * [x] means, |
| 2266 | since >=1 & <=3 includes many floats. |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 2267 | (mpvl: could constrain arguments to parameter types, but needs to be |
| 2268 | done consistently.) |
| 2269 | --> |
| 2270 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2271 | |
| 2272 | #### String operators |
| 2273 | |
| 2274 | Strings can be concatenated using the `+` operator: |
| 2275 | ``` |
Daniel Martí | 107863a | 2020-02-11 15:00:50 +0000 | [diff] [blame] | 2276 | s: "hi " + name + " and good bye" |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2277 | ``` |
| 2278 | String addition creates a new string by concatenating the operands. |
| 2279 | |
| 2280 | A string can be repeated by multiplying it: |
| 2281 | |
| 2282 | ``` |
| 2283 | s: "etc. "*3 // "etc. etc. etc. " |
| 2284 | ``` |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2285 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2286 | <!-- jba: Do these work for byte sequences? If not, why not? --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2287 | |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2288 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2289 | ##### Comparison operators |
| 2290 | |
| 2291 | Comparison operators compare two operands and yield an untyped boolean value. |
| 2292 | |
| 2293 | ``` |
| 2294 | == equal |
| 2295 | != not equal |
| 2296 | < less |
| 2297 | <= less or equal |
| 2298 | > greater |
| 2299 | >= greater or equal |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2300 | =~ matches regular expression |
| 2301 | !~ does not match regular expression |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2302 | ``` |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2303 | |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2304 | <!-- regular expression operator inspired by Bash, Perl, and Ruby. --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2305 | |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2306 | In any comparison, the types of the two operands must unify or one of the |
| 2307 | operands must be null. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2308 | |
| 2309 | The equality operators `==` and `!=` apply to operands that are comparable. |
| 2310 | The ordering operators `<`, `<=`, `>`, and `>=` apply to operands that are ordered. |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2311 | The matching operators `=~` and `!~` apply to a string and regular |
| 2312 | expression operand. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2313 | These terms and the result of the comparisons are defined as follows: |
| 2314 | |
Marcel van Lohuizen | 855243e | 2019-02-07 18:00:55 +0100 | [diff] [blame] | 2315 | - Null is comparable with itself and any other type. |
| 2316 | Two null values are always equal, null is unequal with anything else. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2317 | - Boolean values are comparable. |
| 2318 | Two boolean values are equal if they are either both true or both false. |
| 2319 | - Integer values are comparable and ordered, in the usual way. |
| 2320 | - Floating-point values are comparable and ordered, as per the definitions |
| 2321 | for binary coded decimals in the IEEE-754-2008 standard. |
Marcel van Lohuizen | 4a36099 | 2019-05-11 18:18:31 +0200 | [diff] [blame] | 2322 | - Floating point numbers may be compared with integers. |
Marcel van Lohuizen | 4108f80 | 2019-08-13 18:30:25 +0200 | [diff] [blame] | 2323 | - String and bytes values are comparable and ordered lexically byte-wise. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2324 | - Struct are not comparable. |
Marcel van Lohuizen | 855243e | 2019-02-07 18:00:55 +0100 | [diff] [blame] | 2325 | - Lists are not comparable. |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2326 | - The regular expression syntax is the one accepted by RE2, |
| 2327 | described in https://github.com/google/re2/wiki/Syntax, |
| 2328 | except for `\C`. |
| 2329 | - `s =~ r` is true if `s` matches the regular expression `r`. |
| 2330 | - `s !~ r` is true if `s` does not match regular expression `r`. |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2331 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 2332 | <!--- TODO: consider the following |
| 2333 | - For regular expression, named capture groups are interpreted as CUE references |
| 2334 | that must unify with the strings matching this capture group. |
| 2335 | ---> |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2336 | <!-- TODO: Implementations should adopt an algorithm that runs in linear time? --> |
Marcel van Lohuizen | 88a8a5f | 2019-02-20 01:26:22 +0100 | [diff] [blame] | 2337 | <!-- Consider implementing Level 2 of Unicode regular expression. --> |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2338 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2339 | ``` |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2340 | 3 < 4 // true |
Marcel van Lohuizen | 4a36099 | 2019-05-11 18:18:31 +0200 | [diff] [blame] | 2341 | 3 < 4.0 // true |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 2342 | null == 2 // false |
| 2343 | null != {} // true |
| 2344 | {} == {} // _|_: structs are not comparable against structs |
| 2345 | |
| 2346 | "Wild cats" =~ "cat" // true |
| 2347 | "Wild cats" !~ "dog" // true |
| 2348 | |
| 2349 | "foo" =~ "^[a-z]{3}$" // true |
| 2350 | "foo" =~ "^[a-z]{4}$" // false |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2351 | ``` |
| 2352 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2353 | <!-- jba |
| 2354 | I think I know what `3 < a` should mean if |
| 2355 | |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 2356 | a: >=1 & <=5 |
| 2357 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2358 | It should be a constraint on `a` that can be evaluated once `a`'s value is known more precisely. |
| 2359 | |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 2360 | But what does `3 < (>=1 & <=5)` mean? We'll never get more information, so it must have a definite value. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2361 | --> |
| 2362 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2363 | #### Logical operators |
| 2364 | |
| 2365 | Logical operators apply to boolean values and yield a result of the same type |
| 2366 | as the operands. The right operand is evaluated conditionally. |
| 2367 | |
| 2368 | ``` |
| 2369 | && conditional AND p && q is "if p then q else false" |
| 2370 | || conditional OR p || q is "if p then true else q" |
| 2371 | ! NOT !p is "not p" |
| 2372 | ``` |
| 2373 | |
| 2374 | |
| 2375 | <!-- |
| 2376 | ### TODO TODO TODO |
| 2377 | |
| 2378 | 3.14 / 0.0 // illegal: division by zero |
| 2379 | Illegal conversions always apply to CUE. |
| 2380 | |
| 2381 | Implementation restriction: A compiler may use rounding while computing untyped floating-point or complex constant expressions; see the implementation restriction in the section on constants. This rounding may cause a floating-point constant expression to be invalid in an integer context, even if it would be integral when calculated using infinite precision, and vice versa. |
| 2382 | --> |
| 2383 | |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2384 | <!--- TODO(mpvl): conversions |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2385 | ### Conversions |
| 2386 | Conversions are expressions of the form `T(x)` where `T` and `x` are |
| 2387 | expressions. |
| 2388 | The result is always an instance of `T`. |
| 2389 | |
| 2390 | ``` |
| 2391 | Conversion = Expression "(" Expression [ "," ] ")" . |
| 2392 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2393 | ---> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2394 | <!--- |
| 2395 | |
| 2396 | A literal value `x` can be converted to type T if `x` is representable by a |
| 2397 | value of `T`. |
| 2398 | |
| 2399 | As a special case, an integer literal `x` can be converted to a string type |
| 2400 | using the same rule as for non-constant x. |
| 2401 | |
| 2402 | Converting a literal yields a typed value as result. |
| 2403 | |
| 2404 | ``` |
| 2405 | uint(iota) // iota value of type uint |
| 2406 | float32(2.718281828) // 2.718281828 of type float32 |
| 2407 | complex128(1) // 1.0 + 0.0i of type complex128 |
| 2408 | float32(0.49999999) // 0.5 of type float32 |
| 2409 | float64(-1e-1000) // 0.0 of type float64 |
| 2410 | string('x') // "x" of type string |
| 2411 | string(0x266c) // "♬" of type string |
| 2412 | MyString("foo" + "bar") // "foobar" of type MyString |
| 2413 | string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant |
| 2414 | (*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type |
| 2415 | int(1.2) // illegal: 1.2 cannot be represented as an int |
| 2416 | string(65.0) // illegal: 65.0 is not an integer constant |
| 2417 | ``` |
| 2418 | ---> |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2419 | <!--- |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2420 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2421 | A conversion is always allowed if `x` is an instance of `T`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2422 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2423 | If `T` and `x` of different underlying type, a conversion is allowed if |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2424 | `x` can be converted to a value `x'` of `T`'s type, and |
| 2425 | `x'` is an instance of `T`. |
| 2426 | A value `x` can be converted to the type of `T` in any of these cases: |
| 2427 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2428 | - `x` is a struct and is subsumed by `T`. |
| 2429 | - `x` and `T` are both integer or floating points. |
| 2430 | - `x` is an integer or a byte sequence and `T` is a string. |
| 2431 | - `x` is a string and `T` is a byte sequence. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2432 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2433 | Specific rules apply to conversions between numeric types, structs, |
| 2434 | or to and from a string type. These conversions may change the representation |
| 2435 | of `x`. |
| 2436 | All other conversions only change the type but not the representation of x. |
| 2437 | |
| 2438 | |
| 2439 | #### Conversions between numeric ranges |
| 2440 | For the conversion of numeric values, the following rules apply: |
| 2441 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2442 | 1. Any integer value can be converted into any other integer value |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2443 | provided that it is within range. |
| 2444 | 2. When converting a decimal floating-point number to an integer, the fraction |
| 2445 | is discarded (truncation towards zero). TODO: or disallow truncating? |
| 2446 | |
| 2447 | ``` |
| 2448 | a: uint16(int(1000)) // uint16(1000) |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 2449 | b: uint8(1000) // _|_ // overflow |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2450 | c: int(2.5) // 2 TODO: TBD |
| 2451 | ``` |
| 2452 | |
| 2453 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2454 | #### Conversions to and from a string type |
| 2455 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2456 | Converting a list of bytes to a string type yields a string whose successive |
| 2457 | bytes are the elements of the slice. |
| 2458 | Invalid UTF-8 is converted to `"\uFFFD"`. |
| 2459 | |
| 2460 | ``` |
| 2461 | string('hell\xc3\xb8') // "hellø" |
| 2462 | string(bytes([0x20])) // " " |
| 2463 | ``` |
| 2464 | |
| 2465 | As string value is always convertible to a list of bytes. |
| 2466 | |
| 2467 | ``` |
| 2468 | bytes("hellø") // 'hell\xc3\xb8' |
| 2469 | bytes("") // '' |
| 2470 | ``` |
| 2471 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2472 | #### Conversions between list types |
| 2473 | |
| 2474 | Conversions between list types are possible only if `T` strictly subsumes `x` |
| 2475 | and the result will be the unification of `T` and `x`. |
| 2476 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2477 | If we introduce named types this would be different from IP & [10, ...] |
| 2478 | |
| 2479 | Consider removing this until it has a different meaning. |
| 2480 | |
| 2481 | ``` |
| 2482 | IP: 4*[byte] |
| 2483 | Private10: IP([10, ...]) // [10, byte, byte, byte] |
| 2484 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2485 | |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 2486 | #### Conversions between struct types |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2487 | |
| 2488 | A conversion from `x` to `T` |
| 2489 | is applied using the following rules: |
| 2490 | |
| 2491 | 1. `x` must be an instance of `T`, |
| 2492 | 2. all fields defined for `x` that are not defined for `T` are removed from |
| 2493 | the result of the conversion, recursively. |
| 2494 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2495 | <!-- jba: I don't think you say anywhere that the matching fields are unified. |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2496 | mpvl: they are not, x must be an instance of T, in which case x == T&x, |
| 2497 | so unification would be unnecessary. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2498 | --> |
Marcel van Lohuizen | a3f0097 | 2019-02-01 11:10:39 +0100 | [diff] [blame] | 2499 | <!-- |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2500 | ``` |
| 2501 | T: { |
| 2502 | a: { b: 1..10 } |
| 2503 | } |
| 2504 | |
| 2505 | x1: { |
| 2506 | a: { b: 8, c: 10 } |
| 2507 | d: 9 |
| 2508 | } |
| 2509 | |
| 2510 | c1: T(x1) // { a: { b: 8 } } |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 2511 | c2: T({}) // _|_ // missing field 'a' in '{}' |
| 2512 | c3: T({ a: {b: 0} }) // _|_ // field a.b does not unify (0 & 1..10) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2513 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2514 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2515 | |
| 2516 | ### Calls |
| 2517 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2518 | Calls can be made to core library functions, called builtins. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2519 | Given an expression `f` of function type F, |
| 2520 | ``` |
| 2521 | f(a1, a2, … an) |
| 2522 | ``` |
| 2523 | calls `f` with arguments a1, a2, … an. Arguments must be expressions |
| 2524 | of which the values are an instance of the parameter types of `F` |
| 2525 | and are evaluated before the function is called. |
| 2526 | |
| 2527 | ``` |
| 2528 | a: math.Atan2(x, y) |
| 2529 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2530 | |
| 2531 | In a function call, the function value and arguments are evaluated in the usual |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 2532 | order. |
| 2533 | After they are evaluated, the parameters of the call are passed by value |
| 2534 | to the function and the called function begins execution. |
| 2535 | The return parameters |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2536 | of the function are passed by value back to the calling function when the |
| 2537 | function returns. |
| 2538 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2539 | |
| 2540 | ### Comprehensions |
| 2541 | |
Marcel van Lohuizen | 66db920 | 2018-12-17 19:02:08 +0100 | [diff] [blame] | 2542 | Lists and fields can be constructed using comprehensions. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2543 | |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 2544 | Comprehensions define a clause sequence that consists of a sequence of |
| 2545 | `for`, `if`, and `let` clauses, nesting from left to right. |
| 2546 | The sequence must start with a `for` or `if` clause. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2547 | The `for` and `let` clauses each define a new scope in which new values are |
| 2548 | bound to be available for the next clause. |
| 2549 | |
| 2550 | The `for` clause binds the defined identifiers, on each iteration, to the next |
| 2551 | value of some iterable value in a new scope. |
| 2552 | A `for` clause may bind one or two identifiers. |
Marcel van Lohuizen | 4245fb4 | 2019-09-09 11:22:12 +0200 | [diff] [blame] | 2553 | If there is one identifier, it binds it to the value of |
| 2554 | a list element or struct field value. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2555 | If there are two identifiers, the first value will be the key or index, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2556 | if available, and the second will be the value. |
| 2557 | |
Marcel van Lohuizen | 4245fb4 | 2019-09-09 11:22:12 +0200 | [diff] [blame] | 2558 | For lists, `for` iterates over all elements in the list after closing it. |
| 2559 | For structs, `for` iterates over all non-optional regular fields. |
| 2560 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2561 | An `if` clause, or guard, specifies an expression that terminates the current |
| 2562 | iteration if it evaluates to false. |
| 2563 | |
| 2564 | The `let` clause binds the result of an expression to the defined identifier |
| 2565 | in a new scope. |
| 2566 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2567 | A current iteration is said to complete if the innermost block of the clause |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2568 | sequence is reached. |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 2569 | Syntactically, the comprehension value is a struct. |
| 2570 | A comprehension can generate non-struct values by embedding such values within |
| 2571 | this struct. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2572 | |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 2573 | Within lists, the values yielded by a comprehension are inserted in the list |
| 2574 | at the position of the comprehension. |
| 2575 | Within structs, the values yielded by a comprehension are embedded within the |
| 2576 | struct. |
| 2577 | Both structs and lists may contain multiple comprehensions. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2578 | |
| 2579 | ``` |
Marcel van Lohuizen | 1f5a903 | 2019-09-09 23:53:42 +0200 | [diff] [blame] | 2580 | Comprehension = Clauses StructLit . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2581 | |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 2582 | Clauses = StartClause { [ "," ] Clause } . |
| 2583 | StartClause = ForClause | GuardClause . |
| 2584 | Clause = StartClause | LetClause . |
| 2585 | ForClause = "for" identifier [ "," identifier ] "in" Expression . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2586 | GuardClause = "if" Expression . |
| 2587 | LetClause = "let" identifier "=" Expression . |
| 2588 | ``` |
| 2589 | |
| 2590 | ``` |
| 2591 | a: [1, 2, 3, 4] |
Marcel van Lohuizen | de0c53d | 2020-04-05 15:36:29 +0200 | [diff] [blame] | 2592 | b: [ for x in a if x > 1 { x+1 } ] // [3, 4, 5] |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2593 | |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 2594 | c: { |
| 2595 | for x in a |
| 2596 | if x < 4 |
| 2597 | let y = 1 { |
| 2598 | "\(x)": x + y |
| 2599 | } |
| 2600 | } |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2601 | d: { "1": 2, "2": 3, "3": 4 } |
| 2602 | ``` |
| 2603 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2604 | |
| 2605 | ### String interpolation |
| 2606 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2607 | String interpolation allows constructing strings by replacing placeholder |
| 2608 | expressions with their string representation. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2609 | String interpolation may be used in single- and double-quoted strings, as well |
| 2610 | as their multiline equivalent. |
| 2611 | |
Marcel van Lohuizen | 30ca062 | 2020-08-22 14:07:59 +0200 | [diff] [blame] | 2612 | A placeholder consists of "\(" followed by an expression and a ")". |
| 2613 | The expression is evaluated in the scope within which the string is defined. |
| 2614 | |
| 2615 | The result of the expression is substituted as follows: |
| 2616 | - string: as is |
| 2617 | - bool: the JSON representation of the bool |
| 2618 | - number: a JSON representation of the number that preserves the |
| 2619 | precision of the underlying binary coded decimal |
| 2620 | - bytes: as if substituted within single quotes or |
| 2621 | converted to valid UTF-8 replacing the |
| 2622 | maximal subpart of ill-formed subsequences with a single |
| 2623 | replacement character (W3C encoding standard) otherwise |
| 2624 | - list: illegal |
| 2625 | - struct: illegal |
| 2626 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2627 | |
| 2628 | ``` |
| 2629 | a: "World" |
| 2630 | b: "Hello \( a )!" // Hello World! |
| 2631 | ``` |
| 2632 | |
| 2633 | |
| 2634 | ## Builtin Functions |
| 2635 | |
| 2636 | Built-in functions are predeclared. They are called like any other function. |
| 2637 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2638 | |
| 2639 | ### `len` |
| 2640 | |
| 2641 | The built-in function `len` takes arguments of various types and return |
| 2642 | a result of type int. |
| 2643 | |
| 2644 | ``` |
| 2645 | Argument type Result |
| 2646 | |
| 2647 | string string length in bytes |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2648 | bytes length of byte sequence |
| 2649 | list list length, smallest length for an open list |
Marcel van Lohuizen | edea6f3 | 2020-09-12 20:27:20 +0200 | [diff] [blame] | 2650 | struct number of distinct data fields, excluding optional |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2651 | ``` |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2652 | <!-- TODO: consider not supporting len, but instead rely on more |
| 2653 | precisely named builtin functions: |
| 2654 | - strings.RuneLen(x) |
| 2655 | - bytes.Len(x) // x may be a string |
| 2656 | - struct.NumFooFields(x) |
| 2657 | - list.Len(x) |
| 2658 | --> |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2659 | |
| 2660 | ``` |
| 2661 | Expression Result |
| 2662 | len("Hellø") 6 |
| 2663 | len([1, 2, 3]) 3 |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2664 | len([1, 2, ...]) >=2 |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2665 | ``` |
| 2666 | |
Marcel van Lohuizen | 62658a8 | 2019-06-16 12:18:47 +0200 | [diff] [blame] | 2667 | |
| 2668 | ### `close` |
| 2669 | |
| 2670 | The builtin function `close` converts a partially defined, or open, struct |
| 2671 | to a fully defined, or closed, struct. |
| 2672 | |
| 2673 | |
Marcel van Lohuizen | a460fe8 | 2019-04-26 10:20:51 +0200 | [diff] [blame] | 2674 | ### `and` |
| 2675 | |
| 2676 | The built-in function `and` takes a list and returns the result of applying |
| 2677 | the `&` operator to all elements in the list. |
| 2678 | It returns top for the empty list. |
| 2679 | |
Adieu | 5b4fa8b | 2019-12-03 19:20:58 +0100 | [diff] [blame] | 2680 | ``` |
Marcel van Lohuizen | a460fe8 | 2019-04-26 10:20:51 +0200 | [diff] [blame] | 2681 | Expression: Result |
| 2682 | and([a, b]) a & b |
| 2683 | and([a]) a |
| 2684 | and([]) _ |
Adieu | 5b4fa8b | 2019-12-03 19:20:58 +0100 | [diff] [blame] | 2685 | ``` |
Marcel van Lohuizen | a460fe8 | 2019-04-26 10:20:51 +0200 | [diff] [blame] | 2686 | |
| 2687 | ### `or` |
| 2688 | |
| 2689 | The built-in function `or` takes a list and returns the result of applying |
| 2690 | the `|` operator to all elements in the list. |
| 2691 | It returns bottom for the empty list. |
| 2692 | |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2693 | ``` |
Marcel van Lohuizen | a460fe8 | 2019-04-26 10:20:51 +0200 | [diff] [blame] | 2694 | Expression: Result |
Adieu | 5b4fa8b | 2019-12-03 19:20:58 +0100 | [diff] [blame] | 2695 | or([a, b]) a | b |
| 2696 | or([a]) a |
| 2697 | or([]) _|_ |
Marcel van Lohuizen | 6c35af6 | 2019-05-06 10:50:57 +0200 | [diff] [blame] | 2698 | ``` |
Marcel van Lohuizen | a460fe8 | 2019-04-26 10:20:51 +0200 | [diff] [blame] | 2699 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2700 | |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2701 | ## Cycles |
| 2702 | |
| 2703 | Implementations are required to interpret or reject cycles encountered |
| 2704 | during evaluation according to the rules in this section. |
| 2705 | |
| 2706 | |
| 2707 | ### Reference cycles |
| 2708 | |
| 2709 | A _reference cycle_ occurs if a field references itself, either directly or |
| 2710 | indirectly. |
| 2711 | |
| 2712 | ``` |
| 2713 | // x references itself |
| 2714 | x: x |
| 2715 | |
| 2716 | // indirect cycles |
| 2717 | b: c |
| 2718 | c: d |
| 2719 | d: b |
| 2720 | ``` |
| 2721 | |
Marcel van Lohuizen | 043c534 | 2020-07-25 18:51:21 +0200 | [diff] [blame] | 2722 | Implementations should treat these as `_`. |
| 2723 | Two particular cases are discussed below. |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2724 | |
| 2725 | |
| 2726 | #### Expressions that unify an atom with an expression |
| 2727 | |
| 2728 | An expression of the form `a & e`, where `a` is an atom |
| 2729 | and `e` is an expression, always evaluates to `a` or bottom. |
| 2730 | As it does not matter how we fail, we can assume the result to be `a` |
Marcel van Lohuizen | 043c534 | 2020-07-25 18:51:21 +0200 | [diff] [blame] | 2731 | and postpone validating `a == e` until after all referenecs |
| 2732 | in `e` have been resolved. |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2733 | |
| 2734 | ``` |
Marcel van Lohuizen | eac8f9a | 2019-08-03 13:53:56 +0200 | [diff] [blame] | 2735 | // Config Evaluates to (requiring concrete values) |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2736 | x: { x: { |
| 2737 | a: b + 100 a: _|_ // cycle detected |
| 2738 | b: a - 100 b: _|_ // cycle detected |
| 2739 | } } |
| 2740 | |
| 2741 | y: x & { y: { |
| 2742 | a: 200 a: 200 // asserted that 200 == b + 100 |
| 2743 | b: 100 |
| 2744 | } } |
| 2745 | ``` |
| 2746 | |
| 2747 | |
| 2748 | #### Field values |
| 2749 | |
| 2750 | A field value of the form `r & v`, |
Marcel van Lohuizen | 043c534 | 2020-07-25 18:51:21 +0200 | [diff] [blame] | 2751 | where `r` evaluates to a reference cycle and `v` is a concrete value, |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2752 | evaluates to `v`. |
| 2753 | Unification is idempotent and unifying a value with itself ad infinitum, |
| 2754 | which is what the cycle represents, results in this value. |
| 2755 | Implementations should detect cycles of this kind, ignore `r`, |
| 2756 | and take `v` as the result of unification. |
Marcel van Lohuizen | 0d0b9ad | 2019-10-10 18:19:28 +0200 | [diff] [blame] | 2757 | |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2758 | <!-- Tomabechi's graph unification algorithm |
| 2759 | can detect such cycles at near-zero cost. --> |
| 2760 | |
| 2761 | ``` |
| 2762 | Configuration Evaluated |
| 2763 | // c Cycles in nodes of type struct evaluate |
| 2764 | // ↙︎ ↖ to the fixed point of unifying their |
| 2765 | // a → b values ad infinitum. |
| 2766 | |
| 2767 | a: b & { x: 1 } // a: { x: 1, y: 2, z: 3 } |
| 2768 | b: c & { y: 2 } // b: { x: 1, y: 2, z: 3 } |
| 2769 | c: a & { z: 3 } // c: { x: 1, y: 2, z: 3 } |
| 2770 | |
| 2771 | // resolve a b & {x:1} |
| 2772 | // substitute b c & {y:2} & {x:1} |
| 2773 | // substitute c a & {z:3} & {y:2} & {x:1} |
| 2774 | // eliminate a (cycle) {z:3} & {y:2} & {x:1} |
| 2775 | // simplify {x:1,y:2,z:3} |
| 2776 | ``` |
| 2777 | |
| 2778 | This rule also applies to field values that are disjunctions of unification |
| 2779 | operations of the above form. |
| 2780 | |
| 2781 | ``` |
| 2782 | a: b&{x:1} | {y:1} // {x:1,y:3,z:2} | {y:1} |
| 2783 | b: {x:2} | c&{z:2} // {x:2} | {x:1,y:3,z:2} |
| 2784 | c: a&{y:3} | {z:3} // {x:1,y:3,z:2} | {z:3} |
| 2785 | |
| 2786 | |
| 2787 | // resolving a b&{x:1} | {y:1} |
| 2788 | // substitute b ({x:2} | c&{z:2})&{x:1} | {y:1} |
| 2789 | // simplify c&{z:2}&{x:1} | {y:1} |
| 2790 | // substitute c (a&{y:3} | {z:3})&{z:2}&{x:1} | {y:1} |
| 2791 | // simplify a&{y:3}&{z:2}&{x:1} | {y:1} |
| 2792 | // eliminate a (cycle) {y:3}&{z:2}&{x:1} | {y:1} |
| 2793 | // expand {x:1,y:3,z:2} | {y:1} |
| 2794 | ``` |
| 2795 | |
| 2796 | Note that all nodes that form a reference cycle to form a struct will evaluate |
| 2797 | to the same value. |
| 2798 | If a field value is a disjunction, any element that is part of a cycle will |
| 2799 | evaluate to this value. |
| 2800 | |
| 2801 | |
| 2802 | ### Structural cycles |
| 2803 | |
Marcel van Lohuizen | 043c534 | 2020-07-25 18:51:21 +0200 | [diff] [blame] | 2804 | A structural cycle is when a node references one of its ancestor nodes. |
| 2805 | It is possible to construct a structural cycle by unifying two acyclic values: |
| 2806 | ``` |
| 2807 | // acyclic |
| 2808 | y: { |
| 2809 | f: h: g |
| 2810 | g: _ |
| 2811 | } |
| 2812 | // acyclic |
| 2813 | x: { |
| 2814 | f: _ |
| 2815 | g: f |
| 2816 | } |
| 2817 | // introduces structural cycle |
| 2818 | z: x & y |
| 2819 | ``` |
| 2820 | Implementations should be able to detect such structural cycles dynamically. |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2821 | |
Marcel van Lohuizen | 043c534 | 2020-07-25 18:51:21 +0200 | [diff] [blame] | 2822 | A structural cycle can result in infinite structure or evaluation loops. |
| 2823 | ``` |
| 2824 | // infinite structure |
| 2825 | a: b: a |
| 2826 | |
| 2827 | // infinite evaluation |
| 2828 | f: { |
| 2829 | n: int |
| 2830 | out: n + (f & {n: 1}).out |
| 2831 | } |
| 2832 | ``` |
| 2833 | CUE must allow or disallow structural cycles under certain circumstances. |
| 2834 | |
| 2835 | If a node `a` references an ancestor node, we call it and any of its |
| 2836 | field values `a.f` _cyclic_. |
| 2837 | So if `a` is cyclic, all of its descendants are also regarded as cyclic. |
| 2838 | A given node `x`, whose value is composed of the conjuncts `c1 & ... & cn`, |
| 2839 | is valid if any of its conjuncts is not cyclic. |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2840 | |
| 2841 | ``` |
| 2842 | // Disallowed: a list of infinite length with all elements being 1. |
Marcel van Lohuizen | 043c534 | 2020-07-25 18:51:21 +0200 | [diff] [blame] | 2843 | #List: { |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2844 | head: 1 |
Marcel van Lohuizen | 043c534 | 2020-07-25 18:51:21 +0200 | [diff] [blame] | 2845 | tail: #List |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | // Disallowed: another infinite structure (a:{b:{d:{b:{d:{...}}}}}, ...). |
| 2849 | a: { |
| 2850 | b: c |
| 2851 | } |
| 2852 | c: { |
| 2853 | d: a |
| 2854 | } |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2855 | |
Marcel van Lohuizen | 043c534 | 2020-07-25 18:51:21 +0200 | [diff] [blame] | 2856 | // #List defines a list of arbitrary length. Because the recursive reference |
| 2857 | // is part of a disjunction, this does not result in a structural cycle. |
| 2858 | #List: { |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2859 | head: _ |
Marcel van Lohuizen | 043c534 | 2020-07-25 18:51:21 +0200 | [diff] [blame] | 2860 | tail: null | #List |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2861 | } |
Marcel van Lohuizen | 043c534 | 2020-07-25 18:51:21 +0200 | [diff] [blame] | 2862 | |
| 2863 | // Usage of #List. The value of tail in the most deeply nested element will |
| 2864 | // be `null`: as the value of the disjunct referring to list is the only |
| 2865 | // conjunct, all conjuncts are cyclic and the value is invalid and so |
| 2866 | // eliminated from the disjunction. |
| 2867 | MyList: #List & { head: 1, tail: { head: 2 }} |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2868 | ``` |
| 2869 | |
| 2870 | <!-- |
| 2871 | ### Unused fields |
| 2872 | |
| 2873 | TODO: rules for detection of unused fields |
| 2874 | |
| 2875 | 1. Any alias value must be used |
| 2876 | --> |
| 2877 | |
| 2878 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2879 | ## Modules, instances, and packages |
| 2880 | |
| 2881 | CUE configurations are constructed combining _instances_. |
| 2882 | An instance, in turn, is constructed from one or more source files belonging |
| 2883 | to the same _package_ that together declare the data representation. |
| 2884 | Elements of this data representation may be exported and used |
| 2885 | in other instances. |
| 2886 | |
| 2887 | ### Source file organization |
| 2888 | |
| 2889 | Each source file consists of an optional package clause defining collection |
| 2890 | of files to which it belongs, |
| 2891 | followed by a possibly empty set of import declarations that declare |
| 2892 | packages whose contents it wishes to use, followed by a possibly empty set of |
| 2893 | declarations. |
| 2894 | |
Marcel van Lohuizen | 1f5a903 | 2019-09-09 23:53:42 +0200 | [diff] [blame] | 2895 | Like with a struct, a source file may contain embeddings. |
| 2896 | Unlike with a struct, the embedded expressions may be any value. |
| 2897 | If the result of the unification of all embedded values is not a struct, |
| 2898 | it will be output instead of its enclosing file when exporting CUE |
| 2899 | to a data format |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2900 | |
| 2901 | ``` |
Marcel van Lohuizen | c264475 | 2020-09-12 15:02:25 +0200 | [diff] [blame] | 2902 | SourceFile = { attribute "," } [ PackageClause "," ] { ImportDecl "," } { Declaration "," } . |
Marcel van Lohuizen | 1f5a903 | 2019-09-09 23:53:42 +0200 | [diff] [blame] | 2903 | ``` |
| 2904 | |
| 2905 | ``` |
| 2906 | "Hello \(place)!" |
| 2907 | |
| 2908 | place: "world" |
| 2909 | |
| 2910 | // Outputs "Hello world!" |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2911 | ``` |
| 2912 | |
| 2913 | ### Package clause |
| 2914 | |
| 2915 | A package clause is an optional clause that defines the package to which |
| 2916 | a source file the file belongs. |
| 2917 | |
| 2918 | ``` |
| 2919 | PackageClause = "package" PackageName . |
| 2920 | PackageName = identifier . |
| 2921 | ``` |
| 2922 | |
Marcel van Lohuizen | cb8f4f5 | 2020-03-08 17:39:39 +0100 | [diff] [blame] | 2923 | The PackageName must not be the blank identifier or a definition identifier. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2924 | |
| 2925 | ``` |
| 2926 | package math |
| 2927 | ``` |
| 2928 | |
| 2929 | ### Modules and instances |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2930 | A _module_ defines a tree of directories, rooted at the _module root_. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2931 | |
| 2932 | All source files within a module with the same package belong to the same |
| 2933 | package. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2934 | <!-- jba: I can't make sense of the above sentence. --> |
| 2935 | A module may define multiple packages. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2936 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2937 | An _instance_ of a package is any subset of files belonging |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2938 | to the same package. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2939 | <!-- jba: Are you saying that --> |
| 2940 | <!-- if I have a package with files a, b and c, then there are 8 instances of --> |
| 2941 | <!-- that package, some of which are {a, b}, {c}, {b, c}, and so on? What's the --> |
| 2942 | <!-- purpose of that definition? --> |
| 2943 | It is interpreted as the concatenation of these files. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2944 | |
| 2945 | An implementation may impose conventions on the layout of package files |
| 2946 | to determine which files of a package belongs to an instance. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2947 | For example, an instance may be defined as the subset of package files |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2948 | belonging to a directory and all its ancestors. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2949 | <!-- jba: OK, that helps a little, but I still don't see what the purpose is. --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2950 | |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 2951 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2952 | ### Import declarations |
| 2953 | |
| 2954 | An import declaration states that the source file containing the declaration |
| 2955 | depends on definitions of the _imported_ package (§Program initialization and |
| 2956 | execution) and enables access to exported identifiers of that package. |
| 2957 | The import names an identifier (PackageName) to be used for access and an |
| 2958 | ImportPath that specifies the package to be imported. |
| 2959 | |
| 2960 | ``` |
Marcel van Lohuizen | 4017875 | 2019-08-25 19:17:56 +0200 | [diff] [blame] | 2961 | ImportDecl = "import" ( ImportSpec | "(" { ImportSpec "," } ")" ) . |
Marcel van Lohuizen | fbab65d | 2019-08-13 16:51:15 +0200 | [diff] [blame] | 2962 | ImportSpec = [ PackageName ] ImportPath . |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 2963 | ImportLocation = { unicode_value } . |
| 2964 | ImportPath = `"` ImportLocation [ ":" identifier ] `"` . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2965 | ``` |
| 2966 | |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 2967 | The PackageName is used in qualified identifiers to access |
| 2968 | exported identifiers of the package within the importing source file. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2969 | It is declared in the file block. |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 2970 | It defaults to the identifier specified in the package clause of the imported |
| 2971 | package, which must match either the last path component of ImportLocation |
| 2972 | or the identifier following it. |
| 2973 | |
| 2974 | <!-- |
| 2975 | Note: this deviates from the Go spec where there is no such restriction. |
| 2976 | This restriction has the benefit of being to determine the identifiers |
| 2977 | for packages from within the file itself. But for CUE it is has another benefit: |
| 2978 | when using package hiearchies, one is more likely to want to include multiple |
| 2979 | packages within the same directory structure. This mechanism allows |
| 2980 | disambiguation in these cases. |
| 2981 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2982 | |
| 2983 | The interpretation of the ImportPath is implementation-dependent but it is |
| 2984 | typically either the path of a builtin package or a fully qualifying location |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 2985 | of a package within a source code repository. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2986 | |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 2987 | An ImportLocation must be a non-empty strings using only characters belonging |
| 2988 | Unicode's L, M, N, P, and S general categories |
| 2989 | (the Graphic characters without spaces) |
| 2990 | and may not include the characters !"#$%&'()*,:;<=>?[\]^`{|} |
| 2991 | or the Unicode replacement character U+FFFD. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2992 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2993 | Assume we have package containing the package clause "package math", |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2994 | which exports function Sin at the path identified by "lib/math". |
| 2995 | This table illustrates how Sin is accessed in files |
| 2996 | that import the package after the various types of import declaration. |
| 2997 | |
| 2998 | ``` |
| 2999 | Import declaration Local name of Sin |
| 3000 | |
| 3001 | import "lib/math" math.Sin |
Marcel van Lohuizen | 7414fae | 2019-08-13 17:26:35 +0200 | [diff] [blame] | 3002 | import "lib/math:math" math.Sin |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 3003 | import m "lib/math" m.Sin |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 3004 | ``` |
| 3005 | |
| 3006 | An import declaration declares a dependency relation between the importing and |
| 3007 | imported package. It is illegal for a package to import itself, directly or |
| 3008 | indirectly, or to directly import a package without referring to any of its |
| 3009 | exported identifiers. |
| 3010 | |
| 3011 | |
| 3012 | ### An example package |
| 3013 | |
| 3014 | TODO |