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