diff options
author | Jo-Philipp Wich <jo@mein.io> | 2021-09-25 19:34:14 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-10-11 09:39:12 +0200 |
commit | 4ee06d8138a107908a9fb45220fea32055b3c48a (patch) | |
tree | 2f0bd421931b2dd2daf504719beb63cc2885d23a /lexer.c | |
parent | e43b751aab997c5e74a0712f7569d90bd3d6b429 (diff) |
syntax: introduce optional chaining operators
Introduce new operators `?.`, `?.[…]` and `?.(…)` to simplify looking up
deeply nested property chain in a secure manner.
The `?.` operator behaves like the `.` property access operator but yields
`null` if the left hand side is `null` or not an object.
Like `?.`, the `?.[…]` operator behaves like the `[…]` computed property
access but yields `null` if the left hand side is `null` or neither an
object or array.
Finally the `?.(…)` operator behaves like the function call operator `(…)`
but yields `null` if the left hand side is `null` or not a callable
function.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lexer.c')
-rw-r--r-- | lexer.c | 3 |
1 files changed, 3 insertions, 0 deletions
@@ -71,6 +71,8 @@ static const struct token tokens[] = { { TK_EQS, { .pat = "===" }, 3, NULL }, { TK_NES, { .pat = "!==" }, 3, NULL }, { TK_ELLIP, { .pat = "..." }, 3, NULL }, + { TK_QLBRACK, { .pat = "?.[" }, 3, NULL }, + { TK_QLPAREN, { .pat = "?.(" }, 3, NULL }, { TK_AND, { .pat = "&&" }, 2, NULL }, { TK_ASADD, { .pat = "+=" }, 2, NULL }, { TK_ASBAND, { .pat = "&=" }, 2, NULL }, @@ -96,6 +98,7 @@ static const struct token tokens[] = { { TK_LSTM, { .pat = "{%" }, 2, NULL }, { TK_RSTM, { .pat = "%}" }, 2, NULL }, { TK_ARROW, { .pat = "=>" }, 2, NULL }, + { TK_QDOT, { .pat = "?." }, 2, NULL }, { TK_ADD, { .pat = "+" }, 1, NULL }, { TK_ASSIGN, { .pat = "=" }, 1, NULL }, { TK_BAND, { .pat = "&" }, 1, NULL }, |