From 4ee06d8138a107908a9fb45220fea32055b3c48a Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Sat, 25 Sep 2021 19:34:14 +0200 Subject: syntax: introduce optional chaining operators MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- lexer.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lexer.c') diff --git a/lexer.c b/lexer.c index 75dc04a..fc2c685 100644 --- a/lexer.c +++ b/lexer.c @@ -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 }, -- cgit v1.2.3